/
t_primitives.py
92 lines (83 loc) · 4.14 KB
/
t_primitives.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
import numpy as np
import os
import pickle
import unittest
from gpkit.tests.helpers import run_tests
from robust.robust_gp_tools import RobustGPTools
from robust.twoterm_approximation import TwoTermApproximation
from robust.robust import RobustModel
from robust.margin import MarginModel
from robust.testing.models import simple_wing
from robust.testing.models import sp_test_model, gp_test_model
class TestPrimitives(unittest.TestCase):
def test_MarginModel(self):
""" Tests creation and solution of MarginModel"""
m = sp_test_model()
mm = MarginModel(m, gamma=0.5)
margin_solution = mm.localsolve(verbosity=0)
uncertain_varkeys = [k for k in m.varkeys if RobustGPTools.is_directly_uncertain(k)]
# Checking margin allocation
for key in list(uncertain_varkeys):
assert(mm.substitutions.get(key) == m.substitutions.get(key) *
(1.+mm.setting.get("gamma")*key.pr/100.))
self.assertGreater(margin_solution['cost'], mm.nominal_cost)
def test_conservativeness(self):
""" Testing conservativeness of solution methods"""
m = gp_test_model()
sm = m.solve(verbosity=0)
sem = RobustModel(m, 'elliptical').robustsolve(verbosity=0)
smm = MarginModel(m).solve(verbosity=0)
sbm = RobustModel(m, 'box').robustsolve(verbosity=0)
self.assertTrue(sm['cost'] <= sem['cost'] <= smm['cost'] <= sbm['cost'])
# def test_robustify_monomial(self):
# """ Testing whether monomials are robustified correctly"""
# m = gp_test_model()
# monys = []
# for c in m.flat(constraintsets=False):
# for monomial in c.as_posyslt1()[0].chop():
# monys.append(monomial)
# uncertain_vars = [i for i in m.varkeys if RobustGPTools.is_directly_uncertain(i)]
# rm = RobustModel(m, 'box')
# robust_monys = [rm.robustify_monomial(mony) for mony in monys]
#
# def test_two_term_tolerance(self):
# m = gp_test_model()
# rm = RobustModel(m, 'box')
# posy = [c for c in m.flat(constraintsets=False)][1].left
# tta = TwoTermApproximation(posy, rm.setting)
# data_constr = []
# no_data_constr = []
# for i,v in enumerate(tta.list_of_permutations):
# ndc, dc = tta.equivalent_posynomial(posy, i, v, False)
# no_data_constr.append(ndc)
# data_constr.append(dc)
#
# rm.calculate_value_of_two_term_approximated_posynomial(two_term_approximation, index_of_permutation,
# solution)
def test_methods(self):
m = gp_test_model()
nominal_solution = m.solve(verbosity=0)
methods = [{'name': 'BestPairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False},
{'name': 'LinearizedPerturbations', 'twoTerm': False, 'boyd': False, 'simpleModel': False},
{'name': 'SimpleConservative', 'twoTerm': False, 'boyd': False, 'simpleModel': True},
{'name': 'TwoTerm', 'twoTerm': False, 'boyd': True, 'simpleModel': False}
]
uncertainty_sets = ['box', 'elliptical']
gamma = 0.5
for method in methods:
for uncertainty_set in uncertainty_sets:
rm = RobustModel(m, uncertainty_set, gamma=gamma, twoTerm=method['twoTerm'],
boyd=method['boyd'], simpleModel=method['simpleModel'],
nominalsolve=nominal_solution)
sol = rm.robustsolve(verbosity=0)
#sol.save(os.path.dirname(__file__) +
# 'diffs/test_methods_' +
# method['name'] + '_' + uncertainty_set)
self.assertTrue(sol.almost_equal(pickle.load(open(os.path.dirname(__file__) +
'diffs/test_methods/' +
method['name'] + '_' + uncertainty_set)), reltol=1e-5))
TESTS = [TestPrimitives]
def test():
run_tests(TESTS)
if __name__ == "__main__":
test()