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.