Skip to content

Commit

Permalink
Test fixes (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ozturkbe committed Jul 15, 2020
1 parent 34143d2 commit 08d4673
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
14 changes: 6 additions & 8 deletions robust/robust_gp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def check_if_no_data(p_uncertain_vars, monomial):

@staticmethod
def probability_of_failure(model, solution, directly_uncertain_vars_subs, number_of_iterations, verbosity=0, parallel=False):
def confirmSuccess(model, solution, uncertainsub):
new_model = RobustGPTools.DesignedModel(model, solution, uncertainsub)
fail_success, cost = RobustGPTools.fail_or_success(new_model)
return cost
if parallel:
pool = mp.Pool(mp.cpu_count()-1)
processes = []
Expand All @@ -132,7 +136,6 @@ def probability_of_failure(model, solution, directly_uncertain_vars_subs, number
pool.join()
else:
results = [confirmSuccess(model, solution, directly_uncertain_vars_subs[i]) for i in range(number_of_iterations)]

costs = [0 if i is None else mag(i) for i in results]
if verbosity > 0:
print(costs)
Expand Down Expand Up @@ -160,9 +163,9 @@ def setup(self, model, solution, directly_uncertain_vars_subs):
def fail_or_success(model):
try:
try:
sol = model.solve(verbosity=0)
sol = model.solve(verbosity=0, reltol=1e-3)
except InvalidGPConstraint:
sol = model.localsolve(verbosity=0)
sol = model.localsolve(verbosity=0, reltol=1e-3, use_pccp=True)
return True, sol['cost']
except: # ValueError:
return False, 0
Expand All @@ -188,10 +191,5 @@ def setup(self, model):
self.cost = model.cost
return constraints

def confirmSuccess(model, solution, uncertainsub):
new_model = RobustGPTools.DesignedModel(model, solution, uncertainsub)
fail_success, cost = RobustGPTools.fail_or_success(new_model)
return cost

if __name__ == '__main__':
pass
26 changes: 13 additions & 13 deletions robust/testing/diffs/test_table.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
L/D & 1.7e+01 & 1.63e+01 & 1.71e+01 & 1.74e+01
A & 8.91e+00 & 7.54e+00 & 7.67e+00 & 8.27e+00
Re & 5.01e+06 & 5.78e+06 & 5.92e+06 & 5.60e+06
S & 2.82e+01 & 4.56e+01 & 4.58e+01 & 4.24e+01
V & 5.77e+01 & 4.91e+01 & 4.96e+01 & 5.07e+01
t_s & 1.44e+01 & 1.7e+01 & 1.68e+01 & 1.65e+01
W_w & 2.36e+03 & 3.89e+03 & 3.95e+03 & 3.67e+03
W_{w_{strc}} & 6.7e+02 & 1.01e+03 & 1.07e+03 & 9.88e+02
W_{w_{surf}} & 1.69e+03 & 2.87e+03 & 2.89e+03 & 2.68e+03
W_{fuse} & 3.5e+02 & 3.83e+02 & 3.93e+02 & 3.99e+02
V_{f_{avail}} & 1.5e-01 & 1.95e-01 & 1.96e-01 & 1.88e-01
L/D & 1.98e+01 & 1.94e+01 & 2.02e+01 & 2.04e+01
A & 1.18e+01 & 1.05e+01 & 1.07e+01 & 1.13e+01
Re & 4.25e+06 & 4.84e+06 & 4.88e+06 & 4.69e+06
S & 2.69e+01 & 4.26e+01 & 4.27e+01 & 3.99e+01
V & 5.79e+01 & 5.02e+01 & 5.02e+01 & 5.12e+01
t_s & 1.44e+01 & 1.66e+01 & 1.66e+01 & 1.63e+01
W_w & 2.09e+03 & 3.38e+03 & 3.44e+03 & 3.22e+03
W_{w_{strc}} & 4.82e+02 & 6.99e+02 & 7.43e+02 & 7.05e+02
W_{w_{surf}} & 1.61e+03 & 2.69e+03 & 2.69e+03 & 2.52e+03
W_{fuse} & 4.e+02 & 4.59e+02 & 4.58e+02 & 4.61e+02
V_{f_{avail}} & 1.24e-01 & 1.55e-01 & 1.58e-01 & 1.53e-01
V_{f_{fuse}} & 0.e+00 & 0.e+00 & 0.e+00 & 0.e+00
V_{f_{wing}} & 1.80e-01 & 3.84e-01 & 3.83e-01 & 3.29e-01
cost & 2.93e+03 & 3.6e+03 & 3.59e+03 & 3.48e+03
V_{f_{wing}} & 2.67e-01 & 5.93e-01 & 5.87e-01 & 5.11e-01
cost & 2.73e+03 & 3.23e+03 & 3.26e+03 & 3.18e+03
5 changes: 2 additions & 3 deletions robust/testing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,12 @@ def simple_ac():
'h_{cruise_m}': 5000 * units('m'),
'Range_m': 3000 * units('km'),
'W_{p_m}': 6250 * units('N'),
'\\rho_{p_m}' : 1500*units('kg/m^3'),
'\\rho_{p_m}': 1500*units('kg/m^3'),
'C_m': 120 * units('1/hr'),
'V_{min_m}': 25 * units('m/s'),
'T/O factor_m': 2,
})
c = Variable('c', '-', 'model cost')
m = Model(c, [m, c >= m['W_{f_m}'] * units('1/N') + m['C_m'] * m['t_m']])
m = Model(m['W_{f_m}'] * units('1/N') + m['C_m'] * m['t_m'], m, m.substitutions)
return m


12 changes: 5 additions & 7 deletions robust/testing/t_simulation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import numpy as np
import os
import unittest
from gpkit import units
from gpkit.tests.helpers import run_tests
from gpkit.small_scripts import mag

from robust.margin import MarginModel
from robust.robust import RobustModel
from robust.simulations import simulate
from robust.testing.models import simple_ac
import pickle

class TestSimulation(unittest.TestCase):
solver = None
Expand All @@ -18,13 +18,12 @@ def test_simulate(self):
return # cvxopt does not converge on these tests.

model = simple_ac()
model.cost = model['c']
number_of_time_average_solves = 3
number_of_iterations = 10
uncertainty_sets = ['box']
methods = [{'name': 'Best Pairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False}]
nGammas = 3
gammas = np.linspace(0, 1.0, nGammas)
gammas = np.linspace(0., 1., nGammas)
min_num_of_linear_sections = 3
max_num_of_linear_sections = 99
linearization_tolerance = 1e-4
Expand Down Expand Up @@ -60,7 +59,6 @@ def test_table_diff(self):
return # cvxopt does not converge on these tests.

m = simple_ac()
m.cost = m['c']
sol = m.localsolve(verbosity=0)
gamma = 0.5

Expand All @@ -79,7 +77,6 @@ def test_table_diff(self):

for i in range(len(soltab)):
soltab[i].save(filename=os.path.dirname(__file__) + '/diffs/' + diff_idx[i] + '.pkl')
# soltab[i].save('diffs/' + diff_idx[i] + '.pkl')

filename = os.path.dirname(__file__) + '/diffs/test_table_diff.txt'
origfilename = os.path.dirname(__file__) + '/diffs/test_table.txt'
Expand All @@ -103,8 +100,9 @@ def test_table_diff(self):
f.write(' '.join(["& " + str(np.format_float_scientific(i['cost'], precision=2)) for i in soltab]))
f.write('\n')
f.close()

self.assertAlmostEqual(open(origfilename, 'r').readlines(), open(filename, 'r').readlines())
a = open(origfilename, 'r').readlines()
b = open(filename, 'r').readlines()
self.assertAlmostEqual(a, b)

TESTS = [TestSimulation]

Expand Down

0 comments on commit 08d4673

Please sign in to comment.