def test_box(): EMAlogging.log_to_stderr(EMAlogging.INFO) x = np.loadtxt(r'quasiflow x.txt') y = np.loadtxt(r'quasiflow y.txt') # prim = prim_box(x, y, pasting=True, threshold = 0, threshold_type = -1) prim = perform_prim(x, y, pasting=True, threshold = 0, threshold_type =-1) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(x[:,0], x[:, 1], c=y) print ' \tmass\tmean' for i, entry in enumerate(prim[0:-1]): print 'found box %s:\t%s\t%s' %(i, entry.box_mass, entry.y_mean) print 'rest box :\t%s\t%s' %(prim[-1].box_mass, prim[-1].y_mean) colors = graphs.COLOR_LIST for i, box in enumerate(prim): box = box.box # print box x = np.array([box[0,0], box[1,0], box[1,0], box[0,0], box[0,0]]) y = np.array([box[0,1], box[0,1], box[1,1], box[1,1], box[0,1]]) # print x # print y ax.plot(x,y, c=colors[i%len(colors)], lw=4) plt.show()
def perform_experiments(): logger = EMAlogging.log_to_stderr(level=EMAlogging.INFO) model = SalinizationModel(r"C:\eclipse\workspace\EMA-workbench\models\salinization", "verzilting") model.step = 4 ensemble = SimpleModelEnsemble() ensemble.set_model_structure(model) policies=[{'name': 'no policy', 'file': r'\verzilting 2.vpm'}, {'name': 'policy group 8', 'file': r'\group 8 best policy.vpm'}, {'name': 'policy other group', 'file': r'\other group best policy.vpm'}, {'name': 'policies combined', 'file': r'\best policies combined.vpm'} ] ensemble.add_policies(policies) ensemble.parallel = True nr_of_experiments = 1000 results = ensemble.perform_experiments(nr_of_experiments) return results
# try: # self.modelFile = policy['file'] # except: # EMAlogging.debug("no policy specified") super(EnergyTrans, self).model_init(policy, kwargs) #pop name policy = copy.copy(policy) policy.pop('name') for key, value in policy.items(): vensim.set_value(key, value) if __name__ == "__main__": logger = logging.log_to_stderr(logging.INFO) model = EnergyTrans(r"..\VensimModels\TFSC", "ESDMAElecTrans") ensemble = SimpleModelEnsemble() ensemble.set_model_structure(model) cases, uncertainties = ensemble._generate_cases(1) valuelist = [15.467089994193 , 18.3948367845855 , 17.5216359599053 , 0.0323513175268276 , 0.0267216806566911 , 0.0252897989265933 , 0.0211748970259063 , 0.0192967619764282 , 0.0298868721235403 , 0.026846492561752 , 0.0282265728603356 , 0.0274643497911105 , 0.0206173186487346 , 0.930953610229856 , 1.05807449426449 , 58.6261672319115 , 1.0959476696141 , 48.4897275078371 , 79.8968117041453 , 2.03012275630195 , 2.33576352581696 , 2.60266175740213 , 1.24700542123355 , 3.06884098418713 , 1 , 0 , 0 , 0 , 0 , 1.45807445678444 , 3.53395235847141 , 1.75257486371618 , 2.9795030911447 , 4.00199168664975 , 1.97473349200058 , 4.74196793795403 , 4.72730891245437 , 0 , 0 , 14826.4074143275 , 1.24609526886412 , 1.18827514220571 , 1.09824115488565 , 1245886.83942348 , 6282282.69560999 , 6118827.67237203 , 9531496.10651471 , 8693813.50295679 , 32.948697875027 , 17.1705785135149 , 13.0971274404015 , 3.74255065304761 , 1.36231655867486 , 1.92101352688469 , 3.8941723138427 , 0.898745338298322 , 0.782806406356795 , 0.817631734201507 , 0.705822656618514 , 43.3820783577107] newcases = [] case = {} i=0 for uncertainty in uncertainties: print uncertainty.name
''' Created on 29 sep. 2011 @author: jhkwakkel ''' import numpy as np import matplotlib.pyplot as plt from expWorkbench import load_results from analysis import prim from expWorkbench import EMAlogging from analysis.graphs import envelopes EMAlogging.log_to_stderr(EMAlogging.INFO) results = load_results(r'C:\workspace\EMA-workbench\models\TFSC_all_policies.cPickle') envelopes(results, column='policy', categories=['adaptive policy', 'ap with op']) #exp, res = results # ##get out only the results related to the last policy #exp, res = results # #logical = exp['policy'] == 'adaptive policy' #exp = exp[logical] # #temp_res = {} #for key, value in res.items():
ParameterUncertainty((10, 100), "normal contact rate region 1"), ParameterUncertainty((10, 200), "normal contact rate region 2"), ] def model_init(self, policy, kwargs): """initializes the model""" try: self.modelFile = policy["file"] except KeyError: logging.warning("key 'file' not found in policy") super(FluModel, self).model_init(policy, kwargs) if __name__ == "__main__": logging.log_to_stderr(logging.INFO) model = FluModel(r"..\..\models\flu", "fluCase") ensemble = SimpleModelEnsemble() ensemble.set_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.add_policies(policies) ensemble.parallel = True # turn on parallel processing
import numpy as np import matplotlib.pyplot as plt import analysis.prim as prim from expWorkbench import load_results import expWorkbench.EMAlogging as EMAlogging #perform_prim logs information to the logger EMAlogging.log_to_stderr(level=EMAlogging.INFO) def classify(data): #get the output for deceased population result = data['deceased population region 1'] #make an empty array of length equal to number of cases classes = np.zeros(result.shape[0]) #if deceased population is higher then 1.000.000 people, classify as 1 classes[result[:, -1] > 1000000] = 1 return classes #load data results = load_results(r'../analysis/1000 flu cases.cPickle') experiments, results = results #extract results for 1 policy logicalIndex = experiments['policy'] == 'no policy' newExperiments = experiments[ logicalIndex ] newResults = {} for key, value in results.items():
def perform_loop_knockout(): unique_edges = [['In Goods', 'lost'], ['loss unprofitable extraction capacity', 'decommissioning extraction capacity'], ['production', 'In Goods'], ['production', 'lost'], ['production', 'Supply'], ['Real Annual Demand', 'substitution losses'], ['Real Annual Demand', 'price elasticity of demand losses'], ['Real Annual Demand', 'desired extraction capacity'], ['Real Annual Demand', 'economic demand growth'], ['average recycling cost', 'relative market price'], ['recycling fraction', 'lost'], ['commissioning recycling capacity', 'Recycling Capacity Under Construction'], ['maximum amount recyclable', 'recycling fraction'], ['profitability recycling', 'planned recycling capacity'], ['relative market price', 'price elasticity of demand losses'], ['constrained desired recycling capacity', 'gap between desired and constrained recycling capacity'], ['profitability extraction', 'planned extraction capacity'], ['commissioning extraction capacity', 'Extraction Capacity Under Construction'], ['desired recycling', 'gap between desired and constrained recycling capacity'], ['Installed Recycling Capacity', 'decommissioning recycling capacity'], ['Installed Recycling Capacity', 'loss unprofitable recycling capacity'], ['average extraction costs', 'profitability extraction'], ['average extraction costs', 'relative attractiveness recycling']] unique_cons_edges = [['recycling', 'recycling'], ['recycling', 'supply demand ratio'], ['decommissioning recycling capacity', 'recycling fraction'], ['returns to scale', 'relative attractiveness recycling'], ['shortage price effect', 'relative price last year'], ['shortage price effect', 'profitability extraction'], ['loss unprofitable extraction capacity', 'loss unprofitable extraction capacity'], ['production', 'recycling fraction'], ['production', 'constrained desired recycling capacity'], ['production', 'new cumulatively recycled'], ['production', 'effective fraction recycled of supplied'], ['loss unprofitable recycling capacity', 'recycling fraction'], ['average recycling cost', 'loss unprofitable recycling capacity'], ['recycling fraction', 'new cumulatively recycled'], ['substitution losses', 'supply demand ratio'], ['Installed Extraction Capacity', 'Extraction Capacity Under Construction'], ['Installed Extraction Capacity', 'commissioning extraction capacity'], ['Installed Recycling Capacity', 'Recycling Capacity Under Construction'], ['Installed Recycling Capacity', 'commissioning recycling capacity'], ['average extraction costs', 'profitability extraction']] # CONSTRUCTING THE ENSEMBLE AND SAVING THE RESULTS EMAlogging.log_to_stderr(EMAlogging.INFO) results = load_results(r'base.cPickle') # GETTING OUT THOSE BEHAVIOURS AND EXPERIMENT SETTINGS # Indices of a number of examples, these will be looked at. runs = [526,781,911,988,10,780,740,943,573,991] VOI = 'relative market price' results_of_interest = experiment_settings(results,runs,VOI) cases_of_interest = experiments_to_cases(results_of_interest[0]) behaviour_int = results_of_interest[1][VOI] # CONSTRUCTING INTERVALS OF ATOMIC BEHAVIOUR PATTERNS ints = intervals(behaviour_int,False) # GETTING OUT ONLY THOSE OF MAXIMUM LENGTH PER BEHAVIOUR max_intervals = intervals_interest(ints) # THIS HAS TO DO WITH THE MODEL FORMULATION OF THE SWITCHES/VALUES double_list = [6,9,11,17,19] indCons = len(unique_edges) # for elem in unique_cons_edges: # unique_edges.append(elem) current = os.getcwd() for beh_no in range(0,10): # beh_no = 0 # Varies between 0 and 9, index style. interval = max_intervals[beh_no] rmp = behaviour_int[beh_no] # rmp = rmp[interval[0]:interval[1]] x = range(0,len(rmp)) fig = plt.figure() ax = fig.add_subplot(111) vensim.be_quiet() # for loop_index in range(7,8): for loop_index in range(1,len(unique_edges)+1): if loop_index-indCons > 0: model_location = current + r'\Models\Consecutive\Metals EMA.vpm' elif loop_index == 0: model_location = current + r'\Models\Base\Metals EMA.vpm' else: model_location = current + r'\Models\Switches\Metals EMA.vpm' serie = run_interval(model_location,loop_index, interval,'relative market price', unique_edges,indCons,double_list, cases_of_interest[beh_no]) if serie.shape != rmp.shape: EMAlogging.info('Loop %s created a floating point error' % (loop_index)) EMAlogging.info('Caused by trying to switch %s' % (unique_edges[loop_index-1])) if serie.shape == rmp.shape: ax.plot(x,serie,'b') # data = np.zeros(rmp.shape[0]) # data[0:serie.shape[0]] = serie # ax.plot(x,data,'b') ax.plot(x,rmp,'r') ax.axvspan(interval[0]-1,interval[1], facecolor='lightgrey', alpha=0.5) f_name = 'switched unique edges only'+str(beh_no) plt.savefig(f_name)
plt.xlim(0, 801) color = ['grey', 'lightgrey'] for i, int in enumerate(intervals): no = np.mod(i, len(color)) begin = int[0] end = int[1] plt.axvspan(begin, end, facecolor=color[no], alpha=0.5) plt.show() if __name__ == "__main__": # CONSTRUCTING THE ENSEMBLE AND SAVING THE RESULTS EMAlogging.log_to_stderr(EMAlogging.DEBUG) # # model = ScarcityModel(r'D:\tbm-g367\workspace\EMA workbench\src\sandbox\sils',"scarcity") # # ensemble = ModelEnsemble() # ensemble.set_model_structure(model) ## ensemble.parallel = True # results = ensemble.perform_experiments(1000) # save_results(results, r'base.cPickle') results = load_results(r'base.cPickle') # PLOTS FOR ENSEMBLE # fig = make_interactive_plot(results, outcomes=['relative market price'], type='lines') # fig = lines(results, outcomes = ['relative market price'], density=True, hist=False) # plt.show()
''' Created on 20 sep. 2011 .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> ''' import numpy as np import matplotlib.pyplot as plt from analysis.graphs import multiplot_scatter, multiplot_density, multiplot_lines from expWorkbench.util import load_results import expWorkbench.EMAlogging as logging logging.log_to_stderr(level=logging.DEFAULT_LEVEL) #load the data experiments, results = load_results(r'..\..\src\analysis\1000 flu cases.cPickle') #transform the results to the required format newResults = {} #get time and remove it from the dict time = results.pop('TIME') for key, value in results.items(): if key == 'deceased population region 1': newResults[key] = value[:,-1] #we want the end value else: # we want the maximum value of the peak newResults['max peak'] = np.max(value, axis=1) # we want the time at which the maximum occurred
"""Method for retrieving output after a model run """ return self.result def optimize(self, case, policy): """method called when using the model in an optimization context this method should return a single value that represents the performance of the policy params are the same as for run model """ raise NotImplementedError def reset_model(self): """Method for reseting the model to its initial state before runModel was called""" self.modelInterface.resetModel() if __name__ == '__main__': logger = logging.log_to_stderr(logging.DEBUG) # emailHander = logging.TlsSMTPHandler(("smtp.gmail.com", 587), # '*****@*****.**', # ['*****@*****.**'], # 'finished!', # ('*****@*****.**', 'password')) # emailHander.setLevel(logging.WARNING) # logger.addHandler(emailHander) model = ElectTransEMA(r'C:\workspace\ElectTransEMA\workingDirectory', "test") ensemble = SimpleModelEnsemble() ensemble.set_model_structure(model) ensemble.parallel=True results = ensemble.perform_experiments(10)