Exemplo n.º 1
0
    def test_overhead(self):
        print "Testing overhead..."

        #import cProfile
        #import StringIO
        #import pstats
        for num_timesteps in self.num_timesteps:
            for num_nodes in self.num_nodes:

                #pr = cProfile.Profile(timer=self.timer)
                #pr.enable()

                print "%s timesteps *  %s nodes" % (num_timesteps, num_nodes)
                #Create a simulator object which will be run.
                s = Simulator()

                #Set the timesteps of the simulator.
                timesteps = range(num_timesteps)
                s.set_timesteps(timesteps)

                #Create the network with initial data
                n = Network(name="example network %s nodes %s timesteps" %
                            (num_nodes, num_timesteps))

                for node_num in range(num_nodes):
                    n.add_node(
                        DummyNode(x=node_num,
                                  y=node_num,
                                  name="Node number %s" % node_num))

                #Set the simulator's network to this network.
                s.network = n

                #Create an instance of the deficit allocation engine.
                empty_engine = EmptyEngine(n)

                #add the engine to the simulator
                s.add_engine(empty_engine)

                t = time.time()
                #start the simulator

                s.start()

                self.result_matrix[num_timesteps][num_nodes] = (time.time() -
                                                                t)

                #pr.disable()
                #s = StringIO.StringIO()
                #sortby = 'cumulative'
                #ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
                #ps.print_stats()
                #with open('profile_%s_%s.txt' % (num_timesteps, num_nodes), 'w') as profile_result:
                #    profile_result.write(s.getvalue())

        overhead_result = open('overhead_result.json', 'w')

        overhead_result.write(json.dumps(self.result_matrix))
Exemplo n.º 2
0
    def test_engine_error(self):
        """ Test an engine can raises a non-StopIteration error.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        s.add_engine(TypeErrorEngine(None, error_on_iteration=1))
        s.add_engine(CountEngine(None))
        s.timesteps = [
            0,
        ]

        with self.assertRaises(TypeError):
            s.start()
Exemplo n.º 3
0
    def test_engine_error(self):
        """ Test an engine can raises a non-StopIteration error.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        s.add_engine(TypeErrorEngine(None, error_on_iteration=1))
        s.add_engine(CountEngine(None))
        s.timesteps = [0, ]

        with self.assertRaises(TypeError):
            s.start()
Exemplo n.º 4
0
    def test_dict_overwrite(self):
        """
            Test to ensure that dictionaries and other objects contained in history
            are not overwritten by maintaining references to the same objects.
        """
        print("Testing...")
        network = Network("History Test Network")
        network.add_node(HistoryTestNode(x=0, y=0, name="Test Node"))

        s = Simulator()
        s.network = network
        s.timesteps = ['a', 'b', 'c']


        s.start()
        
        assert s.network.nodes[0]._history['property_dict'] == [{'test': 'a'}, {'test': 'b'}, {'test': 'c'}]
Exemplo n.º 5
0
    def test_overhead(self):
        print("Testing overhead...")

        #import cProfile
        #import StringIO
        #import pstats
        for num_timesteps in self.num_timesteps:
            for num_nodes in self.num_nodes:

                #pr = cProfile.Profile(timer=self.timer)
                #pr.enable()

                print("%s timesteps *  %s nodes"%(num_timesteps, num_nodes))
                #Create a simulator object which will be run.
                s = Simulator()

                #Set the timesteps of the simulator.
                timesteps = range(num_timesteps)
                s.set_timesteps(timesteps)

                #Create the network with initial data
                n = Network(name="example network %s nodes %s timesteps"%(num_nodes, num_timesteps))

                for node_num in range(num_nodes):
                    n.add_node(DummyNode(x=node_num, y=node_num, name="Node number %s"%node_num))

                #Set the simulator's network to this network.
                s.network = n

                #Create an instance of the deficit allocation engine.
                empty_engine = EmptyEngine(n)

                #add the engine to the simulator
                s.add_engine(empty_engine)

                t = time.time()
                #start the simulator

                s.start()

                self.result_matrix[num_timesteps][num_nodes] = (time.time()-t)

                #pr.disable()
                #s = StringIO.StringIO()
                #sortby = 'cumulative'
                #ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
                #ps.print_stats()
                #with open('profile_%s_%s.txt' % (num_timesteps, num_nodes), 'w') as profile_result:
                #    profile_result.write(s.getvalue())

        overhead_result = open('overhead_result.json', 'w')

        overhead_result.write(json.dumps(self.result_matrix))
Exemplo n.º 6
0
    def test_engine_stopping_iteration(self):
        """ Test an engine terminating iteration

        The engine must raise a `StopIteration` exception.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        stopper_engine = StopperEngine(None, stop_on_iteration=2)
        s.add_engine(stopper_engine)
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [
            0,
        ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
        assert stopper_engine.iteration == 2
Exemplo n.º 7
0
    def test_engine_iteration(self):
        """ Test setting the global number of iterations in a simulator. """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [
            0,
        ]
        s.start()

        assert engine.iteration == 5
        assert engine.run_count == 5
Exemplo n.º 8
0
    def test_default_iteration(self):
        """ Test that the default simulation `max_iterations` is one. """
        s = Simulator()
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [
            0,
        ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
Exemplo n.º 9
0
    def test_default_iteration(self):
        """ Test that the default simulation `max_iterations` is one. """
        s = Simulator()
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [0, ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
Exemplo n.º 10
0
    def test_engine_iteration(self):
        """ Test setting the global number of iterations in a simulator. """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [0, ]
        s.start()

        assert engine.iteration == 5
        assert engine.run_count == 5
Exemplo n.º 11
0
    def test_engine_stopping_iteration(self):
        """ Test an engine terminating iteration

        The engine must raise a `StopIteration` exception.
        """
        s = Simulator(max_iterations=5)
        s.network = Network("Iteration test network")

        stopper_engine = StopperEngine(None, stop_on_iteration=2)
        s.add_engine(stopper_engine)
        engine = CountEngine(None)
        s.add_engine(engine)

        s.timesteps = [0, ]
        s.start()

        assert engine.iteration == 1
        assert engine.run_count == 1
        assert stopper_engine.iteration == 2
Exemplo n.º 12
0
    def test_dict_overwrite(self):
        """
            Test to ensure that dictionaries and other objects contained in history
            are not overwritten by maintaining references to the same objects.
        """
        print("Testing...")
        network = Network("History Test Network")
        network.add_node(HistoryTestNode(x=0, y=0, name="Test Node"))

        s = Simulator()
        s.network = network
        s.timesteps = ['a', 'b', 'c']

        s.start()

        assert s.network.nodes[0]._history['property_dict'] == [{
            'test': 'a'
        }, {
            'test': 'b'
        }, {
            'test': 'c'
        }]
Exemplo n.º 13
0
                 'oct':  100.0,
                 'nov':  100.0,
                 'dec':  100.0,
                }   

# Network
network = ReservoirSystem(name="Allocation in a river system")
network.add_nodes(J1,J2,J3,J4,J5,J6,J7,U1,F1,F2,F3,F4,E1)
network.add_links(J1_J2,J2_J3,J2_J4,J3_U1,J3_F4,J4_J5,J5_J6,J6_J7,J7_E1,J5_F1,J6_F2,J7_F3
)


network.timestep = 86400 * 30  # one month

# Simulation

simulation = Simulator()

simulation.set_timesteps(timesteps)

simulation.network = network

engine = SimpleRouting(network)
simulation.add_engine(engine)

simulation.start()

for node in simulation.network.nodes:
    print("%s: %s"%(node.name, node._history['inflow'][0]))
simulation.network.plot('inflow')
Exemplo n.º 14
0
link2._max_flow = {0: 90.0, 1: 100.0, 2: 110.0, 3: 100.0, 4: 90.0, 5: 100.0}

link3._max_flow = {0: 310.0, 1: 300.0, 2: 300.0, 3: 300.0, 4: 310.0, 5: 300.0}

link4._max_flow = {0: 220.0, 1: 250.0, 2: 200.0, 3: 230.0, 4: 240.0, 5: 250.0}

link5._max_flow = {0: 30.0, 1: 40.0, 2: 20.0, 3: 30.0, 4: 30.0, 5: 10.0}

# Define network

network = WaterResourcesSystem(name='Priority based')
network.add_nodes(SR, IRR, URB, J1, In, Out)
network.add_links(link1, link2, link3, link4, link5)

# Run simulation
simulation = Simulator()

simulation.set_timesteps(timesteps)

simulation.network = network

engine = PriorityBased(network)

simulation.add_engine(engine)

simulation.start()

import seaborn
import matplotlib.pyplot as plt

plt.figure(1)
Exemplo n.º 15
0
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with the Pyomo Plugin Demo Suite.  If not, see <http://www.gnu.org/licenses/>.

#Author: Majed Khadem, Silvia Padula, Khaled Mohamed, Stephen Knox, Julien Harou

from components import UrbanNode, AgriculturalNode, SurfaceReservoir, Junction, RiverSection,CostMinimisationNetwork
from engines import PyomoAllocation

from pynsim import Simulator

#create the simulator to run
s = Simulator()

timesteps = ["1", "2", "3", "4", "5", "6"]

#add the timesteps to the simulator. The simulator may use these timesteps
#to access timeseries data in the network.
s.set_timesteps(timesteps)

n = CostMinimisationNetwork(name="Cost Minimisation network")

#Create the surface reservoir with a name, x and y coordinate.
sr1 = SurfaceReservoir(x=1,  y=2,   name="SR1")
#Add internal data to the reservoir. Internal data is signifiied by having an
#underscore before the start of the variable name. 
#The surface reservoir type has an initial storage, inflow, min storage and max storage
#so these are specified here.
Exemplo n.º 16
0
n_YRB = Network('Yarmouk_reservoirs_network')
for us_n in nodes:
    n_YRB.add_node(us_n)

for idx in reservoirs.idx:
    if idx in W.keys():
        n_YRB.add_link(W[idx])
    if idx in C.keys():
        n_YRB.add_link(C[idx])
    if idx in U.keys():
        n_YRB.add_link(U[idx])
n_YRB.add_institutions(Jordan_gvt, Israel_gvt, Syria_gvt)

# Simulator object that will be run
s = Simulator(network=n_YRB)

# Timesteps of the simulator
t = range(0, T)  # months
s.set_timesteps(t)

# Engines
reservoirs_management = SimulationOfSyria(target=Syria_gvt)
treaty = TreatyOfPeace(target=Jordan_gvt)
pumping = ExchangeWithTiberias(target=Israel_gvt)
s.add_engine(reservoirs_management)
# if test alpha & beta possibilities
s.add_engine(treaty)
s.add_engine(pumping)

# Simulation
Exemplo n.º 17
0
import random
import time
from pynsim import Simulator, Network, Node, Link, Institution, Engine

#Create a simulator object which will be run.
s = Simulator()

#Set the timesteps of the simulator.
timesteps = range(100)
s.set_timesteps(timesteps)

#Create the network with initial data
n = Network(name="example network")

num_nodes = 10000
for node_num in range(num_nodes):
    n.add_node(Node(x=node_num, y=node_num, name="Node number %s"%node_num))

for link_num in range(num_nodes):
    start_node = random.choice(n.nodes)
    #Make sure the start and end node aren't the same
    while True:
        end_node = random.choice(n.nodes)
        if end_node.name != start_node.name:
            break
    n.add_link(Link(name="Link number %s"%link_num, start_node=start_node, end_node=end_node))

num_institutions = 100
for inst_num in range(num_institutions):
    i = Institution(name="Instution Number %s"%inst_num)
    i.add_nodes(*n.nodes)
Exemplo n.º 18
0
from components import CitrusFarm, VegetableFarm, SurfaceReservoir, RiverSection
from components import WaterDepartment, IrrigationDecisionMaker
from engines import DeficitAllocation

from pynsim import Simulator, Network

#Create a simulator object which will be run.
s = Simulator()

#Set the timesteps of the simulator.
timesteps = [
    "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01"
]
s.set_timesteps(timesteps)

#Create the network with initial data
n = Network(name="example network")

#set some initial data on this network.
n.incoming_water_qty = {
    "2014-01-01": 50,
    "2014-02-01": 61,
    "2014-03-01": 65,
    "2014-04-01": 45,
    "2014-05-01": 30
}

#Add nodes to the network

#All nodes have an x, y and name. The surface reservoir also has 'release' which
#can be set on creation or after.
Exemplo n.º 19
0
def run_simulation(diversion_pos, discharge_data, draw=False, queue=None):
    """Run the simulation based on a given position of diversion nodes.
    A fixed river network structure is assumed here.

    N1
    |
    N2  N4
    |   |
    N3  N5
     \  /
      N6
      |
      N7   N9
      |    |
      N8  N10
      |  /
      N11
      |
      N12
    """

    N1 = RiverNode(x=0, y=7, name='N1')
    N1.dQdx = copy.deepcopy(discharge_data['N1'])
    N2 = RiverNode(x=0, y=6, name='N2')
    N2.dQdx = copy.deepcopy(discharge_data['N2'])
    N3 = RiverNode(x=0, y=5, name='N3')
    N3.dQdx = copy.deepcopy(discharge_data['N3'])
    N4 = RiverNode(x=2, y=6, name='N4')
    N4.dQdx = copy.deepcopy(discharge_data['N4'])
    N5 = RiverNode(x=2, y=5, name='N5')
    N5.dQdx = copy.deepcopy(discharge_data['N5'])
    N6 = RiverNode(x=1, y=4, name='N6')
    N6.dQdx = copy.deepcopy(discharge_data['N6'])
    N7 = RiverNode(x=1, y=3, name='N7')
    N7.dQdx = copy.deepcopy(discharge_data['N7'])
    N8 = RiverNode(x=1, y=2, name='N8')
    N8.dQdx = copy.deepcopy(discharge_data['N8'])
    N9 = RiverNode(x=3, y=3, name='N9')
    N9.dQdx = copy.deepcopy(discharge_data['N9'])
    N10 = RiverNode(x=3, y=2, name='N10')
    N10.dQdx = copy.deepcopy(discharge_data['N10'])
    N11 = RiverNode(x=2, y=1, name='N11')
    N11.dQdx = copy.deepcopy(discharge_data['N11'])
    N12 = RiverNode(x=2, y=0, name='N12')
    N12.dQdx = copy.deepcopy(discharge_data['N12'])

    node_dict = {
        0: N1,
        1: N2,
        2: N3,
        3: N4,
        4: N5,
        5: N6,
        6: N7,
        7: N8,
        8: N9,
        9: N10,
        10: N11,
        11: N12
    }

    # Add a diversion node based on the position parameter
    div_node = Diversion(x=node_dict[diversion_pos].x,
                         y=node_dict[diversion_pos].y,
                         name='Diversion')
    div_node.dQdx = node_dict[diversion_pos].dQdx
    div_node.demand = 0.5

    node_dict[diversion_pos] = div_node

    L1 = GenericLink(start_node=node_dict[0], end_node=node_dict[1], name='L1')
    L2 = GenericLink(start_node=node_dict[1], end_node=node_dict[2], name='L2')
    L3 = GenericLink(start_node=node_dict[2], end_node=node_dict[5], name='L3')
    L4 = GenericLink(start_node=node_dict[3], end_node=node_dict[4], name='L4')
    L5 = GenericLink(start_node=node_dict[4], end_node=node_dict[5], name='L5')
    L6 = GenericLink(start_node=node_dict[5], end_node=node_dict[6], name='L6')
    L7 = GenericLink(start_node=node_dict[6], end_node=node_dict[7], name='L7')
    L8 = GenericLink(start_node=node_dict[7],
                     end_node=node_dict[10],
                     name='L8')
    L9 = GenericLink(start_node=node_dict[8], end_node=node_dict[9], name='L9')
    L10 = GenericLink(start_node=node_dict[9],
                      end_node=node_dict[10],
                      name='L10')
    L11 = GenericLink(start_node=node_dict[10],
                      end_node=node_dict[11],
                      name='L11')

    river_network = RiverNetwork(name='River network with diversion')

    river_network.add_nodes(*node_dict.values())
    river_network.add_links(L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11)

    if draw:
        river_network.draw(block=False)

    routing = Routing(river_network)

    simulation = Simulator()
    simulation.network = river_network
    simulation.add_engine(routing)
    simulation.set_timesteps([1])

    simulation.start()

    if queue is None:
        return simulation.network.discharge
    else:
        queue.put(simulation.network.discharge)