I have a test function that i am trying to minimize using scipy.optimize but i get the error above.My test function A has for variables which are between 0-100.And the sum of these variables(4) should add up to 100.sum(A)=100.I tried solving the error reading through previous similar cases but I could not.The solution should be 2500,that is the minimum because i solved with gekko optimizer and now am trying to switch to Scipy.Can anyone tell me or show me where am doing wrong?The code is below:
import numpy as np
from scipy.optimize import minimize
def test_function(x):
return np.dot(x, x)
A = np.zeros(4)
# bnds = ([0, 100], [0, 100], [0, 100], [0, 100])
bnds = tuple((0, 100) for x in range (len(A)))
x0 = [1, 5, 5, 1]
def constraint1(A):
sum = 100
for i in range(4):
sum = sum - A[i]
return sum
con1 = {'type': 'ineq', 'fun': constraint1}
sol = minimize(test_function(A), x0, method='SLSQP', bounds=bnds, constraints=con1)
the error is below;
Traceback (most recent call last):
File "C:/Users/Lenovo/Desktop/truss-opt/optimisation2/test_example.py", line 24, in <module>
sol = minimize(test_function(A), x0, method='SLSQP', bounds=bnds, constraints=con1)
File "C:\Users\Lenovo\Anaconda3\envs\practice1\lib\site-packages\scipy\optimize\_minimize.py", line 608, in minimize
constraints, callback=callback, **options)
File "C:\Users\Lenovo\Anaconda3\envs\practice1\lib\site-packages\scipy\optimize\slsqp.py", line 399, in _minimize_slsqp
fx = func(x)
File "C:\Users\Lenovo\Anaconda3\envs\practice1\lib\site-packages\scipy\optimize\optimize.py", line 326, in function_wrapper
return function(*(wrapper_args + args))
TypeError: 'numpy.float64' object is not callable
Process finished with exit code 1
You need to send the function name to minimize() instead of calling it. The changed code would be
sol = minimize(test_function, x0, method='SLSQP', bounds=bnds, constraints=con1)
if you want to optimize test_function. Replace test_function with constraint1 if you want to optimize for constraint1.