2

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

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

在此处输入图像描述

但是提高 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")

谢谢

4

0 回答 0