简体   繁体   中英

How to create binary variables in Gurobi python interface?

I am new to Gurobi Python interface. It would be great somebody guides me to this problem.

I want to create a Binary Decision variable using the Python interface.

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. 1 = processed , 0 = not processed.

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]

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.

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() :

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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