Coverage for picos/constraints/con_expcone.py: 92.11%

38 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-26 07:46 +0000

1# ------------------------------------------------------------------------------ 

2# Copyright (C) 2018-2019 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# ------------------------------------------------------------------------------ 

18 

19"""Implementation of :class:`ExpConeConstraint`.""" 

20 

21from collections import namedtuple 

22 

23import numpy 

24 

25from .. import glyphs 

26from ..apidoc import api_end, api_start 

27from ..caching import cached_property 

28from .constraint import Constraint 

29 

30_API_START = api_start(globals()) 

31# ------------------------------- 

32 

33 

34class ExpConeConstraint(Constraint): 

35 r"""Exponential cone membership constraint. 

36 

37 An exponential cone constraint stating that :math:`(x, y, z)` fulfills 

38 :math:`x \geq y e^{\frac{z}{y}} \land x > 0 \land y > 0`. 

39 """ 

40 

41 def __init__(self, element, customString=None): 

42 """Construct an :class:`ExpConeConstraint`. 

43 

44 :param ~picos.expressions.AffineExpression element: 

45 Three-dimensional expression representing the vector 

46 :math:`(x, y, z)`. 

47 :param str customString: 

48 Optional string description. 

49 """ 

50 from ..expressions import AffineExpression 

51 

52 assert isinstance(element, AffineExpression) 

53 assert len(element) == 3 

54 

55 self.element = element 

56 

57 super(ExpConeConstraint, self).__init__( 

58 "Exponential Cone", customString, printSize=False) 

59 

60 x = property(lambda self: self.element[0]) 

61 y = property(lambda self: self.element[1]) 

62 z = property(lambda self: self.element[2]) 

63 

64 @cached_property 

65 def conic_membership_form(self): 

66 """Implement for :class:`~.constraint.ConicConstraint`.""" 

67 from ..expressions import ExponentialCone 

68 return self.element.vec, ExponentialCone() 

69 

70 Subtype = namedtuple("Subtype", ()) 

71 

72 def _subtype(self): 

73 return self.Subtype() 

74 

75 @classmethod 

76 def _cost(cls, subtype): 

77 return 3 

78 

79 def _expression_names(self): 

80 yield "element" 

81 

82 def _str(self): 

83 return glyphs.ge(self.element[0].string, 

84 glyphs.mul(self.element[1].string, glyphs.exp(glyphs.div( 

85 self.element[2].string, self.element[1].string)))) 

86 

87 def _get_size(self): 

88 return (3, 1) 

89 

90 def _get_slack(self): 

91 x = self.element.safe_value 

92 

93 # TODO: Also consider x_1 > 0 and x_2 > 0 by projecting x on the cone's 

94 # surface and measuring the distance between the projection and x. 

95 return x[0] - x[1] * numpy.exp(x[2] / x[1]) 

96 

97 

98# -------------------------------------- 

99__all__ = api_end(_API_START, globals())