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))
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))
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()
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
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
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
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
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()
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
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
# 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) plt.subplot(2, 5, 1) plt.plot(simulation.network.nodes[0]._history['S']) plt.title('SR storage') plt.subplot(2, 5, 2) plt.plot(simulation.network.nodes[1]._history['delivery']) plt.title('IRR delivery')
#When adding these links to the network, the network's nodes will be updated also #with the link added to the starting node's 'out links' and the ending node's 'in links' n.add_links(l1, l2, l3, l4) #Set the network on the simulator s.network = n #Create an instance of the pyomo allocator engine. allocator = PyomoAllocation(n) #add the engine to the simulator. Multiple engines can be added here. The #engines will be run sequentially based on the order they were added to the #simulator s.add_engine(allocator) #start the simulator s.start() total_deficit = 0 nodes_names=[] for n in n.nodes: nodes_names.append(n.name) if n.type == 'irrigation': print("%s deficit = %s"%(n.name, n.deficit)) total_deficit += n.deficit
'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')
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) n.add_institution(i) #Set the simulator's network to this network. s.network = n class EmptyEngine(Engine): def run(self): pass #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() print "Simulation took: %s"%(time.time() - t)
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 s.start() # Create a Pandas Excel writer using XlsxWriter as the engine writer = pd.ExcelWriter('results/' + results_file, engine='xlsxwriter') for id_a, agent in enumerate(s.network.nodes + s.network.institutions): for H_data in agent.get_properties().keys(): # History H = agent.get_history(H_data) # Create a Pandas dataframe from the data H_df = pd.DataFrame({agent.name: H})
n.add_link(RiverSection(name="rs3", start_node=sr1, end_node=irr3)) #Set the simulator's network to this network. s.network = n #Add some institutions (loosely based on the Jordan project) mow = WaterDepartment("Jordan Ministry of Water") mow.add_nodes(sr1) jva = IrrigationDecisionMaker("Jordan Valley Authority") jva.add_nodes(irr1, irr2, irr3) n.add_institutions(mow, jva) #Create an instance of the deficit allocation engine. allocator = DeficitAllocation(n) #add the engine to the simulator s.add_engine(allocator) #start the simulator s.start() #Print the deficit of each node and an overall deficit figure. total_deficit = 0 for n in n.nodes: if n.type == 'irrigation': print("%s deficit = %s" % (n.name, n.deficit)) total_deficit += n.deficit print("Total deficit: %s" % (total_deficit))
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)