Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos_and_examples/example_08en_signal_reactive_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# Run the simulation for a specific duration (for cases where intervention is desired midway)
while W.check_simulation_ongoing():
# Execute the simulation in increments of 10 seconds
W.exec_simulation(duration_t=10)
W.exec_simulation(duration_t2=10)

# Investigate the number of vehicles per direction
vehicles_per_links = {}
Expand Down
2 changes: 1 addition & 1 deletion demos_and_examples/example_08jp_signal_reactive_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#特定時間だけシミュを回す(途中で介入したいとき用)
while W.check_simulation_ongoing():
#10秒づつシミュレーションを回す
W.exec_simulation(duration_t=10)
W.exec_simulation(duration_t2=10)

#方向別車両台数を調べる
vehicles_per_links = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# simulation
while W.check_simulation_ongoing():
#run simulation for 30 s
W.exec_simulation(duration_t=30)
W.exec_simulation(duration_t2=30)

#count number of vehicles per direction
vehicles_per_links = {tuple(l.signal_group): 0 for l in II.inlinks.values()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def step(self, action_index):

#traffic dynamics. execute simulation for 30 seconds
if self.W.check_simulation_ongoing():
self.W.exec_simulation(duration_t=30)
self.W.exec_simulation(duration_t2=30)

#observe state
observation = np.array(self.comp_state())
Expand Down
60 changes: 59 additions & 1 deletion tests/test_verification_straight_road.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ def test_iterative_exec_rigorous():
print()


def test_iterative_exec_rigorous_random_size():
def test_iterative_exec_rigorous_random_size_old():
for _ in range(100):
deltan = random.randint(1,10)
tmax = random.randint(50,150)
Expand Down Expand Up @@ -1058,3 +1058,61 @@ def test_iterative_exec_rigorous_random_size():
assert W.VEHICLES["0"].x == (tmax//W.DELTAT-1)*W.DELTAT*link_u

print()


def test_iterative_exec_rigorous_random_size_duration_t2():
for _ in range(100):
deltan = random.randint(1,10)
tmax = random.randint(50,150)
link_u = random.randint(10,30)
W = World(
name="",
deltan=deltan,
tmax=tmax,
duo_update_time=10,
print_mode=1, save_mode=0, show_mode=0,
random_seed=42,
)
if not tmax//W.DELTAT-2 > 1:
continue

W.addNode("orig", 0, 0)
W.addNode("dest", 2, 1)
W.addLink("link1", "orig", "dest", length=10000, free_flow_speed=link_u, number_of_lanes=1)
W.addVehicle("orig", "dest", 0)
W.addVehicle("orig", "dest", 50)
W.addVehicle("orig", "dest", 100)

#print("W.T, start_t, end_t")
# print("W.T", "\t", "W.TIME", "duration_t2", "ongoing")

if random.random() < 0.5:
maxt = 30
else:
maxt = 200

while W.check_simulation_ongoing():
duration_t2 = random.randint(deltan, maxt)
# if hasattr(W, "T"):
# print(W.T, "\t", W.TIME, "\t", duration_t2, "\t", W.check_simulation_ongoing())
# else:
# print(0, "\t", 0, "\t", duration_t2, "\t", W.check_simulation_ongoing())
W.exec_simulation(duration_t2=duration_t2)
# else:
# print(W.T, "\t","\t", "\t", W.check_simulation_ongoing())

if hasattr(W.analyzer, "total_distance_traveled"):
print("SUCCESS TO TERMINATE")
W.analyzer.print_simple_stats(force_print=True)
else:
print("FAILED TO TERMINATE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("FAILED TO TERMINATE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("FAILED TO TERMINATE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
assert False
print_columns(["log_t"]+W.VEHICLES["0"].log_t, ["log_x"]+W.VEHICLES["0"].log_x, ["log_v"]+W.VEHICLES["0"].log_v)
print("final_x", W.VEHICLES["0"].x)
assert W.VEHICLES["0"].log_t[-1] == (tmax//W.DELTAT-1)*W.DELTAT
assert W.VEHICLES["0"].log_x[-1] == (tmax//W.DELTAT-2)*W.DELTAT*link_u
assert W.VEHICLES["0"].x == (tmax//W.DELTAT-1)*W.DELTAT*link_u

print()
16 changes: 12 additions & 4 deletions uxsim/uxsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2139,16 +2139,21 @@ def print_scenario_stats(W):
print(" number of nodes:\t", len(W.NODES))
print(" setup time:\t\t", f"{time.time()-W.world_start_time:.2f}", "s")

def exec_simulation(W, until_t=None, duration_t=None):
def exec_simulation(W, until_t=None, duration_t=None, duration_t2=None):
"""
Execute the main loop of the simulation.

Parameters
----------
until_t : float or None, optional
The time until the simulation is to be executed in seconds. If both `until_t` and `duration_t` are None, the simulation is executed until the end. Default is None.
The time until the simulation is to be executed in seconds.
If all of `until_t` and `duration_t` and `duration_t2` are None, the simulation is executed until the end. Default is None.
duration_t : float or None, optional
The duration for which the simulation is to be executed in seconds. If both `until_t` and `duration_t` are None, the simulation is executed until the end. Default is None.
The duration for which the simulation is to be executed in seconds. Simulation runs `duration_t+1` seconds for a technical reason. Old setting, not recommended.
If all of `until_t` and `duration_t` and `duration_t2` are None, the simulation is executed until the end. Default is None. Recommended.
duration_t2 : float or None, optional
The duration for which the simulation is to be executed in seconds. Simulation runs `duration_t2` seconds.
If all of `until_t` and `duration_t` and `duration_t2` are None, the simulation is executed until the end. Default is None.

Returns
-------
Expand All @@ -2171,6 +2176,8 @@ def exec_simulation(W, until_t=None, duration_t=None):
start_ts = W.T
if until_t != None:
end_ts = int(until_t/W.DELTAT)
elif duration_t2 != None:
end_ts = start_ts + int(duration_t2/W.DELTAT)-1
elif duration_t != None:
end_ts = start_ts + int(duration_t/W.DELTAT)
else:
Expand All @@ -2184,7 +2191,7 @@ def exec_simulation(W, until_t=None, duration_t=None):
W.simulation_terminated()
return 1 #end of simulation
if end_ts < start_ts:
raise Exception("exec_simulation error: Simulation duration is negative. Check until_t or duration_t")
raise Exception("exec_simulation error: Simulation duration is not positive. Check until_t or duration_t or duration_t2")

#the main loop
#print("preping:", W.T, start_ts, end_ts, W.check_simulation_ongoing())
Expand Down Expand Up @@ -2234,6 +2241,7 @@ def exec_simulation(W, until_t=None, duration_t=None):
break

W.T += 1
W.TIME = W.T*W.DELTAT

if W.T == W.TSIZE:
if W.print_mode and W.show_progress:
Expand Down
Loading