我打算计算目标函数的最大值。我的编码如下。
import numpy as np
from scipy.optimize import minimize
# this is my objective function
def objective(x,sign=-1.0):
x1=x[0]
x2=x[1]
x3=x[2]
x4=x[3]
x5=x[4]
x6=x[5]
return sign*(sum(x1+x2+x3+x4+x5+x6)) # to get maximum by using '-1' sign
def constraint1(x):
return 5900-(x[0]+x[1]+0.949*x[2]+0.933*x[3]+0.824*x[4]+0.519*x[5])
def constraint2(x):
return 6000-(x[2]+x[3]+0.922*x[4]+0.619*x[5])
def constraint3(x):
return 6450-(x[3]+0.969*x[4]+0.777*x[5])
b1=(0,600)
b2=(0,475)
b3=(0,450)
b4=(0,500)
b5=(0,825)
b6=(0,6800)
bnds=(b1,b2,b3,b4,b5,b6)
cons=[constraint1,constraint2,constraint3]
solution=minimize(objective,x0=None,method='Nelder-Mead', tol=1e-6,
bounds=bnds,constraints=cons)
但我得到了错误:
Traceback (most recent call last):
IndexError: index 1 is out of bounds for axis 0 with size 1
我应该怎么办??? 太感谢了。