Package cplex :: Package _internal :: Module _subinterfaces :: Class IndicatorConstraintInterface
[frames] | no frames]
 

Class IndicatorConstraintInterface


Methods for adding, modifying, and querying indicator constraints.
Instance Methods
 
__init__(self, cplex)
Creates a new IndicatorConstraintInterface.
 
get_num(self)
Returns the number of indicator constraints.
 
add_batch(self, lin_expr=None, sense=None, rhs=None, indvar=None, complemented=None, name=None, indtype=None)
Adds indicator constraints to the problem.
 
add(self, lin_expr=None, sense='E', rhs=0.0, indvar=0, complemented=0, name='', indtype=1)
Adds an indicator constraint to the problem.
 
delete(self, *args)
Deletes indicator constraints from the problem.
 
get_indicator_variables(self, *args)
Returns the indicator variables of a set of indicator constraints.
 
get_complemented(self, *args)
Returns whether a set of indicator constraints is complemented.
 
get_num_nonzeros(self, *args)
Returns the number of nonzeros in a set of indicator constraints.
 
get_rhs(self, *args)
Returns the righthand side of a set of indicator constraints.
 
get_senses(self, *args)
Returns the sense of a set of indicator constraints.
 
get_types(self, *args)
Returns the type of a set of indicator constraints.
 
get_linear_components(self, *args)
Returns the linear constraint of a set of indicator constraints.
 
get_names(self, *args)
Returns the names of a set of indicator constraints.

Inherited from _baseinterface.BaseInterface: get_indices

Class Variables
  type_ = IndicatorType()
See IndicatorType()
Method Details

__init__(self, cplex)
(Constructor)

 

Creates a new IndicatorConstraintInterface.

The indicator constraints interface is exposed by the top-level Cplex class as Cplex.indicator_constraints. This constructor is not meant to be used externally.

Overrides: _baseinterface.BaseInterface.__init__

get_num(self)

 

Returns the number of indicator constraints.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> c.indicator_constraints.add(name="ind1")
0
>>> c.indicator_constraints.get_num()
1

add_batch(self, lin_expr=None, sense=None, rhs=None, indvar=None, complemented=None, name=None, indtype=None)

 

Adds indicator constraints to the problem.

Takes up to eight keyword arguments.

If more than one argument is specified, all arguments must have the same length.

lin_expr : either a list of SparsePair instances or a matrix in list-of-lists format.

Note
lin_expr must not contain duplicate indices. If lin_expr references a variable more than once, either by index, name, or a combination of index and name, an exception will be raised.

sense : must be either a list of single-character strings or a string containing the senses of the indicator constraints. Each entry must be one of 'G', 'L', 'E', indicating greater-than-or-equal-to (>=), less-than-or-equal-to (<=), and equality (=), respectively. Left unspecified, the default is 'E'.

rhs : a list of floats, specifying the righthand side of each indicator constraint.

indvar : a list of names or indices (or a mixture of the two), of the variables that control whether the constraint is active or not.

complemented : a list of values (0 or 1). Default value of 0 instructs CPLEX to interpret indicator constraint as active when the indicator variable is 1. Set complemented to 1 to instruct CPLEX that the indicator constraint is active when indvar = 0.

name : a list of strings that determine the names of the individual constraints.

indtype : a list of the types of indicator constraints. Defaults to CPX_INDICATOR_IF ('->'). See IndicatorType().

Returns an iterator containing the indices of the added indicator constraints.

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names=["x1", "x2", "x3"])
>>> indices = c.indicator_constraints.add_batch(
...     lin_expr=[cplex.SparsePair(ind=["x2"], val=[2.0]),
...               cplex.SparsePair(ind=["x3"], val=[2.0])],
...     sense="LL",
...     rhs=[1.0, 1.0],
...     indvar=["x1", "x2"],
...     complemented=[0, 0],
...     name=["ind1", "ind2"],
...     indtype=[c.indicator_constraints.type_.if_,
...              c.indicator_constraints.type_.if_])
>>> len(list(indices))
2

add(self, lin_expr=None, sense='E', rhs=0.0, indvar=0, complemented=0, name='', indtype=1)

 

Adds an indicator constraint to the problem.

Takes up to eight keyword arguments.

lin_expr : either a SparsePair or a list of two lists, the first of which contains variable indices or names, the second of which contains values.

Note
lin_expr must not contain duplicate indices. If lin_expr references a variable more than once, either by index, name, or a combination of index and name, an exception will be raised.

sense : the sense of the constraint, may be "L", "G", or "E": default is "E"

rhs : a float defining the righthand side of the constraint

indvar : the name or index of the variable that controls if the constraint is active

complemented : default value of 0 instructs CPLEX to interpret indicator constraint as active when the indicator variable is 1. Set complemented to 1 to instruct CPLEX that the indicator constraint is active when indvar = 0.

name : the name of the constraint.

indtype : the type of indicator constraint. Defaults to CPX_INDICATOR_IF ('->'). See IndicatorType().

Returns the index of the added indicator constraint.

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ["x1", "x2"])
>>> c.indicator_constraints.add(
...     indvar="x1",
...     complemented=0,
...     rhs=1.0,
...     sense="G",
...     lin_expr=cplex.SparsePair(ind=["x2"], val=[2.0]),
...     name="ind1",
...     indtype=c.indicator_constraints.type_.if_)
0

delete(self, *args)

 

Deletes indicator constraints from the problem.

There are four forms by which indicator_constraints.delete may be called.

indicator_constraints.delete()
deletes all indicator constraints from the problem.
indicator_constraints.delete(i)
i must be an indicator constraint name or index. Deletes the indicator constraint whose index or name is i.
indicator_constraints.delete(s)
s must be a sequence of indicator constraint names or indices. Deletes the indicator constraints with names or indices contained within s. Equivalent to [indicator_constraints.delete(i) for i in s].
indicator_constraints.delete(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Deletes the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.delete(range(begin, end + 1)). This will give the best performance when deleting batches of indicator constraints.

See CPXdelindconstrs in the Callable Library Reference Manual for more detail.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> [c.indicator_constraints.add(name=str(i)) for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.indicator_constraints.get_num()
10
>>> c.indicator_constraints.delete(8)
>>> c.indicator_constraints.get_names()
['0', '1', '2', '3', '4', '5', '6', '7', '9']
>>> c.indicator_constraints.delete("1", 3)
>>> c.indicator_constraints.get_names()
['0', '4', '5', '6', '7', '9']
>>> c.indicator_constraints.delete([2, "0", 5])
>>> c.indicator_constraints.get_names()
['4', '6', '7']
>>> c.indicator_constraints.delete()
>>> c.indicator_constraints.get_names()
[]

get_indicator_variables(self, *args)

 

Returns the indicator variables of a set of indicator constraints.

May be called by four forms.

indicator_constraints.get_indicator_variables()
return the indicator variables of all indicator constraints from the problem.
indicator_constraints.get_indicator_variables(i)
i must be an indicator constraint name or index. Returns the indicator variables of the indicator constraint whose index or name is i.
indicator_constraints.get_indicator_variables(s)
s must be a sequence of indicator constraint names or indices. Returns the indicator variables of the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_indicator_variables(i) for i in s]
indicator_constraints.get_indicator_variables(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns the indicator variables of the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_indicator_variables(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(11)], types = "B" * 11)
>>> [c.indicator_constraints.add(
...      name=str(i), indvar=i,
...      lin_expr=cplex.SparsePair(ind=[i+1], val=[1.0]))
...  for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.indicator_constraints.get_num()
10
>>> c.indicator_constraints.get_indicator_variables(8)
8
>>> c.indicator_constraints.get_indicator_variables("1",3)
[1, 2, 3]
>>> c.indicator_constraints.get_indicator_variables([2,"0",5])
[2, 0, 5]
>>> c.indicator_constraints.get_indicator_variables()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

get_complemented(self, *args)

 

Returns whether a set of indicator constraints is complemented.

May be called by four forms.

indicator_constraints.get_complemented()
return whether or not all indicator constraints from the problem are complemented.
indicator_constraints.get_complemented(i)
i must be an indicator constraint name or index. Returns whether or not the indicator constraint whose index or name is i is complemented.
indicator_constraints.get_complemented(s)
s must be a sequence of indicator constraint names or indices. Returns whether or not the indicator constraints with indices the members of s are complemented. Equivalent to [indicator_constraints.get_complemented(i) for i in s]
indicator_constraints.get_complemented(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns whether or not the indicator constraints with indices between begin and end, inclusive of end, are complemented. Equivalent to indicator_constraints.get_complemented(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(11)], types = "B" * 11)
>>> [c.indicator_constraints.add(
...      name=str(i), indvar=10,
...      complemented=i % 2)
...  for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.indicator_constraints.get_num()
10
>>> c.indicator_constraints.get_complemented(8)
0
>>> c.indicator_constraints.get_complemented("1",3)
[1, 0, 1]
>>> c.indicator_constraints.get_complemented([2,"0",5])
[0, 0, 1]
>>> c.indicator_constraints.get_complemented()
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

get_num_nonzeros(self, *args)

 

Returns the number of nonzeros in a set of indicator constraints.

May be called by four forms.

indicator_constraints.get_num_nonzeros()
return the number of nonzeros in all indicator constraints from the problem.
indicator_constraints.get_num_nonzeros(i)
i must be an indicator constraint name or index. Returns the number of nonzeros in the indicator constraint whose index or name is i.
indicator_constraints.get_num_nonzeros(s)
s must be a sequence of indicator constraint names or indices. Returns the number of nonzeros in the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_num_nonzeros(i) for i in s]
indicator_constraints.get_num_nonzeros(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns the number of nonzeros in the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_num_nonzeros(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(11)], types = "B" * 11)
>>> [c.indicator_constraints.add(
...      name=str(i), indvar=10,
...      lin_expr=[range(i), [1.0 * (j+1.0) for j in range(i)]])
...  for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.indicator_constraints.get_num()
10
>>> c.indicator_constraints.get_num_nonzeros(8)
8
>>> c.indicator_constraints.get_num_nonzeros("1",3)
[1, 2, 3]
>>> c.indicator_constraints.get_num_nonzeros([2,"0",5])
[2, 0, 5]
>>> c.indicator_constraints.get_num_nonzeros()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

get_rhs(self, *args)

 

Returns the righthand side of a set of indicator constraints.

May be called by four forms.

indicator_constraints.get_rhs()
return the righthand side of all indicator constraints from the problem.
indicator_constraints.get_rhs(i)
i must be an indicator constraint name or index. Returns the righthand side of the indicator constraint whose index or name is i.
indicator_constraints.get_rhs(s)
s must be a sequence of indicator constraint names or indices. Returns the righthand side of the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_rhs(i) for i in s]
indicator_constraints.get_rhs(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns the righthand side of the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_rhs(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> [c.indicator_constraints.add(rhs=1.5 * i, name=str(i)) for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.indicator_constraints.get_num()
10
>>> c.indicator_constraints.get_rhs(8)
12.0
>>> c.indicator_constraints.get_rhs("1",3)
[1.5, 3.0, 4.5]
>>> c.indicator_constraints.get_rhs([2,"0",5])
[3.0, 0.0, 7.5]
>>> c.indicator_constraints.get_rhs()
[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5]

get_senses(self, *args)

 

Returns the sense of a set of indicator constraints.

May be called by four forms.

indicator_constraints.get_senses()
return the senses of all indicator constraints from the problem.
indicator_constraints.get_senses(i)
i must be an indicator constraint name or index. Returns the sense of the indicator constraint whose index or name is i.
indicator_constraints.get_senses(s)
s must be a sequence of indicator constraint names or indices. Returns the senses of the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_senses(i) for i in s]
indicator_constraints.get_senses(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns the senses of the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_senses(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> [c.indicator_constraints.add(name=str(i), sense=j)
...  for i, j in enumerate("EGLE")]
[0, 1, 2, 3]
>>> c.indicator_constraints.get_num()
4
>>> c.indicator_constraints.get_senses(1)
'G'
>>> c.indicator_constraints.get_senses("1",3)
['G', 'L', 'E']
>>> c.indicator_constraints.get_senses([2,"0",1])
['L', 'E', 'G']
>>> c.indicator_constraints.get_senses()
['E', 'G', 'L', 'E']

get_types(self, *args)

 

Returns the type of a set of indicator constraints.

See IndicatorType().

May be called by four forms.

indicator_constraints.get_types()
return the types of all indicator constraints from the problem.
indicator_constraints.get_types(i)
i must be an indicator constraint name or index. Returns the type of the indicator constraint whose index or name is i.
indicator_constraints.get_types(s)
s must be a sequence of indicator constraint names or indices. Returns the types of the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_types(i) for i in s]
indicator_constraints.get_types(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns the types of the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_types(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> idx = c.indicator_constraints.add(name='i1')
>>> c.indicator_constraints.get_types(idx)
1
>>> c.indicator_constraints.type_[1]
'if_'

get_linear_components(self, *args)

 

Returns the linear constraint of a set of indicator constraints.

Returns a list of SparsePair instances or a single SparsePair instance, depending on the form by which it was called.

May be called by four forms.

indicator_constraints.get_linear_components()
return the linear components of all indicator constraints from the problem.
indicator_constraints.get_linear_components(i)
i must be an indicator constraint name or index. Returns the linear component of the indicator constraint whose index or name is i.
indicator_constraints.get_linear_components(s)
s must be a sequence of indicator constraint names or indices. Returns the linear components of the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_linear_components(i) for i in s]
indicator_constraints.get_linear_components(begin, end)
begin and end must be indicator constraint indices or indicator constraint names. Returns the linear components of the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_linear_components(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(
...     names=[str(i) for i in range(4)],
...     types="B" * 4
... )
>>> [c.indicator_constraints.add(
...      name=str(i), indvar=3,
...      lin_expr=[range(i), [1.0 * (j+1.0) for j in range(i)]])
...  for i in range(3)]
[0, 1, 2]
>>> c.indicator_constraints.get_num()
3
>>> c.indicator_constraints.get_linear_components(2)
SparsePair(ind = [0, 1], val = [1.0, 2.0])
>>> for row in c.indicator_constraints.get_linear_components("0", 1):
...     print(row)
SparsePair(ind = [], val = [])
SparsePair(ind = [0], val = [1.0])
>>> for row in c.indicator_constraints.get_linear_components([1, "0"]):
...     print(row)
SparsePair(ind = [0], val = [1.0])
SparsePair(ind = [], val = [])
>>> for row in c.indicator_constraints.get_linear_components():
...     print(row)
SparsePair(ind = [], val = [])
SparsePair(ind = [0], val = [1.0])
SparsePair(ind = [0, 1], val = [1.0, 2.0])

get_names(self, *args)

 

Returns the names of a set of indicator constraints.

May be called by four forms.

indicator_constraints.get_names()
return the names of all indicator constraints from the problem.
indicator_constraints.get_names(i)
i must be an indicator constraint index. Returns the name of constraint i.
indicator_constraints.get_names(s)
s must be a sequence of indicator constraint indices. Returns the names of the indicator constraints with indices the members of s. Equivalent to [indicator_constraints.get_names(i) for i in s]
indicator_constraints.get_names(begin, end)
begin and end must be indicator constraint indices. Returns the names of the indicator constraints with indices between begin and end, inclusive of end. Equivalent to indicator_constraints.get_names(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> [c.indicator_constraints.add(name="i" + str(i))
...  for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.indicator_constraints.get_num()
10
>>> c.indicator_constraints.get_names(8)
'i8'
>>> c.indicator_constraints.get_names(1, 3)
['i1', 'i2', 'i3']
>>> c.indicator_constraints.get_names([2, 0, 5])
['i2', 'i0', 'i5']
>>> c.indicator_constraints.get_names()
['i0', 'i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7', 'i8', 'i9']