简体   繁体   English

如何在 Gurobi python 界面中创建二进制变量?

[英]How to create binary variables in Gurobi python interface?

I am new to Gurobi Python interface.我是 Gurobi Python 界面的新手。 It would be great somebody guides me to this problem.有人指导我解决这个问题会很棒。

I want to create a Binary Decision variable using the Python interface.我想使用 Python 接口创建一个二元决策变量。

The binary variable denoted by $X_{k, u, i, j}$ - indicates whether the task j of appliance i of user u at time slot k processed or not.用$X_{k, u, i, j}$表示的二元变量-指示用户u在时隙k的设备i的任务j是否被处理。 1 = processed , 0 = not processed. 1 = 已处理,0 = 未处理。

I have tried like this,but not able to get the desired output.我试过这样,但无法获得所需的输出。

 x = m.addVars(time_slots, users, appliances, task_appliances, vtype = GRB.BINARY, name = 'x')

Output输出

x[k1,u1,washingmachine, washingmachine], x[k1,u1,washingmachine, dryer] x[k1,u1,洗衣机, 洗衣机], x[k1,u1,洗衣机, 烘干机]

Where 

time_slots = ['k1', 'k2','k3', 'k4', 'k5', 'k6', 'k7', 'k8', 'k9', 'k10']
users = ['u1', 'u2', 'u3', 'u4', 'u5']
appliances = ['washingmachine', 'dryer', 'dishwasher', 'refrigerator', 'gashob1', 'gashob2'] 



 task_appliances = {'washingmachine':['movement', 'heating','washing', 'cooling', '1strinse', '2ndrinse', '3rdrinse'], 'dryer': ['drying1', 'drying2', 'drying3', 'drying4', 'drying5', 'drying6', 'drying7', 'drying8'], 'dishwasher': ['movement', 'heating', 'wash', '1strinse', 'drain', 'heating','2ndrinse', 'drain_and_dry'], 'refrigerator': ['cooling1', 'cooling2', 'cooling3', 'cooling4','cooling5', 'cooling6', 'cooling7','cooling8', 'cooling9', 'cooling10'],'gashob1':['heating'], 'gashob2':['heating']}

How can i create a binary variable represents the x[k1,u1,washingmachine, movement], x[k1,u1,washingmachine, heating] ,.. like this for all washing machine tasks and x[k1,u1,dryer, drying1], x[k1,u1,dryer, drying2] ,.. like this for all dryer tasks and so on for all the appliances.我如何创建一个二进制变量表示x[k1,u1,washingmachine, movement], x[k1,u1,washingmachine, heating] ,.. 像这样适用于所有洗衣机任务和x[k1,u1,dryer, drying1], x[k1,u1,dryer, drying2] ,.. 像这样适用于所有烘干机任务等等所有电器。

First of all note that in task_appliances the task heating is twice for the dishwasher, after fixing this you could use a list comprehension to get a list of tuples and then use m.addVars() :首先请注意,在 task_appliances 中,洗碗机的任务加热是两次,修复此问题后,您可以使用列表理解来获取元组列表,然后使用m.addVars()

from gurobipy import *

# Your lists here

m = Model()
vars_tup = [(t, u, app, task) for t in time_slots for u in users for app in appliances for task in task_appliances[app]]
x = m.addVars(vars_tup, vtype=GRB.BINARY, name="x")
# Your constraints and objective function..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM