0

so just using pymoo - I can't determine from the documentation how to pass parameters so that the evaluate function sees them:

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()

I pass two lists to the Myproblem class, I do see them in the tuple args, but then when I look for these lists in args within evaluate it just gives me an empty tuple.

Suggestions?

Eigenvalue
  • 1,093
  • 1
  • 14
  • 35

1 Answers1

0

I could get my goal by in the init function doing self.parameters=args, then using self.parameter in evaluate.

Eigenvalue
  • 1,093
  • 1
  • 14
  • 35