Coverage for picos/constraints/con_dummy.py: 90.32%
31 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-26 07:46 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-26 07:46 +0000
1# ------------------------------------------------------------------------------
2# Copyright (C) 2020 Maximilian Stahlberg
3#
4# This file is part of PICOS.
5#
6# PICOS is free software: you can redistribute it and/or modify it under the
7# terms of the GNU General Public License as published by the Free Software
8# Foundation, either version 3 of the License, or (at your option) any later
9# version.
10#
11# PICOS is distributed in the hope that it will be useful, but WITHOUT ANY
12# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License along with
16# this program. If not, see <http://www.gnu.org/licenses/>.
17# ------------------------------------------------------------------------------
19"""Implementation of :class:`DummyConstraint`."""
21from collections import namedtuple
23from ..apidoc import api_end, api_start
24from ..caching import cached_property
25from .constraint import ConicConstraint
27_API_START = api_start(globals())
28# -------------------------------
31class DummyConstraint(ConicConstraint):
32 """An explicit way to *not* put a bound on an affine expression.
34 This is produced when declaring an expression a member of the trivial
35 :class:`~.expressions.TheField` cone.
37 A constraint of this type can be used to pass a variable to a solver that
38 does not otherwise appear in the problem.
39 """
41 def __init__(self, x):
42 """Construct a :class:`DummyConstraint`."""
43 from ..expressions import ComplexAffineExpression
45 assert isinstance(x, ComplexAffineExpression)
47 self.x = x
49 super(DummyConstraint, self).__init__("Dummy")
51 @cached_property
52 def conic_membership_form(self):
53 """Implement for :class:`~.constraint.ConicConstraint`."""
54 from ..expressions import TheField
55 return self.x.vec, TheField(dim=len(self.x))
57 Subtype = namedtuple("Subtype", ())
59 def _subtype(self):
60 return self.Subtype()
62 @classmethod
63 def _cost(cls, subtype):
64 return 0
66 def _expression_names(self):
67 yield "x"
69 def _str(self):
70 return "{} is {}".format(
71 self.x.string, "complex" if self.x.complex else "real")
73 def _get_size(self):
74 return (len(self.x), 1)
76 def _get_slack(self):
77 return float("inf")
80# --------------------------------------
81__all__ = api_end(_API_START, globals())