addVars#

addVars(model, indexset, lb=0.0, ub=inf, obj=0.0, vtype='C', name='')[source]#

Create and add multiple variables (corresponding to an index-set) to the gurobipy model.

This function extends gurobipy’s Model.addVars method by wrapping it and returning VarDict1D/VarDictND data structures - that can efficiently sum subsets of variables with a user-friendly syntax based on wildcard patterns.

Parameters:
modelgurobipy.Model

The gurobipy model.

indexsetIndexSet1D or IndexSetND

Index-set for defining the variables. The name/names attribute of IndexSet1D/IndexSetND will be set as the key_name/key_names attribute of VarDict1D/VarDictND.

lbint or float or sequence or dict or ParamDict, optional

Lower bound, in one of the following forms:

  • A number - if all variables share the same lower bound.

  • A sequence of numbers - one for each variable.

  • A dict/ParamDict - with keys following the same structute as the index-set elements and values representing the lower bound; will fallback to the default for index-set elements not found in dict/ParamDict keys.

Default is 0.0.

ubint or float or sequence or dict or ParamDict, optional

Upper bound, in one of the following forms:

  • A number - if all variables share the same upper bound.

  • A sequence of numbers - one for each variable.

  • A dict/ParamDict - with keys following the same structute as the index-set elements and values representing the upper bound; will fallback to the default for index-set elements not found in dict/ParamDict keys.

Default is 1.0 for binary variable type and infinity for other types.

objint or float or sequence or dict or ParamDict, optional

Objective coefficient, in one of the following forms:

  • A number - if all variables share the same objective coefficient.

  • A sequence of numbers - one for each variable.

  • A dict/ParamDict - with keys following the same structute as the index-set elements and values representing the objective coefficient; will fallback to the default for index-set elements not found in dict/ParamDict keys.

Default is 0.0.

vtypestr, optional

Variable type, in one of the following forms:

  • GRB.CONTINUOUS

  • GRB.BINARY

  • GRB.INTEGER

  • GRB.SEMICONT

  • GRB.SEMIINT

Default is GRB.CONTINUOUS.

namestr, optional

Variable name, by default empty string. It will also be used as the value_name attribute of VarDict1D/VarDictND.

Returns:
VarDict1D or VarDictND
Raises:
ValueError

If the index-set is empty.

Examples

Create gurobipy model:

>>> from gurobipy import Model
>>> mdl = Model()

Create index-sets:

>>> nodes = IndexSet1D(['A', 'B', 'C'], name='node')
>>> arcs = IndexSetND([('A', 'B'), ('B', 'C'), ('C', 'B')], names=['ori', 'des'])

Add variables:

>>> node_select = addVars(mdl, nodes, vtype=GRB.BINARY, name='select')
>>> mdl.update()
>>> node_select
VarDict1D: node -> select
{'A': <gurobi.Var select[A]>, 'B': <gurobi.Var select[B]>, 'C': <gurobi.Var select[C]>}
>>> arc_flow = addVars(mdl, arcs, ub=10, vtype=GRB.CONTINUOUS, name='flow')
>>> mdl.update()
>>> arc_flow
VarDictND: (ori, des) -> flow
{('A', 'B'): <gurobi.Var flow[A,B]>,
 ('B', 'C'): <gurobi.Var flow[B,C]>,
 ('C', 'B'): <gurobi.Var flow[C,B]>}