# Creating discrete variables in Gurobi

I am currently working on a project and I want to create a (multiple) decision variable in Gurobi that can only take discrete values with a pre-defined step-width (e.g. 0, 5, 10, ...).

I am currently using the command:

capacity = m.addVars(locations, lb=0.0, name="capacity")

However, this command only produces continuous results. I know that by including the optional aspect vType = ... you can define binary variables for example. Unfortunately, I couldn't find anything to extract discrete variables with a pre-defined step-width.

You can use vType=GRB.INTEGER to specify a discrete variable. A minimum working example will look as follows:

import gurobipy as gp
from gurobipy import GRB

model = gp.Model()
capacity = model.addVars(locations, vType=GRB.INTEGER, name="capacity")


Edit: as mentioned by @Sune, you can get a variable that can only assume values of a multiple of 5 as follows:

model = gp.Model()
capacity_count = model.addVars(locations, vType=GRB.INTEGER, name="capacity_count")
capacity = model.addVars(locations, name="capacity")

# Force multiple of 5
model.addConstrs(capacity[l] == 5*capacity_count[l] for l in locations)


Note that capacity can be a left as a continuous variable, as the constraint forces it to be integer.

• Hi Richard, thanks for answering! Using your working example, the values of the decision variable are all with the step-width 1. My question might be a little bit misleading, sorry for that. I want a decision variable, that can only take discrete variables with a pre-defined step-width (like 0, 5, 10, ...).
– XLF
Aug 3 at 8:28
• @XLF if $z$ is a non-negative integer variable with a "step-width" of 1, then $5z$ is probably what you want.
– Sune
Aug 3 at 9:28
• Hi Richard and Sune, thanks for answering! It seems to work, so thank you all very much :)
– XLF
Aug 5 at 14:55