def setUpClass(cls): # make Java connection cls.connection = Java_Connection() # create a static/bpr model managers this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', 'seven_links.xml') bpr_coefficients = { 0L: [1, 0, 0, 0, 1], 1L: [1, 0, 0, 0, 1], 2L: [5, 0, 0, 0, 5], 3L: [2, 0, 0, 0, 2], 4L: [2, 0, 0, 0, 2], 5L: [1, 0, 0, 0, 1], 6L: [5, 0, 0, 0, 5] } cls.model_manager = Link_Model_Manager_class(configfile, "static", cls.connection, None, "bpr", bpr_coefficients) api = cls.model_manager.beats_api time_period = 1 # Only have one time period for static model paths_list = list(api.get_path_ids()) commodity_list = list(api.get_commodity_ids()) route_list = {} for path_id in paths_list: route_list[path_id] = api.get_subnetwork_with_id( path_id).get_link_ids() # Test used to validate the Demand_Assignment_Class # Creating the demand assignment for initialization cls.demand_assignments = Demand_Assignment_class(route_list, commodity_list, time_period, dt=time_period) demands = {} demand_value = np.zeros(time_period) demand_value1 = np.zeros(time_period) demand_value[0] = 2 demand_value1[0] = 2 demands[(1L, 1L)] = demand_value demands[(2L, 1L)] = demand_value1 demands[(3L, 1L)] = demand_value cls.demand_assignments.set_all_demands(demands) # create link states cls.link_states = cls.model_manager.traffic_model.Run_Model( cls.demand_assignments)
def setUpClass(cls): cls.conn = Java_Connection() this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) configfile = os.path.join(this_folder, os.path.pardir, os.path.pardir, 'configfiles', 'seven_links.xml') sim_dt = 2 coefficients = { 0L: [1, 0, 0, 0, 1], 1L: [1, 0, 0, 0, 1], 2L: [2, 0, 0, 0, 2], 3L: [1, 0, 0, 0, 1], 4L: [2, 0, 0, 0, 2], 5L: [1, 0, 0, 0, 1], 6L: [1, 0, 0, 0, 1] } cls.model_manager = Link_Model_Manager_class(configfile, "mn", cls.conn.gateway, sim_dt, "bpr", coefficients)
def setUpClass(cls): # make Java connection cls.connection = Java_Connection() # create a static/bpr model manager this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', 'seven_links.xml') bpr_coefficients = { 0L: [1, 0, 0, 0, 1], 1L: [1, 0, 0, 0, 1], 2L: [5, 0, 0, 0, 5], 3L: [2, 0, 0, 0, 2], 4L: [2, 0, 0, 0, 2], 5L: [1, 0, 0, 0, 1], 6L: [5, 0, 0, 0, 5] } cls.model_manager = Link_Model_Manager_class(configfile, "static", cls.connection, None, "bpr", bpr_coefficients)
from Solvers.Solver_Class import Solver_class from Model_Manager.Link_Model_Manager import Link_Model_Manager_class from Java_Connection import Java_Connection from copy import copy import matplotlib.pyplot as plt import os import inspect from Solvers.Path_Based_Frank_Wolfe_Solver import Path_Based_Frank_Wolfe_Solver import csv plt.rcParams.update({'font.size': 18}) # Flag that indicates whether we are doing decomposition or not decompositio_flag = False connection = Java_Connection() if connection.pid is not None: # Contains local path to input configfile, for the three_links.xml network this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', 'seven_links.xml') coefficients = {} T = 1800 # Time horizon of interest sim_dt = 0.0 # Duration of one time_step for the traffic model sampling_dt = 300 # Duration of time_step for the solver, in this case it is equal to sim_dt
# from py4j.java_gateway import JavaGateway,GatewayParameters from Model_Manager.Link_Model_Manager import Link_Model_Manager_class from Model_Manager.BeATS_Model_Manager import BeATS_Model_Manager_class from Java_Connection import Java_Connection from Data_Types.Demand_Assignment_Class import Demand_Assignment_class from Traffic_Models.MN_Model import MN_Model_Class from copy import copy import matplotlib.pyplot as plt import os import inspect import csv from Solvers.Path_Based_Frank_Wolfe_Solver import Path_Based_Frank_Wolfe_Solver plt.rcParams.update({'font.size': 18}) connection = Java_Connection() if connection.pid is not None: this_folder = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', 'cfg_vi_mn.xml') coefficients = {} T = 3600 # Time horizon of interest sim_dt = 4.0 # Duration of one time_step for the traffic model sampling_dt = 4 # Duration of time_step for the solver, in this case it is equal to sim_dt model_manager = Link_Model_Manager_class(configfile, connection.gateway, "mn", sim_dt, "bpr", coefficients) #Estimating bpr coefficients with beats num_links = model_manager.beats_api.get_num_links() avg_travel_time = np.zeros(num_links) num_coeff = 5
def call_solver(args): # Flag that indicates whether we are doing decomposition or not decomposition_flag = args.decomposition_flag conn = Java_Connection(decomposition_flag) if conn.pid is not None: sim_dt = 2.0 T = args.T # Time horizon of interest sampling_dt = args.sampling_dt # Duration of one time_step for the solver this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', args.configfile) instantaneous = True # Indicates whether we are doing ctm predictive (True) or instantaneous (False) model_manager = OTM_Model_Manager_class(configfile, args.model, conn.gateway, sim_dt) if (model_manager.is_valid()): num_steps = int(T / sampling_dt) # Get the OD Matrix form Model Manager # OD Matrix can also be initialized from another source, as long as it fits the OD_Matrix class format OD_Matrix = model_manager.get_OD_Matrix(num_steps, sampling_dt) if OD_Matrix is not None: # Algorithm to solve the problem if args.solver_algorithm == "FW": solver_algorithm = Path_Based_Frank_Wolfe_Solver elif args.solver_algorithm == "MSA": solver_algorithm = Method_of_Successive_Averages_Solver else: solver_algorithm = Extra_Projection_Method_Solver scenario_solver = Solver_class(model_manager, solver_algorithm) assignment, solver_run_time = scenario_solver.Solver_function( T, sampling_dt, OD_Matrix, decomposition_flag) if assignment is None: print "Solver did not run" else: #Save assignment into a csv file this_folder = os.path.dirname( os.path.abspath(inspect.getfile( inspect.currentframe()))) outputfile = os.path.join(this_folder, os.path.pardir, 'output', args.configfile[:-4] + '.csv') # # We first save in the paramenters of the scenario # csv_file = open(outputfile, 'wb') # writer = csv.writer(csv_file) # # Saving the model type # writer.writerow(['model type:','BeATS']) # od = model_manager.otm_api.get_od_info() # demand_api = [item * 3600 for item in od[0].get_total_demand_vps().getValues()] # od_dt = od[0].get_total_demand_vps().getDt() # if od_dt is None: # od_dt = sampling_dt # # # Saving the demand per od and time horizon value # writer.writerow(['demand dt (s)', 'od demand (vh)','T (s)']) # writer.writerow([od_dt,demand_api,T]) # # writer.writerow(['(path ID, commodity ID)', 'array of demand (vh) per dt on path']) # # Now we save the assignment values to csv file # for key, value in assignment.get_all_demands().items(): # writer.writerow([key, value]) # # csv_file.close() #print "\nDemand Assignment:" #assignment.print_all() path_costs = model_manager.evaluate(assignment, T, initial_state=None) #print "\nPath costs in secons:" #path_costs.print_all() if decomposition_flag: from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() else: rank = 0 #Distance to Nash error_percentage = scenario_solver.distance_to_Nash( assignment, path_costs, sampling_dt, OD_Matrix) if rank == 0: print "\n" print "%.02f" % error_percentage, "% vehicles from equilibrium" '''
plt.rcParams.update({'font.size': 18}) # Function to save the output data def save_ouput_to_picle(outputfile, assignment, path_costs, run_time, error): # Write results to picke file # The DTA assignment and corresponding path_cost from file with open(outputfile, "wb") as f: pickle.dump(assignment, f) pickle.dump(path_costs, f) pickle.dump(run_time, f) pickle.dump(error, f) # Flag that indicates whether we are doing decomposition or not decompositio_flag = True connection = Java_Connection(decompositio_flag) if connection.pid is not None: # Contains local path to input configfile, for the three_links.xml network this_folder = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) scenario_name = 'seven_links' #Scenario name configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', scenario_name+'.xml') coefficients = {} T = 3600 # Time horizon of interest sim_dt = 0.1 # Duration of one time_step for the traffic model sampling_dt = 1200 # Duration of time_step for the solver, in this case it is equal to sim_dt model_manager = Link_Model_Manager_class(configfile, "static", connection.gateway, sim_dt, "bpr", coefficients)
def setUpClass(cls): cls.conn = Java_Connection()
import sys from Model_Manager.Link_Model_Manager import Link_Model_Manager_class from Java_Connection import Java_Connection from copy import copy from Solvers.Frank_Wolfe_Solver_Static import construct_igraph import os import inspect import argparse # Flag that indicates whether we are doing decomposition or not decompositio_flag = False # Flag that indicates whether we are doing decomposition or not connection = Java_Connection(decompositio_flag) if connection.pid is not None: # Contains local path to input configfile, for the three_links.xml network this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) scenario_name = 'scenario' #Scenario name configfile = os.path.join(this_folder, os.path.pardir, 'configfiles', scenario_name + '.xml') print "Loading data for: ", scenario_name # File where to save the pickled objects this_folder = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe())))