class BudgetModel(BaseBudgetModel):
"""
Budget model class
"""
...Optimizer
Optimizers for models
BaseOptimizer
BaseOptimizer (model:budget_optimizer.utils.model_classes.BaseBudgetMode l, config_path:str|pathlib.Path)
Optimizer wrapper for the pyswarms package
| Type | Details | |
|---|---|---|
| model | BaseBudgetModel | The model to optimize |
| config_path | str | pathlib.Path | Path to the configuration files |
ScipyBudgetOptimizer
ScipyBudgetOptimizer (model:budget_optimizer.utils.model_classes.BaseBud getModel, config_path:str|pathlib.Path)
Optimizer wrapper for the pyswarms package
| Type | Details | |
|---|---|---|
| model | BaseBudgetModel | The model to optimize |
| config_path | str | pathlib.Path | Path to the configuration files |
fast_model = BudgetModel("Revenue Model", "Revenue", "../example_files/fast_model")init_budget = np.array([2, 3])
bounds = [(3, 5), (3, 5)]
constraints = opt.LinearConstraint([[1, 1]], [8], [8])
o = ScipyBudgetOptimizer(fast_model, "../example_files")ScipyBudgetOptimizer.optimize
ScipyBudgetOptimizer.optimize (bounds:list[tuple[float,float]], constraints:None|scipy.optimize._constrain ts.LinearConstraint, init_pos:numpy.ndarray)
Optimize the model
| Type | Details | |
|---|---|---|
| bounds | list | Bounds for the optimizer |
| constraints | None | scipy.optimize._constraints.LinearConstraint | Constraints for the optimizer |
| init_pos | ndarray | Initial position of the optimizer |
o_fitted = o.optimize(bounds, constraints, init_pos=init_budget) message: `gtol` termination condition is satisfied.
success: True
status: 1
fun: -601.6849021505009
x: [ 3.168e+00 4.832e+00]
nit: 15
nfev: 30
njev: 10
nhev: 0
cg_niter: 9
cg_stop_cond: 1
grad: [-1.485e+01 -1.485e+01]
lagrangian_grad: [-9.561e-09 9.561e-09]
constr: [array([ 8.000e+00]), array([ 3.168e+00, 4.832e+00])]
jac: [array([[ 1.000e+00, 1.000e+00]]), array([[ 1.000e+00, 0.000e+00],
[ 0.000e+00, 1.000e+00]])]
constr_nfev: [0, 0]
constr_njev: [0, 0]
constr_nhev: [0, 0]
v: [array([ 1.485e+01]), array([-1.731e-04, 1.731e-04])]
method: tr_interior_point
optimality: 9.56122952780968e-09
constr_violation: 0.0
execution_time: 0.0405728816986084
tr_radius: 29797.67905015749
constr_penalty: 1.0
barrier_parameter: 3.200000000000001e-05
barrier_tolerance: 3.200000000000001e-05
niter: 15
Optimal budget:
{'a': 3.17, 'b': 4.83}
OptunaBudgetOptimizer
OptunaBudgetOptimizer (model:budget_optimizer.utils.model_classes.BaseBu dgetModel, config_path:str|pathlib.Path, objective_name:str='loss', storage:str='sqlite:///db.sqlite3', direction:Lite ral['maximize','minimize']='maximize', sampler:optuna.samplers._base.BaseSampler=<class 'optuna.samplers._tpe.sampler.TPESampler'>, pruner:optuna.pruners._base.BasePruner|None=None, tol:float=0.001, percent_out_tolerance:float=0.1, sampler_kwargs:dict|None=None, pruner_kwargs:dict|None=None)
Optimizer wrapper for the pyswarms package
| Type | Default | Details | |
|---|---|---|---|
| model | BaseBudgetModel | The model to optimize | |
| config_path | str | pathlib.Path | Path to the configuration files | |
| objective_name | str | loss | Name of the objective |
| storage | str | sqlite:///db.sqlite3 | Storage for the optimization defaults to local sqlite |
| direction | Literal | maximize | Direction of the optimization |
| sampler | BaseSampler | TPESampler | Sampler for the optimization |
| pruner | optuna.pruners._base.BasePruner | None | None | Pruner for the optimization |
| tol | float | 0.001 | Tolerance for the constraints |
| percent_out_tolerance | float | 0.1 | Percentage of the budget trials that can be outside the constraints |
| sampler_kwargs | dict | None | None | Additional arguments for the sampler |
| pruner_kwargs | dict | None | None | Additional arguments for the pruner |
#load_dotenv()
#USER = os.environ.get("POSTGRES_USER", "")
#PASSWORD = os.environ.get("POSTGRES_PASSWORD", "")
#POSTGRES_URL = f"postgresql://{USER}:{PASSWORD}@localhost:54320"
POSTGRES_URL = "sqlite:///db.sqlite3"#conn = await asyncpg.connect(POSTGRES_URL)
#database = "budget_optimizer"
#try:
# db_exists = await conn.fetchval(
# 'SELECT 1 FROM pg_database WHERE datname = $1', database
# )
# print(db_exists)
# if not db_exists:
# await conn.execute(f'CREATE DATABASE {database}')
#except Exception as e:
# print(e)
#conn.close()
#POSTGRES_URL = f"{POSTGRES_URL}/{database}"optuna.logging.set_verbosity(optuna.logging.WARNING)
optuna.logging.get_logger("optuna").addHandler(logging.StreamHandler(sys.stdout))
slow_model = BudgetModel("Revenue Model", "Revenue", "../example_files/slow_model")
opt_optimizer = OptunaBudgetOptimizer(
slow_model, "../example_files",
storage=(POSTGRES_URL),
direction="maximize", tol=1e-3, percent_out_tolerance=0.1,
sampler_kwargs={"multivariate": True, "group": True})OptunaBudgetOptimizer.optimize
OptunaBudgetOptimizer.optimize (bounds:dict[str,tuple[float,float]], constraints:None|tuple=None, timeout:int=60, n_trials:int=100, study_name:str='optimizer', load_if_exists:bool=False, n_jobs:int=1)
Optimize the model
| Type | Default | Details | |
|---|---|---|---|
| bounds | dict | Bounds for the optimizer | |
| constraints | None | tuple | None | Constraints for the optimizer |
| timeout | int | 60 | Timeout for the optimization |
| n_trials | int | 100 | Max number of trials to run |
| study_name | str | optimizer | Name of the study |
| load_if_exists | bool | False | Load the study if it exists |
| n_jobs | int | 1 | Number of jobs to run in parallel |
bounds = {"a": (3, 8), "b": (3, 5)}
constraints = (8, 8)
now = datetime.now()
opt_optimizer.optimize(
bounds, constraints,
n_trials=200_000, timeout=30,
study_name=f"Optimization_{now.strftime('%Y%m%d_%H%M%S')}",
n_jobs=4)o_fitted.optimal_budget{'a': 3.1675802082673, 'b': 4.8324197917327005}
Scipy Optimal Total Budget 8.0
opt_optimizer.optimal_budget{'b': 4.831325789181123, 'a': 3.1686742108188772}
Optuna Optimal Total Budget 8.0

Citation
BibTeX citation:
@online{reda,
author = {Reda, Matthew},
title = {Optimizer},
url = {https://optimizer.mattreda.pro/budget_optimizer/optimizer.html},
langid = {en}
}
For attribution, please cite this work as:
Reda, Matthew. n.d. “Optimizer.” https://optimizer.mattreda.pro/budget_optimizer/optimizer.html.