[英]I am getting 'TypeError: 'numpy.float64' object is not callable'
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:我有一个测试函数,我试图使用 scipy.optimize 最小化,但我得到了上面的错误。我的测试函数 A 具有 0-100 之间的变量。这些变量的总和(4)应该加起来 100 .sum(A)=100。我尝试通过以前的类似案例解决错误读取,但我不能。解决方案应该是 2500,这是最小值,因为我用 gekko 优化器解决了,现在正试图切换到 Scipy。任何人都可以告诉我或告诉我哪里做错了?代码如下:
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.您需要将函数名称发送到 minimize() 而不是调用它。 The changed code would be
更改后的代码将是
sol = minimize(test_function, x0, method='SLSQP', bounds=bnds, constraints=con1)
if you want to optimize test_function.如果你想优化 test_function。 Replace test_function with constraint1 if you want to optimize for constraint1.
如果要针对约束 1 进行优化,请将test_function替换为约束1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.