I am using Scipy. optimize for my optimization problem. i have two optimisations inner and outer, and i want to implement the fitness of the first optimisation with the second one. after a good time of researching i achived this:
` global fitness fitness=100
def obj(x):
out=sum(x)+ fitness
return out
def const(x):
out=x[0]*x[1]**2-8
return out
bnds = ((0, 20), (0, 20))
def cbck(x):
def const2(y):
return (y[0])+y[1]-6
cons2 = ({'type': 'ineq', 'fun': const2})
#first opt
res = minimize(const, (x[0],x[1]), method='SLSQP', bounds=bnds,constraints=cons2)
z=res.x
f_z=max(0,obj(z))
fitness=100*f_z
print(x)
print(fitness)
cons = ({'type': 'ineq', 'fun': const} )
#second opt
res2 = minimize(obj, x0=(5,5), method='SLSQP', bounds=bnds,
constraints=cons,callback=cbck,options={ 'ftol': 1e-6, 'disp': False,'maxiter': 100, 'iprint':2,'eps':1.e-6})
print(f"Solution: {res2.x}, Fitness: {res2.fun}")
`
it does not work as i EXPECTED.
: there is no consideration for the new apended fitness even that the total fitness is increasing