Package cplex :: Package _internal :: Module _subinterfaces :: Class QuadraticConstraintInterface
 

Class QuadraticConstraintInterface


Methods for adding, modifying, and querying quadratic constraints.
Instance Methods
 
__init__(self, cplex)
Creates a new QuadraticConstraintInterface.
 
get_num(self)
Returns the number of quadratic constraints.
 
add(self, lin_expr=None, quad_expr=None, sense='L', rhs=0.0, name='')
Adds a quadratic constraint to the problem.
 
delete(self, *args)
Deletes quadratic constraints from the problem.
 
get_rhs(self, *args)
Returns the righthand side of a set of quadratic constraints.
 
get_senses(self, *args)
Returns the senses of a set of quadratic constraints.
 
get_linear_num_nonzeros(self, *args)
Returns the number of nonzeros in the linear part of a set of quadratic constraints.
 
get_linear_components(self, *args)
Returns the linear part of a set of quadratic constraints.
 
get_quad_num_nonzeros(self, *args)
Returns the number of nonzeros in the quadratic part of a set of quadratic constraints.
 
get_quadratic_components(self, *args)
Returns the quadratic part of a set of quadratic constraints.
 
get_names(self, *args)
Returns the names of a set of quadratic constraints.

Inherited from _baseinterface.BaseInterface: get_indices

Method Details

__init__(self, cplex)
(Constructor)

 

Creates a new QuadraticConstraintInterface.

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

Overrides: _baseinterface.BaseInterface.__init__

get_num(self)

 

Returns the number of quadratic constraints.

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ['x','y'])
>>> l = cplex.SparsePair(ind = ['x'], val = [1.0])
>>> q = cplex.SparseTriple(ind1 = ['x'], ind2 = ['y'], val = [1.0])
>>> [c.quadratic_constraints.add(name=str(i), lin_expr=l, quad_expr=q)
...  for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.quadratic_constraints.get_num()
10

add(self, lin_expr=None, quad_expr=None, sense='L', rhs=0.0, name='')

 

Adds a quadratic constraint to the problem.

Takes up to five keyword arguments:

lin_expr : either a SparsePair or a list of two lists specifying the linear component of the constraint.

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.

quad_expr : either a SparseTriple or a list of three lists specifying the quadratic component of the constraint.

Note
quad_expr must not contain duplicate indices. If quad_expr references a matrix entry more than once, either by indices, names, or a combination of indices and names, an exception will be raised.

sense : either "L", "G", or "E"

rhs : a float specifying the righthand side of the constraint.

name : the name of the constraint.

Returns the index of the added quadratic constraint.

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = ['x','y'])
>>> l = cplex.SparsePair(ind = ['x'], val = [1.0])
>>> q = cplex.SparseTriple(ind1 = ['x'], ind2 = ['y'], val = [1.0])
>>> c.quadratic_constraints.add(name = "my_quad",
...                             lin_expr = l,
...                             quad_expr = q,
...                             rhs = 1.0,
...                             sense = "G")
0

delete(self, *args)

 

Deletes quadratic constraints from the problem.

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

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

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

Example usage:

>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names=['x', 'y'])
>>> l = cplex.SparsePair(ind=['x'], val=[1.0])
>>> q = cplex.SparseTriple(ind1=['x'], ind2=['y'], val=[1.0])
>>> [c.quadratic_constraints.add(
...      name=str(i), lin_expr=l, quad_expr=q)
...  for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.quadratic_constraints.get_num()
10
>>> c.quadratic_constraints.delete(8)
>>> c.quadratic_constraints.get_names()
['0', '1', '2', '3', '4', '5', '6', '7', '9']
>>> c.quadratic_constraints.delete("1", 3)
>>> c.quadratic_constraints.get_names()
['0', '4', '5', '6', '7', '9']
>>> c.quadratic_constraints.delete([2, "0", 5])
>>> c.quadratic_constraints.get_names()
['4', '6', '7']
>>> c.quadratic_constraints.delete()
>>> c.quadratic_constraints.get_names()
[]

get_rhs(self, *args)

 

Returns the righthand side of a set of quadratic constraints.

Can be called by four forms.

quadratic_constraints.get_rhs()
return the righthand side of all quadratic constraints from the problem.
quadratic_constraints.get_rhs(i)
i must be a quadratic constraint name or index. Returns the righthand side of the quadratic constraint whose index or name is i.
quadratic_constraints.get_rhs(s)
s must be a sequence of quadratic constraint names or indices. Returns the righthand side of the quadratic constraints with indices the members of s. Equivalent to [quadratic_constraints.get_rhs(i) for i in s]
quadratic_constraints.get_rhs(begin, end)
begin and end must be quadratic constraint indices or quadratic constraint names. Returns the righthand side of the quadratic constraints with indices between begin and end, inclusive of end. Equivalent to quadratic_constraints.get_rhs(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(10)])
>>> [c.quadratic_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.quadratic_constraints.get_num()
10
>>> c.quadratic_constraints.get_rhs(8)
12.0
>>> c.quadratic_constraints.get_rhs("1",3)
[1.5, 3.0, 4.5]
>>> c.quadratic_constraints.get_rhs([2,"0",5])
[3.0, 0.0, 7.5]
>>> c.quadratic_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 senses of a set of quadratic constraints.

Can be called by four forms.

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

get_linear_num_nonzeros(self, *args)

 

Returns the number of nonzeros in the linear part of a set of quadratic constraints.

Can be called by four forms.

quadratic_constraints.get_linear_num_nonzeros()
return the number of nonzeros in all quadratic constraints from the problem.
quadratic_constraints.get_linear_num_nonzeros(i)
i must be a quadratic constraint name or index. Returns the number of nonzeros in the quadratic constraint whose index or name is i.
quadratic_constraints.get_linear_num_nonzeros(s)
s must be a sequence of quadratic constraint names or indices. Returns the number of nonzeros in the quadratic constraints with indices the members of s. Equivalent to [quadratic_constraints.get_linear_num_nonzeros(i) for i in s]
quadratic_constraints.get_linear_num_nonzeros(begin, end)
begin and end must be quadratic constraint indices or quadratic constraint names. Returns the number of nonzeros in the quadratic constraints with indices between begin and end, inclusive of end. Equivalent to quadratic_constraints.get_linear_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.quadratic_constraints.add(
...      name = str(i),
...      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.quadratic_constraints.get_num()
10
>>> c.quadratic_constraints.get_linear_num_nonzeros(8)
8
>>> c.quadratic_constraints.get_linear_num_nonzeros("1",3)
[1, 2, 3]
>>> c.quadratic_constraints.get_linear_num_nonzeros([2,"0",5])
[2, 0, 5]
>>> c.quadratic_constraints.get_linear_num_nonzeros()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

get_linear_components(self, *args)

 

Returns the linear part of a set of quadratic constraints.

Returns a list of SparsePair instances or one SparsePair instance.

Can be called by four forms.

quadratic_constraints.get_linear_components()
return the linear components of all quadratic constraints from the problem.
quadratic_constraints.get_linear_components(i)
i must be a quadratic constraint name or index. Returns the linear component of the quadratic constraint whose index or name is i.
quadratic_constraints.get_linear_components(s)
s must be a sequence of quadratic constraint names or indices. Returns the linear components of the quadratic constraints with indices the members of s. Equivalent to [quadratic_constraints.get_linear_components(i) for i in s]
quadratic_constraints.get_linear_components(begin, end)
begin and end must be quadratic constraint indices or quadratic constraint names. Returns the linear components of the quadratic constraints with indices between begin and end, inclusive of end. Equivalent to quadratic_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.quadratic_constraints.add(
...      name=str(i),
...      lin_expr=[range(i), [1.0 * (j+1.0) for j in range(i)]])
...  for i in range(3)]
[0, 1, 2]
>>> c.quadratic_constraints.get_num()
3
>>> c.quadratic_constraints.get_linear_components(2)
SparsePair(ind = [0, 1], val = [1.0, 2.0])
>>> for row in c.quadratic_constraints.get_linear_components("0", 1):
...     print(row)
SparsePair(ind = [], val = [])
SparsePair(ind = [0], val = [1.0])
>>> for row in c.quadratic_constraints.get_linear_components([1, "0"]):
...     print(row)
SparsePair(ind = [0], val = [1.0])
SparsePair(ind = [], val = [])
>>> for row in c.quadratic_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_quad_num_nonzeros(self, *args)

 

Returns the number of nonzeros in the quadratic part of a set of quadratic constraints.

Can be called by four forms.

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

get_quadratic_components(self, *args)

 

Returns the quadratic part of a set of quadratic constraints.

Can be called by four forms.

quadratic_constraints.get_quadratic_components()
return the quadratic components of all quadratic constraints from the problem.
quadratic_constraints.get_quadratic_components(i)
i must be a quadratic constraint name or index. Returns the quadratic component of the quadratic constraint whose index or name is i.
quadratic_constraints.get_quadratic_components(s)
s must be a sequence of quadratic constraint names or indices. Returns the quadratic components of the quadratic constraints with indices the members of s. Equivalent to [quadratic_constraints.get_quadratic_components(i) for i in s]
quadratic_constraints.get_quadratic_components(begin, end)
begin and end must be quadratic constraint indices or quadratic constraint names. Returns the quadratic components of the quadratic constraints with indices between begin and end, inclusive of end. Equivalent to quadratic_constraints.get_quadratic_components(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(
...     names=[str(i) for i in range(4)]
... )
>>> [c.quadratic_constraints.add(
...      name="q{0}".format(i),
...      quad_expr=[range(i), range(i),
...                 [1.0 * (j+1.0) for j in range(i)]])
...  for i in range(1, 3)]
[0, 1]
>>> c.quadratic_constraints.get_num()
2
>>> c.quadratic_constraints.get_quadratic_components(1)
SparseTriple(ind1 = [0, 1], ind2 = [0, 1], val = [1.0, 2.0])
>>> for quad in c.quadratic_constraints.get_quadratic_components("q1", 1):
...     print(quad)
SparseTriple(ind1 = [0], ind2 = [0], val = [1.0])
SparseTriple(ind1 = [0, 1], ind2 = [0, 1], val = [1.0, 2.0])
>>> for quad in c.quadratic_constraints.get_quadratic_components(["q2", 0]):
...     print(quad)
SparseTriple(ind1 = [0, 1], ind2 = [0, 1], val = [1.0, 2.0])
SparseTriple(ind1 = [0], ind2 = [0], val = [1.0])
>>> for quad in c.quadratic_constraints.get_quadratic_components():
...     print(quad)
SparseTriple(ind1 = [0], ind2 = [0], val = [1.0])
SparseTriple(ind1 = [0, 1], ind2 = [0, 1], val = [1.0, 2.0])

get_names(self, *args)

 

Returns the names of a set of quadratic constraints.

Can be called by four forms.

quadratic_constraints.get_names()
return the names of all quadratic constraints from the problem.
quadratic_constraints.get_names(i)
i must be a quadratic constraint index. Returns the name of constraint i.
quadratic_constraints.get_names(s)
s must be a sequence of quadratic constraint indices. Returns the names of the quadratic constraints with indices the members of s. Equivalent to [quadratic_constraints.get_names(i) for i in s]
quadratic_constraints.get_names(begin, end)
begin and end must be quadratic constraint indices. Returns the names of the quadratic constraints with indices between begin and end, inclusive of end. Equivalent to quadratic_constraints.get_names(range(begin, end + 1)).
>>> import cplex
>>> c = cplex.Cplex()
>>> indices = c.variables.add(names = [str(i) for i in range(11)])
>>> [c.quadratic_constraints.add(
...      name = "q" + str(i),
...      quad_expr = [range(i), range(i), [1.0 * (j+1.0) for j in range(i)]])
...  for i in range(1, 11)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c.quadratic_constraints.get_num()
10
>>> c.quadratic_constraints.get_names(8)
'q9'
>>> c.quadratic_constraints.get_names(1, 3)
['q2', 'q3', 'q4']
>>> c.quadratic_constraints.get_names([2, 0, 5])
['q3', 'q1', 'q6']
>>> c.quadratic_constraints.get_names()
['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10']