【发布时间】:2026-02-06 16:00:02
【问题描述】:
我正在使用 MIP 优化库来解决问题,但我无法正确声明目标函数。我在使用 Gurobi 解决之前编写了代码,我只是想把代码翻译过来。我无法用正确的语法重写目标函数,非常感谢任何帮助,谢谢!
from mip import *
from mip import xsum, minimize
import pandas as pd
import numpy as np
import scipy as sp
m = Model()
m = Model(sense=MINIMIZE, solver_name=GRB)
n = 80
# #x is charging, discharging variable
x = [m.add_var(name='x', var_type = INTEGER, lb=-1.5, ub = 1.5) for i in range(n)]
# #Y is SOC variable
Y = [m.add_var(name='Y', var_type = CONTINUOUS, lb=0, ub = 100) for i in range(n+1)]
# # Add constraint: SOC[start]=50, initial SOC
m += Y[0] == 50 , 'c1'
# #Final targeted SOC
m += Y[n] >= 65 , 'c2'
# # This is the constrain defines relationship between SOCs and charging steps. 3.75% SOC increase decrease are for 15min steps
m += (Y[i+1]-Y[i] == 3.75*x[i] for i in range(n)), 'c0'
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]
(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
# #Objective function. 6 comes from capacity of inverter. The line below is the Gurobi version of the obj function, that works.
# obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
m.objective = xsum((load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])) for i in range (n))
错误的完整堆栈跟踪:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-31-ca98b7470c5c> in <module>
2 # obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
3 # m.objective = minimize(xsum(c[i]*x[i] for i in range(n)))
----> 4 m.objective = minimize(xsum((load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])) for i in range (n)))
5 # m.objective = minimize(xsum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n))))
~/opt/anaconda3/lib/python3.7/site-packages/mip/model.py in xsum(terms)
1321 """
1322 result = LinExpr()
-> 1323 for term in terms:
1324 result.add_term(term)
1325 return result
<ipython-input-31-ca98b7470c5c> in <genexpr>(.0)
2 # obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
3 # m.objective = minimize(xsum(c[i]*x[i] for i in range(n)))
----> 4 m.objective = minimize(xsum((load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])) for i in range (n)))
5 # m.objective = minimize(xsum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n))))
~/opt/anaconda3/lib/python3.7/site-packages/mip/entities.py in __mul__(self, other)
137
138 def __mul__(self: "LinExpr", other: Union[int, float]) -> "LinExpr":
--> 139 assert isinstance(other, (int, float))
140 result = self.copy()
141 result.__const *= other
AssertionError:
【问题讨论】:
-
从未使用过该库(除了运行示例),但
(load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])显然是一些非平凡对象的乘法,其中该库不支持重载运算符。它基本上是expr * expr,它快速深入到计算机代数中(尽管这里仅限于仿射表达式)。 1) 您可能需要手动将其相乘以形成平面表达式。 2) 这看起来是二次的,我不认为这个库支持非线性问题。 Gurobi 可能将其解决为 MIQP/MISOCP,但出于某种原因,此库可能称为 MIP
标签: python pandas optimization mixed-integer-programming