Please, I need your help to fix my code using Pymoo in Python. Next, I will explain what I need to do:
My code starts finding a "value simulated" as follows:
import numpy
value_simulated = numpy.min(numpy.random.uniform(0.1, 1, 1000))
This "value simulated" is used in class "MyProblem" in order to affect the behavior of the constraint "g1" as follows:
from pymoo.core.problem import ElementwiseProblem
class MyProblem(ElementwiseProblem):
def __init__(self):
super().__init__(n_var=2,
n_obj=2,
n_ieq_constr=1,
xl=numpy.array([0.1, 0]),
xu=numpy.array([1, 5]))
def _evaluate(self, x, out, *args, **kwargs):
f1 = x[0]
f2 = (1 + x[1]) / x[0]
if value_simulated <= 1:
a = 0
else:
a = 1
g1 = a * x[0] + a * x[1]
out["F"] = [f1, f2]
out["G"] = [g1]
problem = MyProblem()
I need this "value_simulated" affect constraint "g1" only during generation #1. During next generations "value_simulated" must be replaced by the min value that will be found in the population. By that reason I coded the class "Callback" to detect this min value, but I don't know how to call this value in the next generations in order to replace the "value_simulated" in the problem, callback is coded as follows:
from pymoo.core.callback import Callback
class CustomCallback(Callback):
def __init__(self):
super().__init__()
def notify(self, algorithm):
solutions = algorithm.pop.get("F")
value_f1min = min([row[0] for row in solutions])
return value_f1min
custom_callback = CustomCallback()
I used the function "notify", but I suspect that the right function is "update", but I don't know how to use the function "update". The last part of my code is the following:
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
algorithm = NSGA2(pop_size=100, callback=custom_callback)
res = minimize(problem, algorithm, ('n_gen', 10), seed=1, verbose=True)
F = res.F
Pop = res.pop
final_solutions = res.X
Could you help me please to fix my code in order to affect constaint "g1" correctly in each generation? I will appreciate your help a lot! Thank you.