-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstraint.py
403 lines (326 loc) · 13.8 KB
/
constraint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import pprint
from typing import Dict, List, Optional, Sequence, Union
import numpy as np
import pandas as pd
class Constraint:
"""Base class to define constraints on the input space, g(x) == 0 or g(x) <= 0."""
def __call__(self, data: pd.DataFrame) -> pd.Series:
"""Numerically evaluate the constraint g(x)."""
raise NotImplementedError
def jacobian(self, data: pd.DataFrame) -> pd.DataFrame:
"""Numerically evaluate the jacobian of the constraint J_g(x)"""
raise NotImplementedError
def satisfied(self, data: pd.DataFrame) -> pd.Series:
"""Check if a constraint is satisfied, i.e. g(x) == 0 for equalities and g(x) <= for inequalities."""
raise NotImplementedError
def to_config(self) -> Dict:
raise NotImplementedError
class LinearEquality(Constraint):
def __init__(
self,
names: List[str],
lhs: Union[float, List[float], np.ndarray] = 1,
rhs: float = 0,
):
"""Linear / affine inequality of the form 'lhs * x == rhs'.
Args:
names: Parameter names that the constraint works on.
lhs: Left-hand side / coefficients of the constraint.
rhs: Right-hand side of the constraint.
Examples:
A mixture constraint where A, B and C need to add up to 100 can be defined as
```
LinearEquality(["A", "B", "C"], rhs=100)
```
If the coefficients of A, B and C are not 1 they are passed explicitly.
```
LinearEquality(["A", "B", "C"], lhs=[10, 2, 5], rhs=100)
```
"""
self.names = names
if np.isscalar(lhs):
self.lhs = lhs * np.ones(len(names))
else:
self.lhs = np.asarray(lhs)
if self.lhs.shape != (len(names),):
raise ValueError("Number of parameters and coefficients/lhs don't match.")
self.rhs = rhs
self.is_equality = True
def __call__(self, data: pd.DataFrame) -> pd.Series:
return (data[self.names] @ self.lhs - self.rhs) / np.linalg.norm(self.lhs)
def jacobian(self, data: pd.DataFrame) -> pd.DataFrame:
return pd.DataFrame(
np.tile(self.lhs / np.linalg.norm(self.lhs), [data.shape[0], 1]),
columns=["dg/d" + name for name in self.names],
)
def satisfied(self, data: pd.DataFrame) -> pd.Series:
return pd.Series(np.isclose(self(data), 0), index=data.index)
def __repr__(self):
return (
f"LinearEquality(names={self.names}, lhs={list(self.lhs)}, rhs={self.rhs})"
)
def to_config(self) -> Dict:
return dict(
type="linear-equality",
names=self.names,
lhs=self.lhs.tolist(),
rhs=self.rhs,
)
class LinearInequality(Constraint):
def __init__(
self,
names: List[str],
lhs: Union[float, List[float], np.ndarray] = 1,
rhs: float = 0,
):
"""Linear / affine inequality of the form 'lhs * x <= rhs'.
Args:
names: Parameter names that the constraint works on.
lhs: Left-hand side / coefficients of the constraint.
rhs: Right-hand side of the constraint.
Examples:
A mixture constraint where the values of A, B and C may not exceed 100 can be defined as
```
LinearInequality(["A", "B", "C"], rhs=100)
```
If the coefficients are not 1, they need to be passed explicitly.
```
LinearInequality(["A", "B", "C"], lhs=[10, 2, 5], rhs=100)
```
Inequalities are alway of the form g(x) <= 0. To define a the constraint g(x) >=0 0, both `lhs` and `rhs` need to be multiplied by -1.
```
LinearInequality(["A", "B", "C"], lhs=-1, rhs=-100)
LinearInequality(["A", "B", "C"], lhs=[-10, -2, -5], rhs=-100)
```
"""
self.names = names
if np.isscalar(lhs):
self.lhs = lhs * np.ones(len(names))
else:
self.lhs = np.asarray(lhs)
if self.lhs.shape != (len(names),):
raise ValueError("Number of parameters and coefficients/lhs don't match.")
self.rhs = rhs
self.is_equality = False
def __call__(self, data: pd.DataFrame) -> pd.Series:
return (data[self.names] @ self.lhs - self.rhs) / np.linalg.norm(self.lhs)
def jacobian(self, data: pd.DataFrame) -> pd.DataFrame:
return pd.DataFrame(
np.tile(self.lhs / np.linalg.norm(self.lhs), [data.shape[0], 1]),
columns=["dg/d" + name for name in self.names],
)
def satisfied(self, data: pd.DataFrame) -> pd.Series:
return self(data) <= 0
def __repr__(self):
return f"LinearInequality(names={self.names}, lhs={list(self.lhs)}, rhs={self.rhs})"
def to_config(self) -> Dict:
return dict(
type="linear-inequality",
names=self.names,
lhs=self.lhs.tolist(),
rhs=self.rhs,
)
class NonlinearEquality(Constraint):
def __init__(
self,
expression: str,
jacobian: Optional[str] = None,
names: Optional[List[str]] = None,
):
"""Equality of the form 'expression == 0'.
Args:
expression: Mathematical expression that can be evaluated by `pandas.eval`.
jacobian: List of mathematical expressions that can be evaluated by `pandas.eval`.
The i-th expression should correspond to the partial derivative with respect to
the i-th variable. If `names` attribute is provided, the order of the variables should
correspond to the order of the variables in `names`. Optional.
names: List of variable names present in `expression`. Optional.
Examples:
You can pass any expression that can be evaluated by `pd.eval`.
To define x1**2 + x2**2 = 1, use
```
NonlinearEquality("x1**2 + x2**2 - 1")
```
Standard mathematical operators are supported.
```
NonlinearEquality("sin(A) / (exp(B) - 1)")
```
Parameter names with special characters or spaces need to be enclosed in backticks.
```
NonlinearEquality("1 - `weight A` / `weight B`")
```
"""
self.expression = expression
self.is_equality = True
self.jacobian_expression = jacobian
self.names = names
def __call__(self, data: pd.DataFrame) -> pd.Series:
return data.eval(self.expression)
def jacobian(self, data: pd.DataFrame) -> pd.DataFrame:
if self.jacobian_expression is not None:
res = data.eval(self.jacobian_expression)
for i, col in enumerate(res):
if not hasattr(col, "__iter__"):
res[i] = pd.Series(np.repeat(col, data.shape[0]))
if self.names is not None:
return pd.DataFrame(
res, index=["dg/d" + name for name in self.names]
).transpose()
else:
return pd.DataFrame(
res, index=[f"dg/dx{i}" for i in range(data.shape[1])]
).transpose()
return super().jacobian(data)
def satisfied(self, data: pd.DataFrame) -> pd.Series:
return pd.Series(np.isclose(self(data), 0), index=data.index)
def __repr__(self):
return f"NonlinearEquality('{self.expression}')"
def to_config(self) -> Dict:
return dict(type="nonlinear-equality", expression=self.expression)
class NonlinearInequality(Constraint):
def __init__(
self,
expression: str,
jacobian: Optional[str] = None,
names: Optional[List[str]] = None,
):
"""Inequality of the form 'expression <= 0'.
Args:
expression: Mathematical expression that can be evaluated by `pandas.eval`.
jacobian: List of mathematical expressions that can be evaluated by `pandas.eval`.
The i-th expression should correspond to the partial derivative with respect to
the i-th variable. If `names` attribute is provided, the order of the variables should
correspond to the order of the variables in `names`. Optional.
names: List of variable names present in `expression`. Optional.
Examples:
You can pass any expression that can be evaluated by `pd.eval`.
To define x1**2 + x2**2 < 1, use
```
NonlinearInequality("x1**2 + x2**2 - 1")
```
Standard mathematical operators are supported.
```
NonlinearInequality("sin(A) / (exp(B) - 1)")
```
Parameter names with special characters or spaces need to be enclosed in backticks.
```
NonlinearInequality("1 - `weight A` / `weight B`")
```
"""
self.expression = expression
self.is_equality = False
self.jacobian_expression = jacobian
self.names = names
def __call__(self, data: pd.DataFrame) -> pd.Series:
return data.eval(self.expression)
def jacobian(self, data: pd.DataFrame) -> pd.DataFrame:
if self.jacobian_expression is not None:
res = data.eval(self.jacobian_expression)
for i, col in enumerate(res):
if not hasattr(col, "__iter__"):
res[i] = pd.Series(np.repeat(col, data.shape[0]))
if self.names is not None:
return pd.DataFrame(
res, index=["dg/d" + name for name in self.names]
).transpose()
else:
return pd.DataFrame(
res, index=[f"dg/dx{i}" for i in range(data.shape[1])]
).transpose()
return super().jacobian(data)
def satisfied(self, data: pd.DataFrame) -> pd.Series:
return self(data) <= 0
def __repr__(self):
return f"NonlinearInequality('{self.expression}')"
def to_config(self) -> Dict:
return dict(type="nonlinear-inequality", expression=self.expression)
class NChooseK(Constraint):
def __init__(self, names: List[str], max_active: int):
"""Only k out of n values are allowed to take nonzero values.
Args:
names: Parameter names that the constraint works on.
max_active: Maximium number of non-zero parameter values.
Examples:
A choice of 2 or less from A, B, C, D or E can be defined as
```
NChooseK(["A", "B", "C", "D", "E"], max_active=2)
```
"""
self.names = names
self.max_active = max_active
self.is_equality = False
def __call__(self, data: pd.DataFrame) -> pd.Series:
x = np.abs(data[self.names].values)
num_zeros = x.shape[1] - self.max_active
violation = np.apply_along_axis(
func1d=lambda r: sum(sorted(r)[:num_zeros]), axis=1, arr=x
)
return pd.Series(violation, index=data.index)
def satisfied(self, data: pd.DataFrame) -> pd.Series:
return pd.Series(self(data) <= 0, index=data.index)
def __repr__(self):
return f"NChooseK(names={self.names}, max_active={self.max_active})"
def to_config(self) -> Dict:
return dict(type="n-choose-k", names=self.names, max_active=self.max_active)
class Constraints:
"""List of input constraints"""
def __init__(self, constraints: Sequence):
self.constraints = []
for c in constraints:
if not isinstance(c, Constraint):
if "names" in c and len(c["names"]) == 0:
continue # skip empty constraints
c = make_constraint(**c)
self.constraints.append(c)
def __repr__(self):
return "Constraints(\n" + pprint.pformat(self.constraints) + "\n)"
def __iter__(self):
return iter(self.constraints)
def __len__(self):
return len(self.constraints)
def __getitem__(self, i):
return self.constraints[i]
def __call__(self, data: pd.DataFrame) -> pd.DataFrame:
"""Numerically evaluate all constraints.
Args:
data: Data to evaluate the constraints on.
Returns:
Constraint evaluation g(x) for each of the constraints.
"""
return pd.concat([c(data) for c in self.constraints], axis=1)
def jacobian(self, data: pd.DataFrame) -> List:
"""Numerically evaluate all constraint gradients.
Args:
data: Data to evaluate the constraint gradients on.
Returns:
Jacobian evaluation J_g(x) for each of the constraints as a list of dataframes.
"""
return [c.jacobian(data) for c in self.constraints]
def satisfied(self, data: pd.DataFrame) -> pd.Series:
"""Check if all constraints are satisfied.
Args:
data: Data to evaluate the constraints on.
Returns:
Series of booleans indicating if all constraints are satisfied.
"""
return pd.concat([c.satisfied(data) for c in self.constraints], axis=1).all(
axis=1
)
def to_config(self) -> List[Dict]:
return [obj.to_config() for obj in self.constraints]
def get(self, types) -> "Constraints":
"""Get all constraints of the given type(s)."""
return Constraints([c for c in self if isinstance(c, types)])
def make_constraint(type, **kwargs):
t = type.lower()
if t == "linear-equality":
return LinearEquality(**kwargs)
if t == "linear-inequality":
return LinearInequality(**kwargs)
if t == "nonlinear-equality":
return NonlinearEquality(**kwargs)
if t == "nonlinear-inequality":
return NonlinearInequality(**kwargs)
if t == "n-choose-k":
return NChooseK(**kwargs)
raise ValueError(f"Unknown constraint type: {t}.")