218 lines
7.1 KiB
Python
218 lines
7.1 KiB
Python
import nlopt
|
|
import numpy as np
|
|
|
|
NLOPTIMIZERS = {
|
|
# "gd_mlsl": nlopt.GD_MLSL,
|
|
# "gd_mlsl_lds": nlopt.GD_MLSL_LDS,
|
|
# "gd_stogo": nlopt.GD_STOGO,
|
|
# "gd_stogo_rand": nlopt.GD_STOGO_RAND,
|
|
"gn_ags": nlopt.GN_AGS,
|
|
"gn_crs2_lm": nlopt.GN_CRS2_LM,
|
|
"gn_direct": nlopt.GN_DIRECT,
|
|
"gn_direct_l": nlopt.GN_DIRECT_L,
|
|
"gn_direct_l_noscal": nlopt.GN_DIRECT_L_NOSCAL,
|
|
"gn_direct_l_rand": nlopt.GN_DIRECT_L_RAND,
|
|
"gn_direct_l_rand_noscal": nlopt.GN_DIRECT_L_RAND_NOSCAL,
|
|
"gn_direct_noscal": nlopt.GN_DIRECT_NOSCAL,
|
|
"gn_esch": nlopt.GN_ESCH,
|
|
"gn_isres": nlopt.GN_ISRES,
|
|
"gn_mlsl": nlopt.GN_MLSL,
|
|
"gn_mlsl_lds": nlopt.GN_MLSL_LDS,
|
|
"gn_orig_direct": nlopt.GN_ORIG_DIRECT,
|
|
"gn_orig_direct_l": nlopt.GN_ORIG_DIRECT_L,
|
|
"g_mlsl": nlopt.G_MLSL,
|
|
"g_mlsl_lds": nlopt.G_MLSL_LDS,
|
|
# "ld_auglag": nlopt.LD_AUGLAG,
|
|
# "ld_auglag_eq": nlopt.LD_AUGLAG_EQ,
|
|
# "ld_ccsaq": nlopt.LD_CCSAQ,
|
|
# "ld_lbfgs": nlopt.LD_LBFGS,
|
|
# "ld_lbfgs_nocedal": nlopt.LD_LBFGS_NOCEDAL,
|
|
# "ld_mma": nlopt.LD_MMA,
|
|
# "ld_slsqp": nlopt.LD_SLSQP,
|
|
# "ld_tnewton": nlopt.LD_TNEWTON,
|
|
# "ld_tnewton_precond": nlopt.LD_TNEWTON_PRECOND,
|
|
# "ld_tnewton_precond_restart": nlopt.LD_TNEWTON_PRECOND_RESTART,
|
|
# "ld_tnewton_restart": nlopt.LD_TNEWTON_RESTART,
|
|
# "ld_var1": nlopt.LD_VAR1,
|
|
# "ld_var2": nlopt.LD_VAR2,
|
|
"ln_auglag": nlopt.LN_AUGLAG,
|
|
"ln_auglag_eq": nlopt.LN_AUGLAG_EQ,
|
|
"ln_bobyqa": nlopt.LN_BOBYQA,
|
|
"ln_cobyla": nlopt.LN_COBYLA,
|
|
"ln_neldermead": nlopt.LN_NELDERMEAD,
|
|
"ln_newuoa": nlopt.LN_NEWUOA,
|
|
"ln_newuoa_bound": nlopt.LN_NEWUOA_BOUND,
|
|
"ln_praxis": nlopt.LN_PRAXIS,
|
|
"ln_sbplx": nlopt.LN_SBPLX,
|
|
}
|
|
|
|
|
|
def nlopt_cube_factory(objective, n_trials, n_dim, with_count, method):
|
|
optim = NLOPTIMIZERS[method]
|
|
|
|
feval_count = 0
|
|
best_so_far = None
|
|
|
|
def _objective(x, grad):
|
|
nonlocal feval_count, best_so_far
|
|
fx = objective(x)
|
|
feval_count += 1
|
|
if feval_count <= n_trials:
|
|
if best_so_far is None or fx < best_so_far[0]:
|
|
best_so_far = (fx, x)
|
|
return fx
|
|
|
|
opt = nlopt.opt(optim, n_dim)
|
|
opt.set_lower_bounds([0.0] * n_dim)
|
|
opt.set_upper_bounds([1.0] * n_dim)
|
|
opt.set_min_objective(_objective)
|
|
opt.set_maxeval(n_trials)
|
|
|
|
try:
|
|
opt.optimize([0.5] * n_dim)
|
|
except nlopt.RoundoffLimited as e:
|
|
print( # FIXME: de-uglify this!
|
|
"\033[33m",
|
|
"nlopt_" + method,
|
|
" exited early (",
|
|
type(e).__name__,
|
|
")",
|
|
"\033[0m",
|
|
sep="",
|
|
)
|
|
|
|
assert best_so_far is not None, optimizer.__name__
|
|
fopt, xopt = best_so_far
|
|
|
|
return (fopt, xopt, feval_count) if with_count else (fopt, xopt)
|
|
|
|
|
|
def nlopt_ags_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_ags")
|
|
|
|
|
|
def nlopt_crs2_lm_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_crs2_lm")
|
|
|
|
|
|
def nlopt_direct_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_direct")
|
|
|
|
|
|
def nlopt_direct_l_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_direct_l")
|
|
|
|
|
|
def nlopt_direct_l_noscal_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(
|
|
objective, n_trials, n_dim, with_count, "gn_direct_l_noscal"
|
|
)
|
|
|
|
|
|
def nlopt_direct_lr_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(
|
|
objective, n_trials, n_dim, with_count, "gn_direct_l_rand"
|
|
)
|
|
|
|
|
|
def nlopt_direct_lr_noscal_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(
|
|
objective, n_trials, n_dim, with_count, "gn_direct_l_rand_noscal"
|
|
)
|
|
|
|
|
|
def nlopt_direct_noscal_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(
|
|
objective, n_trials, n_dim, with_count, "gn_direct_noscal"
|
|
)
|
|
|
|
|
|
def nlopt_esch_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_esch")
|
|
|
|
|
|
def nlopt_isres_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_isres")
|
|
|
|
|
|
def nlopt_mlsl_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_mlsl")
|
|
|
|
|
|
def nlopt_mlsl_lds_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_mlsl_lds")
|
|
|
|
|
|
def nlopt_orig_direct_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "gn_orig_direct")
|
|
|
|
|
|
def nlopt_orig_direct_l_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(
|
|
objective, n_trials, n_dim, with_count, "gn_orig_direct_l"
|
|
)
|
|
|
|
|
|
def nlopt_auglag_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_auglag")
|
|
|
|
|
|
def nlopt_auglag_eq_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_auglag_eq")
|
|
|
|
|
|
def nlopt_bobyqa_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_bobyqa")
|
|
|
|
|
|
def nlopt_cobyla_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_cobyla")
|
|
|
|
|
|
def nlopt_neldermead_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_neldermead")
|
|
|
|
|
|
def nlopt_newuoa_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_newuoa")
|
|
|
|
|
|
def nlopt_newuoa_bound_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_newuoa_bound")
|
|
|
|
|
|
def nlopt_praxis_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_praxis")
|
|
|
|
|
|
def nlopt_sbplx_cube(objective, n_trials, n_dim, with_count):
|
|
return nlopt_cube_factory(objective, n_trials, n_dim, with_count, "ln_sbplx")
|
|
|
|
|
|
NLOPT_OPTIMIZERS = [
|
|
# global
|
|
# nlopt_ags_cube, # ValueError: nlopt invalid argument
|
|
nlopt_crs2_lm_cube,
|
|
nlopt_direct_cube,
|
|
nlopt_direct_l_cube,
|
|
nlopt_direct_l_noscal_cube,
|
|
nlopt_direct_lr_cube,
|
|
nlopt_direct_lr_noscal_cube,
|
|
nlopt_direct_noscal_cube,
|
|
nlopt_esch_cube,
|
|
nlopt_isres_cube,
|
|
# nlopt_mlsl_cube, # FIXME: Segmentation fault
|
|
# nlopt_mlsl_lds_cube # FIXME: Segmentation fault,
|
|
nlopt_orig_direct_cube,
|
|
nlopt_orig_direct_l_cube,
|
|
# local
|
|
# nlopt_auglag_cube, # FIXME: Segmentation fault
|
|
# nlopt_auglag_eq_cube, # FIXME: Segmentation fault
|
|
nlopt_bobyqa_cube,
|
|
nlopt_cobyla_cube,
|
|
nlopt_neldermead_cube,
|
|
nlopt_newuoa_cube,
|
|
# nlopt_newuoa_bound_cube, # sadly this times out on shekel. however it wins in nlopt_classic_d21_n550_armor
|
|
# nlopt_praxis_cube, # AssertionError: x is not finite (NaN or Inf or -Inf)
|
|
nlopt_sbplx_cube,
|
|
]
|