def test_optimization(): if os.name != 'nt': return ema_logging.log_to_stderr(ema_logging.INFO) model = FluModel(r'../models', "fluCase") ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.parallel = True pop_size = 8 nr_of_generations = 10 eps = np.array([1e-3, 1e6]) stats, pop = ensemble.perform_outcome_optimization( obj_function=obj_function_multi, algorithm=epsNSGA2, reporting_interval=100, weights=(MAXIMIZE, MAXIMIZE), pop_size=pop_size, nr_of_generations=nr_of_generations, crossover_rate=0.8, mutation_rate=0.05, eps=eps)
def test_vensim_model(self): #instantiate a model wd = r'../models' model = VensimExampleModel(wd, "simpleModel") #instantiate an ensemble ensemble = ModelEnsemble() #set the model on the ensemble ensemble.model_structure = model nr_runs = 10 experiments, outcomes = ensemble.perform_experiments(nr_runs) self.assertEqual(experiments.shape[0], nr_runs) self.assertIn('TIME', outcomes.keys()) self.assertIn(model.outcomes[0].name, outcomes.keys())
def test_running_lookup_uncertainties(self): ''' This is the more comprehensive test, given that the lookup uncertainty replaces itself with a bunch of other uncertainties, check whether we can successfully run a set of experiments and get results back. We assert that the uncertainties are correctly replaced by analyzing the experiments array. ''' if os.name != 'nt': return model = LookupTestModel(r'../models/', 'lookupTestModel') #model.step = 4 #reduce data to be stored ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.perform_experiments(10)
def test_running_lookup_uncertainties(self): ''' This is the more comprehensive test, given that the lookup uncertainty replaces itself with a bunch of other uncertainties, check whether we can successfully run a set of experiments and get results back. We assert that the uncertainties are correctly replaced by analyzing the experiments array. ''' if os.name != 'nt': return model = LookupTestModel( r'../models/', 'lookupTestModel') #model.step = 4 #reduce data to be stored ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.perform_experiments(10)
def test_optimization(): if os.name != 'nt': return ema_logging.log_to_stderr(ema_logging.INFO) model = FluModel(r'../models', "fluCase") ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.parallel=True pop_size = 8 nr_of_generations = 10 eps = np.array([1e-3, 1e6]) stats, pop = ensemble.perform_outcome_optimization(obj_function = obj_function_multi, algorithm=epsNSGA2, reporting_interval=100, weights=(MAXIMIZE, MAXIMIZE), pop_size=pop_size, nr_of_generations=nr_of_generations, crossover_rate=0.8, mutation_rate=0.05, eps=eps)
"susceptible to immune population delay time region 1"), ParameterUncertainty((0.5,2), "susceptible to immune population delay time region 2"), ParameterUncertainty((0.01, 5), "root contact rate region 1"), ParameterUncertainty((0.01, 5), "root contact ratio region 2"), ParameterUncertainty((0, 0.15), "infection ratio region 1"), ParameterUncertainty((0, 0.15), "infection rate region 2"), ParameterUncertainty((10, 100), "normal contact rate region 1"), ParameterUncertainty((10, 200), "normal contact rate region 2")] if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = FluModel(r'./models/flu', "fluCase") ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.parallel = True #turn on parallel processing nr_experiments = 1000 results = ensemble.perform_experiments(nr_experiments) fh = r'./data/{} flu cases no policy.tar.gz'.format(nr_experiments) save_results(results, fh)
a_mean = np.mean(a) b_mean = np.mean(b) if a_mean < 0.5 or b_mean < 0.5: return (np.inf, ) * 2 else: return a_mean, b_mean if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = DummyModel(r"", "dummy") np.random.seed(123456789) ensemble = ModelEnsemble() ensemble.set_model_structure(model) policy_levers = { 'Trigger a': { 'type': 'list', 'values': [0, 0.25, 0.5, 0.75, 1] }, 'Trigger b': { 'type': 'list', 'values': [0, 0.25, 0.5, 0.75, 1] }, 'Trigger c': { 'type': 'list', 'values': [0, 0.25, 0.5, 0.75, 1] }
outcomes = [ Outcome('sheep', time=True), Outcome('wolves', time=True), Outcome('grass', time=True) # TODO patches not working in reporting ] if __name__ == "__main__": #turn on logging ema_logging.log_to_stderr(ema_logging.INFO) #instantiate a model fh = r"./models/predatorPreyNetlogo" model = PredatorPrey(fh, "simpleModel") #instantiate an ensemble ensemble = ModelEnsemble() #set the model on the ensemble ensemble.model_structure = model #run in parallel, if not set, FALSE is assumed ensemble.parallel = True #perform experiments results = ensemble.perform_experiments(100, reporting_interval=1) plotting.lines(results, density=plotting_util.KDE) plt.show()
"normal contact rate region 2")] def model_init(self, policy, kwargs): '''initializes the model''' try: self.model_file = policy['file'] except KeyError: ema_logging.warning("key 'file' not found in policy") super(FluModel, self).model_init(policy, kwargs) if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = FluModel(r'./models/flu', "flucase") ensemble = ModelEnsemble() ensemble.model_structure = model #add policies policies = [{'name': 'no policy', 'file': r'\FLUvensimV1basecase.vpm'}, {'name': 'static policy', 'file': r'\FLUvensimV1static.vpm'}, {'name': 'adaptive policy', 'file': r'\FLUvensimV1dynamic.vpm'} ] ensemble.policies = policies #turn on parallel processing ensemble.parallel = True
else: atomicBehavior.append([last, steps]) last = entry steps = 0 atomicBehavior.append([last, steps]) behavior = [] behavior.append(atomicBehavior.pop(0)) for entry in atomicBehavior: if entry[0] != behavior[-1][0] and entry[1] >2: behavior.append(entry) elif entry[1] <2: continue else: behavior[-1][1] =+ entry[1] behavior = [entry[0] for entry in behavior] return behavior if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = ScarcityModel(r'..\data', "scarcity") ensemble = ModelEnsemble() ensemble.set_model_structure(model) ensemble.parallel = True results = ensemble.perform_experiments(100) # determineBehavior(results)
susceptible_population_region_2 = susceptible_population_region_2_NEXT immune_population_region_1 = immune_population_region_1_NEXT immune_population_region_2 = immune_population_region_2_NEXT deceased_population_region_1.append(deceased_population_region_1_NEXT) deceased_population_region_2.append(deceased_population_region_2_NEXT) #End of main code return (runTime, deceased_population_region_1) #, Max_infected, Max_time) if __name__ == "__main__": np.random.seed(150) #set the seed for replication purposes ema_logging.log_to_stderr(ema_logging.INFO) fluModel = MexicanFlu(None, "mexicanFluExample") ensemble = ModelEnsemble() ensemble.parallel = True ensemble.model_structure = fluModel nr_experiments = 500 results = ensemble.perform_experiments(nr_experiments, reporting_interval=100) 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()
ParameterUncertainty((0.1,0.3), "kk")] #specification of the outcomes outcomes = [Outcome("B4:B1076", time=True), #we can refer to a range in the normal way Outcome("P_t", time=True)] # we can also use named range #name of the sheet sheet = "Sheet1" #relative path to the Excel file workbook = r'\excel example.xlsx' if __name__ == "__main__": ema_logging.log_to_stderr(level=ema_logging.INFO) model = ExcelModel(r"./models/excelModel", "predatorPrey") ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.parallel = True #turn on parallel computing pool = MultiprocessingPool(ensemble.model_structure, nr_processes=2) ensemble.pool = pool #run 100 experiments nr_experiments = 100 results = ensemble.perform_experiments(nr_experiments)
''' This class represents a simple example of how one can extent the basic ModelStructureInterface in order to do EMA on a simple model coded in Python directly ''' #specify uncertainties uncertainties = [ ParameterUncertainty((0.1, 10), "x1"), ParameterUncertainty((-0.01, 0.01), "x2"), ParameterUncertainty((-0.01, 0.01), "x3") ] #specify outcomes outcomes = [Outcome('y')] def model_init(self, policy, kwargs): pass def run_model(self, case): """Method for running an instantiated model structure """ self.output[ self.outcomes[0].name] = case['x1'] * case['x2'] + case['x3'] if __name__ == '__main__': model = SimplePythonModel(None, 'simpleModel') #instantiate the model ensemble = ModelEnsemble() #instantiate an ensemble # ensemble.parallel = True ensemble.set_model_structure(model) #set the model on the ensemble results = ensemble.perform_experiments(1000) #run 1000 experiments
a_mean = np.mean(a) b_mean = np.mean(b) if a_mean < 0.5 or b_mean < 0.5: return (np.inf,) * 2 else: return a_mean, b_mean if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = DummyModel(r"", "dummy") np.random.seed(123456789) ensemble = ModelEnsemble() ensemble.set_model_structure(model) policy_levers = {'Trigger a': {'type':'list', 'values':[0, 0.25, 0.5, 0.75, 1]}, 'Trigger b': {'type':'list', 'values':[0, 0.25, 0.5, 0.75, 1]}, 'Trigger c': {'type':'list', 'values':[0, 0.25, 0.5, 0.75, 1]}} cases = ensemble._generate_samples(10, UNION)[0] ensemble.add_policy({"name":None}) experiments = [entry for entry in ensemble._generate_experiments(cases)] for entry in experiments: entry.pop("model") entry.pop("policy") cases = experiments
ParameterUncertainty, Outcome class SimplePythonModel(ModelStructureInterface): ''' This class represents a simple example of how one can extent the basic ModelStructureInterface in order to do EMA on a simple model coded in Python directly ''' #specify uncertainties uncertainties = [ParameterUncertainty((0.1, 10), "x1"), ParameterUncertainty((-0.01,0.01), "x2"), ParameterUncertainty((-0.01,0.01), "x3")] #specify outcomes outcomes = [Outcome('y')] def model_init(self, policy, kwargs): pass def run_model(self, case): """Method for running an instantiated model structure """ self.output[self.outcomes[0].name] = case['x1']*case['x2']+case['x3'] if __name__ == '__main__': model = SimplePythonModel(None, 'simpleModel') #instantiate the model ensemble = ModelEnsemble() #instantiate an ensemble # ensemble.parallel = True ensemble.set_model_structure(model) #set the model on the ensemble results = ensemble.perform_experiments(1000) #run 1000 experiments
[(-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") ensemble = ModelEnsemble() ensemble.model_structure = model # run policy with old cases results = ensemble.perform_experiments(100) lines(results, "Energy Level", density=BOXPLOT) plt.show()
#note that this reference to the model should be relative #this relative path will be combined with the workingDirectory model_file = r'\model.vpm' #specify outcomes outcomes = [Outcome('a', time=True)] #specify your uncertainties uncertainties = [ParameterUncertainty((0, 2.5), "x11"), ParameterUncertainty((-2.5, 2.5), "x12")] if __name__ == "__main__": #turn on logging ema_logging.log_to_stderr(ema_logging.INFO) #instantiate a model wd = r'./models/vensim example' vensimModel = VensimExampleModel(wd, "simpleModel") #instantiate an ensemble ensemble = ModelEnsemble() #set the model on the ensemble ensemble.model_structure = vensimModel #run in parallel, if not set, FALSE is assumed ensemble.parallel = True #perform experiments result = ensemble.perform_experiments(1000)
speed = kwargs.pop("lookup price substitute speed") begin = kwargs.pop("lookup price substitute begin") end = kwargs.pop("lookup price substitute end") lookup = [self.priceSubstite(x, speed, begin, end) for x in range(0,100, 10)] kwargs['relative price substitute lookup'] = lookup scale = kwargs.pop("lookup returns to scale speed") speed = kwargs.pop("lookup returns to scale scale") lookup = [self.returnsToScale(x, speed, scale) for x in range(0, 101, 10)] kwargs['returns to scale lookup'] = lookup scale = kwargs.pop("lookup approximated learning speed") speed = kwargs.pop("lookup approximated learning scale") start = kwargs.pop("lookup approximated learning start") lookup = [self.approxLearning(x, speed, scale, start) for x in range(0, 101, 10)] kwargs['approximated learning effect lookup'] = lookup super(ScarcityModel, self).run_model(kwargs) if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.DEBUG) model = ScarcityModel(r'./models/scarcity', "scarcity") ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.parallel = True results = ensemble.perform_experiments(2)
CategoricalUncertainty(("true", "true"), "grass?") ] outcomes = [Outcome('sheep', time=True), Outcome('wolves', time=True), Outcome('grass', time=True) # TODO patches not working in reporting ] if __name__ == "__main__": #turn on logging ema_logging.log_to_stderr(ema_logging.INFO) #instantiate a model fh = r"./models/predatorPreyNetlogo" model = PredatorPrey(fh, "simpleModel") #instantiate an ensemble ensemble = ModelEnsemble() #set the model on the ensemble ensemble.model_structure = model #run in parallel, if not set, FALSE is assumed ensemble.parallel = True #perform experiments results = ensemble.perform_experiments(100, reporting_interval=1) plotting.lines(results, density=plotting_util.KDE) plt.show()
This class represents a simple example of how one can extent the basic ModelStructureInterface in order to do EMA on a simple model coded in Python directly ''' #specify uncertainties uncertainties = [ ParameterUncertainty((0.1, 10), "x1"), ParameterUncertainty((-0.01, 0.01), "x2"), ParameterUncertainty((-0.01, 0.01), "x3") ] #specify outcomes outcomes = [Outcome('y')] def model_init(self, policy, kwargs): pass def run_model(self, case): """Method for running an instantiated model structure """ self.output[ self.outcomes[0].name] = case['x1'] * case['x2'] + case['x3'] if __name__ == '__main__': ema_logging.log_to_stderr(ema_logging.INFO) model = SimplePythonModel(None, 'simpleModel') #instantiate the model ensemble = ModelEnsemble() #instantiate an ensemble ensemble.parallel = True ensemble.model_structure = model #set the model on the ensemble results = ensemble.perform_experiments(1000) #run 1000 experiments
kwargs['relative price substitute lookup'] = lookup scale = kwargs.pop("lookup returns to scale speed") speed = kwargs.pop("lookup returns to scale scale") lookup = [ self.returnsToScale(x, speed, scale) for x in range(0, 101, 10) ] kwargs['returns to scale lookup'] = lookup scale = kwargs.pop("lookup approximated learning speed") speed = kwargs.pop("lookup approximated learning scale") start = kwargs.pop("lookup approximated learning start") lookup = [ self.approxLearning(x, speed, scale, start) for x in range(0, 101, 10) ] kwargs['approximated learning effect lookup'] = lookup super(ScarcityModel, self).run_model(kwargs) if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.DEBUG) model = ScarcityModel(r'./models/scarcity', "scarcity") ensemble = ModelEnsemble() ensemble.model_structure = model ensemble.parallel = True results = ensemble.perform_experiments(2)
"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") ensemble = ModelEnsemble() ensemble.model_structure = model #run policy with old cases results = ensemble.perform_experiments(100) lines(results, 'Energy Level', density=BOXPLOT) plt.show()
#specify outcomes outcomes = [Outcome('a', time=True)] #specify your uncertainties uncertainties = [ ParameterUncertainty((0, 2.5), "x11"), ParameterUncertainty((-2.5, 2.5), "x12") ] if __name__ == "__main__": #turn on logging ema_logging.log_to_stderr(ema_logging.INFO) #instantiate a model wd = r'./models/vensim example' vensimModel = VensimExampleModel(wd, "simpleModel") #instantiate an ensemble ensemble = ModelEnsemble() #set the model on the ensemble ensemble.model_structure = vensimModel #run in parallel, if not set, FALSE is assumed ensemble.parallel = True #perform experiments result = ensemble.perform_experiments(1000)
''' Created on Mar 15, 2012 .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> ''' from analysis import clusterer from util import ema_logging from core import ModelEnsemble from test.scarcity_example import ScarcityModel if __name__ == "__main__": ema_logging.log_to_stderr(ema_logging.INFO) model = ScarcityModel(r'..\..\src\test', "fluCase") ensemble = ModelEnsemble() ensemble.set_model_structure(model) ensemble.parallel = True results = ensemble.perform_experiments(200) clusterer.cluster(data=results, outcome='relative market price', distance='gonenc', cMethod='maxclust', cValue=5, plotDendrogram=False)
class SimplePythonModel(ModelStructureInterface): ''' This class represents a simple example of how one can extent the basic ModelStructureInterface in order to do EMA on a simple model coded in Python directly ''' #specify uncertainties uncertainties = [ParameterUncertainty((0.1, 10), "x1"), ParameterUncertainty((-0.01,0.01), "x2"), ParameterUncertainty((-0.01,0.01), "x3")] #specify outcomes outcomes = [Outcome('y')] def model_init(self, policy, kwargs): pass def run_model(self, case): """Method for running an instantiated model structure """ self.output[self.outcomes[0].name] = case['x1']*case['x2']+case['x3'] if __name__ == '__main__': ema_logging.log_to_stderr(ema_logging.INFO) model = SimplePythonModel(None, 'simpleModel') #instantiate the model ensemble = ModelEnsemble() #instantiate an ensemble ensemble.parallel = True ensemble.model_structure = model #set the model on the ensemble results = ensemble.perform_experiments(1000) #run 1000 experiments