-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopt.py
365 lines (272 loc) · 10.8 KB
/
opt.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
## Copyright 2015-2017 Tom Brown (FIAS), Jonas Hoersch (FIAS)
## 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 <http://www.gnu.org/licenses/>.
"""
Tools for fast Pyomo linear problem building.
Essentially this library replaces Pyomo expressions with more strict
objects with a pre-defined affine structure.
This code is also available as a gist
https://gist.github.com/nworbmot/db3d446fa3b5c388519390e46fd5d8c3
under a more permissive Apache 2.0 licence to allow sharing with other
projects.
"""
# make the code as Python 3 compatible as possible
from __future__ import division, absolute_import
from six.moves import range
import logging
logger = logging.getLogger(__name__)
from pyomo.environ import Constraint, Objective, Var, ComponentUID
from weakref import ref as weakref_ref
import pyomo
from contextlib import contextmanager
from six import iteritems
from six.moves import cPickle as pickle
import pandas as pd
import gc, os, tempfile
__author__ = "Tom Brown (FIAS), Jonas Hoersch (FIAS)"
__copyright__ = "Copyright 2015-2017 Tom Brown (FIAS), Jonas Hoersch (FIAS), GNU GPL 3"
class LExpression(object):
"""Affine expression of optimisation variables.
Affine expression of the form:
constant + coeff1*var1 + coeff2*var2 + ....
Parameters
----------
variables : list of tuples of coefficients and variables
e.g. [(coeff1,var1),(coeff2,var2),...]
constant : float
"""
def __init__(self,variables=None,constant=0.):
if variables is None:
self.variables = []
else:
self.variables = variables
self.constant = constant
def __repr__(self):
return "{} + {}".format(self.variables, self.constant)
def __mul__(self,constant):
try:
constant = float(constant)
except:
logger.error("Can only multiply an LExpression with a float!")
return None
return LExpression([(constant*item[0],item[1]) for item in self.variables],
constant*self.constant)
def __rmul__(self,constant):
return self.__mul__(constant)
def __add__(self,other):
if type(other) is LExpression:
return LExpression(self.variables + other.variables,self.constant+other.constant)
else:
try:
constant = float(other)
except:
logger.error("Can only add an LExpression to another LExpression or a constant!")
return None
return LExpression(self.variables[:],self.constant+constant)
def __radd__(self,other):
return self.__add__(other)
def __pos__(self):
return self
def __neg__(self):
return -1*self
class LConstraint(object):
"""Constraint of optimisation variables.
Linear constraint of the form:
lhs sense rhs
Parameters
----------
lhs : LExpression
sense : string
rhs : LExpression
"""
def __init__(self,lhs=None,sense="==",rhs=None):
if lhs is None:
self.lhs = LExpression()
else:
self.lhs = lhs
self.sense = sense
if rhs is None:
self.rhs = LExpression()
else:
self.rhs = rhs
def __repr__(self):
return "{} {} {}".format(self.lhs, self.sense, self.rhs)
def l_constraint(model,name,constraints,*args):
"""A replacement for pyomo's Constraint that quickly builds linear
constraints.
Instead of
model.name = Constraint(index1,index2,...,rule=f)
call instead
l_constraint(model,name,constraints,index1,index2,...)
where constraints is a dictionary of constraints of the form:
constraints[i] = LConstraint object
OR using the soon-to-be-deprecated list format:
constraints[i] = [[(coeff1,var1),(coeff2,var2),...],sense,constant_term]
i.e. the first argument is a list of tuples with the variables and their
coefficients, the second argument is the sense string (must be one of
"==","<=",">=","><") and the third argument is the constant term
(a float). The sense "><" allows lower and upper bounds and requires
`constant_term` to be a 2-tuple.
Variables may be repeated with different coefficients, which pyomo
will sum up.
Parameters
----------
model : pyomo.environ.ConcreteModel
name : string
Name of constraints to be constructed
constraints : dict
A dictionary of constraints (see format above)
*args :
Indices of the constraints
"""
setattr(model,name,Constraint(*args,noruleinit=True))
v = getattr(model,name)
for i in v._index:
c = constraints[i]
if type(c) is LConstraint:
variables = c.lhs.variables + [(-item[0],item[1]) for item in c.rhs.variables]
sense = c.sense
constant = c.rhs.constant - c.lhs.constant
else:
variables = c[0]
sense = c[1]
constant = c[2]
v._data[i] = pyomo.core.base.constraint._GeneralConstraintData(None,v)
v._data[i]._body = pyomo.core.base.expr_coopr3._SumExpression()
v._data[i]._body._args = [item[1] for item in variables]
v._data[i]._body._coef = [item[0] for item in variables]
v._data[i]._body._const = 0.
if sense == "==":
v._data[i]._equality = True
v._data[i]._lower = pyomo.core.base.numvalue.NumericConstant(constant)
v._data[i]._upper = pyomo.core.base.numvalue.NumericConstant(constant)
elif sense == "<=":
v._data[i]._equality = False
v._data[i]._lower = None
v._data[i]._upper = pyomo.core.base.numvalue.NumericConstant(constant)
elif sense == ">=":
v._data[i]._equality = False
v._data[i]._lower = pyomo.core.base.numvalue.NumericConstant(constant)
v._data[i]._upper = None
elif sense == "><":
v._data[i]._equality = False
v._data[i]._lower = pyomo.core.base.numvalue.NumericConstant(constant[0])
v._data[i]._upper = pyomo.core.base.numvalue.NumericConstant(constant[1])
else: raise KeyError('`sense` must be one of "==","<=",">=","><"; got: {}'.format(sense))
def l_objective(model,objective=None):
"""
A replacement for pyomo's Objective that quickly builds linear
objectives.
Instead of
model.objective = Objective(expr=sum(vars[i]*coeffs[i] for i in index)+constant)
call instead
l_objective(model,objective)
where objective is an LExpression.
Variables may be repeated with different coefficients, which pyomo
will sum up.
Parameters
----------
model : pyomo.environ.ConcreteModel
objective : LExpression
"""
if objective is None:
objective = LExpression()
#initialise with a dummy
model.objective = Objective(expr = 0.)
model.objective._expr = pyomo.core.base.expr_coopr3._SumExpression()
model.objective._expr._args = [item[1] for item in objective.variables]
model.objective._expr._coef = [item[0] for item in objective.variables]
model.objective._expr._const = objective.constant
@contextmanager
def empty_model(model):
logger.debug("Storing pyomo model to disk")
rules = {}
for obj in model.component_objects(ctype=Constraint):
if obj.rule is not None:
rules[obj.name] = obj.rule
obj.rule = None
bounds = {}
for obj in model.component_objects(ctype=Var):
if obj._bounds_init_rule is not None:
bounds[obj.name] = obj._bounds_init_rule
obj._bounds_init_rule = None
smap_id, symbol_map = (next(iteritems(model.solutions.symbol_map))
if model.solutions.symbol_map
else (None, None))
if smap_id is not None:
for m in ('bySymbol', 'aliases'):
setattr(symbol_map, m,
{n: ComponentUID(obj())
for n, obj in iteritems(getattr(symbol_map, m))})
fd, fn = tempfile.mkstemp()
with os.fdopen(fd, 'wb') as f:
pickle.dump(model.__getstate__(), f, -1)
model.__dict__.clear()
logger.debug("Stored pyomo model to disk")
gc.collect()
yield
logger.debug("Reloading pyomo model")
with open(fn, 'rb') as f:
state = pickle.load(f)
os.remove(fn)
model.__setstate__(state)
for n, rule in iteritems(rules):
getattr(model, n).rule = rule
for n, bound in iteritems(bounds):
getattr(model, n)._bounds_init_rule = bound
if smap_id is not None:
for m in ('bySymbol', 'aliases'):
setattr(symbol_map, m,
{n: weakref_ref(cuid.find_component(model))
for n, cuid in iteritems(getattr(symbol_map, m))})
symbol_map.byObject = {id(obj()): symb
for symb, obj in iteritems(symbol_map.bySymbol)}
model.solutions.symbol_map[smap_id] = symbol_map
logger.debug("Reloaded pyomo model")
@contextmanager
def empty_network(network):
logger.debug("Storing pypsa timeseries to disk")
from .components import all_components
panels = {}
for c in all_components:
attr = network.components[c]["list_name"] + "_t"
panels[attr] = getattr(network, attr)
setattr(network, attr, None)
fd, fn = tempfile.mkstemp()
with os.fdopen(fd, 'wb') as f:
pickle.dump(panels, f, -1)
del panels
gc.collect()
yield
logger.debug("Reloading pypsa timeseries from disk")
with open(fn, 'rb') as f:
panels = pickle.load(f)
os.remove(fn)
for attr, pnl in iteritems(panels):
setattr(network, attr, pnl)
def patch_optsolver_free_model_before_solving(opt, model):
orig_apply_solver = opt._apply_solver
def wrapper():
with empty_model(model):
return orig_apply_solver()
opt._apply_solver = wrapper
def patch_optsolver_record_memusage_before_solving(opt, network):
try:
import resource
orig_apply_solver = opt._apply_solver
def wrapper():
network.max_memusage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
return orig_apply_solver()
opt._apply_solver = wrapper
return True
except ImportError:
logger.debug("Unable to measure memory usage, since the resource library is missing")
return False