Python pymoo - 将自变量作为参数传递
Python pymoo - Passing independent variables as arguments
我正在使用 pymoo 包进行多重 objective 优化,但我在设置模型时遇到了问题,因为在尝试将其他自变量作为参数传递时出现错误(除了正在优化)。
我尝试按照 getting_started 示例 (https://pymoo.org/getting_started.html) 进行 OOP 和函数式编程。我的 objective 函数有自变量 t、total 和 G,其中 t 和 total 是数组,G 是标量。我试着像这样传递它们:
class MyProblem(Problem):
def __init__(self):
super().__init__(n_var = 3,
n_obj = 2,
n_constr = 0,
xl = np.array([0.0.0,0.0, -0.5]),
xu = np.array([0.8, 10.0, 0.9]),
elementwise_evaluation = True)
def _evaluate(self, p, out, total, G, t): # *args = [total, G, t]
f1 = 1/3*total*(1+2*((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
f2 = 1/3*total*G*(1-((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
out["F"] = np.column_stack([f1, f2])
elementwise_problem = MyProblem()
problem = elementwise_problem
导致:
TypeError: _evaluate() got an unexpected keyword argument 'algorithm'
p 是我要优化的三个参数的列表。
使用函数式编程,我找不到可以在 FunctionalProblem 对象中传递 args 的位置,所以我这样做了:
objs = [
lambda p, total, t: 1/3*total*(1+2*((p[0]-p[2])*np.exp(-t/p[1]) + p[2])),
lambda p, total, t, G: 1/3*total*G*(1-((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
]
constr_ieq = []
functional_problem = FunctionalProblem(3,
objs,
constr_ieq = constr_ieq,
xl = np.array([0.0, 0.01, -0.1]),
xu = np.array([0.8, 50.0, 0.8]))
problem = functional_problem
这导致:
TypeError: () missing 2 required positional arguments: 'total' and 't'
其余代码(算法和终止对象等)与 Getting_started 示例中的代码相同,因为我只是想 运行 现在..
有没有人尝试过使用 pymoo 传递参数并且知道如何正确地做到这一点?
您可以在 MyProblem
class 中定义自变量,然后
def _evaluate(self, p, out):
f1 = 1/3*self.total*(1+2*((p[0]-p[2])*np.exp(-self.t/p[1]) + p[2]))
f2 = 1/3*self.total*self.G*(1-((p[0]-p[2])*np.exp(-self.t/p[1]) + p[2]))
我正在使用 pymoo 包进行多重 objective 优化,但我在设置模型时遇到了问题,因为在尝试将其他自变量作为参数传递时出现错误(除了正在优化)。 我尝试按照 getting_started 示例 (https://pymoo.org/getting_started.html) 进行 OOP 和函数式编程。我的 objective 函数有自变量 t、total 和 G,其中 t 和 total 是数组,G 是标量。我试着像这样传递它们:
class MyProblem(Problem):
def __init__(self):
super().__init__(n_var = 3,
n_obj = 2,
n_constr = 0,
xl = np.array([0.0.0,0.0, -0.5]),
xu = np.array([0.8, 10.0, 0.9]),
elementwise_evaluation = True)
def _evaluate(self, p, out, total, G, t): # *args = [total, G, t]
f1 = 1/3*total*(1+2*((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
f2 = 1/3*total*G*(1-((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
out["F"] = np.column_stack([f1, f2])
elementwise_problem = MyProblem()
problem = elementwise_problem
导致:
TypeError: _evaluate() got an unexpected keyword argument 'algorithm'
p 是我要优化的三个参数的列表。
使用函数式编程,我找不到可以在 FunctionalProblem 对象中传递 args 的位置,所以我这样做了:
objs = [
lambda p, total, t: 1/3*total*(1+2*((p[0]-p[2])*np.exp(-t/p[1]) + p[2])),
lambda p, total, t, G: 1/3*total*G*(1-((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
]
constr_ieq = []
functional_problem = FunctionalProblem(3,
objs,
constr_ieq = constr_ieq,
xl = np.array([0.0, 0.01, -0.1]),
xu = np.array([0.8, 50.0, 0.8]))
problem = functional_problem
这导致:
TypeError: () missing 2 required positional arguments: 'total' and 't'
其余代码(算法和终止对象等)与 Getting_started 示例中的代码相同,因为我只是想 运行 现在..
有没有人尝试过使用 pymoo 传递参数并且知道如何正确地做到这一点?
您可以在 MyProblem
class 中定义自变量,然后
def _evaluate(self, p, out):
f1 = 1/3*self.total*(1+2*((p[0]-p[2])*np.exp(-self.t/p[1]) + p[2]))
f2 = 1/3*self.total*self.G*(1-((p[0]-p[2])*np.exp(-self.t/p[1]) + p[2]))