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

Class VariablesInterface


Methods for adding, querying, and modifying variables.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ["x0", "x1", "x2"])
>>> # default values for lower_bounds are 0.0
>>> c.variables.get_lower_bounds()
[0.0, 0.0, 0.0]
>>> # values can be set either one at a time or many at a time
>>> c.variables.set_lower_bounds(0, 1.0)
>>> c.variables.set_lower_bounds([("x1", -1.0), (2, 3.0)])
>>> # values can be queried as a range
>>> c.variables.get_lower_bounds(0, "x1")
[1.0, -1.0]
>>> # values can be queried as a sequence in arbitrary order
>>> c.variables.get_lower_bounds(["x1", "x2", 0])
[-1.0, 3.0, 1.0]
>>> # can query the number of variables
>>> c.variables.get_num()
3
>>> c.variables.set_types(0, c.variables.type.binary)
>>> c.variables.get_num_binary()
1
Instance Methods
 
__init__(self, cplex)
Creates a new VariablesInterface.
 
get_num(self)
Returns the number of variables in the problem.
 
get_num_integer(self)
Returns the number of integer variables in the problem.
 
get_num_binary(self)
Returns the number of binary variables in the problem.
 
get_num_semicontinuous(self)
Returns the number of semi-continuous variables in the problem.
 
get_num_semiinteger(self)
Returns the number of semi-integer variables in the problem.
 
add(self, obj=None, lb=None, ub=None, types='', names=None, columns=None)
Adds variables and related data to the problem.
 
delete(self, *args)
Deletes variables from the problem.
 
set_lower_bounds(self, *args)
Sets the lower bound for a variable or set of variables.
 
set_upper_bounds(self, *args)
Sets the upper bound for a variable or set of variables.
 
set_names(self, *args)
Sets the name of a variable or set of variables.
 
set_types(self, *args)
Sets the type of a variable or set of variables.
 
get_lower_bounds(self, *args)
Returns the lower bounds on variables from the problem.
 
get_upper_bounds(self, *args)
Returns the upper bounds on variables from the problem.
 
get_names(self, *args)
Returns the names of variables from the problem.
 
get_types(self, *args)
Returns the types of variables from the problem.
 
get_cols(self, *args)
Returns a set of columns of the linear constraint matrix.
 
get_histogram(self)
Returns a histogram of the columns of the linear constraint matrix.

Inherited from BaseInterface: get_indices

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables
  type = VarTypes()
See VarTypes()
Instance Variables
  advanced
See AdvancedVariablesInterface()
Properties

Inherited from object: __class__

Method Details

__init__(self, cplex)
(Constructor)

 

Creates a new VariablesInterface.

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

Overrides: object.__init__

get_num(self)

 

Returns the number of variables in the problem.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(types = [t.continuous, t.binary, t.integer])
>>> c.variables.get_num()
3

get_num_integer(self)

 

Returns the number of integer variables in the problem.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(types = [t.continuous, t.binary, t.integer])
>>> c.variables.get_num_integer()
1

get_num_binary(self)

 

Returns the number of binary variables in the problem.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(types = [t.semi_continuous, t.binary, t.integer])
>>> c.variables.get_num_binary()
1

get_num_semicontinuous(self)

 

Returns the number of semi-continuous variables in the problem.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(types = [t.semi_continuous, t.semi_integer, t.semi_integer])
>>> c.variables.get_num_semicontinuous()
1

get_num_semiinteger(self)

 

Returns the number of semi-integer variables in the problem.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(types = [t.semi_continuous, t.semi_integer, t.semi_integer])
>>> c.variables.get_num_semiinteger()
2

add(self, obj=None, lb=None, ub=None, types='', names=None, columns=None)

 

Adds variables and related data to the problem.

variables.add accepts the keyword arguments obj, lb, ub, types, names, and columns.

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

obj is a list of floats specifying the linear objective coefficients of the variables.

lb is a list of floats specifying the lower bounds on the variables.

ub is a list of floats specifying the upper bounds on the variables.

types must be either a list of single-character strings or a string containing the types of the variables.

Note
If types is specified, the problem type will be a MIP, even if all variables are specified to be continuous.

names is a list of strings.

columns may be either a list of sparse vectors or a matrix in list-of-lists format.

Note
The entries of columns must not contain duplicate indices. If an entry of columns references a row more than once, either by index, name, or a combination of index and name, an exception will be raised.

Returns an iterator containing the indices of the added variables.

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.linear_constraints.add(names = ["c0", "c1", "c2"])
>>> indices = c.variables.add(obj = [1.0, 2.0, 3.0],
                              types = [c.variables.type.integer] * 3)
>>> indices = c.variables.add(obj = [1.0, 2.0, 3.0],
                              lb = [-1.0, 1.0, 0.0],
                              ub = [100.0, cplex.infinity, cplex.infinity],
                              types = [c.variables.type.integer] * 3,
                              names = ["0", "1", "2"],
                              columns = [cplex.SparsePair(ind = ['c0', 2], val = [1.0, -1.0]),
                              [['c2'],[2.0]],
                              cplex.SparsePair(ind = [0, 1], val = [3.0, 4.0])])
>>> c.variables.get_lower_bounds()
[0.0, 0.0, 0.0, -1.0, 1.0, 0.0]
>>> c.variables.get_cols("1")
SparsePair(ind = [2], val = [2.0])

delete(self, *args)

 

Deletes variables from the problem.

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

variables.delete()
deletes all variables from the problem.
variables.delete(i)
i must be a variable name or index. Deletes the variable whose index or name is i.
variables.delete(s)
s must be a sequence of variable names or indices. Deletes the variables with indices the members of s. Equivalent to [variables.delete(i) for i in s]
variables.delete(begin, end)
begin and end must be variable indices with begin <= end or variable names whose indices respect this order. Deletes the variables with indices between begin and end, inclusive of end. Equivalent to variables.delete(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(10)])
>>> c.variables.get_num()
10
>>> c.variables.delete(8)
>>> c.variables.get_names()
['0', '1', '2', '3', '4', '5', '6', '7', '9']
>>> c.variables.delete("1",3)
>>> c.variables.get_names()
['0', '4', '5', '6', '7', '9']
>>> c.variables.delete([2,"0",5])
>>> c.variables.get_names()
['4', '6', '7']
>>> c.variables.delete()
>>> c.variables.get_names()
[]

set_lower_bounds(self, *args)

 

Sets the lower bound for a variable or set of variables.

There are two forms by which variables.set_lower_bounds may be called.

variables.set_lower_bounds(i, lb)
i must be a variable name or index and lb must be a real number. Sets the lower bound of the variable whose index or name is i to lb.
variables.set_lower_bounds(seq_of_pairs)
seq_of_pairs must be a list or tuple of (i, lb) pairs, each of which consists of a variable name or index and a real number. Sets the lower bound of the specified variables to the corresponding values. Equivalent to [variables.set_lower_bounds(pair[0], pair[1]) for pair in seq_of_pairs].
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ["x0", "x1", "x2"])
>>> c.variables.set_lower_bounds(0, 1.0)
>>> c.variables.get_lower_bounds()
[1.0, 0.0, 0.0]
>>> c.variables.set_lower_bounds([(2, 3.0), ("x1", -1.0)])
>>> c.variables.get_lower_bounds()
[1.0, -1.0, 3.0]

set_upper_bounds(self, *args)

 

Sets the upper bound for a variable or set of variables.

There are two forms by which variables.set_upper_bounds may be called.

variables.set_upper_bounds(i, ub)
i must be a variable name or index and ub must be a real number. Sets the upper bound of the variable whose index or name is i to ub.
variables.set_upper_bounds(seq_of_pairs)
seq_of_pairs must be a list or tuple of (i, ub) pairs, each of which consists of a variable name or index and a real number. Sets the upper bound of the specified variables to the corresponding values. Equivalent to [variables.set_upper_bounds(pair[0], pair[1]) for pair in seq_of_pairs].
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ["x0", "x1", "x2"])
>>> c.variables.set_upper_bounds(0, 1.0)
>>> c.variables.set_upper_bounds([("x1", 10.0), (2, 3.0)])
>>> c.variables.get_upper_bounds()
[1.0, 10.0, 3.0]

set_names(self, *args)

 

Sets the name of a variable or set of variables.

There are two forms by which variables.set_names may be called.

variables.set_names(i, name)
i must be a variable name or index and name must be a string.
variables.set_names(seq_of_pairs)
seq_of_pairs must be a list or tuple of (i, name) pairs, each of which consists of a variable name or index and a string. Sets the name of the specified variables to the corresponding strings. Equivalent to [variables.set_names(pair[0], pair[1]) for pair in seq_of_pairs].
>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(types = [t.continuous, t.binary, t.integer])
>>> c.variables.set_names(0, "first")
>>> c.variables.set_names([(2, "third"), (1, "second")])
>>> c.variables.get_names()
['first', 'second', 'third']

set_types(self, *args)

 

Sets the type of a variable or set of variables.

There are two forms by which variables.set_types may be called.

variables.set_types(i, type)
i must be a variable name or index and name must be a single-character string.
variables.set_types(seq_of_pairs)
seq_of_pairs must be a list or tuple of (i, type) pairs, each of which consists of a variable name or index and a single-character string. Sets the type of the specified variables to the corresponding strings. Equivalent to [variables.set_types(pair[0], pair[1]) for pair in seq_of_pairs].
Note
If the types are set, the problem will be treated as a MIP, even if all variable types are continuous.
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(5)])
>>> c.variables.set_types(0, c.variables.type.continuous)
>>> c.variables.set_types([("1", c.variables.type.integer),
                           ("2", c.variables.type.binary),
                           ("3", c.variables.type.semi_continuous),
                           ("4", c.variables.type.semi_integer)])
>>> c.variables.get_types()
['C', 'I', 'B', 'S', 'N']
>>> c.variables.type[c.variables.get_types(0)]
'continuous'

get_lower_bounds(self, *args)

 

Returns the lower bounds on variables from the problem.

There are four forms by which variables.get_lower_bounds may be called.

variables.get_lower_bounds()
return the lower bounds on all variables from the problem.
variables.get_lower_bounds(i)
i must be a variable name or index. Returns the lower bound on the variable whose index or name is i.
variables.get_lower_bounds(s)
s must be a sequence of variable names or indices. Returns the lower bounds on the variables with indices the members of s. Equivalent to [variables.get_lower_bounds(i) for i in s]
variables.get_lower_bounds(begin, end)
begin and end must be variable indices with begin <= end or variable names whose indices respect this order. Returns the lower bounds on the variables with indices between begin and end, inclusive of end. Equivalent to variables.get_lower_bounds(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(lb = [1.5 * i for i in range(10)],
                              names = [str(i) for i in range(10)])
>>> c.variables.get_num()
10
>>> c.variables.get_lower_bounds(8)
12.0
>>> c.variables.get_lower_bounds("1",3)
[1.5, 3.0, 4.5]
>>> c.variables.get_lower_bounds([2,"0",5])
[3.0, 0.0, 7.5]
>>> c.variables.get_lower_bounds()
[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5]

get_upper_bounds(self, *args)

 

Returns the upper bounds on variables from the problem.

There are four forms by which variables.get_upper_bounds may be called.

variables.get_upper_bounds()
return the upper bounds on all variables from the problem.
variables.get_upper_bounds(i)
i must be a variable name or index. Returns the upper bound on the variable whose index or name is i.
variables.get_upper_bounds(s)
s must be a sequence of variable names or indices. Returns the upper bounds on the variables with indices the members of s. Equivalent to [variables.get_upper_bounds(i) for i in s]
variables.get_upper_bounds(begin, end)
begin and end must be variable indices with begin <= end or variable names whose indices respect this order. Returns the upper bounds on the variables with indices between begin and end, inclusive of end. Equivalent to variables.get_upper_bounds(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(ub = [(1.5 * i) + 1.0 for i in range(10)],
                              names = [str(i) for i in range(10)])
>>> c.variables.get_num()
10
>>> c.variables.get_upper_bounds(8)
13.0
>>> c.variables.get_upper_bounds("1",3)
[2.5, 4.0, 5.5]
>>> c.variables.get_upper_bounds([2,"0",5])
[4.0, 1.0, 8.5]
>>> c.variables.get_upper_bounds()
[1.0, 2.5, 4.0, 5.5, 7.0, 8.5, 10.0, 11.5, 13.0, 14.5]

get_names(self, *args)

 

Returns the names of variables from the problem.

There are four forms by which variables.get_names may be called.

variables.get_names()
return the names of all variables from the problem.
variables.get_names(i)
i must be a variable index. Returns the name of variable i.
variables.get_names(s)
s must be a sequence of variable indices. Returns the names of the variables with indices the members of s. Equivalent to [variables.get_names(i) for i in s]
variables.get_names(begin, end)
begin and end must be variable indices with begin <= end. Returns the names of the variables with indices between begin and end, inclusive of end. Equivalent to variables.get_names(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ['x' + str(i) for i in range(10)])
>>> c.variables.get_num()
10
>>> c.variables.get_names(8)
'x8'
>>> c.variables.get_names(1,3)
['x1', 'x2', 'x3']
>>> c.variables.get_names([2,0,5])
['x2', 'x0', 'x5']
>>> c.variables.get_names()
['x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9']

get_types(self, *args)

 

Returns the types of variables from the problem.

There are four forms by which variables.types may be called.

variables.types()
return the types of all variables from the problem.
variables.types(i)
i must be a variable name or index. Returns the type of the variable whose index or name is i.
variables.types(s)
s must be a sequence of variable names or indices. Returns the types of the variables with indices the members of s. Equivalent to [variables.types(i) for i in s]
variables.types(begin, end)
begin and end must be variable indices with begin <= end or variable names whose indices respect this order. Returns the types of the variables with indices between begin and end, inclusive of end. Equivalent to variables.get_upper_bounds(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> t = c.variables.type
>>> indices = c.variables.add(names = [str(i) for i in range(5)],
                              types = [t.continuous, t.integer,
                              t.binary, t.semi_continuous, t.semi_integer])
>>> c.variables.get_num()
5
>>> c.variables.get_types(3)
'S'
>>> c.variables.get_types(1,3)
['I', 'B', 'S']
>>> c.variables.get_types([2,0,4])
['B', 'C', 'N']
>>> c.variables.get_types()
['C', 'I', 'B', 'S', 'N']

get_cols(self, *args)

 

Returns a set of columns of the linear constraint matrix.

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

There are four forms by which variables.get_cols may be called.

variables.get_cols()
return the entire linear constraint matrix.
variables.get_cols(i)
i must be a variable name or index. Returns the column of the linear constraint matrix associated with variable i.
variables.get_cols(s)
s must be a sequence of variable names or indices. Returns the columns of the linear constraint matrix associated with the variables with indices the members of s. Equivalent to [variables.get_cols(i) for i in s]
variables.get_cols(begin, end)
begin and end must be variable indices with begin <= end or variable names whose indices respect this order. Returns the columns of the linear constraint matrix associated with the variables with indices between begin and end, inclusive of end. Equivalent to variables.get_cols(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.linear_constraints.add(names = ['c1', 'c2'])
>>> indices = c.variables.add(names = [str(i) for i in range(3)],
                              columns = [cplex.SparsePair(ind = ['c1'],val = [1.0]),
                                         cplex.SparsePair(ind = ['c2'],val = [2.0]),
                                         cplex.SparsePair(ind = ['c1','c2'],val = [3.0,4.0])])
>>> c.variables.get_num()
3
>>> c.variables.get_cols(2)
SparsePair(ind = [0, 1], val = [3.0, 4.0])
>>> c.variables.get_cols(1,2)
[SparsePair(ind = [1], val = [2.0]), SparsePair(ind = [0, 1], val = [3.0, 4.0])]
>>> c.variables.get_cols([2,0,1])
[SparsePair(ind = [0, 1], val = [3.0, 4.0]), SparsePair(ind = [0], val = [1.0]), SparsePair(ind = [1], val = [2.0])]
>>> c.variables.get_cols()
[SparsePair(ind = [0], val = [1.0]), SparsePair(ind = [1], val = [2.0]), SparsePair(ind = [0, 1], val = [3.0, 4.0])]

get_histogram(self)

 

Returns a histogram of the columns of the linear constraint matrix.

To access the number of columns with given nonzero counts, use slice notation. If a negative nonzero count is queried in this manner an IndexError will be raised.

The __str__ method of the histogram object returns a string displaying the number of columns with given nonzeros counts in human readable form.

The data member "orientation" of the histogram object is "column", indicating that the histogram shows the nonzero counts for the columns of the linear constraint matrix.

>>> import cplex
>>> c = cplex.Cplex("ind.lp")
>>> histogram = c.variables.get_histogram()
>>> print(histogram)
Column counts (excluding fixed variables):
<BLANKLINE>
    Nonzero Count:   1   2   3
Number of Columns:   1   6  36
<BLANKLINE>
<BLANKLINE>
>>> histogram[2]
6
>>> histogram[0:4]
[0, 1, 6, 36]