def run_interval(model,loop_index,interval,VOI,edges,ind_cons, double_list,case): # Load the model. vensim.load_model(model) case = copy.deepcopy(case) set_lookups(case) for key,value in case.items(): vensim.set_value(key,repr(value))
'Active causes', 'Subscripts', 'Combination Subscripts', 'Minimum value', 'Maximum value', 'Range', 'Variable type', 'Main group'] attributes = range(len(attributeNames)) attributesInterest = [3, 1, 2, 12] varTypeNames = ['All', 'Levels', 'Auxiliary', 'Data', 'Initial', 'Constant', 'Lookup', 'Group', 'Subscript Ranges', 'Constraint', 'Test Input', 'Time Base', 'Gaming'] varTypes = range(len(varTypeNames)) varTypes[0:2] = [1] # Do not look at all types varTypes[4:] = [5] # Do not look after lookup print 'Vensim file: '+ vensimRootName + vensimFileName + vensimExtension print 'CSV file: '+ vensimRootName + csvFileName print 'Converting starts...' load_model(vensimRootName + vensimFileName + vensimExtension) with open (vensimRootName + csvFileName, 'wb') as f: writer = csv.writer(f) writer.writerow(firstLine) # The first lines are written writer.writerow(secondLine) writer.writerow(thirdLine) writer.writerow(blank) writer.writerow(csvArray) for varType in varTypes: # Now get the variables per type type = varTypeNames[varType] typeNr = varType varNames = get_varnames(0, varType) for varName in varNames: # per name, look in to their attributes csvArray[0]=lineNumber # setup the line which needs to go to the csv csvArray[1]=varName
def run_interval(model,loop_index,interval,VOI,edges,ind_cons, double_list,case): # Load the model. vensim.load_model(model) case = copy.deepcopy(case) set_lookups(case) for key,value in case.items(): vensim.set_value(key,repr(value)) # print key, repr(value), vensim.get_val(key), value-vensim.get_val(key) # We run the model in game mode. step = vensim.get_val(r'TIME STEP') start_interval = interval[0]*step end_interval = interval[1]*step venDLL.command('GAME>GAMEINTERVAL|'+str(start_interval)) # Initiate the model to be run in game mode. venDLL.command("MENU>GAME") if start_interval > 0: venDLL.command('GAME>GAMEON') loop_on = 1 loop_off = 0 loop_turned_off = False while True: # Initiate the experiment of interest. # In other words set the uncertainties to the same value as in # those experiments. time = vensim.get_val(r'TIME') ema_logging.debug(time) if time ==(2000+step*interval[0]) and not loop_turned_off: loop_turned_off = True if loop_index != 0: # If loop elimination method is based on unique edge. if loop_index-1 < ind_cons: constant_value = vensim.get_val(edges[int(loop_index-1)][0]) if loop_off==1: constant_value = 0 vensim.set_value('value loop '+str(loop_index), constant_value) vensim.set_value('switch loop '+str(loop_index), loop_off) # Else it is based on unique consecutive edges. else: constant_value = vensim.get_val(edges[int(loop_index-1)][0]) if loop_off==1: constant_value = 0 # Name of constant value used does not fit loop index minus 'start of cons'-index. if loop_index-ind_cons in double_list: vensim.set_value('value cons loop '+str(loop_index-ind_cons-1), constant_value) vensim.set_value('switch cons loop '+str(loop_index-ind_cons-1), loop_off) else: vensim.set_value('value cons loop '+str(loop_index-ind_cons), constant_value) vensim.set_value('switch cons loop '+str(loop_index-ind_cons), loop_off) venDLL.command('GAME>GAMEINTERVAL|'+str(end_interval-start_interval)) elif time ==(2000+step*interval[1]) and loop_turned_off: loop_turned_off = False if loop_index != 0: # If loop elimination method is based on unique edge. if loop_index-1 < ind_cons: constant_value = 0 vensim.set_value('value loop '+str(loop_index), constant_value) vensim.set_value('switch loop '+str(loop_index), loop_on) # Else it is based on unique consecutive edges. else: constant_value = 0 # Name of constant value used does not fit loop index minus 'start of cons'-index. if loop_index-ind_cons in double_list: vensim.set_value('value cons loop '+str(loop_index-ind_cons-1), constant_value) vensim.set_value('switch cons loop '+str(loop_index-ind_cons-1), loop_on) else: vensim.set_value('value cons loop '+str(loop_index-ind_cons), constant_value) vensim.set_value('switch cons loop '+str(loop_index-ind_cons), loop_on) finalT = vensim.get_val('FINAL TIME') currentT = vensim.get_val('TIME') venDLL.command('GAME>GAMEINTERVAL|'+str(finalT - currentT)) else: break finalT = vensim.get_val('FINAL TIME') currentT = vensim.get_val('TIME') if finalT != currentT: venDLL.command('GAME>GAMEON') venDLL.command('GAME>ENDGAME') interval_series = vensim.get_data('Base.vdf',VOI) return interval_series
def run_interval(model, loop_index, interval, VOI, edges, ind_cons, double_list, uncertain_names, uncertain_values): # Load the model. vensim.load_model(model) # We don't want any screens. vensim.be_quiet() # We run the model in game mode. step = vensim.get_val(r"TIME STEP") start_interval = str(interval[0] * step) venDLL.command("GAME>GAMEINTERVAL|" + start_interval) # Initiate the model to be run in game mode. venDLL.command("MENU>GAME") while True: if vensim.get_val(r"TIME") == 2000: # Initiate the experiment of interest. # In other words set the uncertainties to the same value as in # those experiments. for i, value in enumerate(uncertain_values): name = uncertain_names[i] value = uncertain_values[i] vensim.set_value(name, value) print vensim.get_val(r"TIME") try: # Run the model for the length specified in game on-interval. venDLL.command("GAME>GAMEON") step = vensim.get_val(r"TIME STEP") if vensim.get_val(r"TIME") == (2000 + step * interval[0]): if loop_index != 0: # If loop elimination method is based on unique edge. if loop_index - 1 < ind_cons: constant_value = vensim.get_val(edges[int(loop_index - 1)][0]) vensim.set_value("value loop " + str(loop_index), constant_value) vensim.set_value("switch loop " + str(loop_index), 0) # Else it is based on unique consecutive edges. else: constant_value = vensim.get_val(edges[int(loop_index - 1)][0]) print constant_value # Name of constant value used does not fit loop index minus 'start of cons'-index. if loop_index - ind_cons in double_list: vensim.set_value("value cons loop " + str(loop_index - ind_cons - 1), constant_value) vensim.set_value("switch cons loop " + str(loop_index - ind_cons - 1), 0) else: vensim.set_value("value cons loop " + str(loop_index - ind_cons), constant_value) vensim.set_value("switch cons loop " + str(loop_index - ind_cons), 0) except venDLL.VensimWarning: # The game on command will continue to the end of the simulation and # than raise a warning. print "The end of simulation." break venDLL.finish_simulation() interval_series = vensim.get_data("Base.vdf", VOI) interval_series = interval_series[interval[0] : interval[1]] return interval_series
''' import networkx as nx import matplotlib.pyplot as plt import cPickle from expWorkbench.vensimDLLwrapper import VensimWarning try: from networkx import graphviz_layout except ImportError: raise ImportError("This example needs Graphviz and either PyGraphviz or Pydot") from expWorkbench import vensim from expWorkbench import vensimDLLwrapper as venDLL vensim.load_model(r'C:\workspace\EMA-workbench\src\sandbox\sils\MODEL.vpm') vars = venDLL.get_varnames() graph = nx.DiGraph() graph.add_nodes_from(vars) for var in vars: try: causes = venDLL.get_varattrib(var, attribute=4) #cause for cause in causes: graph.add_edge(cause, var) except VensimWarning: print var cPickle.dump(graph, open("model of Oliva.cPickle",'wb'))
''' import networkx as nx import matplotlib.pyplot as plt import cPickle from expWorkbench.vensimDLLwrapper import VensimWarning try: from networkx import graphviz_layout except ImportError: raise ImportError( "This example needs Graphviz and either PyGraphviz or Pydot") from expWorkbench import vensim from expWorkbench import vensimDLLwrapper as venDLL vensim.load_model(r'D:\epruyt\Desktop\Ageing2.vpm') #C:\workspace\EMA-workbench\src\sandbox\sils\MODEL.vpm vars = venDLL.get_varnames() graph = nx.DiGraph() graph.add_nodes_from(vars) for var in vars: try: causes = venDLL.get_varattrib(var, attribute=4) #cause for cause in causes: graph.add_edge(cause, var) except VensimWarning: print var
def run_interval(model, loop_index, interval, VOI, edges, ind_cons, double_list, uncertain_names, uncertain_values): # Load the model. vensim.load_model(model) # We don't want any screens. vensim.be_quiet() # We run the model in game mode. step = vensim.get_val(r'TIME STEP') start_interval = str(interval[0] * step) venDLL.command('GAME>GAMEINTERVAL|' + start_interval) # Initiate the model to be run in game mode. venDLL.command("MENU>GAME") while True: if vensim.get_val(r'TIME') == 2000: # Initiate the experiment of interest. # In other words set the uncertainties to the same value as in # those experiments. for i, value in enumerate(uncertain_values): name = uncertain_names[i] value = uncertain_values[i] vensim.set_value(name, value) print vensim.get_val(r'TIME') try: # Run the model for the length specified in game on-interval. venDLL.command('GAME>GAMEON') step = vensim.get_val(r'TIME STEP') if vensim.get_val(r'TIME') == (2000 + step * interval[0]): if loop_index != 0: # If loop elimination method is based on unique edge. if loop_index - 1 < ind_cons: constant_value = vensim.get_val(edges[int(loop_index - 1)][0]) vensim.set_value('value loop ' + str(loop_index), constant_value) vensim.set_value('switch loop ' + str(loop_index), 0) # Else it is based on unique consecutive edges. else: constant_value = vensim.get_val(edges[int(loop_index - 1)][0]) print constant_value # Name of constant value used does not fit loop index minus 'start of cons'-index. if loop_index - ind_cons in double_list: vensim.set_value( 'value cons loop ' + str(loop_index - ind_cons - 1), constant_value) vensim.set_value( 'switch cons loop ' + str(loop_index - ind_cons - 1), 0) else: vensim.set_value( 'value cons loop ' + str(loop_index - ind_cons), constant_value) vensim.set_value( 'switch cons loop ' + str(loop_index - ind_cons), 0) except venDLL.VensimWarning: # The game on command will continue to the end of the simulation and # than raise a warning. print "The end of simulation." break venDLL.finish_simulation() interval_series = vensim.get_data('Base.vdf', VOI) interval_series = interval_series[interval[0]:interval[1]] return interval_series
''' Created on May 30, 2012 @author: jhkwakkel ''' from expWorkbench import vensim from expWorkbench import vensimDLLwrapper as venDLL vensim.load_model(r'C:\workspace\EMA-workbench\src\sandbox\sils\MODEL.vpm') venDLL.start_simulation(True, 0, True) venDLL.continue_simulation(50) venDLL.finish_simulation() time_series = vensim.get_data('test.vdf', 'Rookies', step=1) print "blaat"