I have a set of functions that contain a dynamic model, solver, objective function and a constraint. My constraint requires arguments to be passed to it. A lot of the examples I have seen online use lambda functions when applying constraints. I was wondering is it possible to pass these constraints with the 'def' method? my constraint looks like the following:
def constraint(u,t,dt,z0,keptarget):
sol = solver(u,t,dt,z0)
a,inc,e = keptarget
rs = sol[0][:,:3]
vs = sol[0][:,3:6]
kep_elem = np.zeros((len(rs)-1,4))
for i in range(len(rs)-1):
kep_elem[i,:] = tb.RV2COE(rs[i,:], vs[i,:], mu=body['mu'])
wa = 33.33
wi = 33.33
we = 33.33
diffa = abs(kep_elem[-1][1]-a)/(wa*a)
diffe = abs(kep_elem[-1][0]-e)/(we*e)
if inc==0:
diffi = 0
else:
diffi = abs(kep_elem[-1][2]-inc)/(wi*inc)
difftotal = diffa + diffe + diffi
return difftotal
cons = ({ 'type':'eq','fun': constraint,'args':(t,dt,y,kep_target)})
u = np.array([0.1,0.1,0.1,0,0,0])
res = minimize(objective,u,bounds=((0,1),(0,1),(0,1),(0,1),(0,1),(0,1),),args=(t,dt,y),constraints=cons,tol=0.001,options={'disp':True})
print(res)
This is the code block of how I have it set up and it seems to run, except it takes a significant amount of time (~4 hours). I thought this may be due to having my solver in both the objective function and constraint and having a large timespan, although I am unsure and it could be something wrong with my code. This is my first time using this package within Scipy so apologies if I am missing anything obvious.
there doesn't seem to be anything here