超参数优化HyperparameterProblem
from pymoo.algorithms.hyperparameters import SingleObjectiveSingleRun, HyperparameterProblem from pymoo.algorithms.soo.nonconvex.g3pcx import G3PCX from pymoo.algorithms.soo.nonconvex.optuna import Optuna from pymoo.core.mixed import MixedVariableGA from pymoo.core.parameters import set_params, hierarchical from pymoo.optimize import minimize from pymoo.problems.single import Sphere
algorithm = G3PCX()
problem = get_problem("g1") n_evals = 500
performance = SingleObjectiveSingleRun(problem, termination=("n_evals", n_evals), seed=1)
res = minimize(HyperparameterProblem(algorithm, performance), MixedVariableGA(pop_size=5), termination=('n_evals', 50), seed=1, verbose=False)
hyperparams = res.X print(hyperparams) set_params(algorithm, hierarchical(hyperparams))
res = minimize(Sphere(), algorithm, termination=("n_evals", n_evals), seed=1) print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))
Why does an error occur when dealing with a problem with constraints during hyperparameter optimization?
Exception: ('Problem Error: G can not be set, expected shape (5, 0) but provided (5, 9)', ValueError('cannot reshape array of size 45 into shape (5,0)'))
the problem you are trying to optimize is not a mixed variable problem. (I admit the error message is misleading)