# specify uncertainties lake_model.uncertainties = [ RealParameter("b", 0.1, 0.45), RealParameter("q", 2.0, 4.5), RealParameter("mean", 0.01, 0.05), RealParameter("stdev", 0.001, 0.005), RealParameter("delta", 0.93, 0.99), ] # set levers, one for each time step lake_model.levers = [RealParameter(str(i), 0, 0.1) for i in range(lake_model.time_horizon)] # specify outcomes lake_model.outcomes = [ ScalarOutcome("max_P"), ScalarOutcome("utility"), ScalarOutcome("inertia"), ScalarOutcome("reliability"), ] # override some of the defaults of the model lake_model.constants = [Constant("alpha", 0.41), Constant("nsamples", 150)] # generate some random policies by sampling over levers policies = samplers.sample_levers(lake_model, 4, sampler=samplers.MonteCarloSampler(), name=util.counter) # perform experiments nr_experiments = 100 results = perform_experiments(lake_model, nr_experiments, policies, parallel=True)
self.uncertainties = [LookupUncertainty('hearne2', [(-1, 3), (-2, 1), (0, 0.9), (0.1, 1), (0.99, 1.01), (0.99, 1.01)], "accomplishments per hour lookup", self, 0, 1), LookupUncertainty('hearne2', [(-0.75, 0.75), (-0.75, 0.75), (0, 1.5), (0.1, 1.6), (-0.3, 1.5), (0.25, 2.5)], "fractional change in expectations from perceived adequacy lookup", self, -1, 1), LookupUncertainty('hearne2', [(-2, 2), (-1, 2), (0, 1.5), (0.1, 1.6), (0.5, 2), (0.5, 2)], "effect of perceived adequacy on energy drain lookup", self, 0, 10), LookupUncertainty('hearne2', [(-2, 2), (-1, 2), (0, 1.5), (0.1, 1.6), (0.5, 1.5), (0.1, 2)], "effect of perceived adequacy of hours worked lookup", self, 0, 2.5), LookupUncertainty('hearne2', [(-1, 1), (-1, 1), (0, 0.9), (0.1, 1), (0.5, 1.5), (1, 1.5)], "effect of energy levels on hours worked lookup", self, 0, 1.5), LookupUncertainty('hearne2', [(-1, 1), (-1, 1), (0, 0.9), (0.1, 1), (0.5, 1.5), (1, 1.5)], "effect of high energy on further recovery lookup", self, 0, 1.25), LookupUncertainty('hearne2', [(-2, 2), (-1, 1), (0, 100), (20, 120), (0.5, 1.5), (0.5, 2)], "effect of hours worked on energy recovery lookup", self, 0, 1.5), LookupUncertainty('approximation', [(-0.5, 0.35), (3, 5), (1, 10), (0.2, 0.4), (0, 120)], "effect of hours worked on energy drain lookup", self, 0, 3), LookupUncertainty('hearne1', [(0, 1), (0, 0.15), (1, 1.5), (0.75, 1.25)], "effect of low energy on further depletion lookup", self, 0, 1)] self._delete_lookup_uncertainties() if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = Burnout(r'./models/burnout', "burnout") # run policy with old cases results = perform_experiments(model, 100) lines(results, 'Energy Level', density=BOXPLOT) plt.show()
This file illustrated the use the EMA classes for a contrived vensim example .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> chamarat <c.hamarat (at) tudelft (dot) nl> ''' from __future__ import (division, unicode_literals, absolute_import, print_function) from ema_workbench import (TimeSeriesOutcome, perform_experiments, RealParameter, ema_logging) from ema_workbench.connectors.vensim import VensimModel if __name__ == "__main__": # turn on logging ema_logging.log_to_stderr(ema_logging.INFO) # instantiate a model wd = './models/vensim example' vensimModel = VensimModel("simpleModel", wd=wd, model_file='model.vpm') vensimModel.uncertainties = [ RealParameter("x11", 0, 2.5), RealParameter("x12", -2.5, 2.5) ] vensimModel.outcomes = [TimeSeriesOutcome('a')] results = perform_experiments(vensimModel, 1000)
wd=r'./models/flu', model_file=r'/FLUvensimV1basecase.vpm') #outcomes model.outcomes = [ TimeSeriesOutcome('deceased population region 1'), TimeSeriesOutcome('infected fraction R1'), ScalarOutcome('max infection fraction', variable_name='infected fraction R1', function=np.max), ScalarOutcome('time of max', variable_name=['infected fraction R1', 'TIME'], function=time_of_max) ] #create uncertainties based on csv model.uncertainties = create_parameters( './models/flu/flu_uncertainties.csv') #add policies policies = [ Policy('no policy', model_file=r'/FLUvensimV1basecase.vpm'), Policy('static policy', model_file=r'/FLUvensimV1static.vpm'), Policy('adaptive policy', model_file=r'/FLUvensimV1dynamic.vpm') ] results = perform_experiments(model, 1000, policies=policies, parallel=True)
RealParameter('additional seasonal immune population fraction R2', 0, 0.5), RealParameter('fatality ratio region 1', 0.0001, 0.1), RealParameter('fatality rate region 2', 0.0001, 0.1), RealParameter('initial immune fraction of the population of region 1', 0, 0.5), RealParameter('initial immune fraction of the population of region 2', 0, 0.5), RealParameter('normal interregional contact rate', 0, 0.9), RealParameter('permanent immune population fraction R1', 0, 0.5), RealParameter('permanent immune population fraction R2', 0, 0.5), RealParameter('recovery time region 1', 0.1, 0.75), RealParameter('recovery time region 2', 0.1, 0.75), RealParameter('susceptible to immune population delay time region 1', 0.5, 2), RealParameter('susceptible to immune population delay time region 2', 0.5, 2), RealParameter('root contact rate region 1', 0.01, 5), RealParameter('root contact ratio region 2', 0.01, 5), RealParameter('infection ratio region 1', 0, 0.15), RealParameter('infection rate region 2', 0, 0.15), RealParameter('normal contact rate region 1', 10, 100), RealParameter('normal contact rate region 2', 10, 200)] #add policies policies = [Policy('no policy', model_file=r'FLUvensimV1basecase.vpm'), Policy('static policy', model_file=r'FLUvensimV1static.vpm'), Policy('adaptive policy', model_file=r'FLUvensimV1dynamic.vpm') ] results = perform_experiments(model, 1000, policies=policies)
def run_experiments( self, design: pd.DataFrame = None, evaluator=None, *, design_name=None, db=None, ): """ Runs a design of combined experiments using this model. A combined experiment includes a complete set of input values for all exogenous uncertainties (a Scenario) and all policy levers (a Policy). Unlike the perform_experiments function in the EMA Workbench, this method pairs each Scenario and Policy in sequence, instead of running all possible combinations of Scenario and Policy. This change ensures compatibility with the EMAT database modules, which preserve the complete set of input information (both uncertainties and levers) for each experiment. To conduct a full cross-factorial set of experiments similar to the default settings for EMA Workbench, use a factorial design, by setting the `jointly` argument for the `design_experiments` to False, or by designing experiments outside of EMAT with your own approach. Args: design (pandas.DataFrame, optional): experiment definitions given as a DataFrame, where each exogenous uncertainties and policy levers is given as a column, and each row is an experiment. evaluator (ema_workbench.Evaluator, optional): Optionally give an evaluator instance. If not given, a default SequentialEvaluator will be instantiated. design_name (str, optional): The name of a design of experiments to load from the database. This design is only used if `design` is None. db (Database, optional): The database to use for loading and saving experiments. If none is given, the default database for this model is used. If there is no default db, and none is given here, the results are not stored in a database. Set to False to explicitly not use the default database, even if it exists. Returns: pandas.DataFrame: A DataFrame that contains all uncertainties, levers, and measures for the experiments. Raises: ValueError: If there are no experiments defined. This includes the situation where `design` is given but no database is available. """ from ema_workbench import Scenario, Policy, perform_experiments # catch user gives only a design, not experiment_parameters if isinstance(design, str) and design_name is None: design_name, design = design, None if design_name is None and design is None: raise ValueError(f"must give design_name or design") if db is None: db = self.db if design_name is not None and design is None: if not db: raise ValueError( f'cannot load design "{design_name}", there is no db') design = db.read_experiment_parameters(self.scope.name, design_name) if design.empty: raise ValueError(f"no experiments available") scenarios = [ Scenario(**dict(zip(self.scope.get_uncertainty_names(), i))) for i in design[self.scope.get_uncertainty_names()].itertuples( index=False, name='ExperimentX') ] policies = [ Policy(f"Incognito{n}", **dict(zip(self.scope.get_lever_names(), i))) for n, i in enumerate(design[self.scope.get_lever_names()]. itertuples(index=False, name='ExperimentL')) ] if not evaluator: from ema_workbench import SequentialEvaluator evaluator = SequentialEvaluator(self) experiments, outcomes = perform_experiments( self, scenarios=scenarios, policies=policies, zip_over={'scenarios', 'policies'}, evaluator=evaluator) experiments.index = design.index outcomes = pd.DataFrame.from_dict(outcomes) outcomes.index = design.index if db: db.write_experiment_measures(self.scope.name, self.metamodel_id, outcomes) return self.ensure_dtypes( pd.concat([ experiments.drop(columns=['scenario', 'policy', 'model']), outcomes ], axis=1, sort=False))
model_file=r'/FLUvensimV1basecase.vpm') # outcomes model.outcomes = [ TimeSeriesOutcome('deceased population region 1'), TimeSeriesOutcome('infected fraction R1'), ScalarOutcome('max infection fraction', variable_name='infected fraction R1', function=np.max), ScalarOutcome('time of max', variable_name=['infected fraction R1', 'TIME'], function=time_of_max) ] # create uncertainties based on csv model.uncertainties = create_parameters( './models/flu/flu_uncertainties.csv') # add policies policies = [ Policy('no policy', model_file=r'/FLUvensimV1basecase.vpm'), Policy('static policy', model_file=r'/FLUvensimV1static.vpm'), Policy('adaptive policy', model_file=r'/FLUvensimV1dynamic.vpm') ] with MultiprocessingEvaluator(model, 2) as evaluator: results = perform_experiments(model, 1000, policies=policies, evaluator == evaluator)
ArrayOutcome('y3'), ScalarOutcome('y4'), ScalarOutcome('y5'), ScalarOutcome('y6'), ScalarOutcome('y7') ] # override some of the defaults of the model model.constants = [ Constant("X4rSnow", 0.7), Constant("xCostDay", 6), Constant("xRevenueDay", 10) ] #results = perform_experiments(model, 1500, 5) results = perform_experiments(model, 200, 20) #with MultiprocessingEvaluator(model, n_processes=4) as evaluator: # results = evaluator.perform_experiments(scenarios=4, policies=5) print('end!') training_time = time.time() - start_time print("--- %s seconds ---" % (training_time)) print('training time : {} mins and {} seconds'.format( (training_time // 60), round((training_time % 60), 1))) print('training time : {} hours {} mins and {} seconds '.format( training_time // 3600, round((training_time % 3600 // 60), 1), round((training_time % 3600) % 60, 1))) # Save the outputs from ema_workbench import save_results
model = VensimModel("fluCase", wd=r'./models/flu', model_file=r'/FLUvensimV1basecase.vpm') # outcomes model.outcomes = [TimeSeriesOutcome('deceased population region 1'), TimeSeriesOutcome('infected fraction R1'), ScalarOutcome('max infection fraction', variable_name='infected fraction R1', function=np.max), ScalarOutcome('time of max', variable_name=[ 'infected fraction R1', 'TIME'], function=time_of_max)] # create uncertainties based on csv model.uncertainties = create_parameters( './models/flu/flu_uncertainties.csv') # add policies policies = [Policy('no policy', model_file=r'/FLUvensimV1basecase.vpm'), Policy('static policy', model_file=r'/FLUvensimV1static.vpm'), Policy('adaptive policy', model_file=r'/FLUvensimV1dynamic.vpm') ] with MultiprocessingEvaluator(model, 2) as evaluator: results = perform_experiments(model, 1000, policies=policies, evaluator == evaluator)
model = VensimModel("fluCase", wd=r'./models/flu', model_file = r'/FLUvensimV1basecase.vpm') #outcomes model.outcomes = [TimeSeriesOutcome('deceased population region 1'), TimeSeriesOutcome('infected fraction R1'), ScalarOutcome('max infection fraction', variable_name='infected fraction R1', function=np.max), ScalarOutcome('time of max', variable_name=['infected fraction R1', 'TIME'], function=time_of_max)] #create uncertainties based on csv model.uncertainties = create_parameters('./models/flu/flu_uncertainties.csv') #add policies policies = [Policy('no policy', model_file=r'/FLUvensimV1basecase.vpm'), Policy('static policy', model_file=r'/FLUvensimV1static.vpm'), Policy('adaptive policy', model_file=r'/FLUvensimV1dynamic.vpm') ] results = perform_experiments(model, 1000, policies=policies, parallel=True)
This file illustrated the use the EMA classes for a contrived vensim example .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> chamarat <c.hamarat (at) tudelft (dot) nl> ''' from __future__ import (division, unicode_literals, absolute_import, print_function) from ema_workbench import (TimeSeriesOutcome, perform_experiments, RealParameter, ema_logging) from ema_workbench.connectors.vensim import VensimModel if __name__ == "__main__": #turn on logging ema_logging.log_to_stderr(ema_logging.INFO) #instantiate a model wd = r'./models/vensim example' vensimModel = VensimModel("simpleModel", wd=wd, model_file=r'\model.vpm') vensimModel.uncertainties = [ RealParameter("x11", 0, 2.5), RealParameter("x12", -2.5, 2.5) ] vensimModel.outcomes = [TimeSeriesOutcome('a')] results = perform_experiments(vensimModel, 1000, parallel=True)
RealParameter('fatality rate region 2', 0.0001, 0.1), RealParameter('initial immune fraction of the population of region 1', 0, 0.5), RealParameter('initial immune fraction of the population of region 2', 0, 0.5), RealParameter('normal interregional contact rate', 0, 0.9), RealParameter('permanent immune population fraction R1', 0, 0.5), RealParameter('permanent immune population fraction R2', 0, 0.5), RealParameter('recovery time region 1', 0.1, 0.75), RealParameter('recovery time region 2', 0.1, 0.75), RealParameter('susceptible to immune population delay time region 1', 0.5, 2), RealParameter('susceptible to immune population delay time region 2', 0.5, 2), RealParameter('root contact rate region 1', 0.01, 5), RealParameter('root contact ratio region 2', 0.01, 5), RealParameter('infection ratio region 1', 0, 0.15), RealParameter('infection rate region 2', 0, 0.15), RealParameter('normal contact rate region 1', 10, 100), RealParameter('normal contact rate region 2', 10, 200) ] # add policies policies = [ Policy('no policy', model_file=r'FLUvensimV1basecase.vpm'), Policy('static policy', model_file=r'FLUvensimV1static.vpm'), Policy('adaptive policy', model_file=r'FLUvensimV1dynamic.vpm') ] results = perform_experiments(model, 1000, policies=policies)
# initiate ETM connection ref_scenario = 769771 ETM = init_ETM(ref_scenario, metrics) # override some of the defaults of the model model.constants = [ # standard = 1920 hours / year Constant("ETM_instance", ETM), Constant("flh_of_energy_power_wind_turbine_inland", 3000), Constant("flh_of_solar_pv_solar_radiation", 1000), # standard = 867 / year Constant("costs_co2", 100), # standard = 8 €/tonne Constant("total_supply", 55e6), # MWh 35e6 minimum, RES 1.0 55e6 ] results = perform_experiments(model, policies=25) experiments, outcomes = results policies = experiments["policy"] for i, policy in enumerate(np.unique(policies)): experiments.loc[policies == policy, "policy"] = str(i) data = pd.DataFrame(outcomes) data["solarVSwind"] = experiments["solarVSwind"] #%% plotting section colors = sns.color_palette("Set2", len(data.columns)) sns.set_style("whitegrid")
#specify uncertainties criticality_model.uncertainties = [ IntegerParameter('det_idx', 1, 3), RealParameter('od_exp', 0.75, 1.5), RealParameter('od_loc1', 0.75, 1.5), RealParameter('od_loc2', 0.75, 1.5), RealParameter('od_loc3', 0.75, 1.5), RealParameter('od_loc4', 0.75, 1.5), RealParameter('theta', 30, 70), RealParameter('beta', 0.25, 0.75), RealParameter('cutoff', 0.025, 0.075), RealParameter('m10_buffer', 0.0025, 0.0075), RealParameter('penalty', 1, 1.5) ] #specify outcomes criticality_model.outcomes = [ ScalarOutcome(key) for key, val in edge_dict.iteritems() ] from ema_workbench import Policy, perform_experiments from ema_workbench import ema_logging ema_logging.log_to_stderr(ema_logging.INFO) n_scenarios = 3 results = perform_experiments(criticality_model, n_scenarios) fh = r'./data/{} experiments_22may_v01.tar.gz'.format(nr_experiments) save_results(results, fh)
# initiate model model = Model(name='taxidriver', function=ema_experiment) # levers model.levers = [ RealParameter("startup_fee", 2, 5), RealParameter("km_fee", 0.2, 0.5), RealParameter("speed", 90, 130), ] # uncertainties model.uncertainties = [ RealParameter("distance", 3, 60), RealParameter("fuel_price", 1.1, 1.8), RealParameter("driver_salary_rate", 10, 20), ] # specify outcomes model.outcomes = [ ScalarOutcome("profit"), ScalarOutcome("fuel_use"), ScalarOutcome("time_spent"), ] # run experiments results = perform_experiments(model, scenarios=10, policies=3) # # save results # results_file_name = os.path.join(RESULT_FOLDER, f"cabelpooling_ema_results_{RUNID}.tar.gz") # save_results(results, file_name=results_file_name)
if __name__ == '__main__': ema_logging.LOG_FORMAT = '[%(name)s/%(levelname)s/%(processName)s] %(message)s' ema_logging.log_to_stderr(ema_logging.INFO) model = Model('simpleModel', function=modelvpl) #instantiate the model #specify uncertainties model.uncertainties = [RealParameter("indice_custo", 0.9, 1.1), RealParameter("indice_faturamento", 0.9,1.3), RealParameter("i", 0.01,0.05)] #specify outcomes model.outcomes = [ScalarOutcome('y')] results = perform_experiments(model, 1000) util.utilities.save_results(results, './results/results.tar.gz') # print(results) # ensemble = ModelEnsemble() #instantiate an ensemble # ensemble.model_structure = model #set the model on the ensemble # results = ensemble.perform_experiments(100, reporting_interval=1) #run 1000 experiments
@author: Pedro """ from __future__ import (division, print_function, absolute_import, unicode_literals) from ema_workbench import (RealParameter, ScalarOutcome, ema_logging, perform_experiments) from ema_workbench.connectors.excel import ExcelModel if __name__ == "__main__": ema_logging.log_to_stderr(level=ema_logging.DEBUG) model = ExcelModel("excelModel", wd="./models", model_file= '/excelModel.xlsx') model.uncertainties = [RealParameter("B2", 0.04, 0.2), #we can refer to a cell in the normal way RealParameter("B3", 4000,6000), # we can also use named cells RealParameter("B4", 0.9,1.1), RealParameter("B5", 0.9,1.1)] #specification of the outcomes model.outcomes = [ScalarOutcome("B7")] # we can also use named range #name of the sheet model.sheet = "EMA" results = perform_experiments(model, 100, parallel=True, reporting_interval=1) print("blaat")
''' ''' # Created on Jul 23, 2016 # # .. codeauthor::jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> from ema_workbench import (RealParameter, TimeSeriesOutcome, ema_logging, perform_experiments) from ema_workbench.connectors.pysd_connector import PysdModel if __name__ == '__main__': ema_logging.log_to_stderr(ema_logging.INFO) mdl_file = './models/pysd/Teacup.mdl' model = PysdModel(mdl_file=mdl_file) model.uncertainties = [RealParameter('Room Temperature', 33, 120)] model.outcomes = [TimeSeriesOutcome('Teacup Temperature')] perform_experiments(model, 100)
RealParameter('x21', 0.0001, 0.1), RealParameter('x22', 0.0001, 0.1), RealParameter('x31', 0, 0.5), RealParameter('x32', 0, 0.5), RealParameter('x41', 0, 0.9), RealParameter('x51', 0, 0.5), RealParameter('x52', 0, 0.5), RealParameter('x61', 0, 0.8), RealParameter('x62', 0, 0.8), RealParameter('x81', 1, 10), RealParameter('x82', 1, 10), RealParameter('x91', 0, 0.1), RealParameter('x92', 0, 0.1), RealParameter('x101', 0, 200), RealParameter('x102', 0, 200)] model.outcomes = [TimeSeriesOutcome("TIME"), TimeSeriesOutcome("deceased_population_region_1")] nr_experiments = 500 with MultiprocessingEvaluator(model) as evaluator: results = perform_experiments(model, nr_experiments, evaluator=evaluator) lines(results, outcomes_to_show="deceased_population_region_1", show_envelope=True, density=Density.KDE, titles=None, experiments_to_show=np.arange(0, nr_experiments, 10) ) plt.show()
#specify outcomes model.outcomes = [ScalarOutcome('p_10'), ScalarOutcome('r_10'), ScalarOutcome('a_10'), ScalarOutcome('p_30'), ScalarOutcome('r_30'), ScalarOutcome('a_30'), ScalarOutcome('p_50'), ScalarOutcome('r_50'), ScalarOutcome('a_50'), ScalarOutcome('p_70'), ScalarOutcome('r_70'), ScalarOutcome('a_70'), ScalarOutcome('p_100'), ScalarOutcome('r_100'), ScalarOutcome('a_100'), ScalarOutcome('p_avg'), ScalarOutcome('r_avg'), ScalarOutcome('a_avg')] from ema_workbench import save_results results = perform_experiments(model, 1000) save_results(results, '1000_scenarios_%s_new_model_diverse.tar.gz'%domain) sa_results = perform_experiments(model, 1050, uncertainty_sampling='sobol') save_results(sa_results, '1050_scenarios_%s_sobol_new_model_diverse.tar.gz'%domain)
RealParameter('additional seasonal immune population fraction R2', 0, 0.5), RealParameter('fatality ratio region 1', 0.0001, 0.1), RealParameter('fatality rate region 2', 0.0001, 0.1), RealParameter('initial immune fraction of the population of region 1', 0, 0.5), RealParameter('initial immune fraction of the population of region 2', 0, 0.5), RealParameter('normal interregional contact rate', 0, 0.9), RealParameter('permanent immune population fraction R1', 0, 0.5), RealParameter('permanent immune population fraction R2', 0, 0.5), RealParameter('recovery time region 1', 0.1, 0.75), RealParameter('recovery time region 2', 0.1, 0.75), RealParameter('susceptible to immune population delay time region 1', 0.5, 2), RealParameter('susceptible to immune population delay time region 2', 0.5, 2), RealParameter('root contact rate region 1', 0.01, 5), RealParameter('root contact ratio region 2', 0.01, 5), RealParameter('infection ratio region 1', 0, 0.15), RealParameter('infection rate region 2', 0, 0.15), RealParameter('normal contact rate region 1', 10, 100), RealParameter('normal contact rate region 2', 10, 200) ] nr_experiments = 10 with MultiprocessingEvaluator(model) as evaluator: results = perform_experiments(model, nr_experiments, evaluator=evaluator)
from ema_workbench import (RealParameter, TimeSeriesOutcome, ema_logging, perform_experiments) from ema_workbench.connectors.excel import ExcelModel from ema_workbench.em_framework.evaluators import MultiprocessingEvaluator if __name__ == "__main__": ema_logging.log_to_stderr(level=ema_logging.INFO) model = ExcelModel("predatorPrey", wd="./models/excelModel", model_file='excel example.xlsx') model.uncertainties = [RealParameter("K2", 0.01, 0.2), # we can refer to a cell in the normal way # we can also use named cells RealParameter("KKK", 450, 550), RealParameter("rP", 0.05, 0.15), RealParameter("aaa", 0.00001, 0.25), RealParameter("tH", 0.45, 0.55), RealParameter("kk", 0.1, 0.3)] # specification of the outcomes model.outcomes = [TimeSeriesOutcome("B4:B1076"), # we can refer to a range in the normal way TimeSeriesOutcome("P_t")] # we can also use named range # name of the sheet model.sheet = "Sheet1" with MultiprocessingEvaluator(model) as evaluator: results = perform_experiments(model, 100, reporting_interval=1, evaluator=evaluator)
""" """ from __future__ import unicode_literals, print_function, absolute_import, division # Created on Jul 23, 2016 # # .. codeauthor::jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> from ema_workbench import RealParameter, TimeSeriesOutcome, ema_logging, perform_experiments from ema_workbench.connectors import PysdModel if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.DEBUG) mdl_file = "./models/pysd/Teacup.mdl" model = PysdModel(mdl_file=mdl_file) model.uncertainties = [RealParameter("Room Temperature", 33, 120)] model.outcomes = [TimeSeriesOutcome("Teacup Temperature")] perform_experiments(model, 100)
model_file = 'FLUvensimV1basecase.vpm') #outcomes model.outcomes = [TimeSeriesOutcome('deceased population region 1'), TimeSeriesOutcome('infected fraction R1')] #Plain Parametric Uncertainties model.uncertainties = [ RealParameter('additional seasonal immune population fraction R1', 0, 0.5), RealParameter('additional seasonal immune population fraction R2', 0, 0.5), RealParameter('fatality ratio region 1', 0.0001, 0.1), RealParameter('fatality rate region 2', 0.0001, 0.1), RealParameter('initial immune fraction of the population of region 1', 0, 0.5), RealParameter('initial immune fraction of the population of region 2', 0, 0.5), RealParameter('normal interregional contact rate', 0, 0.9), RealParameter('permanent immune population fraction R1', 0, 0.5), RealParameter('permanent immune population fraction R2', 0, 0.5), RealParameter('recovery time region 1', 0.1, 0.75), RealParameter('recovery time region 2', 0.1, 0.75), RealParameter('susceptible to immune population delay time region 1', 0.5, 2), RealParameter('susceptible to immune population delay time region 2', 0.5, 2), RealParameter('root contact rate region 1', 0.01, 5), RealParameter('root contact ratio region 2', 0.01, 5), RealParameter('infection ratio region 1', 0, 0.15), RealParameter('infection rate region 2', 0, 0.15), RealParameter('normal contact rate region 1', 10, 100), RealParameter('normal contact rate region 2', 10, 200)] nr_experiments = 100 results = perform_experiments(model, nr_experiments, parallel=True)
RealParameter("x22", 0.0001, 0.1), RealParameter("x31", 0, 0.5), RealParameter("x32", 0, 0.5), RealParameter("x41", 0, 0.9), RealParameter("x51", 0, 0.5), RealParameter("x52", 0, 0.5), RealParameter("x61", 0, 0.8), RealParameter("x62", 0, 0.8), RealParameter("x81", 1, 10), RealParameter("x82", 1, 10), RealParameter("x91", 0, 0.1), RealParameter("x92", 0, 0.1), RealParameter("x101", 0, 200), RealParameter("x102", 0, 200), ] model.outcomes = [TimeSeriesOutcome("TIME"), TimeSeriesOutcome("deceased_population_region_1")] nr_experiments = 500 results = perform_experiments(model, nr_experiments, parallel=True) lines( results, outcomes_to_show="deceased_population_region_1", show_envelope=True, density=KDE, titles=None, experiments_to_show=np.arange(0, nr_experiments, 10), ) plt.show()
model.levers = [RealParameter("b", 0.0, 0.01)] # specify outcomes model.outcomes = [ ScalarOutcome('c'), ScalarOutcome('d'), ScalarOutcome('e') ] #model.constants = [Constant('replications', 10)] n_scenarios = 10 n_policies = 10 res = perform_experiments(model, n_scenarios, n_policies) experiments, outcomes = res print(experiments) print(outcomes) ''' edgeColor = setEdgeColors(G) finalColorCode = setNodeColors(G) plt.figure(figsize=[15,10]) plt.subplot(121) nx.draw(G, with_labels=True, node_color=initialColorCode, edge_color= edgeColor, pos = nx.planar_layout(G), font_weight='bold', node_size=[1000,300,300,300,300]) plt.subplot(122)
This file illustrated the use the EMA classes for a contrived vensim example .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> chamarat <c.hamarat (at) tudelft (dot) nl> ''' from __future__ import (division, unicode_literals, absolute_import, print_function) from ema_workbench import (TimeSeriesOutcome, perform_experiments, RealParameter, ema_logging) from ema_workbench.connectors.vensim import VensimModel if __name__ == "__main__": #turn on logging ema_logging.log_to_stderr(ema_logging.INFO) #instantiate a model wd = r'./models/vensim example' vensimModel = VensimModel("simpleModel", wd=wd, model_file=r'\model.vpm') vensimModel.uncertainties = [RealParameter("x11", 0, 2.5), RealParameter("x12", -2.5, 2.5)] vensimModel.outcomes = [TimeSeriesOutcome('a')] results = perform_experiments(vensimModel, 1000, parallel=True)
# override some of the defaults of the model lake_model.constants = [Constant('alpha', 0.41), Constant('nsamples', 100), Constant('timehorizon', lake_model.time_horizon), ] import os from ema_workbench import (perform_experiments, ema_logging, save_results, load_results, Policy) from ema_workbench.em_framework import samplers # turn on logging ema_logging.log_to_stderr(ema_logging.INFO) # perform experiments nr_experiments = 2 nr_policies = 1 fn = './data/{}_experiments_openloop_noApollution.tar.gz'.format(nr_experiments) policy = Policy({'c1':0, 'c2':0, 'r1':1, 'r2':0, 'w1':0}) results = perform_experiments(lake_model, 10, policies=[policy]) #try: # why regenerate the data? # results = load_results(fn) #except IOError: # results = perform_experiments(lake_model, scenarios=nr_experiments) save_results(results, fn)