add optimizers from evolopy

This commit is contained in:
Connor Olding 2023-05-06 20:42:23 -07:00
parent bb755f67b9
commit eea778a67c
2 changed files with 76 additions and 1 deletions

67
evolopycube2.py Normal file
View file

@ -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

View file

@ -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,
)