]>
gitweb.michael.orlitzky.com - dunshire.git/blob - cones.py
b60811e548de76d95abe0933a245b5a52141b606
2 Class definitions for all of the symmetric cones (and their superclass,
3 SymmetricCone) supported by CVXOPT.
6 from cvxopt
import matrix
7 from matrices
import norm
11 An instance of a symmetric (self-dual and homogeneous) cone.
13 There are three types of symmetric cones supported by CVXOPT:
15 * The nonnegative orthant in the real n-space.
17 * The Lorentz "ice cream" cone, or the second-order cone.
19 * The cone of symmetric positive-semidefinite matrices.
21 This class is intended to encompass them all.
23 def __init__(self
, dimension
):
25 A generic constructor for symmetric cones.
27 When constructing a single symmetric cone (i.e. not a cartesian
28 product of them), the only information that we need is its
29 dimension. We take that dimension as a parameter, and store it
33 raise ValueError('cones must have dimension greater than zero')
35 self
._dimension
= dimension
37 def __contains__(self
, point
):
39 Return whether or not ``point`` belongs to this cone.
41 raise NotImplementedError
43 def contains_strict(self
, point
):
45 Return whether or not ``point`` belongs to the interior
48 raise NotImplementedError
52 Return the dimension of this symmetric cone.
54 The dimension of this symmetric cone is recorded during its
55 creation. This method simply returns the recorded value, and
56 should not need to be overridden in subclasses. We prefer to do
57 any special computation in ``__init__()`` and record the result
58 in ``self._dimension``.
60 return self
._dimension
63 class NonnegativeOrthant(SymmetricCone
):
65 The nonnegative orthant in ``n`` dimensions.
69 >>> K = NonnegativeOrthant(3)
71 Nonnegative orthant in the real 3-space
76 Output a human-readable description of myself.
78 tpl
= 'Nonnegative orthant in the real {:d}-space'
79 return tpl
.format(self
.dimension())
81 def __contains__(self
, point
):
83 Return whether or not ``point`` belongs to this cone.
87 An instance of the ``cvxopt.base.matrix`` class having
88 dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
92 >>> K = NonnegativeOrthant(3)
93 >>> matrix([1,2,3]) in K
96 >>> K = NonnegativeOrthant(3)
97 >>> matrix([1,-0.1,3]) in K
100 >>> K = NonnegativeOrthant(3)
102 Traceback (most recent call last):
104 TypeError: the given point is not a cvxopt.base.matrix
106 >>> K = NonnegativeOrthant(3)
107 >>> matrix([1,2]) in K
108 Traceback (most recent call last):
110 TypeError: the given point has the wrong dimensions
113 if not isinstance(point
, matrix
):
114 raise TypeError('the given point is not a cvxopt.base.matrix')
115 if not point
.size
== (self
.dimension(), 1):
116 raise TypeError('the given point has the wrong dimensions')
118 return all([x
>= 0 for x
in point
])
121 def contains_strict(self
, point
):
123 Return whether or not ``point`` belongs to the interior of this
128 An instance of the ``cvxopt.base.matrix`` class having
129 dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
133 >>> K = NonnegativeOrthant(3)
134 >>> K.contains_strict(matrix([1,2,3]))
137 >>> K = NonnegativeOrthant(3)
138 >>> K.contains_strict(matrix([1,0,1]))
141 >>> K = NonnegativeOrthant(3)
142 >>> K.contains_strict(matrix([1,-0.1,3]))
145 >>> K = NonnegativeOrthant(3)
146 >>> K.contains_strict([1,2,3])
147 Traceback (most recent call last):
149 TypeError: the given point is not a cvxopt.base.matrix
151 >>> K = NonnegativeOrthant(3)
152 >>> K.contains_strict(matrix([1,2]))
153 Traceback (most recent call last):
155 TypeError: the given point has the wrong dimensions
158 if not isinstance(point
, matrix
):
159 raise TypeError('the given point is not a cvxopt.base.matrix')
160 if not point
.size
== (self
.dimension(), 1):
161 raise TypeError('the given point has the wrong dimensions')
163 return all([x
> 0 for x
in point
])
167 class IceCream(SymmetricCone
):
169 The nonnegative orthant in ``n`` dimensions.
175 Lorentz "ice cream" cone in the real 3-space
180 Output a human-readable description of myself.
182 tpl
= 'Lorentz "ice cream" cone in the real {:d}-space'
183 return tpl
.format(self
.dimension())
186 def __contains__(self
, point
):
188 Return whether or not ``point`` belongs to this cone.
192 An instance of the ``cvxopt.base.matrix`` class having
193 dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
198 >>> matrix([1,0.5,0.5]) in K
202 >>> matrix([1,0,1]) in K
206 >>> matrix([1,1,1]) in K
211 Traceback (most recent call last):
213 TypeError: the given point is not a cvxopt.base.matrix
216 >>> matrix([1,2]) in K
217 Traceback (most recent call last):
219 TypeError: the given point has the wrong dimensions
222 if not isinstance(point
, matrix
):
223 raise TypeError('the given point is not a cvxopt.base.matrix')
224 if not point
.size
== (self
.dimension(), 1):
225 raise TypeError('the given point has the wrong dimensions')
228 if self
.dimension() == 1:
229 # In one dimension, the ice cream cone is the nonnegative
234 return height
>= norm(radius
)
237 def contains_strict(self
, point
):
239 Return whether or not ``point`` belongs to the interior
244 An instance of the ``cvxopt.base.matrix`` class having
245 dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
250 >>> K.contains_strict(matrix([1,0.5,0.5]))
254 >>> K.contains_strict(matrix([1,0,1]))
258 >>> K.contains_strict(matrix([1,1,1]))
262 >>> K.contains_strict([1,2,3])
263 Traceback (most recent call last):
265 TypeError: the given point is not a cvxopt.base.matrix
268 >>> K.contains_strict(matrix([1,2]))
269 Traceback (most recent call last):
271 TypeError: the given point has the wrong dimensions
274 if not isinstance(point
, matrix
):
275 raise TypeError('the given point is not a cvxopt.base.matrix')
276 if not point
.size
== (self
.dimension(), 1):
277 raise TypeError('the given point has the wrong dimensions')
280 if self
.dimension() == 1:
281 # In one dimension, the ice cream cone is the nonnegative
286 return height
> norm(radius
)
289 class SymmetricPSD(SymmetricCone
):
291 The nonnegative orthant in ``n`` dimensions.
295 >>> K = SymmetricPSD(3)
297 Cone of symmetric positive-semidefinite matrices on the real 3-space
302 Output a human-readable description of myself.
304 tpl
= 'Cone of symmetric positive-semidefinite matrices ' \
305 'on the real {:d}-space'
306 return tpl
.format(self
.dimension())
309 class CartesianProduct(SymmetricCone
):
311 A cartesian product of symmetric cones, which is itself a symmetric
316 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(2))
318 Cartesian product of dimension 5 with 2 factors:
319 * Nonnegative orthant in the real 3-space
320 * Lorentz "ice cream" cone in the real 2-space
323 def __init__(self
, *factors
):
324 my_dimension
= sum([f
.dimension() for f
in factors
])
325 super().__init
__(my_dimension
)
326 self
._factors
= factors
330 Output a human-readable description of myself.
332 tpl
= 'Cartesian product of dimension {:d} with {:d} factors:'
333 tpl
+= '\n * {!s}' * len(self
.factors())
334 format_args
= [self
.dimension(), len(self
.factors())]
335 format_args
+= list(self
.factors())
336 return tpl
.format(*format_args
)
338 def __contains__(self
, point
):
340 Return whether or not ``point`` belongs to this cone.
344 An instance of the ``cvxopt.base.matrix`` class having
345 dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
349 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
350 >>> matrix([1,2,3,1,0.5,0.5]) in K
353 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
354 >>> matrix([0,0,0,1,0,1]) in K
357 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
358 >>> matrix([1,1,1,1,1,1]) in K
361 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
362 >>> matrix([1,-1,1,1,0,1]) in K
365 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
366 >>> [1,2,3,4,5,6] in K
367 Traceback (most recent call last):
369 TypeError: the given point is not a cvxopt.base.matrix
371 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
372 >>> matrix([1,2]) in K
373 Traceback (most recent call last):
375 TypeError: the given point has the wrong dimensions
378 if not isinstance(point
, matrix
):
379 raise TypeError('the given point is not a cvxopt.base.matrix')
380 if not point
.size
== (self
.dimension(), 1):
381 raise TypeError('the given point has the wrong dimensions')
383 for factor
in self
.factors():
384 # Split off the components of ``point`` corresponding to
386 factor_part
= point
[0:factor
.dimension()]
387 if not factor_part
in factor
:
389 point
= point
[factor
.dimension():]
394 def contains_strict(self
, point
):
396 Return whether or not ``point`` belongs to the interior
401 An instance of the ``cvxopt.base.matrix`` class having
402 dimensions ``(n,1)`` where ``n`` is the dimension of this cone.
406 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
407 >>> K.contains_strict(matrix([1,2,3,1,0.5,0.5]))
410 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
411 >>> K.contains_strict(matrix([1,2,3,1,0,1]))
414 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
415 >>> K.contains_strict(matrix([0,1,1,1,0.5,0.5]))
418 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
419 >>> K.contains_strict(matrix([1,1,1,1,1,1]))
422 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
423 >>> K.contains_strict(matrix([1,-1,1,1,0,1]))
426 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
427 >>> K.contains_strict([1,2,3,4,5,6])
428 Traceback (most recent call last):
430 TypeError: the given point is not a cvxopt.base.matrix
432 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(3))
433 >>> K.contains_strict(matrix([1,2]))
434 Traceback (most recent call last):
436 TypeError: the given point has the wrong dimensions
439 if not isinstance(point
, matrix
):
440 raise TypeError('the given point is not a cvxopt.base.matrix')
441 if not point
.size
== (self
.dimension(), 1):
442 raise TypeError('the given point has the wrong dimensions')
444 for factor
in self
.factors():
445 # Split off the components of ``point`` corresponding to
447 factor_part
= point
[0:factor
.dimension()]
448 if not factor
.contains_strict(factor_part
):
450 point
= point
[factor
.dimension():]
457 Return a tuple containing the factors (in order) of this
462 >>> K = CartesianProduct(NonnegativeOrthant(3), IceCream(2))
469 def cvxopt_dims(self
):
471 Return a dictionary of dimensions corresponding to the factors
472 of this cartesian product. The format of this dictionary is
473 described in the CVXOPT user's guide:
475 http://cvxopt.org/userguide/coneprog.html#linear-cone-programs
479 >>> K = CartesianProduct(NonnegativeOrthant(3),
482 >>> d = K.cvxopt_dims()
483 >>> (d['l'], d['q'], d['s'])
487 dims
= {'l':0, 'q':[], 's':[]}
488 dims
['l'] += sum([K
.dimension()
489 for K
in self
.factors()
490 if isinstance(K
, NonnegativeOrthant
)])
491 dims
['q'] = [K
.dimension()
492 for K
in self
.factors()
493 if isinstance(K
, IceCream
)]
494 dims
['s'] = [K
.dimension()
495 for K
in self
.factors()
496 if isinstance(K
, SymmetricPSD
)]