remove with_count parameter (act like it's True)

This commit is contained in:
Connor Olding 2023-05-07 07:21:13 -07:00
parent ac39efe00a
commit 6778965d70
10 changed files with 93 additions and 106 deletions

View File

@ -3,9 +3,9 @@ from .random import another_random_cube
from ..utilities import wrap_untrustworthy, final from ..utilities import wrap_untrustworthy, final
def dlib_cube(objective, n_trials, n_dim, with_count): def dlib_cube(objective, n_trials, n_dim):
if n_dim > 35: if n_dim > 35:
return another_random_cube(objective, n_trials, n_dim, with_count) return another_random_cube(objective, n_trials, n_dim)
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
@ -13,5 +13,4 @@ def dlib_cube(objective, n_trials, n_dim, with_count):
return _objective(list(args)) return _objective(list(args))
find_min_global(__objective, [0.0] * n_dim, [1.0] * n_dim, n_trials) find_min_global(__objective, [0.0] * n_dim, [1.0] * n_dim, n_trials)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)

View File

@ -43,7 +43,7 @@ def make_evolopy(optimizer_name, popsize=None):
} }
optimizer = optimizers[optimizer_name.upper()] optimizer = optimizers[optimizer_name.upper()]
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
# ps = n_dim if popsize is None else popsize # ps = n_dim if popsize is None else popsize
# ps = max(4, ps) if optimizer_name.upper() == "DE" else ps # ps = max(4, ps) if optimizer_name.upper() == "DE" else ps
ps = int(4 + 3 * np.log(n_dim)) if popsize is None else popsize ps = int(4 + 3 * np.log(n_dim)) if popsize is None else popsize
@ -58,8 +58,7 @@ def make_evolopy(optimizer_name, popsize=None):
except ExhaustedTrialsError: except ExhaustedTrialsError:
pass pass
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = f"evolopy_{optimizer.__name__.lower()}" name = f"evolopy_{optimizer.__name__.lower()}"
name += f"_auto" if popsize is None else f"_ps{popsize:02}" name += f"_auto" if popsize is None else f"_ps{popsize:02}"

View File

@ -21,13 +21,12 @@ def _fix_logging(original_function):
def make_biteopt(depth=1): def make_biteopt(depth=1):
from fcmaes.optimizer import Bite_cpp from fcmaes.optimizer import Bite_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
optim = Bite_cpp(max_evaluations=n_trials, M=depth) optim = Bite_cpp(max_evaluations=n_trials, M=depth)
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = f"fcmaes_biteopt_d{depth}" name = f"fcmaes_biteopt_d{depth}"
f.__name__ = name + "_cube" f.__name__ = name + "_cube"
@ -38,15 +37,14 @@ def make_biteopt(depth=1):
def make_csma(isigma=3): def make_csma(isigma=3):
from fcmaes.optimizer import Csma_cpp from fcmaes.optimizer import Csma_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
optim = Csma_cpp(max_evaluations=n_trials) optim = Csma_cpp(max_evaluations=n_trials)
_xopt, _fopt, _feval_count = optim.minimize( _xopt, _fopt, _feval_count = optim.minimize(
_objective, bounds, sdevs=1 / isigma _objective, bounds, sdevs=1 / isigma
) )
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = f"fcmaes_csma_is{isigma:02}" name = f"fcmaes_csma_is{isigma:02}"
f.__name__ = name + "_cube" f.__name__ = name + "_cube"
@ -57,7 +55,7 @@ def make_csma(isigma=3):
def make_fcmaes(popsize=None): def make_fcmaes(popsize=None):
from fcmaes.optimizer import Cma_cpp from fcmaes.optimizer import Cma_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
ps = int(4 + 3 * np.log(n_dim)) if popsize is None else popsize ps = int(4 + 3 * np.log(n_dim)) if popsize is None else popsize
@ -69,8 +67,7 @@ def make_fcmaes(popsize=None):
delayed_update=False, delayed_update=False,
) )
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds, sdevs=1 / 3) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds, sdevs=1 / 3)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = "fcmaes_cma" name = "fcmaes_cma"
name += f"_auto" if popsize is None else f"_ps{popsize}" name += f"_auto" if popsize is None else f"_ps{popsize}"
@ -82,14 +79,13 @@ def make_fcmaes(popsize=None):
def make_crfmnes(popsize=None): def make_crfmnes(popsize=None):
from fcmaes.optimizer import Crfmnes_cpp from fcmaes.optimizer import Crfmnes_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
ps = int(4 + 3 * np.log(n_dim)) if popsize is None else popsize ps = int(4 + 3 * np.log(n_dim)) if popsize is None else popsize
optim = Crfmnes_cpp(max_evaluations=n_trials, popsize=ps) optim = Crfmnes_cpp(max_evaluations=n_trials, popsize=ps)
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds, sdevs=1 / 3) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds, sdevs=1 / 3)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = "fcmaes_crfmnes" name = "fcmaes_crfmnes"
name += f"_auto" if popsize is None else f"_ps{popsize}" name += f"_auto" if popsize is None else f"_ps{popsize}"
@ -102,13 +98,12 @@ _broken = """
def make_lde(popsize=None): def make_lde(popsize=None):
from fcmaes.optimizer import LDe_cpp from fcmaes.optimizer import LDe_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
optim = LDe_cpp(max_evaluations=n_trials, popsize=popsize) optim = LDe_cpp(max_evaluations=n_trials, popsize=popsize)
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = "fcmaes_lde" name = "fcmaes_lde"
name += f"_auto" if popsize is None else f"_ps{popsize}" name += f"_auto" if popsize is None else f"_ps{popsize}"
@ -121,13 +116,12 @@ def make_lde(popsize=None):
def make_da(local=True): def make_da(local=True):
from fcmaes.optimizer import Da_cpp from fcmaes.optimizer import Da_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
optim = Da_cpp(max_evaluations=n_trials, use_local_search=local) optim = Da_cpp(max_evaluations=n_trials, use_local_search=local)
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = "fcmaes_da" name = "fcmaes_da"
if local: if local:
@ -140,13 +134,12 @@ def make_da(local=True):
def make_gclde(popsize=None): def make_gclde(popsize=None):
from fcmaes.optimizer import GCLDE_cpp from fcmaes.optimizer import GCLDE_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
optim = GCLDE_cpp(max_evaluations=n_trials, popsize=popsize) optim = GCLDE_cpp(max_evaluations=n_trials, popsize=popsize)
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = "fcmaes_gclde" name = "fcmaes_gclde"
name += f"_auto" if popsize is None else f"_ps{popsize}" name += f"_auto" if popsize is None else f"_ps{popsize}"
@ -158,13 +151,12 @@ def make_gclde(popsize=None):
def make_lclde(popsize=None): def make_lclde(popsize=None):
from fcmaes.optimizer import LCLDE_cpp from fcmaes.optimizer import LCLDE_cpp
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials) _objective = wrap_untrustworthy(objective, n_trials)
bounds = Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = Bounds([0.0] * n_dim, [1.0] * n_dim)
optim = LCLDE_cpp(max_evaluations=n_trials, popsize=popsize) optim = LCLDE_cpp(max_evaluations=n_trials, popsize=popsize)
_xopt, _fopt, _feval_count = optim.minimize(_objective, bounds) _xopt, _fopt, _feval_count = optim.minimize(_objective, bounds)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = "fcmaes_lclde" name = "fcmaes_lclde"
name += f"_auto" if popsize is None else f"_ps{popsize}" name += f"_auto" if popsize is None else f"_ps{popsize}"

View File

@ -303,7 +303,7 @@ assert ol.RSLSQP is ol.RSQP, "weirdness is gone, please adjust accordingly"
assert ol.SLSQP is ol.SQP, "weirdness is gone, please adjust accordingly" assert ol.SLSQP is ol.SQP, "weirdness is gone, please adjust accordingly"
def nevergrad_cube_factory(optimizer, objective, n_trials, n_dim, with_count): def nevergrad_cube_factory(optimizer, objective, n_trials, n_dim):
instrument = ng.p.Array(lower=0, upper=1, shape=(n_dim,)) # better sigma instrument = ng.p.Array(lower=0, upper=1, shape=(n_dim,)) # better sigma
# ev.RBO, ev.QRBO, ev.MidQRBO, and ev.LBO still complain, though: # ev.RBO, ev.QRBO, ev.MidQRBO, and ev.LBO still complain, though:
# /home/py/.local/lib/python3.10/site-packages/nevergrad/parametrization/_datalayers.py:107: NevergradRuntimeWarning: Bounds are 1.0 sigma away from each other at the closest, you should aim for at least 3 for better quality. # /home/py/.local/lib/python3.10/site-packages/nevergrad/parametrization/_datalayers.py:107: NevergradRuntimeWarning: Bounds are 1.0 sigma away from each other at the closest, you should aim for at least 3 for better quality.
@ -326,7 +326,7 @@ def nevergrad_cube_factory(optimizer, objective, n_trials, n_dim, with_count):
best_x = recommendation.value best_x = recommendation.value
best_val = cube_objective(best_x) # don't trust recommendation.loss best_val = cube_objective(best_x) # don't trust recommendation.loss
return (best_val, best_x, feval_count) if with_count else (best_val, best_x) return best_val, best_x, feval_count
def named_optimizer(optimizer, experimental=False): def named_optimizer(optimizer, experimental=False):

View File

@ -47,7 +47,7 @@ NLOPTIMIZERS = {
} }
def nlopt_cube_factory(objective, n_trials, n_dim, with_count, method): def nlopt_cube_factory(objective, n_trials, n_dim, method):
optim = NLOPTIMIZERS[method] optim = NLOPTIMIZERS[method]
feval_count = 0 feval_count = 0
@ -83,110 +83,109 @@ def nlopt_cube_factory(objective, n_trials, n_dim, with_count, method):
assert best_so_far is not None, optimizer.__name__ assert best_so_far is not None, optimizer.__name__
fopt, xopt = best_so_far fopt, xopt = best_so_far
return fopt, xopt, feval_count
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
def nlopt_ags_cube(objective, n_trials, n_dim, with_count): def nlopt_ags_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_ags") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_ags")
def nlopt_crs2_lm_cube(objective, n_trials, n_dim, with_count): def nlopt_crs2_lm_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_crs2_lm") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_crs2_lm")
def nlopt_direct_cube(objective, n_trials, n_dim, with_count): def nlopt_direct_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_direct") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_direct")
def nlopt_direct_l_cube(objective, n_trials, n_dim, with_count): def nlopt_direct_l_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_direct_l") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_direct_l")
def nlopt_direct_l_noscal_cube(objective, n_trials, n_dim, with_count): def nlopt_direct_l_noscal_cube(objective, n_trials, n_dim):
return nlopt_cube_factory( return nlopt_cube_factory(
objective, n_trials, n_dim, with_count, "gn_direct_l_noscal" objective, n_trials, n_dim, "gn_direct_l_noscal"
) )
def nlopt_direct_lr_cube(objective, n_trials, n_dim, with_count): def nlopt_direct_lr_cube(objective, n_trials, n_dim):
return nlopt_cube_factory( return nlopt_cube_factory(
objective, n_trials, n_dim, with_count, "gn_direct_l_rand" objective, n_trials, n_dim, "gn_direct_l_rand"
) )
def nlopt_direct_lr_noscal_cube(objective, n_trials, n_dim, with_count): def nlopt_direct_lr_noscal_cube(objective, n_trials, n_dim):
return nlopt_cube_factory( return nlopt_cube_factory(
objective, n_trials, n_dim, with_count, "gn_direct_l_rand_noscal" objective, n_trials, n_dim, "gn_direct_l_rand_noscal"
) )
def nlopt_direct_noscal_cube(objective, n_trials, n_dim, with_count): def nlopt_direct_noscal_cube(objective, n_trials, n_dim):
return nlopt_cube_factory( return nlopt_cube_factory(
objective, n_trials, n_dim, with_count, "gn_direct_noscal" objective, n_trials, n_dim, "gn_direct_noscal"
) )
def nlopt_esch_cube(objective, n_trials, n_dim, with_count): def nlopt_esch_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_esch") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_esch")
def nlopt_isres_cube(objective, n_trials, n_dim, with_count): def nlopt_isres_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_isres") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_isres")
def nlopt_mlsl_cube(objective, n_trials, n_dim, with_count): def nlopt_mlsl_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_mlsl") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_mlsl")
def nlopt_mlsl_lds_cube(objective, n_trials, n_dim, with_count): def nlopt_mlsl_lds_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_mlsl_lds") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_mlsl_lds")
def nlopt_orig_direct_cube(objective, n_trials, n_dim, with_count): def nlopt_orig_direct_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_orig_direct") return nlopt_cube_factory(objective, n_trials, n_dim, "gn_orig_direct")
def nlopt_orig_direct_l_cube(objective, n_trials, n_dim, with_count): def nlopt_orig_direct_l_cube(objective, n_trials, n_dim):
return nlopt_cube_factory( return nlopt_cube_factory(
objective, n_trials, n_dim, with_count, "gn_orig_direct_l" objective, n_trials, n_dim, "gn_orig_direct_l"
) )
def nlopt_auglag_cube(objective, n_trials, n_dim, with_count): def nlopt_auglag_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_auglag") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_auglag")
def nlopt_auglag_eq_cube(objective, n_trials, n_dim, with_count): def nlopt_auglag_eq_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_auglag_eq") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_auglag_eq")
def nlopt_bobyqa_cube(objective, n_trials, n_dim, with_count): def nlopt_bobyqa_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_bobyqa") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_bobyqa")
def nlopt_cobyla_cube(objective, n_trials, n_dim, with_count): def nlopt_cobyla_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_cobyla") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_cobyla")
def nlopt_neldermead_cube(objective, n_trials, n_dim, with_count): def nlopt_neldermead_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_neldermead") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_neldermead")
def nlopt_newuoa_cube(objective, n_trials, n_dim, with_count): def nlopt_newuoa_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_newuoa") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_newuoa")
def nlopt_newuoa_bound_cube(objective, n_trials, n_dim, with_count): def nlopt_newuoa_bound_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_newuoa_bound") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_newuoa_bound")
def nlopt_praxis_cube(objective, n_trials, n_dim, with_count): def nlopt_praxis_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_praxis") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_praxis")
def nlopt_sbplx_cube(objective, n_trials, n_dim, with_count): def nlopt_sbplx_cube(objective, n_trials, n_dim):
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_sbplx") return nlopt_cube_factory(objective, n_trials, n_dim, "ln_sbplx")
NLOPT_OPTIMIZERS = [ NLOPT_OPTIMIZERS = [

View File

@ -6,7 +6,7 @@ import numpy as np
def make_birect(deepness=23, *, longest=False, pruning=False): def make_birect(deepness=23, *, longest=False, pruning=False):
from ..internal.birect import birect from ..internal.birect import birect
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
feval_count = 0 feval_count = 0
def _objective(x): def _objective(x):
@ -24,7 +24,7 @@ def make_birect(deepness=23, *, longest=False, pruning=False):
pruning=pruning, pruning=pruning,
F=np.float64, F=np.float64,
) )
return (fopt, xopt, feval_count) if with_count else (fopt, xopt) return fopt, xopt, feval_count
name = f"birect{deepness:02}" name = f"birect{deepness:02}"
name += "_longest" if longest else "" name += "_longest" if longest else ""
@ -39,7 +39,7 @@ def make_soo(deepness=None, *, K=3):
assert K >= 2 assert K >= 2
from ..internal.soo import soo from ..internal.soo import soo
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
feval_count = 0 feval_count = 0
def _objective(x): def _objective(x):
@ -51,7 +51,7 @@ def make_soo(deepness=None, *, K=3):
_objective, np.full(n_dim, 0.5), 0.5, n_trials, K=K, h_max=deepness _objective, np.full(n_dim, 0.5), 0.5, n_trials, K=K, h_max=deepness
) )
fopt = history[-1] fopt = history[-1]
return (fopt, xopt, feval_count) if with_count else (fopt, xopt) return fopt, xopt, feval_count
name = f"soo{deepness}_k{K}" name = f"soo{deepness}_k{K}"
f.__name__ = name + "_cube" f.__name__ = name + "_cube"
@ -63,7 +63,7 @@ def make_mercury(
): ):
from ..internal.hg import minimize as hg from ..internal.hg import minimize as hg
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy(objective, n_trials, bounding=bounding) _objective = wrap_untrustworthy(objective, n_trials, bounding=bounding)
init = (0.5,) * n_dim init = (0.5,) * n_dim
@ -89,8 +89,7 @@ def make_mercury(
x = prng.uniform(size=n_dim) x = prng.uniform(size=n_dim)
fx = _objective(x) fx = _objective(x)
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = f"hg{flags:02}" name = f"hg{flags:02}"
name += f"_{bounding}" if bounding != "clip" else "" name += f"_{bounding}" if bounding != "clip" else ""

View File

@ -2,7 +2,7 @@ from ..utilities import phi
import numpy as np import numpy as np
def another_random_cube(objective, n_trials, n_dim, with_count, seed=None): def another_random_cube(objective, n_trials, n_dim, seed=None):
prng = np.random.default_rng(seed) prng = np.random.default_rng(seed)
fopt = None fopt = None
xopt = None xopt = None
@ -12,10 +12,10 @@ def another_random_cube(objective, n_trials, n_dim, with_count, seed=None):
if fopt is None or xopt is None or fx < fopt: if fopt is None or xopt is None or fx < fopt:
fopt = fx fopt = fx
xopt = x xopt = x
return (fopt, xopt, n_trials) if with_count else (fopt, xopt) return fopt, xopt, n_trials
def quasirandom_cube(objective, n_trials, n_dim, with_count): def quasirandom_cube(objective, n_trials, n_dim):
# http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ # http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/
magic = phi(n_dim) magic = phi(n_dim)
alpha = np.zeros(n_dim) alpha = np.zeros(n_dim)
@ -32,4 +32,4 @@ def quasirandom_cube(objective, n_trials, n_dim, with_count):
if best_so_far is None or fx < best_so_far[0]: if best_so_far is None or fx < best_so_far[0]:
best_so_far = (fx, x) best_so_far = (fx, x)
fopt, xopt = best_so_far fopt, xopt = best_so_far
return (fopt, xopt, n_trials) if with_count else (fopt, xopt) return fopt, xopt, n_trials

View File

@ -23,7 +23,7 @@ def make_scipy(method, *, jacobian=None, hessian=None):
# "trust-krylov", # "trust-krylov",
), method ), method
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
prng = np.random.default_rng() prng = np.random.default_rng()
_objective = wrap_untrustworthy( _objective = wrap_untrustworthy(
objective, n_trials, raising=True, bounding="sine" objective, n_trials, raising=True, bounding="sine"
@ -107,8 +107,7 @@ def make_scipy(method, *, jacobian=None, hessian=None):
# TODO: run without this, try to minimize number of attempts (i.e. list length) # TODO: run without this, try to minimize number of attempts (i.e. list length)
# if len(checks) >= 5: print(method, [b - a for a, b in zip(checks, checks[1:])]) # if len(checks) >= 5: print(method, [b - a for a, b in zip(checks, checks[1:])])
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = f"scipy_{method.replace('-', '').lower()}" name = f"scipy_{method.replace('-', '').lower()}"
if jacobian == "2-point": if jacobian == "2-point":
@ -127,7 +126,7 @@ def make_scipy(method, *, jacobian=None, hessian=None):
return f return f
def scipy_basinhopping_cube(objective, n_trials, n_dim, with_count): def scipy_basinhopping_cube(objective, n_trials, n_dim):
progress = 1e-2 # TODO: make configurable? progress = 1e-2 # TODO: make configurable?
# NOTE: could also callbacks to extract solutions instead of wrapping objective functions? # NOTE: could also callbacks to extract solutions instead of wrapping objective functions?
@ -151,10 +150,10 @@ def scipy_basinhopping_cube(objective, n_trials, n_dim, with_count):
) )
fopt, xopt, feval_count = res.fun, res.x, res.nfev fopt, xopt, feval_count = res.fun, res.x, res.nfev
# print("success:", res.success) # print("success:", res.success)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt) return fopt, xopt, feval_count
def scipy_direct_cube(objective, n_trials, n_dim, with_count): def scipy_direct_cube(objective, n_trials, n_dim):
bounds = scopt.Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = scopt.Bounds([0.0] * n_dim, [1.0] * n_dim)
# TODO: try different values of eps. default 0.0001 # TODO: try different values of eps. default 0.0001
res = scopt.direct( res = scopt.direct(
@ -166,10 +165,10 @@ def scipy_direct_cube(objective, n_trials, n_dim, with_count):
) )
fopt, xopt, feval_count = res.fun, res.x, res.nfev fopt, xopt, feval_count = res.fun, res.x, res.nfev
# print("success:", res.success) # print("success:", res.success)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt) return fopt, xopt, feval_count
def scipy_direct_l_cube(objective, n_trials, n_dim, with_count): def scipy_direct_l_cube(objective, n_trials, n_dim):
bounds = scopt.Bounds([0.0] * n_dim, [1.0] * n_dim) bounds = scopt.Bounds([0.0] * n_dim, [1.0] * n_dim)
# TODO: try different values of eps. default 0.0001 # TODO: try different values of eps. default 0.0001
res = scopt.direct( res = scopt.direct(
@ -183,7 +182,7 @@ def scipy_direct_l_cube(objective, n_trials, n_dim, with_count):
) )
fopt, xopt, feval_count = res.fun, res.x, res.nfev fopt, xopt, feval_count = res.fun, res.x, res.nfev
# print("success:", res.success) # print("success:", res.success)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt) return fopt, xopt, feval_count
def make_shgo(method="cobyla", init="large", it=1, mei=False, li=False, ftol=12): def make_shgo(method="cobyla", init="large", it=1, mei=False, li=False, ftol=12):
@ -199,7 +198,7 @@ def make_shgo(method="cobyla", init="large", it=1, mei=False, li=False, ftol=12)
# https://docs.scipy.org/doc/scipy/reference/optimize.minimize-cobyla.html # https://docs.scipy.org/doc/scipy/reference/optimize.minimize-cobyla.html
pass pass
def f(objective, n_trials, n_dim, with_count): def f(objective, n_trials, n_dim):
_objective = wrap_untrustworthy( _objective = wrap_untrustworthy(
objective, n_trials, bounding="clip", raising=True objective, n_trials, bounding="clip", raising=True
) )
@ -220,8 +219,7 @@ def make_shgo(method="cobyla", init="large", it=1, mei=False, li=False, ftol=12)
) )
except ExhaustedTrialsError: except ExhaustedTrialsError:
pass pass
fopt, xopt, feval_count = _objective(final) return _objective(final)
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
name = f"shgo_{method}" name = f"shgo_{method}"
if it != 1: if it != 1:

View File

@ -380,7 +380,7 @@ def main(argv, display=True):
note( note(
f"Using {opt_name} to optimize {obj_realname} ({obj_name}) [{run}] ..." f"Using {opt_name} to optimize {obj_realname} ({obj_name}) [{run}] ..."
) )
_ = optimizer(wrapped, n_trials=n_trials, n_dim=n_dim, with_count=False) _ = optimizer(wrapped, n_trials=n_trials, n_dim=n_dim)
fopt, xopt = wrapped.finish() fopt, xopt = wrapped.finish()
result = (fopt, opt_name, wrapped.history) result = (fopt, opt_name, wrapped.history)
results.setdefault(obj_name, []).append(result) results.setdefault(obj_name, []).append(result)

View File

@ -32,6 +32,7 @@ EVOLOPY_OPTIMIZERS = [
FCMAES_OPTIMIZERS = [ FCMAES_OPTIMIZERS = [
make_biteopt(1), make_biteopt(1),
make_biteopt(2), make_biteopt(2),
make_biteopt(3),
make_csma(3), make_csma(3),
make_da(False), # TODO: fix local=True case. make_da(False), # TODO: fix local=True case.
] + [ ] + [