Skip to content

Commit

Permalink
Comments, and testing/diffs.
Browse files Browse the repository at this point in the history
  • Loading branch information
1ozturkbe committed Sep 24, 2019
1 parent db6e500 commit 96312f6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
26 changes: 12 additions & 14 deletions robust/robust.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ def set(self, option_name, value):
class RobustModel(object):
"""
RobustModel extends gpkit.Model through the robust counterpart.
It uses the nominal solution of the GP or SP to
"""
def __init__(self, model, type_of_uncertainty_set, **options):
self.nominal_model = model
self.substitutions = model.substitutions
self.type_of_uncertainty_set = type_of_uncertainty_set

self.setting = RobustnessSetting(**options)
slopes_intercepts = LinearizeTwoTermPosynomials. \
slopes, intercepts, _, _, _ = LinearizeTwoTermPosynomials. \
linearization_coeff(self.setting.get('minNumOfLinearSections'))
self.robust_solve_properties = {'setuptime': 0,
'numoflinearsections': self.setting.get('minNumOfLinearSections'),
'slopes': slopes_intercepts[0],
'intercepts': slopes_intercepts[1]
'slopes': slopes,
'intercepts': intercepts
}

if 'nominalsolve' in options:
Expand All @@ -97,8 +96,8 @@ def __init__(self, model, type_of_uncertainty_set, **options):
self.setting.set('numberOfRegressionPoints', self.setting.get('numberOfRegressionPointsElliptical'))

self.ready_gp_constraints = []
self.to_linearize_gp_posynomials = []
self.large_gp_posynomials = []
self.to_linearize_gp_posynomials = [] # two-term posynomials
self.large_gp_posynomials = [] # posynomials that need to be broken up
self.sp_constraints = []
self.sp_equality_constraints = []

Expand Down Expand Up @@ -148,14 +147,13 @@ def __init__(self, model, type_of_uncertainty_set, **options):

self.number_of_gp_posynomials = len(gp_posynomials)

constraints_posynomials_tuple = self.classify_gp_constraints(gp_posynomials)

self.ready_gp_constraints += constraints_posynomials_tuple[0]
self.to_linearize_gp_posynomials = constraints_posynomials_tuple[1]
self.large_gp_posynomials = constraints_posynomials_tuple[2]
# Classifying GP-compatible constraints.
ready_gp_constraints, self.to_linearize_gp_posynomials, \
self.large_gp_posynomials = self.classify_gp_constraints(gp_posynomials)
self.ready_gp_constraints += ready_gp_constraints

if equality_constraints:
warnings.warn('equality constraints will not be robustified')
warnings.warn('Equality constraints will not be robustified.')

def setup(self, verbosity=0, **options):
for option, key in options.items():
Expand Down Expand Up @@ -410,11 +408,11 @@ def find_number_of_piece_wise_linearization(self, two_term_data_posynomials, rea
upper_model_infeasible = 0
try:
sol_upper = RobustModel.internalsolve(model_upper, verbosity=0)
except RuntimeWarning:
except (RuntimeWarning, ValueError):
upper_model_infeasible = 1
try:
sol_lower = RobustModel.internalsolve(model_lower, verbosity=0)
except RuntimeWarning:
except (RuntimeWarning, ValueError):
raise Exception("The model is infeasible")
if not two_term_data_posynomials:
self.robust_solve_properties['upperLowerRelError'] = 0
Expand Down
File renamed without changes.
16 changes: 11 additions & 5 deletions robust/testing/t_primitives.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import os

import unittest
from gpkit.tests.helpers import run_tests
Expand Down Expand Up @@ -40,11 +41,12 @@ def test_conservativeness(self):
# rm = RobustModel(m, 'elliptical')

def test_methods(self):
m = simple_wing()
m = gp_test_model()
nominal_solution = m.solve(verbosity=0)
methods = [{'name': 'Best Pairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False},
{'name': 'Linearized Perturbations', 'twoTerm': False, 'boyd': False, 'simpleModel': False},
{'name': 'Simple Conservative', 'twoTerm': False, 'boyd': False, 'simpleModel': True}
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']
for method in methods:
Expand All @@ -53,7 +55,11 @@ def test_methods(self):
rm = RobustModel(m, uncertainty_set, gamma=gamma, twoTerm=method['twoTerm'],
boyd=method['boyd'], simpleModel=method['simpleModel'],
nominalsolve=nominal_solution)
_ = rm.robustsolve(verbosity=0)
sol = rm.robustsolve(verbosity=0)
# print os.path.dirname(__file__)
# self.assertIsNone(sol.diff(os.path.dirname(__file__) +
# 'diffs/test_methods_' +
# method['name'] + '_' + uncertainty_set))

TESTS = [TestPrimitives]

Expand Down
4 changes: 2 additions & 2 deletions robust/testing/t_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def test_table_diff(self):

soltab = [sol, msol, bsol, esol]

origfilename = os.path.dirname(__file__) + '/' + 'test_table.txt'
filename = os.path.dirname(__file__) + '/' + 'test_table_diff.txt'
origfilename = os.path.dirname(__file__) + '/' + 'diffs/test_table.txt'
filename = os.path.dirname(__file__) + '/' + 'diffs/test_table_diff.txt'
f = open(filename, 'w+')

for i in ['L/D', 'A', 'Re', 'S', 'V', 't_s', 'W_w', 'W_{w_{strc}}', 'W_{w_{surf}}',
Expand Down

0 comments on commit 96312f6

Please sign in to comment.