3
$\begingroup$

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)
$\endgroup$
3
$\begingroup$

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.

| improve this answer | |
$\endgroup$
3
$\begingroup$

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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.