2
$\begingroup$

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.

$\endgroup$
2

1 Answer 1

6
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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