From f4d87b1b7814ba0636c6b5766be5fda8c3fa6a9f Mon Sep 17 00:00:00 2001 From: Toru Seo <34780089+toruseo@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:07:30 +0900 Subject: [PATCH 1/3] fix `change_jam_density` function --- tests/test_other_functions.py | 49 ++++++++++++++++++++++++++++++++++- uxsim/uxsim.py | 1 + 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/test_other_functions.py b/tests/test_other_functions.py index 381a04e..57e49b5 100644 --- a/tests/test_other_functions.py +++ b/tests/test_other_functions.py @@ -823,7 +823,54 @@ def test_vehicle_group_stats(): assert df["average_traveled_distance"][df["group"]=="depart_t=0"].values[0] < df["average_traveled_distance"][df["group"]=="depart_t=1500"].values[0] assert df["average_detour_ratio"][df["group"]=="depart_t=0"].values[0] < df["average_detour_ratio"][df["group"]=="depart_t=1500"].values[0] assert df["average_speed"][df["group"]=="depart_t=0"].values[0] > df["average_speed"][df["group"]=="depart_t=1500"].values[0] - + +def test_change_jam_density(): + W = World( + name="", + deltan=5, + tmax=1200, + print_mode=1, save_mode=0, show_mode=1, + ) + + W.addNode("orig", 0, 0) + W.addNode("mid1", 0, 2) + W.addNode("mid2", 1, 1, signal=[240, 240]) + W.addNode("dest", 2, 1) + W.addLink("link1", "orig", "mid1", length=1000, free_flow_speed=20, number_of_lanes=1) + link2 = W.addLink("link2", "mid1", "mid2", length=400, free_flow_speed=20, jam_density=0.2, number_of_lanes=1, signal_group=0) + W.addLink("link3", "mid2", "dest", length=1000, free_flow_speed=20, number_of_lanes=1) + W.adddemand("orig", "dest", 0, 1200, 0.4) + + check_flag = 0 + while W.check_simulation_ongoing(): + W.exec_simulation(duration_t2=30) + print(W.TIME, link2.delta_per_lane, link2.num_vehicles, link2.num_vehicles_queue) + if W.TIME > 600: + link2.change_jam_density(0.1) + + if W.TIME == 180: + assert equal_tolerance(link2.num_vehicles, 10) + assert equal_tolerance(link2.num_vehicles_queue, 0) + check_flag += 1 + if W.TIME == 450: + assert equal_tolerance(link2.num_vehicles, 400*0.2) + assert equal_tolerance(link2.num_vehicles_queue, 400*0.2) + check_flag += 1 + if W.TIME == 540: + assert equal_tolerance(link2.num_vehicles, 35) + assert equal_tolerance(link2.num_vehicles_queue, 25) + check_flag += 1 + if W.TIME == 900: + assert equal_tolerance(link2.num_vehicles, 400*0.1) + assert equal_tolerance(link2.num_vehicles_queue, 400*0.1) + check_flag += 1 + + assert check_flag == 4 + + W.analyzer.print_simple_stats() + + #W.analyzer.time_space_diagram_traj_links(["link1","link2","link3"]) + def test_user_functions(): # define custom user_functions diff --git a/uxsim/uxsim.py b/uxsim/uxsim.py index 02fee26..1366f56 100644 --- a/uxsim/uxsim.py +++ b/uxsim/uxsim.py @@ -852,6 +852,7 @@ def change_jam_density(s, new_value): s.w = 1/s.tau/s.kappa s.capacity = s.u*s.w*s.kappa/(s.u+s.w) s.delta = 1/s.kappa + s.delta_per_lane = s.delta*s.number_of_lanes else: warnings.warn(f"ignored negative jam_density at {s}", UserWarning) From 7a40e60835cd919b1495aadc7b39459c66b857b3 Mon Sep 17 00:00:00 2001 From: Toru Seo <34780089+toruseo@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:08:09 +0900 Subject: [PATCH 2/3] minor adjustments of tests --- tests/test_other_functions.py | 3 + tests/test_verification_dta_solvers.py | 4 +- uxsim/DTAsolvers/ALNS.py | 108 ++++++++++++------------- 3 files changed, 59 insertions(+), 56 deletions(-) diff --git a/tests/test_other_functions.py b/tests/test_other_functions.py index 57e49b5..304c54c 100644 --- a/tests/test_other_functions.py +++ b/tests/test_other_functions.py @@ -63,6 +63,9 @@ def test_analyzer(): W.analyzer.network_anim(detailed=0, network_font_size=0, figsize=(12,12), state_variables="flow_speed") W.analyzer.network_fancy(animation_speed_inverse=15, sample_ratio=0.3, interval=5, trace_length=5) + W.analyzer.network_pillow(600, state_variables="density_speed") + W.analyzer.network_pillow(600, state_variables="density_flow") + # Convert results to pandas.DataFrame for easier analysis print(W.analyzer.basic_to_pandas()) # Basic statistics print(W.analyzer.od_to_pandas()) # Information by OD diff --git a/tests/test_verification_dta_solvers.py b/tests/test_verification_dta_solvers.py index 03c9d52..eee3f49 100644 --- a/tests/test_verification_dta_solvers.py +++ b/tests/test_verification_dta_solvers.py @@ -113,8 +113,8 @@ def create_World(): solver_DSO_GA.plot_vehicle_stats(orig="4", dest="7") solver_DSO_ALNS.plot_convergence() - solver_DUE.plot_link_stats() - solver_DUE.plot_vehicle_stats(orig="4", dest="7") + solver_DSO_ALNS.plot_link_stats() + solver_DSO_ALNS.plot_vehicle_stats(orig="4", dest="7") solver_DSO_D2D.plot_convergence() solver_DSO_D2D.plot_link_stats() diff --git a/uxsim/DTAsolvers/ALNS.py b/uxsim/DTAsolvers/ALNS.py index 36f5a47..692b483 100644 --- a/uxsim/DTAsolvers/ALNS.py +++ b/uxsim/DTAsolvers/ALNS.py @@ -1068,26 +1068,26 @@ def get_best_x(state: ALNSState) -> List[Any]: """ return _copy_vec(state.x_best) -def evaluate_candidates(state: ALNSState, xs: Iterable[Sequence[Any]]) -> List[float]: - """複数の候補解を評価. +# def evaluate_candidates(state: ALNSState, xs: Iterable[Sequence[Any]]) -> List[float]: +# """複数の候補解を評価. - Parameters - ---------- - state : ALNSState - ALNS状態 - xs : Iterable[Sequence[Any]] - 評価する候補解のイテラブル +# Parameters +# ---------- +# state : ALNSState +# ALNS状態 +# xs : Iterable[Sequence[Any]] +# 評価する候補解のイテラブル - Returns - ------- - List[float] - 各候補解の目的関数値(ユーザー向き) - """ - vals = [] - for x in xs: - v = state.obj(x) - vals.append(v if state.sense == "min" else -v) - return vals +# Returns +# ------- +# List[float] +# 各候補解の目的関数値(ユーザー向き) +# """ +# vals = [] +# for x in xs: +# v = state.obj(x) +# vals.append(v if state.sense == "min" else -v) +# return vals def finalize(state: ALNSState, stop_reason: str = "") -> Tuple[Sequence[Any], RunLog]: """ALNS実行を終了し、結果を取得. @@ -1127,39 +1127,39 @@ def finalize(state: ALNSState, stop_reason: str = "") -> Tuple[Sequence[Any], Ru # ------------------------- # デモ(__main__) # ------------------------- -if __name__ == "__main__": - # 例:各位置 ∈ {0,1,2,3}、重み付き合計を target に合わせる(制約なし) - random.seed(42) - n_elem = 20 - w = [random.uniform(0,10) for _ in range(n_elem)] - target = sum(w)*1.5 - x0 = [0 for _ in range(n_elem)] - domains = [[0,1,2,3] for _ in x0] - - def obj_example(x1): - return abs(sum(wj*xj for wj, xj in zip(w, x1)) - target) - - state = init_alns( - x0, obj_example, - domains=domains, - k_min=1, k_max=4, - total_iters=2000, - segment_len=50, - seed=0, - additional_info={"departure_times": np.linspace(0,1,len(x0))} #無意味な例 - ) - - # 1000 ステップだけ走らせて終了 - run_auto(state, n=1000) - x_star, run = finalize(state, stop_reason="done") - - try: - df = run.to_dataframe() - print(df.to_string()) - except Exception: - pass - - print("best obj:", run.best_obj) - print("best x :", x_star) - print("op stats:") - pprint(run.operator_stats) +# if __name__ == "__main__": +# # 例:各位置 ∈ {0,1,2,3}、重み付き合計を target に合わせる(制約なし) +# random.seed(42) +# n_elem = 20 +# w = [random.uniform(0,10) for _ in range(n_elem)] +# target = sum(w)*1.5 +# x0 = [0 for _ in range(n_elem)] +# domains = [[0,1,2,3] for _ in x0] + +# def obj_example(x1): +# return abs(sum(wj*xj for wj, xj in zip(w, x1)) - target) + +# state = init_alns( +# x0, obj_example, +# domains=domains, +# k_min=1, k_max=4, +# total_iters=2000, +# segment_len=50, +# seed=0, +# additional_info={"departure_times": np.linspace(0,1,len(x0))} #無意味な例 +# ) + +# # 1000 ステップだけ走らせて終了 +# run_auto(state, n=1000) +# x_star, run = finalize(state, stop_reason="done") + +# try: +# df = run.to_dataframe() +# print(df.to_string()) +# except Exception: +# pass + +# print("best obj:", run.best_obj) +# print("best x :", x_star) +# print("op stats:") +# pprint(run.operator_stats) From b7edee1d7e2fa16bbd9420d7295b31a10e5ff6f1 Mon Sep 17 00:00:00 2001 From: Toru Seo <34780089+toruseo@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:43:33 +0900 Subject: [PATCH 3/3] Update README Removed sections detailing main files and updated related links. --- README.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/README.md b/README.md index 781f23c..f3f1038 100644 --- a/README.md +++ b/README.md @@ -210,12 +210,6 @@ To learn more about UXsim, please see: - [arXiv preprint](https://arxiv.org/abs/2309.17114): Scientific overview - [JOSS paper](https://doi.org/10.21105/joss.07617): Peer-reviewed article in Journal of Open Source Software -## Main Files - -- `uxsim` directory: UXsim main package - - `uxsim/uxsim.py`: UXsim main code -- `demos_and_examples` directory: Tutorials and examples of UXsim -- `dat` directory: Sample scenario files ## Terms of Use & License @@ -266,4 +260,4 @@ Specifically, UXsim directly uses the following works: ## Related Links - [Toru Seo (Author)](https://toruseo.jp/index_en.html) -- [Seo Laboratory, Tokyo Institute of Technology](http://seo.cv.ens.titech.ac.jp/en/) +- [Seo Laboratory, Institute of Science Tokyo](http://seo.cv.ens.titech.ac.jp/en/)