-
Notifications
You must be signed in to change notification settings - Fork 3
/
relaxed_constants.py
49 lines (40 loc) · 1.63 KB
/
relaxed_constants.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
from gpkit.constraints.relax import ConstantsRelaxed
from gpkit import Model
"""
Methods to precondition an SP so that it solves with a relaxed constants algorithm
and postcondition an SP to ensure all relax values are 1
"""
def relaxed_constants(model, include_only=None, exclude=None):
"""
Method to precondition an SP so it solves with a relaxed constants algorithm
ARGUMENTS
---------
model: the model to solve with relaxed constants
RETURNS
-------
feas: the input model but with relaxed constants and a new objective
"""
if model.substitutions:
constsrelaxed = ConstantsRelaxed(model, include_only, exclude)
feas = Model(constsrelaxed.relaxvars.prod()**20 * model.cost,
constsrelaxed)
# NOTE: It hasn't yet been seen but might be possible that
# the model.cost component above could cause infeasibility
else:
feas = Model(model.cost, model)
return feas
def post_process(sol):
"""
Model to print relevant info for a solved model with relaxed constants
ARGUMENTS
--------
sol: the solution to the solved model
"""
print("Checking for relaxed constants...")
for i in range(len(sol.program.gps)):
varkeys = [k for k in sol.program.gps[i].varlocs if "Relax" in k.models and sol.program.gps[i].result(k) >= 1.00001]
if varkeys:
print("GP iteration %s has relaxed constants" % i)
print(sol.program.gps[i].result.table(varkeys))
if i == len(sol.program.gps) - 1:
print("WARNING: The final GP iteration had relaxation values greater than 1")