2

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.

XLF
  • 61
  • 4

1 Answers1

6

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.

Richard
  • 3,459
  • 7
  • 19
  • 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 03 '22 at 08:28
  • 1
    @XLF if $z$ is a non-negative integer variable with a "step-width" of 1, then $5z$ is probably what you want. – Sune Aug 03 '22 at 09:28
  • Hi Richard and Sune, thanks for answering! It seems to work, so thank you all very much :) – XLF Aug 05 '22 at 14:55