我最近使用 pymoo 来解决离散优化问题。我正在尝试使用差分进化算法来解决问题。按照文档,我尝试了几件事..
import numpy as np
from pymoo.factory import get_algorithm, get_crossover, get_mutation, get_sampling
from pymoo.optimize import minimize
from pymoo.core.problem import Problem
class MyProblem(Problem):
def __init__(self):
super().__init__(n_var=2, n_obj=1, n_constr=1, xl=0, xu=10, type_var=int)
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = - np.min(x * [3, 1], axis=1)
out["G"] = x[:, 0] + x[:, 1] - 10
method = get_algorithm("de",pop_size=100,sampling=get_sampling("int_random"),
mutation=get_mutation("int_pm", eta=3.0),eliminate_duplicates=True)
res = minimize(MyProblem(),method, seed=1,save_history=True)
print("Best solution found: %s" % res.X)
print("Function value: %s" % res.F)
print("Constraint violation: %s" % res.CV)
并且效果很好。因此推断 pymoo 支持离散变量的 DE。
我正在尝试在pymoo中使用可用于DE算法的示例..
from pymoo.algorithms.soo.nonconvex.de import DE
from pymoo.factory import get_problem
from pymoo.operators.sampling.lhs import LHS
from pymoo.optimize import minimize
problem = get_problem("ackley", n_var=10)
algorithm = DE(
pop_size=100,
sampling=LHS(),
variant="DE/rand/1/bin",
CR=0.3,
dither="vector",
jitter=False
)
res = minimize(problem,
algorithm,
seed=1,
verbose=False)
print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))
我面临的问题是我不确定type_var
在此示例中将参数传递到何处。我尝试过 get_prboblem() 但它说未定义。在文档中找不到它..有人可以帮助我在哪里传递type_var
以使 DE 作为离散优化工作吗?