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 |
= BudgetModel("Revenue Model", "Revenue", "../example_files/fast_model") fast_model
= np.array([2, 3])
init_budget = [(3, 5), (3, 5)]
bounds = opt.LinearConstraint([[1, 1]], [8], [8])
constraints = ScipyBudgetOptimizer(fast_model, "../example_files") o
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.optimize(bounds, constraints, init_pos=init_budget) o_fitted
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"
= "sqlite:///db.sqlite3" POSTGRES_URL
#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").addHandler(logging.StreamHandler(sys.stdout))
optuna.logging.get_logger(= BudgetModel("Revenue Model", "Revenue", "../example_files/slow_model")
slow_model = OptunaBudgetOptimizer(
opt_optimizer "../example_files",
slow_model, =(POSTGRES_URL),
storage="maximize", tol=1e-3, percent_out_tolerance=0.1,
direction={"multivariate": True, "group": True}) sampler_kwargs
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 |
= {"a": (3, 8), "b": (3, 5)}
bounds = (8, 8)
constraints = datetime.now()
now
opt_optimizer.optimize(
bounds, constraints, =200_000, timeout=30,
n_trials=f"Optimization_{now.strftime('%Y%m%d_%H%M%S')}",
study_name=4) n_jobs
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.