Python Gurobi:无法向模型添加约束

时间:2018-09-30 20:48:42

标签: python gurobi mixed-integer-programming

有人可以纠正我这段代码有什么问题吗?

我正尝试写出如图所示的约束。

enter image description here

但是引发TypeError

<ipython-input-189-be5360af354c> in <module>()
      1 balance_energy = m.addConstrs((quicksum(electricity_demand[t, u, app, task] * x[t, u, app, task] + gas_demand[t, u, app, task] * y[t, u, app, task]+ hotwater_demand[t, u, app, task] * z[t, u, app, task] for t in time_slots) ==
      2               (task_electricity_req[app][task] + task_gas_req[app][task] + task_hotwater_req[app][task])
----> 3             for u in users for app in appliances for task in task_appliances[app]), name = "balance_energy")
      4 

model.pxi in gurobipy.Model.addConstrs (../../src/python/gurobipy.c:89287)()

<ipython-input-189-be5360af354c> in <genexpr>(.0)
      1 balance_energy = m.addConstrs((quicksum(electricity_demand[t, u, app, task] * x[t, u, app, task] + gas_demand[t, u, app, task] * y[t, u, app, task]+ hotwater_demand[t, u, app, task] * z[t, u, app, task] for t in time_slots) ==
      2               (task_electricity_req[app][task] + task_gas_req[app][task] + task_hotwater_req[app][task])
----> 3             for u in users for app in appliances for task in task_appliances[app]), name = "balance_energy")
      4 

TypeError: must be str, not int

到目前为止,我已经尝试过

balance_energy = m.addConstrs((quicksum(
  electricity_demand[t, u, app, task] * x[t, u, app, task]
  + gas_demand[t, u, app, task] * y[t, u, app, task]
  + hotwater_demand[t, u, app, task] * z[t, u, app, task]
  for t in time_slots) ==
  (task_electricity_req[app][task]
  + task_gas_req[app][task]
  + task_hotwater_req[app][task])      
  for u in users for app in appliances for task in task_appliances[app]),
  name = "balance_energy")

这里 连续变量

electricity_demand[t, u, app, task]
gas_demand[t, u, app, task]
hotwater_demand[t, u, app, task]

二进制变量

x[t, u, app, task]
y[t, u, app, task]
z[t, u, app, task]

参数:包含整数值的字典

task_electricity_req[app][task] 
task_gas_req[app][task] 
task_hotwater_req[app][task]

解决方案: 做下面的把戏解决了我的问题。

 balance_energy = m.addConstrs((quicksum(electricity_demand[t, u, app, task] * x[t, u, app, task] 
                                         + gas_demand[t, u, app, task] * y[t, u, app, task]
                                         + hotwater_demand[t, u, app, task] * z[t, u, app, task] for t in time_slots) ==
               (float(task_electricity_req[app][task]) + float(task_gas_req[app][task]) + float(task_hotwater_req[app][task]))      
             for u in users for app in appliances for task in task_appliances[app]), name = "balance_energy")

谢谢

0 个答案:

没有答案