def test_discrete_peel(self): x = pd.DataFrame(np.random.randint(0, 10, size=(100, ), dtype=np.int), columns=['a']) y = np.zeros(100, ) y[x.a > 5] = 1 primalg = prim.Prim(x, y, threshold=0.8) boxlims = primalg.box_init box = prim.PrimBox(primalg, boxlims, primalg.yi) peels = primalg._discrete_peel(box, 'a', 0, primalg.x_int) self.assertEqual(len(peels), 2) for peel in peels: self.assertEqual(len(peel), 2) indices, tempbox = peel self.assertTrue(isinstance(indices, np.ndarray)) self.assertTrue(isinstance(tempbox, pd.DataFrame)) # have modified boxlims as starting point primalg = prim.Prim(x, y, threshold=0.8) boxlims = primalg.box_init boxlims.a = [1, 8] box = prim.PrimBox(primalg, boxlims, primalg.yi) peels = primalg._discrete_peel(box, 'a', 0, primalg.x_int) self.assertEqual(len(peels), 2) for peel in peels: self.assertEqual(len(peel), 2) indices, tempbox = peel self.assertTrue(isinstance(indices, np.ndarray)) self.assertTrue(isinstance(tempbox, pd.DataFrame)) # have modified boxlims as starting point x.a[x.a > 5] = 5 primalg = prim.Prim(x, y, threshold=0.8) boxlims = primalg.box_init boxlims.a = [5, 8] box = prim.PrimBox(primalg, boxlims, primalg.yi) peels = primalg._discrete_peel(box, 'a', 0, primalg.x_int) self.assertEqual(len(peels), 2) x.a[x.a < 5] = 5 primalg = prim.Prim(x, y, threshold=0.8) boxlims = primalg.box_init boxlims.a = [5, 8] box = prim.PrimBox(primalg, boxlims, primalg.yi) peels = primalg._discrete_peel(box, 'a', 0, primalg.x_int) self.assertEqual(len(peels), 2)
def test_box_init(self): # test init box without NANS x = pd.DataFrame([(0,1,2), (2,5,6), (3,2,7)], columns=['a', 'b', 'c']) y = np.array([0,1,2]) prim_obj = prim.Prim(x,y, threshold=0.5, mode='regression') box_init = prim_obj.box_init # some test on the box self.assertTrue(box_init.loc[0, 'a']==0) self.assertTrue(box_init.loc[1, 'a']==3) self.assertTrue(box_init.loc[0, 'b']==1) self.assertTrue(box_init.loc[1, 'b']==5) self.assertTrue(box_init.loc[0, 'c']==2) self.assertTrue(box_init.loc[1, 'c']==7) # heterogenous without NAN x = pd.DataFrame([[0.1, 0, 'a'], [0.2, 1, 'b'], [0.3, 2, 'a'], [0.4, 3, 'b'], [0.5, 4, 'a'], [0.6, 5, 'a'], [0.7, 6, 'b'], [0.8, 7, 'a'], [0.9, 8, 'b'], [1.0, 9, 'a']], columns=['a', 'b', 'c']) y = np.arange(0, x.shape[0]) prim_obj = prim.Prim(x,y, threshold=0.5, mode='regression') box_init = prim_obj.box_init # some test on the box self.assertTrue(box_init['a'][0]==0.1) self.assertTrue(box_init['a'][1]==1.0) self.assertTrue(box_init['b'][0]==0) self.assertTrue(box_init['b'][1]==9) self.assertTrue(box_init['c'][0]==set(['a','b'])) self.assertTrue(box_init['c'][1]==set(['a','b']))
def test_show_ppt(self): x = pd.DataFrame([(0, 1, 2), (2, 5, 6), (3, 2, 1)], columns=['a', 'b', 'c']) y = np.array([1, 1, 0]) prim_obj = prim.Prim(x, y, threshold=0.8) box = PrimBox(prim_obj, prim_obj.box_init, prim_obj.yi) cols = ['mean', 'mass', 'coverage', 'density', 'res_dim'] data = np.zeros((100, 5)) data[:, 0:4] = np.random.rand(100, 4) data[:, 4] = np.random.randint(0, 5, size=(100, )) box.peeling_trajectory = pd.DataFrame(data, columns=cols) box.show_ppt()
def test_inspect(self): x = pd.DataFrame([(0,1,2), (2,5,6), (3,2,1)], columns=['a', 'b', 'c']) y = np.array([1,1,0]) prim_obj = prim.Prim(x, y, threshold=0.8) box = PrimBox(prim_obj, prim_obj.box_init, prim_obj.yi) new_box_lim = pd.DataFrame([(0,1,1), (2,5,6)], columns=['a', 'b', 'c']) indices = np.array([0,1], dtype=np.int) box.update(new_box_lim, indices) box.inspect(1)
def test_inspect(self): x = pd.DataFrame([(0, 1, 2), (2, 5, 6), (3, 2, 1)], columns=['a', 'b', 'c']) y = np.array([1, 1, 0]) prim_obj = prim.Prim(x, y, threshold=0.8) box = PrimBox(prim_obj, prim_obj.box_init, prim_obj.yi) new_box_lim = pd.DataFrame([(0, 1, 1), (2, 5, 6)], columns=['a', 'b', 'c']) indices = np.array([0, 1], dtype=np.int) box.update(new_box_lim, indices) box.inspect(1) box.inspect() box.inspect(style='graph') with self.assertRaises(ValueError): box.inspect(style='some unknown style')
#-------------------PRIM------------------------------------ transformedY = [] for i in range(x1.shape[0]): if x1[i] < 0.8: transformedY.append(0) else: transformedY.append(1) y1 = np.array(transformedY) print(data) print(x1) prim_alg = prim.Prim(data, x1, threshold=0.9, peel_alpha=0.1, mode=sdutil.RuleInductionType.REGRESSION) box1 = prim_alg.find_box() box1.show_tradeoff() box1.inspect(style='table') box1.inspect(7, style='table') plt.show() ''' # ------------------------------------------------------- airq = pd.read_csv("Airq.csv") x= pd.concat([airq['Ozone'],airq['Wind'],airq['Temp']],axis=1,keys=['Ozone','Wind','Temp']) ozon = x['Ozone'].to_list() wind = x['Wind'].to_list() temp = x['Temp'].to_list()
''' Created on 12 Nov 2018 @author: jhkwakkel ''' import pandas as pd import matplotlib.pyplot as plt from ema_workbench.analysis import prim from ema_workbench.util import ema_logging ema_logging.log_to_stderr(ema_logging.INFO); data = pd.read_csv('./data/bryant et al 2010 data.csv', index_col=False) x = data.iloc[:, 2:11] y = data.iloc[:, 15].values prim_alg = prim.Prim(x, y, threshold=0.8, peel_alpha=0.1) box1 = prim_alg.find_box() box1.show_tradeoff() print(box1.resample(21)) box1.inspect(21) box1.inspect(21, style='graph') plt.show()
def test_box_init(self): # test init box without NANS x = np.array([(0, 1, 2), (2, 5, 6), (3, 2, 7)], dtype=[('a', np.float), ('b', np.float), ('c', np.float)]) y = np.array([0, 1, 2]) prim_obj = prim.Prim(x, y, threshold=0.5) box_init = prim_obj.box_init # some test on the box self.assertTrue(box_init['a'][0] == 0) self.assertTrue(box_init['a'][1] == 3) self.assertTrue(box_init['b'][0] == 1) self.assertTrue(box_init['b'][1] == 5) self.assertTrue(box_init['c'][0] == 2) self.assertTrue(box_init['c'][1] == 7) # test init box with NANS x = np.array([(0, 1, 2), (2, 5, np.NAN), (3, 2, 7)], dtype=[('a', np.float), ('b', np.float), ('c', np.float)]) y = np.array([0, 1, 2]) x = np.ma.array(x) prim_obj = prim.Prim(x, y, threshold=0.5) box_init = prim_obj.box_init # some test on the box self.assertTrue(box_init['a'][0] == 0) self.assertTrue(box_init['a'][1] == 3) self.assertTrue(box_init['b'][0] == 1) self.assertTrue(box_init['b'][1] == 5) self.assertTrue(box_init['c'][0] == 2) self.assertTrue(box_init['c'][1] == 7) # heterogenous without NAN dtype = [('a', np.float), ('b', np.int), ('c', np.object)] x = np.empty((10, ), dtype=dtype) x['a'] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.7, 0.8, 0.9, 1.0] x['b'] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x['c'] = [ 'a', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'a', ] prim_obj = prim.Prim(x, y, threshold=0.5) box_init = prim_obj.box_init # some test on the box self.assertTrue(box_init['a'][0] == 0.1) self.assertTrue(box_init['a'][1] == 1.0) self.assertTrue(box_init['b'][0] == 0) self.assertTrue(box_init['b'][1] == 9) self.assertTrue(box_init['c'][0] == set(['a', 'b'])) self.assertTrue(box_init['c'][1] == set(['a', 'b'])) # heterogenous with NAN dtype = [('a', np.float), ('b', np.int), ('c', np.object)] x = np.empty((10, ), dtype=dtype) x[:] = np.NAN x['a'] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.7, 0.8, np.NAN, 1.0] x['b'] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x['c'] = [ 'a', 'b', 'a', 'b', np.NAN, 'a', 'b', 'a', 'b', 'a', ] # x = np.ma.array(x) # x['a'] = np.ma.masked_invalid(x['a']) # x['b'] = np.ma.masked_invalid(x['b']) # x['c'][4] = np.ma.masked prim_obj = prim.Prim(x, y, threshold=0.5) box_init = prim_obj.box_init # some test on the box self.assertTrue(box_init['a'][0] == 0.1) self.assertTrue(box_init['a'][1] == 1.0) self.assertTrue(box_init['b'][0] == 0) self.assertTrue(box_init['b'][1] == 9) self.assertTrue(box_init['c'][0] == set(['a', 'b'])) self.assertTrue(box_init['c'][1] == set(['a', 'b']))
#with MultiprocessingEvaluator(dike_model) as evaluator: # results = evaluator.perform_experiments(scenarios=100, policies=4) #%% experiments, outcomes = results experiments.to_csv('./results/exp_unc_10p_100s.csv') pd.DataFrame(outcomes).to_csv('./results/out_unc_10p_100s.csv') classification = outcomes['Expected Number of Deaths'] != 0 exp1 = experiments.loc[:, list(dike_model.uncertainties.keys())] #%% prim_alg = prim.Prim(exp1.astype(float), classification, threshold=0.7, peel_alpha=0.1) box1 = prim_alg.find_box() fig = box1.show_pairs_scatter() plt.title('Only uncertainties') plt.show() fig.savefig('./results/prim_unc_10p_100s.png', dpi=300) #%% With rotated rotated_experiments, rotation_matrix = prim.pca_preprocess( exp1.astype(float), classification) prim_alg = prim.Prim(rotated_experiments, classification,
dfresults.to_excel(r'.\results{}.xlsx'.format(timestamp), index=False) to_tar = True if to_tar == True: from ema_workbench import save_results save_results(results, 'HyperCube_open_exploration_iterations.tar.gz') #%% Prim # This section sets the conditions that are acceptable for our analysis cleaned_experiments = experiments.drop( labels=[l.name for l in dike_model.levers], axis=1) y = (dfoutcomes['Expected Number of Deaths'] < death_threshold) & (dfoutcomes['Total costs'] > 0) #y.value_counts() prim_alg = prim.Prim(cleaned_experiments, y, threshold=0.8) box1 = prim_alg.find_box() box1.show_tradeoff() plt.show() box1.inspect(2) box1.inspect(2, style='graph') plt.show() #%% Save results ''' # If scenarios is not a number specifying the number of scenarios, but a Policy object, assume ref scenario is run if isinstance(scenarios, int): n_scenarios = scenarios else: n_scenarios = 1
from Fontsize import change_fontsize import matplotlib.pyplot as plt import numpy as np # Load the uncertainty input for each experiment x = experiments # Classify the output space per experiment as True/False based on the ... #... specified conditions. y = ((outcomes['Total PV capacity (PJ)'] > \ np.quantile(np.array(solar_PJ.loc[2050]),0.75))&\ (outcomes['Total PV capacity (PJ)'] <= 12.5)) # Perform PRIM # Threshold should be 0.8 for more accurate analysis. # Mass_min can be reduced for OoIs with a low number of 'True' experiments. prim_alg = prim.Prim(x, y, threshold=0.8, peel_alpha=0.05, mass_min=0.05) box1 = prim_alg.find_box() # Coverage = fraction of cases of interest within the box # Density = fraction of cases in the box which are of interest # Quasi p-values (qp) = one sided binomial test, proxy for statistical ... # significance of each uncertainty in isolation (>=0.05). # The algorithm automatically selects the 'best' datapoint for analysis. box1.show_tradeoff() plt.show() box1.inspect() ax = box1.inspect(style='graph') change_fontsize(ax, fs=17) plt.show() box1.show_pairs_scatter() plt.show()
''' import matplotlib.pyplot as plt from ema_workbench import ema_logging, load_results import ema_workbench.analysis.prim as prim # # .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> ema_logging.log_to_stderr(level=ema_logging.INFO) # load data fn = r'./data/1000 flu cases no policy.tar.gz' x, outcomes = load_results(fn) # specify y y = outcomes['deceased population region 1'][:, -1] > 1000000 rotated_experiments, rotation_matrix = prim.pca_preprocess( x, y, exclude=['model', 'policy']) # perform prim on modified results tuple prim_obj = prim.Prim(rotated_experiments, y, threshold=0.8) box = prim_obj.find_box() box.show_tradeoff() box.inspect(22) plt.show()
def test_road_test(self): road_test_scope_file = emat.package_file('model', 'tests', 'road_test.yaml') road_scope = emat.Scope(road_test_scope_file) # <emat.Scope with 2 constants, 7 uncertainties, 4 levers, 7 measures> assert len(road_scope.get_measures()) == 7 assert len(road_scope.get_levers()) == 4 assert len(road_scope.get_uncertainties()) == 7 assert len(road_scope.get_constants()) == 2 emat_db = emat.SQLiteDB() road_scope.store_scope(emat_db) with pytest.raises(KeyError): road_scope.store_scope(emat_db) assert emat_db.read_scope_names() == ['EMAT Road Test'] design = design_experiments(road_scope, db=emat_db, n_samples_per_factor=10, sampler='lhs') design.head() large_design = design_experiments(road_scope, db=emat_db, n_samples=5000, sampler='lhs', design_name='lhs_large') large_design.head() assert list(large_design.columns) == [ 'alpha', 'amortization_period', 'beta', 'debt_type', 'expand_capacity', 'input_flow', 'interest_rate', 'interest_rate_lock', 'unit_cost_expansion', 'value_of_time', 'yield_curve', 'free_flow_time', 'initial_capacity', ] assert list(large_design.head().index) == [111, 112, 113, 114, 115] assert emat_db.read_design_names('EMAT Road Test') == ['lhs', 'lhs_large'] m = PythonCoreModel(Road_Capacity_Investment, scope=road_scope, db=emat_db) with SequentialEvaluator(m) as eval_seq: lhs_results = m.run_experiments(design_name='lhs', evaluator=eval_seq) lhs_results.head() assert lhs_results.head()['present_cost_expansion'].values == approx( [2154.41598475, 12369.38053473, 4468.50683924, 6526.32517089, 2460.91070514]) assert lhs_results.head()['net_benefits'].values == approx( [-79.51551505, -205.32148044, -151.94431822, -167.62487134, -3.97293985]) with SequentialEvaluator(m) as eval_seq: lhs_large_results = m.run_experiments(design_name='lhs_large', evaluator=eval_seq) lhs_large_results.head() assert lhs_large_results.head()['net_benefits'].values == approx( [-584.36098322, -541.5458395, -185.16661464, -135.85689709, -357.36106457]) lhs_outcomes = m.read_experiment_measures(design_name='lhs') assert lhs_outcomes.head()['time_savings'].values == approx( [13.4519273, 26.34172999, 12.48385198, 15.10165981, 15.48056139]) correct_scores = numpy.array( [[0.06603461, 0.04858595, 0.06458574, 0.03298163, 0.05018515, 0., 0., 0.53156587, 0.05060416, 0.02558088, 0.04676956, 0.04131266, 0.04179378], [0.06003223, 0.04836434, 0.06059554, 0.03593644, 0.27734396, 0., 0., 0.28235419, 0.05303979, 0.03985181, 0.04303371, 0.05004349, 0.04940448], [0.08760605, 0.04630414, 0.0795043, 0.03892201, 0.10182534, 0., 0., 0.42508457, 0.04634321, 0.03216387, 0.0497183, 0.04953772, 0.0429905], [0.08365598, 0.04118732, 0.06716887, 0.03789444, 0.06509519, 0., 0., 0.31494171, 0.06517462, 0.02895742, 0.04731707, 0.17515158, 0.07345581], [0.06789382, 0.07852257, 0.05066944, 0.04807088, 0.32054735, 0., 0., 0.15953055, 0.05320201, 0.02890069, 0.07033928, 0.06372418, 0.05859923], [0.05105435, 0.09460353, 0.04614178, 0.04296901, 0.45179611, 0., 0., 0.04909801, 0.05478798, 0.023099, 0.08160785, 0.05642169, 0.04842069], [0.04685703, 0.03490931, 0.03214081, 0.03191602, 0.56130318, 0., 0., 0.04011044, 0.04812986, 0.02228924, 0.09753361, 0.04273004, 0.04208045], ]) scores = m.get_feature_scores('lhs', random_state=123) for _i in range(scores.metadata.values.shape[0]): for _j in range(scores.metadata.values.shape[1]): assert scores.metadata.values[_i,_j] == approx(correct_scores[_i,_j], rel=.1) from ema_workbench.analysis import prim x = m.read_experiment_parameters(design_name='lhs_large') prim_alg = prim.Prim( m.read_experiment_parameters(design_name='lhs_large'), m.read_experiment_measures(design_name='lhs_large')['net_benefits'] > 0, threshold=0.4, ) box1 = prim_alg.find_box() assert dict(box1.peeling_trajectory.iloc[45]) == approx({ 'coverage': 0.8014705882352942, 'density': 0.582109479305741, 'id': 45, 'mass': 0.1498, 'mean': 0.582109479305741, 'res_dim': 4, }) from emat.util.xmle import Show from emat.util.xmle.elem import Elem assert isinstance(Show(box1.show_tradeoff()), Elem) from ema_workbench.analysis import cart cart_alg = cart.CART( m.read_experiment_parameters(design_name='lhs_large'), m.read_experiment_measures(design_name='lhs_large')['net_benefits'] > 0, ) cart_alg.build_tree() cart_dict = dict(cart_alg.boxes[0].iloc[0]) assert cart_dict['debt_type'] == {'GO Bond', 'Paygo', 'Rev Bond'} assert cart_dict['interest_rate_lock'] == {False, True} del cart_dict['debt_type'] del cart_dict['interest_rate_lock'] assert cart_dict == approx({ 'free_flow_time': 60, 'initial_capacity': 100, 'alpha': 0.10001988547129116, 'beta': 3.500215589924521, 'input_flow': 80.0, 'value_of_time': 0.00100690634109406, 'unit_cost_expansion': 95.00570832093116, 'interest_rate': 0.0250022738169142, 'yield_curve': -0.0024960505548531774, 'expand_capacity': 0.0006718732232418368, 'amortization_period': 15, }) assert isinstance(Show(cart_alg.show_tree(format='svg')), Elem) from emat import Measure MAXIMIZE = Measure.MAXIMIZE MINIMIZE = Measure.MINIMIZE robustness_functions = [ Measure( 'Expected Net Benefit', kind=Measure.INFO, variable_name='net_benefits', function=numpy.mean, # min=-150, # max=50, ), Measure( 'Probability of Net Loss', kind=MINIMIZE, variable_name='net_benefits', function=lambda x: numpy.mean(x < 0), min=0, max=1, ), Measure( '95%ile Travel Time', kind=MINIMIZE, variable_name='build_travel_time', function=functools.partial(numpy.percentile, q=95), min=60, max=150, ), Measure( '99%ile Present Cost', kind=Measure.INFO, variable_name='present_cost_expansion', function=functools.partial(numpy.percentile, q=99), # min=0, # max=10, ), Measure( 'Expected Present Cost', kind=Measure.INFO, variable_name='present_cost_expansion', function=numpy.mean, # min=0, # max=10, ), ] from emat import Constraint constraint_1 = Constraint( "Maximum Log Expected Present Cost", outcome_names="Expected Present Cost", function=Constraint.must_be_less_than(4000), ) constraint_2 = Constraint( "Minimum Capacity Expansion", parameter_names="expand_capacity", function=Constraint.must_be_greater_than(10), ) constraint_3 = Constraint( "Maximum Paygo", parameter_names='debt_type', outcome_names='99%ile Present Cost', function=lambda i, j: max(0, j - 1500) if i == 'Paygo' else 0, ) from emat.optimization import HyperVolume, EpsilonProgress, SolutionViewer, ConvergenceMetrics convergence_metrics = ConvergenceMetrics( HyperVolume.from_outcomes(robustness_functions), EpsilonProgress(), SolutionViewer.from_model_and_outcomes(m, robustness_functions), ) with SequentialEvaluator(m) as eval_seq: robust_results, convergence = m.robust_optimize( robustness_functions, scenarios=20, nfe=5, constraints=[ constraint_1, constraint_2, constraint_3, ], epsilons=[0.05, ] * len(robustness_functions), convergence=convergence_metrics, evaluator=eval_seq, ) assert isinstance(robust_results, pandas.DataFrame) mm = m.create_metamodel_from_design('lhs') design2 = design_experiments(road_scope, db=emat_db, n_samples_per_factor=10, sampler='lhs', random_seed=2) design2_results = mm.run_experiments(design2)
def test_road_test(self): road_test_scope_file = emat.package_file('model', 'tests', 'road_test.yaml') road_scope = emat.Scope(road_test_scope_file) # <emat.Scope with 2 constants, 7 uncertainties, 4 levers, 7 measures> assert len(road_scope.get_measures()) == 7 assert len(road_scope.get_levers()) == 4 assert len(road_scope.get_uncertainties()) == 7 assert len(road_scope.get_constants()) == 2 emat_db = emat.SQLiteDB() road_scope.store_scope(emat_db) with pytest.raises(KeyError): road_scope.store_scope(emat_db) assert emat_db.read_scope_names() == ['EMAT Road Test'] design = design_experiments(road_scope, db=emat_db, n_samples_per_factor=10, sampler='lhs') design.head() large_design = design_experiments(road_scope, db=emat_db, n_samples=5000, sampler='lhs', design_name='lhs_large') large_design.head() assert list(large_design.columns) == [ 'alpha', 'amortization_period', 'beta', 'debt_type', 'expand_capacity', 'input_flow', 'interest_rate', 'interest_rate_lock', 'unit_cost_expansion', 'value_of_time', 'yield_curve', 'free_flow_time', 'initial_capacity', ] assert list(large_design.head().index) == [111, 112, 113, 114, 115] assert emat_db.read_design_names('EMAT Road Test') == [ 'lhs', 'lhs_large' ] m = PythonCoreModel(Road_Capacity_Investment, scope=road_scope, db=emat_db) with SequentialEvaluator(m) as eval_seq: lhs_results = m.run_experiments(design_name='lhs', evaluator=eval_seq) lhs_results.head() assert lhs_results.head()['present_cost_expansion'].values == approx([ 2154.41598475, 12369.38053473, 4468.50683924, 6526.32517089, 2460.91070514 ]) assert lhs_results.head()['net_benefits'].values == approx([ -22.29090499, -16.84301382, -113.98841188, 11.53956058, 78.03661612 ]) with SequentialEvaluator(m) as eval_seq: lhs_large_results = m.run_experiments(design_name='lhs_large', evaluator=eval_seq) lhs_large_results.head() assert lhs_large_results.head()['net_benefits'].values == approx([ -522.45283083, -355.1599307, -178.6623215, 23.46263498, -301.17700968 ]) lhs_outcomes = m.read_experiment_measures(design_name='lhs') assert lhs_outcomes.head()['time_savings'].values == approx( [13.4519273, 26.34172999, 12.48385198, 15.10165981, 15.48056139]) scores = m.get_feature_scores('lhs', random_state=123) stable_df("./road_test_feature_scores.pkl.gz", scores.data) from ema_workbench.analysis import prim x = m.read_experiment_parameters(design_name='lhs_large') prim_alg = prim.Prim( m.read_experiment_parameters(design_name='lhs_large'), m.read_experiment_measures(design_name='lhs_large')['net_benefits'] > 0, threshold=0.4, ) box1 = prim_alg.find_box() stable_df("./road_test_box1_peeling_trajectory.pkl.gz", box1.peeling_trajectory) from emat.util.xmle import Show from emat.util.xmle.elem import Elem assert isinstance(Show(box1.show_tradeoff()), Elem) from ema_workbench.analysis import cart cart_alg = cart.CART( m.read_experiment_parameters(design_name='lhs_large'), m.read_experiment_measures(design_name='lhs_large')['net_benefits'] > 0, ) cart_alg.build_tree() stable_df("./road_test_cart_box0.pkl.gz", cart_alg.boxes[0]) cart_dict = dict(cart_alg.boxes[0].iloc[0]) assert cart_dict['debt_type'] == {'GO Bond', 'Paygo', 'Rev Bond'} assert cart_dict['interest_rate_lock'] == {False, True} assert isinstance(Show(cart_alg.show_tree(format='svg')), Elem) from emat import Measure MAXIMIZE = Measure.MAXIMIZE MINIMIZE = Measure.MINIMIZE robustness_functions = [ Measure( 'Expected Net Benefit', kind=Measure.INFO, variable_name='net_benefits', function=numpy.mean, # min=-150, # max=50, ), Measure( 'Probability of Net Loss', kind=MINIMIZE, variable_name='net_benefits', function=lambda x: numpy.mean(x < 0), min=0, max=1, ), Measure( '95%ile Travel Time', kind=MINIMIZE, variable_name='build_travel_time', function=functools.partial(numpy.percentile, q=95), min=60, max=150, ), Measure( '99%ile Present Cost', kind=Measure.INFO, variable_name='present_cost_expansion', function=functools.partial(numpy.percentile, q=99), # min=0, # max=10, ), Measure( 'Expected Present Cost', kind=Measure.INFO, variable_name='present_cost_expansion', function=numpy.mean, # min=0, # max=10, ), ] from emat import Constraint constraint_1 = Constraint( "Maximum Log Expected Present Cost", outcome_names="Expected Present Cost", function=Constraint.must_be_less_than(4000), ) constraint_2 = Constraint( "Minimum Capacity Expansion", parameter_names="expand_capacity", function=Constraint.must_be_greater_than(10), ) constraint_3 = Constraint( "Maximum Paygo", parameter_names='debt_type', outcome_names='99%ile Present Cost', function=lambda i, j: max(0, j - 1500) if i == 'Paygo' else 0, ) from emat.optimization import HyperVolume, EpsilonProgress, SolutionViewer, ConvergenceMetrics convergence_metrics = ConvergenceMetrics( HyperVolume.from_outcomes(robustness_functions), EpsilonProgress(), SolutionViewer.from_model_and_outcomes(m, robustness_functions), ) with SequentialEvaluator(m) as eval_seq: robust = m.robust_optimize( robustness_functions, scenarios=20, nfe=5, constraints=[ constraint_1, constraint_2, constraint_3, ], epsilons=[ 0.05, ] * len(robustness_functions), convergence=convergence_metrics, evaluator=eval_seq, ) robust_results, convergence = robust.result, robust.convergence assert isinstance(robust_results, pandas.DataFrame) mm = m.create_metamodel_from_design('lhs') design2 = design_experiments(road_scope, db=emat_db, n_samples_per_factor=10, sampler='lhs', random_seed=2) design2_results = mm.run_experiments(design2)