From eea778a67cff6442be82d3f294c8e702c25114a5 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 6 May 2023 20:42:23 -0700 Subject: [PATCH] add optimizers from evolopy --- evolopycube2.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ notwacube.py | 10 +++++++- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 evolopycube2.py diff --git a/evolopycube2.py b/evolopycube2.py new file mode 100644 index 0000000..d9473b4 --- /dev/null +++ b/evolopycube2.py @@ -0,0 +1,67 @@ +from utils import wrap_untrustworthy, final, ExhaustedTrialsError +import numpy as np + + +def make_evolopy(optimizer_name, popsize=None): + from evolopy.BAT import BAT + from evolopy.CS import CS + from evolopy.DE import DE + from evolopy.FFA import FFA + from evolopy.GA import GA + from evolopy.GWO import GWO + from evolopy.HHO import HHO + from evolopy.JAYA import JAYA + from evolopy.MFO import MFO + from evolopy.MVO import MVO + from evolopy.PSO import PSO + from evolopy.SCA import SCA + from evolopy.SSA import SSA + from evolopy.WOA import WOA + from unittest.mock import patch + + def no_print(*args, **kwargs): + pass + + optimizers = dict( + BAT=BAT, + CS=CS, + DE=DE, + FFA=FFA, + GA=GA, + GWO=GWO, + HHO=HHO, + JAYA=JAYA, + MFO=MFO, + MVO=MVO, + PSO=PSO, + SCA=SCA, + SSA=SSA, + WOA=WOA, + ) + optimizers = { + k: patch("builtins.print", no_print)(v) for k, v in optimizers.items() + } + optimizer = optimizers[optimizer_name.upper()] + + def f(objective, n_trials, n_dim, with_count): + # ps = n_dim if popsize is None else popsize + # 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 + if optimizer_name.upper() == "GA" and ps & 1: + ps += 1 # force popsize to be even + + _objective = wrap_untrustworthy( + objective, n_trials, bounding="sine", raising=True + ) + try: + optimizer(_objective, 0.0, 1.0, n_dim, ps, n_trials) + except ExhaustedTrialsError: + pass + + fopt, xopt, feval_count = _objective(final) + return (fopt, xopt, feval_count) if with_count else (fopt, xopt) + + name = f"evolopy_{optimizer.__name__.lower()}" + name += f"_auto" if popsize is None else f"_ps{popsize:02}" + f.__name__ = name + "_cube" + return f diff --git a/notwacube.py b/notwacube.py index ebe4ad4..fb000e2 100644 --- a/notwacube.py +++ b/notwacube.py @@ -1,4 +1,5 @@ from dlibcube2 import dlib_cube +from evolopycube2 import make_evolopy from nevergradcube2 import NEVERGRAD2_OPTIMIZERS from nloptcube2 import nlopt_neldermead_cube from notwacube2 import make_birect, make_mercury, make_soo @@ -55,6 +56,12 @@ NOTWA_OPTIMIZERS = [ make_soo(deepness=53, K=2), ] +EVOLOPY_OPTIMIZERS = [ + make_evolopy(name, popsize) + for name in "BAT CS DE FFA GA GWO HHO JAYA MFO MVO PSO SCA SSA WOA".split() + for popsize in (None, 5, 10, 15) +] + def collect_everything(): G = globals().values() @@ -130,8 +137,9 @@ else: book_of_optimizers = dict( baseline=BASELINE_OPTIMIZERS, everything=FUCKING_EVERYTHING, - shgo=SHGO_OPTIMIZERS, + evolopy=EVOLOPY_OPTIMIZERS, negative=PREVIOUSLY_NEGATIVE, positive=PREVIOUSLY_POSITIVE, + shgo=SHGO_OPTIMIZERS, whitelisted=WHITELISTED_OPTIMIZERS, )