def simulate_parallel(config="../data/config/parallel.json", inData=None, params=None, search_space=None, **kwargs): if inData is None: # othwerwise we use what is passed from MaaSSim.data_structures import structures inData = structures.copy() # fresh data if params is None: params = get_config(config, root_path = kwargs.get('root_path')) # load from .json file if len(inData.G) == 0: # only if no graph in input inData = load_G(inData, params, stats=True) # download graph for the 'params.city' and calc the skim matrices if len(inData.passengers) == 0: # only if no passengers in input inData = generate_demand(inData, params, avg_speed=True) if len(inData.vehicles) == 0: # only if no vehicles in input inData.vehicles = generate_vehicles(inData, params.nV) if len(inData.platforms) == 0: # only if no platforms in input inData.platforms = initialize_df(inData.platforms) inData.platforms.loc[0] = empty_series(inData.platforms) inData.platforms.fare = [1] inData.vehicles.platform = 0 inData.passengers.platforms = inData.passengers.apply(lambda x: [0], axis=1) inData = prep_shared_rides(inData, params.shareability) # obligatory to prepare schedules brute(func=single_pararun, ranges=slice_space(search_space, replications=params.parallel.get("nReplications",1)), args=(inData, params, search_space), full_output=True, finish=None, workers=params.parallel.get('nThread',1))
def test_platform_competition(self): # make sure when you compete with the prcie, lowering the fare and increasing the fee from MaaSSim.data_structures import structures as local_inData from MaaSSim.decisions import f_platform_choice from MaaSSim.simulators import simulate as platform_simulator_1 from MaaSSim.utils import get_config, generate_vehicles, generate_demand, initialize_df, load_G CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config_platform_choices.json') params = get_config(CONFIG_PATH, root_path=os.path.dirname(__file__)) # load from .json file params.nP = 200 # reuqests (and passengers) params.simTime = 4 params.nD = 1 fare = 1.5 fleet = 10 params.nV = 20 + fleet inData = local_inData.copy() inData = load_G(inData, params, stats=True) # download graph for the 'params.city' and calc the skim matrices inData = generate_demand(inData, params, avg_speed=True) inData.vehicles = generate_vehicles(inData, params.nV) inData.platforms = initialize_df(inData.platforms) inData.platforms.loc[0] = [1, 'Platform1', 30] inData.platforms.loc[1] = [fare, 'Platform2', 30] inData = generate_demand(inData, params, avg_speed=True) inData.vehicles = generate_vehicles(inData, params.nV) inData.vehicles.platform = [0] * 20 + [1] * fleet inData.passengers.platforms = inData.passengers.apply(lambda x: [0, 1], axis=1) sim = platform_simulator_1(params=params, inData=inData, f_platform_choice=f_platform_choice) ret = sim.res[0].veh_exp.copy() ret['platform'] = inData.vehicles.platform first = ret[ret.platform == 1].ARRIVES_AT_DROPOFF.sum() fare = 0.5 fleet = 50 params.nV = 20 + fleet inData = generate_demand(inData, params, avg_speed=True) inData.vehicles = generate_vehicles(inData, params.nV) inData.platforms = initialize_df(inData.platforms) inData.platforms.loc[0] = [1, 'Platform1', 30] inData.platforms.loc[1] = [fare, 'Platform2', 30] inData = generate_demand(inData, params, avg_speed=True) inData.vehicles = generate_vehicles(inData, params.nV) inData.vehicles.platform = [0] * 20 + [1] * fleet inData.passengers.platforms = inData.passengers.apply(lambda x: [0, 1], axis=1) from MaaSSim.simulators import simulate as platform_simulator_2 sim2 = platform_simulator_2(params=params, inData=inData, f_platform_choice=f_platform_choice) ret = sim2.res[0].veh_exp ret['platform'] = inData.vehicles.platform second = ret[ret.platform == 1].ARRIVES_AT_DROPOFF.sum() self.assertGreater(second, first) del sim del sim2 del params del inData
def simulate(config="../data/config/default.json", inData=None, params=None, **kwargs): """ main runner and wrapper loads or uses json config to prepare the data for simulation, run it and process the results :param config: .json file path :param inData: optional input data :param params: loaded json file :param kwargs: optional arguments :return: simulation object with results """ if inData is None: # otherwise we use what is passed from MaaSSim.data_structures import structures inData = structures.copy() # fresh data if params is None: params = get_config( config, root_path=kwargs.get('root_path')) # load from .json file if kwargs.get('make_main_path', False): from MaaSSim.utils import make_config_paths params = make_config_paths(params, main=kwargs.get('make_main_path', False), rel=True) if params.paths.get('requests', False): inData = read_requests_csv(inData, path=params.paths.requests) if params.paths.get('vehicles', False): inData = read_vehicle_positions(inData, path=params.paths.vehicles) if len(inData.G) == 0: # only if no graph in input inData = load_G( inData, params, stats=True ) # download graph for the 'params.city' and calc the skim matrices if len(inData.passengers) == 0: # only if no passengers in input inData = generate_demand(inData, params, avg_speed=True) if len(inData.vehicles) == 0: # only if no vehicles in input inData.vehicles = generate_vehicles(inData, params.nV) if len(inData.platforms) == 0: # only if no platforms in input inData.platforms = initialize_df(inData.platforms) inData.platforms.loc[0] = empty_series(inData.platforms) inData.platforms.fare = [1] inData = prep_shared_rides(inData, params.shareability) # prepare schedules sim = Simulator(inData, params=params, **kwargs) # initialize for day in range(params.get('nD', 1)): # run iterations sim.make_and_run(run_id=day) # prepare and SIM sim.output() # calc results if sim.functions.f_stop_crit(sim=sim): break return sim
def test_multiple_days(self): # have two runs in the same instance and see if they are different from MaaSSim.data_structures import structures as my_inData from MaaSSim.decisions import f_platform_choice from MaaSSim.simulators import simulate as multiple_days_simulator from MaaSSim.utils import get_config, generate_vehicles, generate_demand, initialize_df, load_G from MaaSSim.simulators import simulate as simulator_for_multiple_days CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config_results_test.json') params = get_config( CONFIG_PATH, root_path=os.path.dirname(__file__)) # load from .json file params.times.patience = 3600 # 1 hour of simulation params.simTime = 1 # 1 hour of simulation params.nP = 30 # reuqests (and passengers) params.nV = 30 # vehicles params.nD = 3 inData = my_inData.copy() inData = load_G( inData, params, stats=True ) # download graph for the 'params.city' and calc the skim matrices inData = generate_demand(inData, params, avg_speed=True) inData.vehicles = generate_vehicles(inData, params.nV) sim = multiple_days_simulator(params=params, inData=inData, print=False, f_platform_choice=f_platform_choice) self.assertEqual(len(sim.runs), 3) del sim del params
def test_rejects(self): # make sure that rejection works for drivers and travellers (dummy reject with fixed probability) from MaaSSim.utils import get_config from MaaSSim.decisions import dummy_False from MaaSSim.traveller import travellerEvent from MaaSSim.driver import driverEvent from MaaSSim.data_structures import structures as this_inData from MaaSSim.simulators import simulate as reject_simulator CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config_platform_choices.json') def rand_reject8(**kwargs): # sample function to reject with probability of 50% return random.random() >= 0.8 params = get_config( CONFIG_PATH, root_path=os.path.dirname(__file__)) # load from .json file params.times.patience = 3600 # 1 hour of simulation params.simTime = 1 # 1 hour of simulation params.nP = 100 # reuqests (and passengers) params.nV = 100 # vehicles # A no rejections sim = reject_simulator(params=params, inData=this_inData.copy(), f_trav_mode=dummy_False, f_driver_decline=dummy_False) self.assertNotIn(travellerEvent.IS_REJECTED_BY_VEHICLE.name, sim.runs[0].trips.event.values) # no rejections self.assertNotIn(travellerEvent.REJECTS_OFFER.name, sim.runs[0].trips.event.values) # no rejections self.assertNotIn(driverEvent.REJECTS_REQUEST.name, sim.runs[0].rides.event.values) # no rejections self.assertNotIn(driverEvent.IS_REJECTED_BY_TRAVELLER.name, sim.runs[0].rides.event.values) # no rejections # B vehicle rejects del sim from MaaSSim.simulators import simulate as reject_simulator2 sim = reject_simulator2(params=params, inData=this_inData.copy(), f_trav_mode=dummy_False, f_driver_decline=rand_reject8, f_platform_choice=dummy_False) self.assertIn(travellerEvent.IS_REJECTED_BY_VEHICLE.name, sim.runs[0].trips.event.values) # no rejections self.assertNotIn(travellerEvent.REJECTS_OFFER.name, sim.runs[0].trips.event.values) # no rejections self.assertIn(driverEvent.REJECTS_REQUEST.name, sim.runs[0].rides.event.values) # no rejections self.assertNotIn(driverEvent.IS_REJECTED_BY_TRAVELLER.name, sim.runs[0].rides.event.values) # no rejections # # C traveller rejects del sim from MaaSSim.simulators import simulate as reject_simulator3 sim = reject_simulator3(params=params, inData=this_inData.copy(), f_trav_mode=rand_reject8, f_driver_decline=dummy_False, f_platform_choice=dummy_False) self.assertNotIn(travellerEvent.IS_REJECTED_BY_VEHICLE.name, sim.runs[0].trips.event.values) # no rejections self.assertIn(travellerEvent.REJECTS_OFFER.name, sim.runs[0].trips.event.values) # no rejections self.assertNotIn(driverEvent.REJECTS_REQUEST.name, sim.runs[0].rides.event.values) # no rejections self.assertIn(driverEvent.IS_REJECTED_BY_TRAVELLER.name, sim.runs[0].rides.event.values) # no rejections # # # D both reject del sim from MaaSSim.simulators import simulate as reject_simulator4 sim = reject_simulator4(params=params, inData=this_inData.copy(), f_trav_mode=rand_reject8, f_driver_decline=rand_reject8, f_platform_choice=dummy_False) self.assertIn(travellerEvent.IS_REJECTED_BY_VEHICLE.name, sim.runs[0].trips.event.values) # no rejections self.assertIn(travellerEvent.REJECTS_OFFER.name, sim.runs[0].trips.event.values) # no rejections self.assertIn(driverEvent.REJECTS_REQUEST.name, sim.runs[0].rides.event.values) # no rejections self.assertIn(driverEvent.IS_REJECTED_BY_TRAVELLER.name, sim.runs[0].rides.event.values) # no rejections del sim