Example #1
0
    def run_model(self, case):
        
        for key, value in self.policy.items():
            vensim.set_value(key, value)
        
        switches = case.pop("preference switches")

        case["SWITCH preference for MIC"] = switches[0]
        case["SWITCH preference for expected cost per MWe"]= switches[1]
        case["SWITCH preference against unknown"]= switches[2]
        case["SWITCH preference for expected progress"]= switches[3]
        case["SWITCH preference against specific CO2 emissions"]= switches[4]
            
        for key, value in case.items():
            vensim.set_value(key, value)
        ema_logging.debug("model parameters set successfully")
        
        ema_logging.debug("run simulation, results stored in " + self.workingDirectory+self.resultFile)
        try:
            vensim.run_simulation(self.workingDirectory+self.resultFile)
        except VensimError:
            raise

        results = {}
        error = False
        for output in self.outcomes:
            ema_logging.debug("getting data for %s" %output.name)
            result = vensim.get_data(self.workingDirectory+self.resultFile, 
                              output.name 
                              )
            ema_logging.debug("successfully retrieved data for %s" %output.name)
            if not result == []:
                if result.shape[0] != self.runLength:
                    a = np.zeros((self.runLength))
                    a[0:result.shape[0]] = result
                    result = a
                    error = True
            
            else:
                result = result[0::self.step]
            try:
                results[output.name] = result
            except ValueError:
                print "what"

        a = results.keys()
        for output in self.activation:
            value = results[output.name]
            time = results["TIME"]
            activationTimeStep = time[value>0]
            if activationTimeStep.shape[0] > 0:
                activationTimeStep = activationTimeStep[0]
            else:
                activationTimeStep = np.array([0])
            results[output.name] = activationTimeStep
            
        
        self.output = results   
        if error:
            raise CaseError("run not completed", case) 
Example #2
0
    def run_model(self, case):
            
        for key, value in case.items():
            vensim.set_value(key, value)
        ema_logging.debug("model parameters set successfully")
        
        ema_logging.debug("run simulation, results stored in " + self.workingDirectory+self.resultFile)
        try:
            vensim.run_simulation(self.workingDirectory+self.resultFile)
        except VensimError:
            raise

        results = {}
        error = False
        for output in self.outcomes:
            ema_logging.debug("getting data for %s" %output.name)
            result = vensim.get_data(self.workingDirectory+self.resultFile, 
                              output.name 
                              )
            ema_logging.debug("successfully retrieved data for %s" %output.name)
            if not result == []:
                if result.shape[0] != self.runLength:
                    a = np.zeros((self.runLength))
                    a[0:result.shape[0]] = result
                    result = a
                    error = True
            
            else:
                result = result[0::self.step]
            try:
                results[output.name] = result
            except ValueError:
                print "what"
    
        self.output = results   
        if error:
            raise CaseError("run not completed", case) 
Example #3
0
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
Example #5
0
class EnergyTrans(VensimModelStructureInterface):
    def __init__(self, workingDirectory, name):
        """interface to the model"""
        super(EnergyTrans, self).__init__(workingDirectory, name)

        self.modelFile = r'\CESUN_optimized_new.vpm'

        #outcomes
        self.outcomes.append(
            Outcome('total fraction new technologies', time=True))
        self.outcomes.append(Outcome('total capacity installed', time=True))

        #Initial values
        self.uncertainties.append(
            ParameterUncertainty((14000, 16000), "ini cap T1"))  #
        self.uncertainties.append(ParameterUncertainty((1, 2),
                                                       "ini cap T2"))  #
        self.uncertainties.append(ParameterUncertainty((1, 2),
                                                       "ini cap T3"))  #
        self.uncertainties.append(ParameterUncertainty((1, 2),
                                                       "ini cap T4"))  #
        self.uncertainties.append(
            ParameterUncertainty((500000, 1500000), "ini cost T1"))  #1000000
        self.uncertainties.append(
            ParameterUncertainty((5000000, 10000000), "ini cost T2"))  #8000000
        self.uncertainties.append(
            ParameterUncertainty((5000000, 10000000), "ini cost T3"))  #8000000
        self.uncertainties.append(
            ParameterUncertainty((5000000, 10000000), "ini cost T4"))  #8000000
        self.uncertainties.append(
            ParameterUncertainty((5000000, 10000000), "ini cum decom cap T1"))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "ini cum decom cap T2"))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "ini cum decom cap T3"))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "ini cum decom cap T4"))
        self.uncertainties.append(
            ParameterUncertainty(
                (1, 5), "average planning and construction period T1"))
        self.uncertainties.append(
            ParameterUncertainty(
                (1, 5), "average planning and construction period T2"))
        self.uncertainties.append(
            ParameterUncertainty(
                (1, 5), "average planning and construction period T3"))
        self.uncertainties.append(
            ParameterUncertainty(
                (1, 5), "average planning and construction period T4"))
        self.uncertainties.append(
            ParameterUncertainty((0.85, 0.95), "ini PR T1"))
        self.uncertainties.append(
            ParameterUncertainty((0.7, 0.95), "ini PR T2"))
        self.uncertainties.append(
            ParameterUncertainty((0.7, 0.95), "ini PR T3"))
        self.uncertainties.append(
            ParameterUncertainty((0.7, 0.95), "ini PR T4"))

        #Plain Parametric Uncertainties
        self.uncertainties.append(ParameterUncertainty((30, 50),
                                                       "lifetime T1"))
        self.uncertainties.append(ParameterUncertainty((15, 20),
                                                       "lifetime T2"))
        self.uncertainties.append(ParameterUncertainty((15, 20),
                                                       "lifetime T3"))
        self.uncertainties.append(ParameterUncertainty((15, 20),
                                                       "lifetime T4"))
        #
        #        #One uncertain development over time -- smoothed afterwards
        self.uncertainties.append(
            ParameterUncertainty((0.03, 0.035), "ec gr t1"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t2"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t3"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t4"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t5"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t6"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t7"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t8"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t9"))  #0.03
        self.uncertainties.append(
            ParameterUncertainty((-0.01, 0.03), "ec gr t10"))  #0.03

        #Uncertainties in Random Functions
        self.uncertainties.append(
            ParameterUncertainty((0.9, 1), "random PR min"))
        self.uncertainties.append(
            ParameterUncertainty((1, 1.1), "random PR max"))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "seed PR T1", integer=True))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "seed PR T2", integer=True))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "seed PR T3", integer=True))
        self.uncertainties.append(
            ParameterUncertainty((1, 100), "seed PR T4", integer=True))

        #Uncertainties in Preference Functions
        self.uncertainties.append(
            ParameterUncertainty((2, 5), "absolute preference for MIC"))
        self.uncertainties.append(
            ParameterUncertainty(
                (1, 3), "absolute preference for expected cost per MWe"))
        self.uncertainties.append(
            ParameterUncertainty((2, 5),
                                 "absolute preference against unknown"))
        self.uncertainties.append(
            ParameterUncertainty((1, 3),
                                 "absolute preference for expected progress"))
        self.uncertainties.append(
            ParameterUncertainty(
                (2, 5), "absolute preference against specific CO2 emissions"))
        #TOEVOEGEN SWITCHES ZODAT BOVENSTAANDE CRITERIA WEL OF NIET GEBRUIKT WORDEN...
        self.uncertainties.append(
            CategoricalUncertainty((0, 1),
                                   "SWITCH preference for MIC",
                                   default=1))
        self.uncertainties.append(
            CategoricalUncertainty(
                (0, 1),
                "SWITCH preference for expected cost per MWe",
                default=0))
        self.uncertainties.append(
            CategoricalUncertainty((0, 1),
                                   "SWITCH preference against unknown",
                                   default=0))
        self.uncertainties.append(
            CategoricalUncertainty((0, 1),
                                   "SWITCH preference for expected progress",
                                   default=0))
        self.uncertainties.append(
            CategoricalUncertainty(
                (0, 1),
                "SWITCH preference against specific CO2 emissions",
                default=0))
        #Uncertainties DIE NOG AANGEPAST MOETEN WORDEN
        self.uncertainties.append(
            ParameterUncertainty((1, 2),
                                 "performance expected cost per MWe T1"))
        self.uncertainties.append(
            ParameterUncertainty((1, 5),
                                 "performance expected cost per MWe T2"))
        self.uncertainties.append(
            ParameterUncertainty((1, 5),
                                 "performance expected cost per MWe T3"))
        self.uncertainties.append(
            ParameterUncertainty((1, 5),
                                 "performance expected cost per MWe T4"))
        self.uncertainties.append(
            ParameterUncertainty((4, 5), "performance CO2 avoidance T1"))
        self.uncertainties.append(
            ParameterUncertainty((1, 5), "performance CO2 avoidance T2"))
        self.uncertainties.append(
            ParameterUncertainty((1, 5), "performance CO2 avoidance T3"))
        self.uncertainties.append(
            ParameterUncertainty((1, 5), "performance CO2 avoidance T4"))

        #        #Switches op technologies
        self.uncertainties.append(
            CategoricalUncertainty((0, 1), "SWITCH T3", default=1))
        self.uncertainties.append(
            CategoricalUncertainty((0, 1), "SWITCH T4", default=1))
        self.uncertainties.append(
            CategoricalUncertainty(([(0, 0, 0, 0, 1), (0, 0, 0, 1, 0),
                                     (0, 0, 0, 1, 1), (0, 0, 1, 0, 0),
                                     (0, 0, 1, 0, 1), (0, 0, 1, 1, 0),
                                     (0, 0, 1, 1, 1), (0, 1, 0, 0, 0),
                                     (0, 1, 0, 0, 1), (0, 1, 0, 1, 0),
                                     (0, 1, 0, 1, 1), (0, 1, 1, 0, 0),
                                     (0, 1, 1, 0, 1), (0, 1, 1, 1, 0),
                                     (0, 1, 1, 1, 1), (1, 0, 0, 0, 0),
                                     (1, 0, 0, 0, 1), (1, 0, 0, 1, 0),
                                     (1, 0, 0, 1, 1), (1, 0, 1, 0, 0),
                                     (1, 0, 1, 0, 1), (1, 0, 1, 1, 0),
                                     (1, 0, 1, 1, 1), (1, 1, 0, 0, 0),
                                     (1, 1, 0, 0, 1), (1, 1, 0, 1, 0),
                                     (1, 1, 0, 1, 1), (1, 1, 1, 0, 0),
                                     (1, 1, 1, 0, 1), (1, 1, 1, 1, 0),
                                     (1, 1, 1, 1, 1)]), "preference switches"))

        #switch for addfactor activation
#        self.uncertainties.append(CategoricalUncertainty((0,1), "switchaddfactorco2", default = 1))

    def model_init(self, policy, kwargs):
        try:
            self.modelFile = policy['file']
        except:
            logging.debug("no policy specified")
        super(EnergyTrans, self).model_init(policy, kwargs)

    def run_model(self, case):
        switches = case.pop("preference switches")

        case["SWITCH preference for MIC"] = switches[0]
        case["SWITCH preference for expected cost per MWe"] = switches[1]
        case["SWITCH preference against unknown"] = switches[2]
        case["SWITCH preference for expected progress"] = switches[3]
        case["SWITCH preference against specific CO2 emissions"] = switches[4]

        if np.sum(switches) == 0:
            print "sifir olan cikti haci!"

        for key, value in case.items():
            vensim.set_value(key, value)
        ema_logging.debug("model parameters set successfully")

        ema_logging.debug("run simulation, results stored in " +
                          self.workingDirectory + self.resultFile)
        try:
            vensim.run_simulation(self.workingDirectory + self.resultFile)
        except VensimError as e:
            raise

        results = {}
        error = False
        for output in self.outcomes:
            ema_logging.debug("getting data for %s" % output.name)
            result = vensim.get_data(self.workingDirectory + self.resultFile,
                                     output.name)
            ema_logging.debug("successfully retrieved data for %s" %
                              output.name)
            if not result == []:
                if result.shape[0] != self.runLength:
                    a = np.zeros((self.runLength))
                    a[0:result.shape[0]] = result
                    result = a
                    error = True

            else:
                result = result[0::self.step]
            try:
                results[output.name] = result
            except ValueError as e:
                print "what"


#        for output in self.activation:
#            value = results[output.name]
#            time = results["TIME"]
#            activationTimeStep = time[value>0]
#            if len(activationTimeStep) > 0:
#                activationTimeStep = activationTimeStep[0]
#            else:
#                activationTimeStep = np.array([0])
##            if activationTimeStep.shape[0] > 0:
##                activationTimeStep = activationTimeStep
##            else:
##                activationTimeStep = np.array([0])
#            results[output.name] = activationTimeStep

        self.output = results
        if error:
            raise CaseError("run not completed", case)
Example #6
0
                                         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 dominance_distance(base,interval_series):
    distances = []

    # For every series with a loop switched off,
    # the distance to the base series is constructed.
    for series in interval_series:
        comp = np.array([base,
                         series])
        out = dg.distance_gonenc(comp)
        distance = out[0]
Example #7
0
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
Example #8
0
'''
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"