TypeError in survival.py module of pymoo

Viewed 296

I'm trying to optimize the variable of one of my objects using pymoo. The setup of the problem is:

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_sampling, get_crossover, get_mutation, get_termination
from pymoo.model.problem import Problem
from pymoo.visualization.scatter import Scatter


def print_res(res):
    print(res.X)
    print(res.F)

    # Objective Space
    plot = Scatter(title="Objective Space")
    plot.add(res.F, color="red")
    plot.show()


class MyProblem(Problem):
    def __init__(self, calculatorobj, obj1, obj2, obj3, var_no=1, obj_no=1):
        super().__init__(n_var=var_no,
                         n_obj=obj_no,
                         n_constr=0,
                         xl=[0.1, 0.1],
                         xu=[5, 15],
                         elementwise_evaluation=True)
        self.calculatorobj = calculatorobj
        self.obj1 = obj1
        self.obj2 = obj2
        self.obj3 = obj3
        self.algorithm = NSGA2(
            pop_size=2,
            n_offsprings=5,
            sampling=get_sampling("real_random"),
            crossover=get_crossover("real_sbx", prob=0.9, eta=15),
            mutation=get_mutation("real_pm", eta=20),
            eliminate_duplicates=True
        )
        self.termination = get_termination("n_gen", 10)

    def _evaluate(self, x, out, *args, **kwargs):
        calculator = calculatorobj(obj1, obj2, obj3)
        calculator.var1 = x[0] 
        calculator.var2 = x[1]
        f1 = - calculator.var3
        out["F"] = [f1]

I'm creating my object within the evaluate class as I'm caching the variables within that class and want it to reset for every iteration, so I'm creating a new object each iteration. I then initialize the problem and try to solve it with minimize:

problem = optimize.MyProblem(calculatorobj=calculatorobj, var_no=2, obj_no=1, obj1=obj1,
                              obj2=obj2, obj3=obj3)
res = minimize(problem, problem.algorithm, problem.termination, seed=1, save_history=True, verbose=True)
optimize.print_res(res)

But this error comes up:

File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\optimize.py", line 85, in minimize
    res = algorithm.solve()
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\model\algorithm.py", line 227, in solve
    self._solve(self.problem)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\model\algorithm.py", line 322, in _solve
    self.next()
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\model\algorithm.py", line 244, in next
    self.initialize()
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\model\algorithm.py", line 216, in initialize
    self._initialize()
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\algorithms\genetic_algorithm.py", line 85, in _initialize
    pop = self.survival.do(self.problem, pop, len(pop), algorithm=self,
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\model\survival.py", line 91, in do
    feas, infeas = split_by_feasibility(pop, sort_infeasbible_by_cv=True)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pymoo\model\survival.py", line 138, in split_by_feasibility
    b = (CV <= 0)
TypeError: '<=' not supported between instances of 'NoneType' and 'int'

I was wondering what 'CV' is? I couldn't find the reference for it in the program so I'm confused as to where it's getting the 'NoneType' from.

1 Answers

CV stands for constraint violation and is derived from the constraints set to G during evaluation. Does your problem set n_constr to a number greater than one, but you in fact to not set any constraints?

@EDIT: I just saw you have updated your question. Not sure what you are trying to do. See the template below for optimization.

(you need to set more than one objective to out["F"] to solve a multi-objective problem)

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_sampling, get_crossover, get_mutation, get_termination
from pymoo.model.problem import Problem
from pymoo.visualization.scatter import Scatter
from pymoo.optimize import minimize



class MyProblem(Problem):

    def __init__(self):
        super().__init__(n_var=2,
                         n_obj=1,
                         n_constr=0,
                         xl=[0.1, 0.1],
                         xu=[5.0, 15.0],
                         elementwise_evaluation=True)

    def _evaluate(self, x, out, *args, **kwargs):

        # add your objective calculation HERE
        out["F"] = x[0] ** 2 + x[1] ** 2


problem = MyProblem()

algorithm = NSGA2(
            pop_size=2,
            n_offsprings=5,
            sampling=get_sampling("real_random"),
            crossover=get_crossover("real_sbx", prob=0.9, eta=15),
            mutation=get_mutation("real_pm", eta=20),
            eliminate_duplicates=True
        )

termination = get_termination("n_gen", 10)


res = minimize(problem, algorithm, termination)

print(res.X)
print(res.F)
Related