Operations Research Asked on December 5, 2021
I’m trying to find a way of setting the values of a binary MVar object (which is my decision variable) of size n to {-1,1}. Right now I have a vector that can either take values 0 or 1 (due to binary condition), but instead of 0 and 1, I need the variable to take the values -1 and 1. This is what I tried doing, but I don’t think it’s working:
x = m.addMVar(shape = n, vtype = GRB.BINARY, name = 'X') #My decision variable
y = m.addMVar(shape = n, vtype = GRB.INTEGER, name = 'Y') #Auxiliar decision variable
m.addConstr(y == 2*x - 1) #Transformation from {0,1} to {-1,1}
m.setObjective(#function with y as decision variable)
You need to change the lower bound of the auxiliary variables, i.e.,
y = m.addMVar(shape = n, lb = -1, vtype = GRB.INTEGER, name = 'Y').
Note that variables in Gurobi are usually defined between 0 and infinity except binary.
Answered by Sebastian Kraul on December 5, 2021
If the shape n is for example a list of indices, then you need to index the variables when you add the constraints and loop through and each index:
m.addConstrs(y[i] == 2*x[i] - 1 for i in n)
Also note the “s” added to the addConstrs.
Answered by Mason on December 5, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP