Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pseudo random numbers cannot be reproduced by fixing seeds #86

Closed
toruseo opened this issue Jul 23, 2024 · 2 comments
Closed

pseudo random numbers cannot be reproduced by fixing seeds #86

toruseo opened this issue Jul 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@toruseo
Copy link
Owner

toruseo commented Jul 23, 2024

UXsim uses pseudo random numbers to simulate stochastic route choice and traffic merging behaviors at nodes. I have implemented a function to fix the random seed by random_seed arg of World.__init__(), but it does not work properly under certain conditions.

UXsim/uxsim/uxsim.py

Lines 1381 to 1384 in 1e5fac5

## parameter setting
random.seed(random_seed)
np.random.seed(random_seed)

In simple scenarios it works well, so I guess there are some small mistakes.

Example

Code:

from uxsim import *
from uxsim import Utilities

#########################################
# Iter 1
#########################################

W = World(
    name="",
    deltan=10,
    tmax=3600,
    print_mode=1, save_mode=1, show_mode=0,
    random_seed=42
)

n_nodes = 5
Utilities.generate_grid_network(W, n_nodes, n_nodes, length=1000)
od_pairs = [
    (f"n(0, 0)", f"n({n_nodes-1}, {n_nodes-1})"),
    (f"n({n_nodes-1}, 0)", f"n(0, {n_nodes-1})"),
    (f"n(0, {n_nodes-1})", f"n({n_nodes-1}, 0)"),
    (f"n({n_nodes-1}, {n_nodes-1})", f"n(0, 0)"),
]
for od_pair in od_pairs:
    W.adddemand(od_pair[0], od_pair[1], 0, 3000, 0.7)

W.exec_simulation()
W.analyzer.print_simple_stats()

#########################################
# Iter 2
#########################################

W = World(
    name="",
    deltan=10,
    tmax=3600,
    print_mode=1, save_mode=1, show_mode=0,
    random_seed=42
)

n_nodes = 5
Utilities.generate_grid_network(W, n_nodes, n_nodes, length=1000)
od_pairs = [
    (f"n(0, 0)", f"n({n_nodes-1}, {n_nodes-1})"),
    (f"n({n_nodes-1}, 0)", f"n(0, {n_nodes-1})"),
    (f"n(0, {n_nodes-1})", f"n({n_nodes-1}, 0)"),
    (f"n({n_nodes-1}, {n_nodes-1})", f"n(0, 0)"),
]
for od_pair in od_pairs:
    W.adddemand(od_pair[0], od_pair[1], 0, 3000, 0.7)

W.exec_simulation()
W.analyzer.print_simple_stats()

Results for Iter 1

results:
 average speed:	 16.7 m/s
 number of completed trips:	 8400 / 8400
 average travel time of trips:	 450.7 s
 average delay of trips:	 50.7 s
 delay ratio:			 0.112

Results for Iter 2

results:
 average speed:	 16.0 m/s
 number of completed trips:	 8400 / 8400
 average travel time of trips:	 471.8 s
 average delay of trips:	 71.8 s
 delay ratio:			 0.152
@toruseo toruseo added the bug Something isn't working label Jul 23, 2024
@EwoutH
Copy link
Contributor

EwoutH commented Jul 23, 2024

scientific-python/specs#180 might be interesting to keep an eye on.

@toruseo
Copy link
Owner Author

toruseo commented Jul 24, 2024

Finally I have solved this issue. The ultimate source of this bug was that set in Python does not preserve the order.

for outlink in {veh.route_next_link for veh in s.incoming_vehicles if veh.route_next_link != None}:

@EwoutH Thanks for the info. I have also updated the random number generators to the modern implementation.

@toruseo toruseo closed this as completed Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants