0

所以只使用 pymoo - 我无法从文档中确定如何传递参数以便评估函数看到它们:

import matplotlib.pyplot as plt
import numpy as np

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.model.problem import Problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter


    class MyProblem(Problem):
        def __init__(self,*args, **kwargs):
            """
            max f1 = X1 <br>
            max f2 = 3 X1 + 4 X2 <br>
            st  X1 <= 20 <br>
                X2 <= 40 <br>
                5 X1 + 4 X2 <= 200 <br>
            """
            print(args)
            super().__init__(n_var=2,
                             n_obj=2,
                             n_constr=1,
                             xl=np.array([0, 0]),
                             xu=np.array([20, 40]))
    
    
        def _evaluate(self, x, out, *args, **kwargs):
            # define both objectives
            f1 = x[:, 0]
            f2 = 3 * x[:, 0] + 4 * x[:, 1]
    
            # we have to negate the objectives because by default we assume minimization
            f1, f2 = -f1, -f2
    
            # define the constraint as a less or equal to zero constraint
            g1 = 5 * x[:, 0] + 4 * x[:, 1] - 200
            print(args)
            out["F"] = np.column_stack([f1, f2])
            out["G"] = g1
    
    
    problem = MyProblem([1,2,3],[3,4,5])
    
    algorithm = NSGA2()
    
    res = minimize(problem,
                   algorithm,
                   ('n_gen', 20000),
                   seed=1,
                   verbose=False)
    #
    # print(res.X)
    # print(res.F)
    # print(dir(res))
    # print(res.opt)
    print(res.F)
    
    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))
    Scatter(fig=fig, ax=ax1, title="Design Space").add(res.X, color="blue").do()
    Scatter(fig=fig, ax=ax2, title="Objective Space").add(res.F, color="red").do()
    plt.show()

我将两个列表传递给 Myproblem 类,我确实在元组 args 中看到了它们,但是当我在评估中的 args 中查找这些列表时,它只会给我一个空元组。

建议?

4

1 回答 1

0

我可以通过在 init 函数中执行 self.parameters=args,然后在评估中使用 self.parameter 来实现我的目标。

于 2021-07-21T14:12:10.370 回答