Пример #1
0
def maxmin_optimize():
    ema_logging.log_to_stderr(ema_logging.INFO)
 
    model = TestModel("", 'simpleModel') #instantiate the model
    ensemble = ModelEnsemble() #instantiate an ensemble
    ensemble.set_model_structure(model) #set the model on the ensemble
    ensemble.parallel = True
    ensemble.processes = 12
    
    def obj_function1(outcomes):
        return outcomes['y']
    
    policy_levers = { "L1": (0,1),
                      "L2": (0,1)}
    
    
    
    results = ensemble.perform_maximin_optimization(obj_function1 = obj_function1, 
                                                policy_levers = policy_levers,
                                                minimax1='minimize',
                                                nrOfGenerations1=50,
                                                nrOfPopMembers1=200,
                                                minimax2 = "maximize",                                   
                                                nrOfGenerations2 = 50,
                                                nrOfPopMembers2 = 100,
                                                )

    graph_errorbars_raw(results['stats'])
    plt.show()
Пример #2
0
def transition_test():

    model = EnergyTrans(r"..\..\..\models\EnergyTrans", "fluCase")
    ensemble = ModelEnsemble()
    ensemble.set_model_structure(model)

    ensemble.perform_experiments(cases=10, callback=HDF5Callback)
Пример #3
0
def flu_test():

    model = FluModel(r"..\..\..\models\flu", "fluCase")
    ensemble = ModelEnsemble()
    ensemble.set_model_structure(model)

    ensemble.perform_experiments(cases=10, callback=HDF5Callback)
Пример #4
0
def test_optimization():
    ema_logging.log_to_stderr(ema_logging.INFO)
    
    model = FluModel(r'..\data', "fluCase")
    ensemble = ModelEnsemble()
    
    ensemble.set_model_structure(model)
    ensemble.parallel=True
#    ensemble.processes = 12
        
    stats, pop  = ensemble.perform_outcome_optimization(obj_function = obj_function_multi,
                                                    reporting_interval=10, 
                                                    weights=(MAXIMIZE, MAXIMIZE),
                                                    pop_size=100,
                                                    nr_of_generations=5,
                                                    crossover_rate=0.5,
                                                    mutation_rate=0.05)
    res = stats.hall_of_fame.keys
    
    x = [entry.values[0] for entry in res]
    y = [entry.values[1] for entry in res]
    
    print len(x), len(y)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(x,y)
    ax.set_ylabel("deceased population")
    ax.set_xlabel("infected fraction")
    
    plt.show()
Пример #5
0
def test_inspect():
    import inspect_test
    model = FluModel(r'..\..\..\models\flu', "fluCase")
    ensemble = ModelEnsemble()
    ensemble.set_model_structure(model)
    
    ensemble.perform_experiments(cases = 10,
                                 callback=inspect_test.InspectCallback)
def perform_experiments():
    ema_logging.log_to_stderr(level=ema_logging.INFO)
    model = SalinizationModel(r"C:\workspace\EMA-workbench\models\salinization", "verzilting")
    model.step = 4
    
    ensemble = ModelEnsemble()
    ensemble.set_model_structure(model)
    
    ensemble.parallel = True
    nr_of_experiments = 10000
    results = ensemble.perform_experiments(nr_of_experiments)
    return results
Пример #7
0
def test_save_results():
    os.remove("test.h5")

    nrOfExperiments = 10
    fileName = "test.h5"
    experimentName = "one_exp_test"

    ensemble = ModelEnsemble()
    ensemble.set_model_structure(FluModel(r"..\..\..\models\flu", "fluCase"))

    ensemble.perform_experiments(
        nrOfExperiments, callback=HDF5Callback, fileName=fileName, experimentName=experimentName
    )
Пример #8
0
 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. 
     
     '''
     model = LookupTestModel( r'../models/', 'lookupTestModel')
     
     #model.step = 4 #reduce data to be stored
     ensemble = ModelEnsemble()
     ensemble.set_model_structure(model)
     
     ensemble.perform_experiments(10)
Пример #9
0
 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.set_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_optimization():
    ema_logging.log_to_stderr(ema_logging.INFO)
    
    model = FluModel(r'..\data', "fluCase")
    ensemble = ModelEnsemble()
    
    ensemble.set_model_structure(model)
    ensemble.parallel=True
        
    stats, pop  = ensemble.perform_outcome_optimization(obj_function = obj_function_multi,
                                                    reporting_interval=100, 
                                                    weights=(MAXIMIZE, MAXIMIZE),
                                                    pop_size=100,
                                                    nr_of_generations=20,
                                                    crossover_rate=0.5,
                                                    mutation_rate=0.05,
                                                    caching=False)
    res = stats.hall_of_fame.keys
    
    print len(stats.tried_solutions.values())
Пример #11
0
def test_tree():
    
    log_to_stderr(level= INFO)
        
    model = FluModel(r'..\..\models\flu', "fluCase")
    ensemble = ModelEnsemble()
    ensemble.parallel = True
    ensemble.set_model_structure(model)
    
    policies = [{'name': 'no policy',
                 'file': r'\FLUvensimV1basecase.vpm'},
                {'name': 'static policy',
                 'file': r'\FLUvensimV1static.vpm'},
                {'name': 'adaptive policy',
                 'file': r'\FLUvensimV1dynamic.vpm'}
                ]
    ensemble.add_policies(policies)
    
    results = ensemble.perform_experiments(10)
   
    a_tree = tree(results, classify)
Пример #12
0
def outcome_optimize():
    ema_logging.log_to_stderr(ema_logging.INFO)
 
    model = TestModel("", 'simpleModel') #instantiate the model
    ensemble = ModelEnsemble() #instantiate an ensemble
    ensemble.set_model_structure(model) #set the model on the ensemble
    policy = {"name": "test",
              "L1": 1,
              "L2": 1}
    ensemble.add_policy(policy)
    
    def obj_func(results):
        return results['y']        
    
    results = ensemble.perform_outcome_optimization(obj_function=obj_func, 
                                          minimax = 'minimize', 
                                          nrOfGenerations = 1000, 
                                          nrOfPopMembers = 10)

    graph_errorbars_raw(results['stats'])
    plt.show()
Пример #13
0
def test_feature_selection():
    log_to_stderr(level= INFO)
        
    model = FluModel(r'..\..\models\flu', "fluCase")
    ensemble = ModelEnsemble()
    ensemble.parallel = True
    ensemble.set_model_structure(model)
    
    policies = [{'name': 'no policy',
                 'file': r'\FLUvensimV1basecase.vpm'},
                {'name': 'static policy',
                 'file': r'\FLUvensimV1static.vpm'},
                {'name': 'adaptive policy',
                 'file': r'\FLUvensimV1dynamic.vpm'}
                ]
    ensemble.add_policies(policies)
    
    results = ensemble.perform_experiments(5000)
   
    results = feature_selection(results, classify)
    for entry in results:
        print entry[0] +"\t" + str(entry[1])
Пример #14
0
def robust_optimize():
    ema_logging.log_to_stderr(ema_logging.INFO)
 
    model = TestModel("", 'simpleModel') #instantiate the model
    ensemble = ModelEnsemble() #instantiate an ensemble
    ensemble.set_model_structure(model) #set the model on the ensemble
    
    
    policy_levers = { "L1": (0,1),
                      "L2": (0,1)}
    
    def obj_func(results):
        return np.average(results['y'])        
    
    results = ensemble.perform_robust_optimization(cases=1000, 
                                                   obj_function=obj_func, 
                                                   policy_levers=policy_levers, 
                                                   minimax='minimize', 
                                                   nrOfGenerations=50, 
                                                   nrOfPopMembers=20 )
    graph_errorbars_raw(results['stats'])
    plt.show()
def test_optimization():
    ema_logging.log_to_stderr(ema_logging.INFO)
    
    model = FluModel(r'../models', "fluCase")
    ensemble = ModelEnsemble()
    
    ensemble.set_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)
    fn = '../data/test optimization save.bz2'
Пример #16
0
        if error:
            raise CaseError("run not completed", case) 

def obj_func(outcomes):
    outcome = outcomes['total fraction new technologies']
    zeros = np.zeros((outcome.shape[0], 1))
    zeros[outcome[:,-1]>0.6] = 1
    value = np.sum(zeros)/zeros.shape[0] 
    return value,

if __name__ == "__main__":
    ema_logging.log_to_stderr(ema_logging.INFO)
    model = EnergyTrans(r"..\data", "ESDMAElecTrans")
       
    ensemble = ModelEnsemble()
    ensemble.set_model_structure(model)
    ensemble.parallel = True
    
    policy_levers = {'Trigger subsidy T2': {'type':'range', 'values':(0,1)},
                   'Trigger subsidy T3': {'type':'range', 'values':(0,1)},
                   'Trigger subsidy T4': {'type':'range', 'values':(0,1)},
                   'Trigger addnewcom': {'type':'list', 'values':[0, 0.25, 0.5, 0.75, 1]}}
    
    stats_callback, pop   = ensemble.perform_robust_optimization(cases=10,
                                               reporting_interval=100,
                                               obj_function=obj_func,
                                               policy_levers=policy_levers,
                                               weights = (MAXIMIZE,),
                                               nr_of_generations=100,
                                               pop_size=10,
                                               crossover_rate=0.5, 
Пример #17
0
    ]

    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
    vensimModel = PredatorPrey(r"..\..\models\predatorPreyNetlogo", "simpleModel")

    # instantiate an ensemble
    ensemble = ModelEnsemble()

    # set the model on the ensemble
    ensemble.set_model_structure(vensimModel)

    # run in parallel, if not set, FALSE is assumed
    ensemble.parallel = True

    # perform experiments
    results = ensemble.perform_experiments(100)

    plotting.lines(results, density=plotting.KDE)
    plt.show()
Пример #18
0
                r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils\Models\Consecutive',
                "scarcity")
            model_location = r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils\Models\Consecutive\Metals EMA.vpm'
        elif loop_index == 0:
            model = ScarcityModel(
                r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils\Models\Base',
                "scarcity")
            model_location = r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils\Models\Base\Metals EMA.vpm'
        else:
            model = ScarcityModel(
                r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils\Models\Switches',
                "scarcity")
            model_location = r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils\Models\Switches\Metals EMA.vpm'

        ensemble = ModelEnsemble()
        ensemble.set_model_structure(model)

        serie = run_interval(model_location, loop_index, interval,
                             'relative market price', unique_edges, indCons,
                             double_list, uncertain_names,
                             uncertain_values[beh_no])
        interval_series.append(serie)

    print(interval_series)
    base = behaviour_int[beh_no][interval[0]:interval[1]]
    print len(base), len(interval_series[0])
    distances, dominant = dominance_distance(behaviour_int[beh_no],
                                             interval_series)
    print distances, dominant

#    x = range(0,len(interval_series))
Пример #19
0
    outcomes = [
        Outcome('CumulativeGHGreduction', time=True),
    ]


if __name__ == "__main__":
    #turn on logging
    ema_logging.log_to_stderr(ema_logging.INFO)

    #instantiate a model
    vensimModel = EVO(r"./models", "simpleModel")

    #instantiate an ensemble
    ensemble = ModelEnsemble()

    #set the model on the ensemble
    ensemble.set_model_structure(vensimModel)
    #
    #     run in parallel, if not set, FALSE is assumed
    ensemble.parallel = True

    cases = [{} for _ in range(1000)]

    #perform experiments
    results = ensemble.perform_experiments(cases, reporting_interval=100)

    save_results(
        results,
        r'.\data\EMA results ModelSebastiaanGreeven 1000 exp Stoch Test.tar.gz'
    )
Пример #20
0
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.set_model_structure(model)  # set the model on the ensemble
    results = ensemble.perform_experiments(1000)  # generate 1000 cases
Пример #21
0
        susceptible_population_region_1 = susceptible_population_region_1_NEXT
        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__":
    import expWorkbench.ema_logging as logging
    np.random.seed(150) #set the seed for replication purposes
    logging.log_to_stderr(logging.INFO)
    
    fluModel = MexicanFlu(None, "mexicanFluExample")
    ensemble = ModelEnsemble()
    ensemble.parallel = True
    ensemble.set_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()