Я пытаюсь преобразовать свой код gurobi в scipy, но у меня возникают проблемы с определением целевой функции. При проверке, чтобы увидеть, правильно ли я определил функцию, я получаю сообщение об ошибке:
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