Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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"""Linear matrix inequalities.""" 

20 

21from collections import namedtuple 

22 

23from .. import glyphs, settings 

24from ..apidoc import api_end, api_start 

25from ..caching import cached_property 

26from .constraint import ConicConstraint, ConstraintConversion 

27 

28_API_START = api_start(globals()) 

29# ------------------------------- 

30 

31 

32class LMIConstraint(ConicConstraint): 

33 """Linear matrix inequality. 

34 

35 An inequality with respect to the positive semidefinite cone, also known as 

36 a Linear Matrix Inequality (LMI) or an SDP constraint. 

37 """ 

38 

39 def __init__(self, lhs, relation, rhs, customString=None): 

40 """Construct a :class:`LMIConstraint`. 

41 

42 :param ~picos.expressions.AffineExpression lhs: 

43 Left hand side expression. 

44 :param str relation: 

45 Constraint relation symbol. 

46 :param ~picos.expressions.AffineExpression rhs: 

47 Right hand side expression. 

48 :param str customString: 

49 Optional string description. 

50 """ 

51 from ..expressions import (AffineExpression, HermitianVariable, 

52 SymmetricVariable) 

53 from ..expressions.vectorizations import (HermitianVectorization, 

54 SymmetricVectorization) 

55 from ..expressions.data import cvxopt_equals 

56 

57 required_type = self._required_type() 

58 

59 assert isinstance(lhs, required_type) 

60 assert isinstance(rhs, required_type) 

61 assert relation in self.LE + self.GE 

62 

63 if lhs.shape != rhs.shape: 

64 raise ValueError("Failed to form a constraint: " 

65 "Expressions have incompatible dimensions.") 

66 

67 if lhs.shape[0] != lhs.shape[1]: 

68 raise ValueError("Failed to form a constraint: " 

69 "LMI expressions are not square.") 

70 

71 self.lhs = lhs 

72 self.rhs = rhs 

73 self.relation = relation 

74 

75 psd = self.psd 

76 if not psd.hermitian: 

77 needed = "symmetric" if required_type is AffineExpression \ 

78 else "hermitian" 

79 

80 raise ValueError("Failed to form a constraint: {} is not " 

81 "necessarily {}. Consider a constraint on {} instead.".format( 

82 psd.string, needed, glyphs.Tr("{}.hermitianized")(psd.string))) 

83 

84 # Check if the constraint simply poses positive semidefiniteness on a 

85 # matrix variable, as certain solvers can handle this more efficiently 

86 # than a general linear matrix inequality. 

87 self.semidefVar = None 

88 if len(psd._linear_coefs) == 1 and not psd._constant_coef: 

89 var, coef = list(psd._linear_coefs.items())[0] 

90 

91 if isinstance(var, SymmetricVariable) and cvxopt_equals( 

92 coef, SymmetricVectorization(psd.shape).identity, 

93 relTol=settings.RELATIVE_HERMITIANNESS_TOLERANCE): 

94 self.semidefVar = var 

95 elif isinstance(var, HermitianVariable) and cvxopt_equals( 

96 coef, HermitianVectorization(psd.shape).identity, 

97 relTol=settings.RELATIVE_HERMITIANNESS_TOLERANCE): 

98 self.semidefVar = var 

99 

100 super(LMIConstraint, self).__init__( 

101 self._get_type_term(), customString, printSize=True) 

102 

103 def _get_type_term(self): 

104 return "LMI" 

105 

106 def _required_type(self): 

107 from ..expressions import AffineExpression 

108 

109 return AffineExpression 

110 

111 @property 

112 def smaller(self): 

113 """The smaller-or-equal side expression.""" 

114 return self.rhs if self.relation == self.GE else self.lhs 

115 

116 @property 

117 def greater(self): 

118 """The greater-or-equal side expression.""" 

119 return self.lhs if self.relation == self.GE else self.rhs 

120 

121 @cached_property 

122 def psd(self): 

123 """The matrix expression posed to be positive semidefinite.""" 

124 if self.relation == self.GE: 

125 return self.lhs - self.rhs 

126 else: 

127 return self.rhs - self.lhs 

128 

129 @cached_property 

130 def nsd(self): 

131 """The matrix expression posed to be negative semidefinite.""" 

132 if self.relation == self.GE: 

133 return self.rhs - self.lhs 

134 else: 

135 return self.lhs - self.rhs 

136 

137 nnd = psd 

138 npd = nsd 

139 

140 @cached_property 

141 def conic_membership_form(self): 

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

143 from ..expressions import PositiveSemidefiniteCone 

144 

145 element = self.psd.svec 

146 return element, PositiveSemidefiniteCone(dim=len(element)) 

147 

148 Subtype = namedtuple("Subtype", ("diag",)) 

149 

150 def _subtype(self): 

151 return self.Subtype(self.lhs.shape[0]) 

152 

153 @classmethod 

154 def _cost(cls, subtype): 

155 n = subtype.diag 

156 return n*(n + 1)//2 

157 

158 def _expression_names(self): 

159 yield "lhs" 

160 yield "rhs" 

161 

162 def _str(self): 

163 if self.relation == self.LE: 

164 return glyphs.psdle(self.lhs.string, self.rhs.string) 

165 else: 

166 return glyphs.psdge(self.lhs.string, self.rhs.string) 

167 

168 def _get_size(self): 

169 return self.lhs.shape 

170 

171 def _get_slack(self): 

172 return self.psd.safe_value 

173 

174 

175class ComplexLMIConstraint(LMIConstraint): 

176 """Complex linear matrix inequality.""" 

177 

178 class RealConversion(ConstraintConversion): 

179 """Complex LMI to real LMI conversion.""" 

180 

181 @classmethod 

182 def predict(cls, subtype, options): 

183 """Implement :meth:`~.constraint.ConstraintConversion.predict`.""" 

184 n = subtype.diag 

185 

186 yield ("con", LMIConstraint.make_type(diag=2*n), 1) 

187 

188 @classmethod 

189 def convert(cls, con, options): 

190 """Implement :meth:`~.constraint.ConstraintConversion.convert`.""" 

191 from ..expressions.algebra import block 

192 from ..modeling import Problem 

193 

194 P = Problem() 

195 Z = con.psd 

196 P.add_constraint(block([[Z.real, -Z.imag], [Z.imag, Z.real]]) >> 0) 

197 

198 return P 

199 

200 @classmethod 

201 def dual(cls, auxVarPrimals, auxConDuals, options): 

202 """Implement :meth:`~.constraint.ConstraintConversion.dual`.""" 

203 assert len(auxConDuals) == 1 

204 

205 auxConDual = auxConDuals[0] 

206 if auxConDual is None: 

207 return None 

208 else: 

209 assert auxConDual.size[0] == auxConDual.size[1] 

210 n = auxConDual.size[0] // 2 

211 assert 2*n == auxConDual.size[0] 

212 A = auxConDual[:n, :n] 

213 B = auxConDual[:n, n:] 

214 D = auxConDual[n:, n:] 

215 return (A + 1j*B) + (D + 1j*B).H 

216 

217 def _get_type_term(self): 

218 return "Complex LMI" 

219 

220 def _required_type(self): 

221 from ..expressions import ComplexAffineExpression 

222 

223 return ComplexAffineExpression 

224 

225 @classmethod 

226 def _cost(cls, subtype): 

227 return subtype.diag**2 

228 

229 

230# -------------------------------------- 

231__all__ = api_end(_API_START, globals())