Decision variable transformation in Gurobi

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)

Answered by Mason on December 5, 2021

Related Questions

What is a general procedure to prove that the LP relaxation of an IP delivers the optimal IP solution?

2  Asked on August 19, 2021 by k88074

CPLEX MIP warm start seems slow down the program?

1  Asked on August 19, 2021 by mengfan-ma

(Iterative?) Solutions to a certain quadratic program with non-convex constraints

2  Asked on August 19, 2021 by cfp

Problem with implementing squared terms in the objective function

1  Asked on August 19, 2021 by poofybridge

Scheduling with setup cost

0  Asked on August 19, 2021 by dirk-nachbar

Is a convex or MILP (without big-M) formulation possible for this problem

1  Asked on August 19, 2021 by batwing

MINLP Solution same as Global Optimum?

2  Asked on August 19, 2021 by clement

Linear objective function with non-linear constraints

2  Asked on August 19, 2021 by fightmilk

Meta papers on operations research

2  Asked on August 19, 2021 by luke599999

Verifying the correctness of KKT conditions

0  Asked on August 19, 2021 by s_scouse

How can I use warm start in C#

1  Asked on August 19, 2021 by fhm-ider

Feasible sets represented as point clouds

1  Asked on August 19, 2021 by harry-cohen

Is this the same as Agent Based DES or something different?

0  Asked on August 19, 2021 by brendan-hill

Column generation when intractable variables appear in the objective function

1  Asked on August 19, 2021 by mostafa

Is there any OR way to solve this problem?

3  Asked on August 19, 2021 by samiczy

Find a particular optimal solution

1  Asked on August 19, 2021 by ljg

How to find all vertices of a polyhedron

3  Asked on August 19, 2021

Can every convex problem use Lagrangian dual method?

0  Asked on August 19, 2021

Does strong duality hold when I dualize only a subset of the constraints?

1  Asked on August 19, 2021 by george-chang