UMWRG / pynsim

The Python Network Modelling Simulator
GNU General Public License v3.0
48 stars 19 forks source link

Allow sub-simulations #13

Open knoxsp opened 4 years ago

knoxsp commented 4 years ago

Consider a use case where you want to run multiple variations of a simulation with different inputs, for example damand scenarios.

Currently this is achieved by running multiple pynsim simulations, and managing the scenarios externally. A more neat solution would be for pynism to manage this itself.

In practice, this would means applying multiple values to a node within a time-step.

To give a pseudo-code example:

import pynsim 

class DemandNode(pynsim.Node):

    _parameters = {
       'demand' : 0
    }

    def setup(timestep):
        """
           Assume the presence of self.all_demands,
           a dictionary containing the demands for each time step. 
           If the timestep can't be found, default to 0
        """  
        self.demand = self.all_demands.get(timestep, 0)

class Supply(pynsim.Node):
    pass

The simulation would look like:

timesteps = [1, 2]
d1_demands = {
                         1: 10,
                         2: 11
                        }

d2_demands = {
                         1: 100,
                         2: 110
                        }
d1 = Demand()
d1.all_demands = d1_demands

d2 = Demand()
d2.all_demands = d2_demands
#a supply node has no data on it (in reality it would)
s1 = Supply()

l1 = pynsim.Link(d1, s1)
l2 = pynsim.Link(d2, s1)

network = Network()
network.nodes = [d1, d2, s1]
network.links    = [l1, l2]

#runs a single simulation
simulation = Simulation()
simulation.timesteps = timesteps
simulation.network = network
simulation.start()

in the new system, the simulation woulg look like:

timesteps = [1, 2]
d1_demands = {
                         1: [9, 10],
                         2: [11, 12]
                        }

d2_demands = {
                         1: [100, 101],
                         2: [110, 111]
                        }
...

#somewhere tell the simulator object: "The demand parameter on Demand nodes has 2 scenarios"
simulation.scenarios = [Demand.demand]????

#runs 2 sub-scenarios
simulation.start()
KevisPachos commented 4 years ago

Thanks Steve, it looks good to me

Jmiguel17 commented 4 years ago

For clarification, we would like to have the multi-scenarios running in pynsim in a sequential?? mode.

timesteps = [1, 2]
d1_demands = {
                         1: [9, 10],
                         2: [11, 12]
                        }

d2_demands = {
                         1: [100, 101],
                         2: [110, 111]
                        }

Pysim will solve Scenario 1:

timesteps = [1, 2]
d1_demands = {
                         1: 9,
                         2: 11
                        }

d2_demands = {
                         1: 100,
                         2: 110
                        }

Scenario 2:

timesteps = [1, 2]
d1_demands = {
                         1: 10,
                         2: 12
                        }

d2_demands = {
                         1: 100,
                         2: 110
                        }

Scenario 3:

timesteps = [1, 2]
d1_demands = {
                         1: 9,
                         2: 11
                        }

d2_demands = {
                         1: 101,
                         2: 111
                        }

Scenario 4:

timesteps = [1, 2]
d1_demands = {
                         1: 10,
                         2: 12
                        }

d2_demands = {
                         1:  101,
                         2:  111
                        }

The previous 4 scenarios will be solved sequentially?? one after the other.