addVariables#

addVariables(model, indexset, lb=0.0, ub=inf, obj=0.0, type=<HighsVarType.kContinuous: 0>, name=None, name_prefix=None)[source]#

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

This function extends highspy’s Highs.addVariables 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. The out_array argument is not supported.

Parameters:
modelhighspy.Highs

The highspy 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 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.

typeHighsVarType, optional

Variable type, in one of the following forms:

  • HighsVarType.kContinuous

  • HighsVarType.kInteger

  • HighsVarType.kSemiContinuous

  • HighsVarType.kSemiInteger

Default is HighsVarType.kContinuous.

namesequence, optional

Sequence of names for variables.

Default is None.

name_prefixstr, optional

Prefix for variable names if argument name not provided, by default None. Constructed name will be name_prefix + index. 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 highspy model:

>>> from highspy import Highs, HighsVarType
>>> mdl = Highs()

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 = addVariables(
...     mdl, nodes, type=HighsVarType.kInteger, lb=0, ub=1, name_prefix='select'
... )
>>> node_select
VarDict1D: node -> select
{'A': highs_var(0), 'B': highs_var(1), 'C': highs_var(2)}
>>> arc_flow = addVariables(mdl, arcs, ub=10, type=HighsVarType.kContinuous, name_prefix='flow')
>>> arc_flow
VarDictND: (ori, des) -> flow
{('A', 'B'): highs_var(3), ('B', 'C'): highs_var(4), ('C', 'B'): highs_var(5)}