PuLP Optimiztion, не могу добавить в модель целевую функцию - PullRequest
0 голосов
/ 25 февраля 2020

Я использую PuLP для решения проблемы оптимизации. Моя целевая функция содержит умножение непостоянных значений. Код:

import pulp as plp
from pulp import *
opt_model = plp.LpProblem(name="PHEV") #plug-in hybrid electric vehicle
# adding variables
steps = 80
iSOC = 50
tSOC = 65
set_I = range(0, steps+1)
x_var = {(i): pulp.LpVariable(name = 'x', lowBound=-1, upBound=1, cat='Integer') for i in set_I}
Y_var = {(i): pulp.LpVariable(name = 'Y', lowBound=0, upBound=100, cat='Continuous') for i in set_I}
opt_model += Y_var[0] == iSOC
opt_model += Y_var[len(Y_var)-1] == tSOC
opt_model.addConstraint(plp.LpConstraint(e = plp.lpSum(Y_var[i+1] - Y_var[i] - (3.75*x_var[i]) for i in range(steps)),sense=plp.LpConstraintEQ, rhs= 0)
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]
load1=np.array(iload)-np.array(fload)
load1
load2 = load1.tolist()
load=load2
objective = plp.lpSum((load[i+1]-(6*x_var[i])) * (load[i+1]-(6*x_var[i])) for i in set_I )

Полная трассировка стека ошибок:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-98e34fc93d51> in <module>
----> 1 objective = plp.lpSum((load[i+1]-(6*x_var[i])) * (load[i+1]-(6*x_var[i])) for i in set_I )
      2 
      3 
      4 # objective = plp.lpSum(x_vars[i,j] * c[i,j]
      5 #                     for i in set_I

~/opt/anaconda3/lib/python3.7/site-packages/pulp/pulp.py in lpSum(vector)
   2052     :param vector: A list of linear expressions
   2053     """
-> 2054     return LpAffineExpression().addInPlace(vector)
   2055 
   2056 def lpDot(v1, v2):

~/opt/anaconda3/lib/python3.7/site-packages/pulp/pulp.py in addInPlace(self, other)
    740         elif (isinstance(other,list)
    741               or isinstance(other, Iterable)):
--> 742            for e in other:
    743                 self.addInPlace(e)
    744         else:

<ipython-input-53-98e34fc93d51> in <genexpr>(.0)
----> 1 objective = plp.lpSum((load[i+1]-(6*x_var[i])) * (load[i+1]-(6*x_var[i])) for i in set_I )
      2 
      3 
      4 # objective = plp.lpSum(x_vars[i,j] * c[i,j]
      5 #                     for i in set_I

~/opt/anaconda3/lib/python3.7/site-packages/pulp/pulp.py in __mul__(self, other)
    801             if len(other):
    802                 if len(self):
--> 803                     raise TypeError("Non-constant expressions cannot be multiplied")
    804                 else:
    805                     c = self.constant

TypeError: Non-constant expressions cannot be multiplied

Я видел, что PuLP можно использовать для решения нелинейных программ, запустив пример кода на https://medium.com/opex-analytics/optimization-modeling-in-python-pulp-gurobi-and-cplex-83a62129807a, но я не могу перевести свою целевую функцию в правильный синтаксис. Любая помощь будет принята с благодарностью !!

...