Scipy Objective 函数
Scipy Objective function
我正在尝试将我的 gurobi 代码转换为 scipy,但我在定义 objective 函数时遇到了问题。当测试我是否正确定义函数时,我收到错误:
TypeError: 'float' object is not iterable
代码在这里:
import pandas as pd
import numpy as np
import scipy as sp
from scipy.optimize import minimize
import matplotlib.pyplot as plt
%matplotlib inline
step=80
f1load=[44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48]
fload=f1load[0:step+1]
i1load=[40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]
iload=i1load[0:step+1]
#Following command converts fload&iload to arrays and subtracts them from each other
load1=np.array(iload)-np.array(fload)
load1
#This command converts array to list so we can use it as a list in the rest of the code
load2=load1.tolist()
load=load2
x = np.zeros(80)
x = x.tolist()
def objective(x,load):
# this line is from my gurobi code obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
for i in range(step):
obj1 = sum((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])))
obj2 = obj2 + obj1
return obj2
objective(x,load)
错误的完整堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-cef8470baf0d> in <module>
----> 1 objective(x,load)
<ipython-input-25-a9de3ab9ff2b> in objective(x, load)
2 # obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
3 for i in range(step):
----> 4 obj1 = sum((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])))
5 obj2 = obj2 + obj1
6 return obj2
TypeError: 'float' object is not iterable
这一行:
obj1 = sum((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])))
括号内的表达式 (load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i]))
的计算结果为 float
,并且对该表达式调用 sum
会给出错误,因为 sum
函数需要一个可迭代的参数。
我正在尝试将我的 gurobi 代码转换为 scipy,但我在定义 objective 函数时遇到了问题。当测试我是否正确定义函数时,我收到错误:
TypeError: 'float' object is not iterable
代码在这里:
import pandas as pd
import numpy as np
import scipy as sp
from scipy.optimize import minimize
import matplotlib.pyplot as plt
%matplotlib inline
step=80
f1load=[44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48]
fload=f1load[0:step+1]
i1load=[40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]
iload=i1load[0:step+1]
#Following command converts fload&iload to arrays and subtracts them from each other
load1=np.array(iload)-np.array(fload)
load1
#This command converts array to list so we can use it as a list in the rest of the code
load2=load1.tolist()
load=load2
x = np.zeros(80)
x = x.tolist()
def objective(x,load):
# this line is from my gurobi code obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
for i in range(step):
obj1 = sum((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])))
obj2 = obj2 + obj1
return obj2
objective(x,load)
错误的完整堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-cef8470baf0d> in <module>
----> 1 objective(x,load)
<ipython-input-25-a9de3ab9ff2b> in objective(x, load)
2 # obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
3 for i in range(step):
----> 4 obj1 = sum((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])))
5 obj2 = obj2 + obj1
6 return obj2
TypeError: 'float' object is not iterable
这一行:
obj1 = sum((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])))
括号内的表达式 (load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i]))
的计算结果为 float
,并且对该表达式调用 sum
会给出错误,因为 sum
函数需要一个可迭代的参数。