From f5d6cf4473392a129393fb50935cf2ce1a1cde14 Mon Sep 17 00:00:00 2001 From: Toru Seo <34780089+toruseo@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:48:24 +0900 Subject: [PATCH] add `reduce_memory_delele_vehicle_route_pref` --- tests/test_other_functions.py | 95 ++++++++++++++++++++++++++++++++++- uxsim/uxsim.py | 19 ++++++- 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/tests/test_other_functions.py b/tests/test_other_functions.py index 9ddd540..64937ac 100644 --- a/tests/test_other_functions.py +++ b/tests/test_other_functions.py @@ -782,4 +782,97 @@ def veh_user_function(veh): link1 = W.get_link("link1") print("average speed of link1:", np.nanmean(link1.user_attribute["speed_record"])) - assert equal_tolerance(np.nanmean(link1.user_attribute["speed_record"]), 15.89) \ No newline at end of file + assert equal_tolerance(np.nanmean(link1.user_attribute["speed_record"]), 15.89) + +def test_reduce_memory_delele_vehicle_route_pref(): + W = World( + name="", + deltan=10, + tmax=3000, + print_mode=1, save_mode=1, show_mode=0, + reduce_memory_delele_vehicle_route_pref=False, + random_seed=0, + ) + + n_nodes = 4 + imax = n_nodes + jmax = n_nodes + nodes = {} + for i in range(imax): + for j in range(jmax): + nodes[i,j] = W.addNode(f"n{(i,j)}", i, j, flow_capacity=1.6) + + links = {} + for i in range(imax): + for j in range(jmax): + if i != imax-1: + links[i,j,i+1,j] = W.addLink(f"l{(i,j,i+1,j)}", nodes[i,j], nodes[i+1,j], length=1000) + if i != 0: + links[i,j,i-1,j] = W.addLink(f"l{(i,j,i-1,j)}", nodes[i,j], nodes[i-1,j], length=1000) + if j != jmax-1: + links[i,j,i,j+1] = W.addLink(f"l{(i,j,i,j+1)}", nodes[i,j], nodes[i,j+1], length=1000) + if j != 0: + links[i,j,i,j-1] = W.addLink(f"l{(i,j,i,j-1)}", nodes[i,j], nodes[i,j-1], length=1000) + + + area_dict = { + "areaN": [nodes[0,i] for i in range(n_nodes)], + "areaS": [nodes[n_nodes-1,i] for i in range(n_nodes)], + "areaNW": [nodes[0,0]], + "areaSE": [nodes[n_nodes-1, n_nodes-1]] + } + + W.adddemand_nodes2nodes(area_dict["areaN"], area_dict["areaS"], 0, 3000, volume=7000) + + W.exec_simulation() + W.analyzer.print_simple_stats() + + df1 = W.analyzer.area_to_pandas(list(area_dict.values()), list(area_dict.keys()), border_include=True) + print(df1) + + W = World( + name="", + deltan=10, + tmax=3000, + print_mode=1, save_mode=1, show_mode=0, + reduce_memory_delele_vehicle_route_pref=True, + random_seed=0, + ) + + n_nodes = 4 + imax = n_nodes + jmax = n_nodes + nodes = {} + for i in range(imax): + for j in range(jmax): + nodes[i,j] = W.addNode(f"n{(i,j)}", i, j, flow_capacity=1.6) + + links = {} + for i in range(imax): + for j in range(jmax): + if i != imax-1: + links[i,j,i+1,j] = W.addLink(f"l{(i,j,i+1,j)}", nodes[i,j], nodes[i+1,j], length=1000) + if i != 0: + links[i,j,i-1,j] = W.addLink(f"l{(i,j,i-1,j)}", nodes[i,j], nodes[i-1,j], length=1000) + if j != jmax-1: + links[i,j,i,j+1] = W.addLink(f"l{(i,j,i,j+1)}", nodes[i,j], nodes[i,j+1], length=1000) + if j != 0: + links[i,j,i,j-1] = W.addLink(f"l{(i,j,i,j-1)}", nodes[i,j], nodes[i,j-1], length=1000) + + + area_dict = { + "areaN": [nodes[0,i] for i in range(n_nodes)], + "areaS": [nodes[n_nodes-1,i] for i in range(n_nodes)], + "areaNW": [nodes[0,0]], + "areaSE": [nodes[n_nodes-1, n_nodes-1]] + } + + W.adddemand_nodes2nodes(area_dict["areaN"], area_dict["areaS"], 0, 3000, volume=7000) + + W.exec_simulation() + W.analyzer.print_simple_stats() + + df2 = W.analyzer.area_to_pandas(list(area_dict.values()), list(area_dict.keys()), border_include=True) + print(df2) + + assert df1["total_travel_time"][0] == df2["total_travel_time"][0] \ No newline at end of file diff --git a/uxsim/uxsim.py b/uxsim/uxsim.py index 03bde72..3e6a916 100644 --- a/uxsim/uxsim.py +++ b/uxsim/uxsim.py @@ -1086,6 +1086,9 @@ def end_trip(s): s.record_log(enforce_log=1) + if s.W.reduce_memory_delele_vehicle_route_pref: + s.route_pref = None + def carfollow(s): """ Drive withing a link. @@ -1422,7 +1425,16 @@ class World: World (i.e., simulation environment). A World object is consistently referred to as `W` in this code. """ - def __init__(W, name="", deltan=5, reaction_time=1, duo_update_time=600, duo_update_weight=0.5, duo_noise=0.01, eular_dt=120, eular_dx=100, random_seed=None, print_mode=1, save_mode=1, show_mode=0, route_choice_principle="homogeneous_DUO", route_choice_update_gradual=False, show_progress=1, show_progress_deltat=600, tmax=None, vehicle_logging_timestep_interval=1, instantaneous_TT_timestep_interval=5, hard_deterministic_mode=False, meta_data={}, user_attribute=None, user_function=None): + def __init__(W, name="", deltan=5, reaction_time=1, + duo_update_time=600, duo_update_weight=0.5, duo_noise=0.01, route_choice_principle="homogeneous_DUO", route_choice_update_gradual=False, instantaneous_TT_timestep_interval=5, + eular_dt=120, eular_dx=100, + random_seed=None, + print_mode=1, save_mode=1, show_mode=0, show_progress=1, show_progress_deltat=600, + tmax=None, + vehicle_logging_timestep_interval=1, + reduce_memory_delele_vehicle_route_pref=False, + hard_deterministic_mode=False, + meta_data={}, user_attribute=None, user_function=None): """ Create a World. @@ -1471,6 +1483,8 @@ def __init__(W, name="", deltan=5, reaction_time=1, duo_update_time=600, duo_upd If it is longer than the DUO update timestep interval, it is substituted by DUO update timestep interval to maintain reasonable route choice behavior. hard_deterministic_mode : bool, optional If True, the simulation will not use any random variables. At a merging node, a link with higher merge_priority will be always prioritized, and vehicles always choose the shortest path. This may be useful for analysis that need strict predictability. Be aware that the simulation results will be significantly different from ones with `hard_deterministic_mode=False`. + reduce_memory_delele_vehicle_route_pref : bool, optional + If True, the simulation will delete the route preference of vehicles after its ends. This is useful when the route preference is not needed after the simulation ends. meta_data : dict, optinal Meta data for simulation scenario. Can store arbitrary data, such as licences and simulation explanation. user_attribute : any, optinal @@ -1534,6 +1548,9 @@ def __init__(W, name="", deltan=5, reaction_time=1, duo_update_time=600, duo_upd W.show_progress = show_progress W.show_progress_deltat_timestep = int(show_progress_deltat/W.DELTAT) + ## memory setting + W.reduce_memory_delele_vehicle_route_pref = reduce_memory_delele_vehicle_route_pref + ## system setting W.name = name