-
Notifications
You must be signed in to change notification settings - Fork 0
/
var.py
408 lines (312 loc) · 11 KB
/
var.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
404
405
406
407
408
import numpy as np
from scipy.optimize import minimize
"""
Wrapper classes for functional expressions
Copyright (C) 2015-2019
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
"""
Classes:
* Operation
* Expression(Operation)
* Constraint(Expression)
* Array(Expression)
* Variable(Operation)
"""
def sum(arr):
return Operation.sum(Array(arr))
def max(arr):
return Operation.max(Array(arr))
def min(arr):
return Operation.min(Array(arr))
def exp(arr):
return Operation.exp(Array(arr))
def log(arr):
return Operation.log(Array(arr))
@np.vectorize
def value_of(variable):
if isinstance(variable, Operation):
return variable.value
elif variable is not None:
return float(variable)
else:
return variable
class Operation:
#######################
# basic arithmetic
#######################
def __add__(self, right):
return Expression(self, right, lambda x, y: x + y, '+')
__radd__ = __add__
def __sub__(self, right):
return Expression(self, right, lambda x, y: x - y, '-')
def __rsub__(self, left):
return Expression(left, self, lambda x, y: x - y, '-')
def __mul__(self, right):
return Expression(right, self, lambda x, y: x * y, '*')
__rmul__ = __mul__
def __pow__(self, right):
return Expression(self, right, lambda x, y: x ** y, '**')
def __truediv__(self, right):
return Expression(self, right, lambda x, y: x / y, '/')
def __rtruediv__(self, left):
return Expression(left, self, lambda x, y: x / y, '/')
def __floordiv__(self, right):
return Expression(self, right, lambda x, y: x // y, '//')
def __rfloordiv__(self, left):
return Expression(left, self, lambda x, y: x // y, '//')
def __matmul__(self, right):
return Expression(self, right, lambda x, y: x @ y, '@')
def __rmatmul__(self, left):
return Expression(left, self, lambda x, y: x @ y, '@')
#######################
# unary operations
#######################
def __pos__(self):
return self
def __neg__(self):
return Expression(None, self, lambda x, y: -y, lambda x, y: f'-{y}')
def __abs__(self):
return Expression(None, self, lambda x, y: abs(y),
lambda x, y: f'abs({y})')
#######################
# constraints
#######################
def __eq__(self, right):
return Constraint(self, right, lambda x, y: x == y, '==')
def __le__(self, right):
return Constraint(self, right, lambda x, y: x <= y, '<=')
def __lt__(self, right):
return self.__le__(right)
def __ge__(self, right):
return Constraint(self, right, lambda x, y: x >= y, '>=')
def __gt__(self, right):
return self.__ge__(right)
#######################
# other functions
#######################
def sum(self):
return Expression(None, self, lambda x, y: np.sum(y),
lambda x, y: f'sum({y})')
def min(self):
return Expression(None, self, lambda x, y: np.min(y),
lambda x, y: f'min({y})')
def max(self):
return Expression(None, self, lambda x, y: np.max(y),
lambda x, y: f'max({y})')
def exp(self):
return Expression(None, self, lambda x, y: np.exp(y),
lambda x, y: f'exp({y})')
def log(self):
return Expression(None, self, lambda x, y: np.log(y),
lambda x, y: f'log({y})')
def abs(self):
return self.__abs__()
#######################
# other
#######################
@property
def value(self):
raise NotImplementedError
class Expression(Operation):
def __init__(self, left, right, method, symbol):
self._left = left
self._right = right
self._method = method
self._symbol = symbol
def __repr__(self):
right_name = self._name(self._right)
left_name = self._name(self._left)
if isinstance(self._symbol, str):
return f'({left_name} {self._symbol} {right_name})'
else:
return self._symbol(left_name, right_name)
@staticmethod
def _name(var):
if var is not None:
return var if not hasattr(var, 'name') else var.name
else:
return ''
@property
def left(self):
return self._left
@property
def right(self):
return self._right
@property
def value(self):
left_val = value_of(self._left)
right_val = value_of(self._right)
return self._method(left_val, right_val)
def variables(self):
# helper function
def update(value, variables, expressions):
if isinstance(value, Array):
variables = variables.union([v.id for v in value.variables()])
elif isinstance(value, Expression):
expressions.append(value)
elif isinstance(value, Variable):
variables.add(value.id)
return variables, expressions
# initialise
list_expr = [self]
list_var = set()
while len(list_expr) > 0:
expression = list_expr.pop()
list_var, list_expr = update(expression.left, list_var, list_expr)
list_var, list_expr = update(expression.right, list_var, list_expr)
variables = [Variable._vars[i] for i in list_var]
return variables
class Constraint(Expression):
@property
def is_equality(self):
return self._symbol == '=='
@property
def is_greater_than(self):
return self._symbol == '>='
@property
def is_less_than(self):
return self._symbol == '<='
class Array(Expression):
def __init__(self, value=None, shape=None):
if value is not None and shape is None:
self._shape = np.shape(value)
self._value = np.array(value)
elif value is None and shape is not None:
self._shape = shape
self._value = np.empty(shape, dtype=object)
for i in range(np.prod(shape)):
self._value[np.unravel_index(i, shape)] = Variable()
def __repr__(self):
value = np.copy(self._value)
value_ravelled = value.ravel()
original_value_ravelled = self._value.ravel()
for i in range(len(original_value_ravelled)):
val = original_value_ravelled[i]
if isinstance(val, Variable):
value_ravelled[i] = val.name
else:
value_ravelled[i] = val
s = value.__repr__()
s = s.replace('array', '').replace(', dtype=object', '')
s = '\n'.join(x.strip() for x in s.split('\n'))
return s[1:-1]
def __getitem__(self, key):
return self._value[key]
@property
def shape(self):
return self._shape
@property
def value(self):
return value_of(self._value)
def variables(self):
variables = []
for var in self._value.flatten():
if isinstance(var, Variable):
variables.append(var)
elif isinstance(var, Expression):
variables.extend(var.variables())
return variables
class Variable(Operation):
_n = 0
_initial_guesses = []
_vars = []
def __init__(self, name=None, value=None):
self.value = value
self._id = Variable._n
self._name = f'var{self._id}' if name is None else name
# TODO: add variables that are not just scalars
Variable._n += 1
Variable._vars.append(self)
def __repr__(self):
if self.value:
return f'<{self._name}: {self.value:.4f}>'
else:
return f'<{self._name}>'
def __str__(self):
return f'<{self._name}: {self.value:.4f}>'
@property
def id(self):
return self._id
@property
def name(self):
return self._name
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value
class Problem:
def __init__(self, obj, constraints=None, tol=1e-6):
self._obj = obj
self._constraints = constraints
self._variables = obj.variables()
self._obj_fun = self._build_objective_function(obj)
if isinstance(constraints, Constraint):
constraints = [constraints]
elif constraints is None:
constraints = []
self._constraint_funs = self._build_constraints(constraints)
def __repr__(self):
return (f'minimise {self._obj}\n' +
's.t.\n' +
'\n'.join([f'{c}' for c in self._constraints]))
def _assign_to_variables(self, x):
for i, var in enumerate(self._variables):
var.value = x[i]
def _initial_guess(self):
return [1. if x.value is None else x.value
for x in self._variables]
def minimize(self):
opt = minimize(self._obj_fun, self._initial_guess(),
constraints=self._constraint_funs)
self._assign_to_variables(opt.x)
return opt
def _build_objective_function(self, expression):
def fun(x):
self._assign_to_variables(x)
return expression.value
return fun
def _build_constraints(self, constraints):
new = []
for constraint in constraints:
if constraint.is_equality:
def fun(x, constraint=constraint):
self._assign_to_variables(x)
return (constraint._left - constraint._right).value
new.append({'type': 'eq', 'fun': fun})
elif constraint.is_greater_than:
def fun(x, constraint=constraint):
self._assign_to_variables(x)
return (constraint._left - constraint._right).value
new.append({'type': 'ineq', 'fun': fun})
elif constraint.is_less_than:
def fun(x, constraint=constraint):
self._assign_to_variables(x)
return (constraint._right - constraint._left).value
new.append({'type': 'ineq', 'fun': fun})
return new
if __name__ == '__main__':
# fake data
a = 2
m = 3
x = np.linspace(0, 10)
y = a * x + m + np.random.randn(len(x))
a_ = Variable()
m_ = Variable()
y_ = a_ * x + m_
error = y_ - y
prob = Problem((error**2).sum(), None)
prob.minimize()
print(f'a = {a}, a_ = {a_}')
print(f'm = {m}, m_ = {m_}')