Reputation: 35
I have searched about this a bit, and couldn't solve this problem. I am working with the minimize function from scipy.optimize, and keep getting the error:
'str' object not callable.
My code is complicated, so I looked up an easy example online to make sure I was inputting everything correctly, and still got the same error. Here is the easy example I found on youtube:
https://www.youtube.com/watch?v=cXHvC_FGx24
import numpy as np
from scipy.optimize import minimize
def objective(x):
x1=x[0]
x2=x[1]
x3=x[2]
x4=x[3]
return x1*x4*(x1+x2+x3)+x3
def constraint1(x):
return x[0]*x[1]*x[2]*x[3]-25
def constraint2(x):
sum_sq = np.sum(np.square(x))
return sum_sq-40
x0=[1,5,5,1]
b=(1, 5)
bnds = (b,b,b,b)
con1 = {'type':'ineq','fun':'constraint1'}
con2 = {'type':'eq','fun':'constraint2'}
cons=[con1,con2]
sol=minimize(objective, x0, method='SLSQP',bounds=bnds,constraints=cons)
This is code directly from an example on youtube, that appears to be working correctly on someone else's machine, but not mine. What have I got wrong here?
[EDIT: If I remove the contraints, then it works fine. What is wrong with the way I am entering the constraint functions?]
Thank you so much for the time.
Upvotes: 1
Views: 792
Reputation: 7157
The error is in your constraints. You're passing a string instead of the functions for your constraints. Just change it to:
con1 = {'type':'ineq','fun': constraint1 }
con2 = {'type':'eq','fun': constraint2 }
cons=[con1,con2]
Upvotes: 3