def read_cin_file(file): ''' read a .cin file :param file: location of the .cin file. :exception: raises a :class:`~EMAExceptions.VensimWarning` if the cin file cannot be read. ''' ema_logging.debug("executing COMMAND: SIMULATE>READCIN|"+file) try: command(r"SIMULATE>READCIN|"+file) except VensimWarning as w: ema_logging.debug(str(w)) raise w
def load_model(file): ''' load the model :param file: the location of the .vpm file to be loaded. :exception: raises a :class:`~EMAExceptions.VensimError` if the model cannot be loaded. .. note: only works for .vpm files ''' ema_logging.debug("executing COMMAND: SIMULATE>SPECIAL>LOADMODEL|"+file) try: command(r"SPECIAL>LOADMODEL|"+file) except VensimWarning as w: ema_logging.warning(str(w)) raise VensimError("vensim file not found")
def run_simulation(file): ''' Convenient function to run a model and store the results of the run in the specified .vdf file. The specified output file will be overwritten by default :param file: the location of the outputfile :exception: raises a :class:`~EMAExceptions.VensimError` if running the model failed in some way. ''' try: ema_logging.debug(" executing COMMAND: SIMULATE>RUNNAME|"+file+"|O") command("SIMULATE>RUNNAME|"+file+"|O") ema_logging.debug(r"MENU>RUN|o") command(r"MENU>RUN|o") except VensimWarning as w: ema_logging.warning((str(w))) raise VensimError(str(w))
def set_value(variable, value): ''' set the value of a variable to value current implementation only works for lookups and normal values. In case of a list, a lookup is assumed, else a normal value is assumed. See the DSS reference supplement, p. 58 for details. :param variable: name of the variable to set. :param value: the value for the variable. **note**: the value can be either a list, or an float/integer. If it is a list, it is assumed the variable is a lookup. ''' if type(value) == types.ListType: command(r"SIMULATE>SETVAL|"+variable+"("+ str(value)[1:-1] + ")") else: try: command(r"SIMULATE>SETVAL|"+variable+"="+str(value)) except VensimWarning: ema_logging.warning('variable: \'' +variable+'\' not found')
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
# 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.
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
@author: localadmin ''' from expWorkbench import vensim from expWorkbench import vensimDLLwrapper as venDLL #load model vensim.load_model(r'C:\workspace\EMA-workbench\src\sandbox\sils\MODEL.vpm') # we don't want any screens vensim.be_quiet() # we run the model in game mode, with 10 timesteps at a time # the interval can be modified even during the game, thus allowing for dynamic # interaction venDLL.command('GAME>GAMEINTERVAL|10') # initiate the model to be run in game mode venDLL.command("MENU>GAME") while True: print vensim.get_val(r'FRAC EP FOR TRAINING') print vensim.get_val(r'TIME') try: #run the model for the length specified via the game interval command venDLL.command('GAME>GAMEON') except venDLL.VensimWarning: # the game on command will continue to the end of the simulation and # than raise a warning print "blaat" break