From 58a7b9fdd113588f424668b3774be5a699726de8 Mon Sep 17 00:00:00 2001 From: henri Date: Sun, 24 Oct 2021 18:52:12 +0200 Subject: [PATCH 01/10] add implementation of Nagel and Schrekenberg algo --- cellular_automata/nasch.py | 148 +++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 cellular_automata/nasch.py diff --git a/cellular_automata/nasch.py b/cellular_automata/nasch.py new file mode 100644 index 000000000000..2f9c8353ee9f --- /dev/null +++ b/cellular_automata/nasch.py @@ -0,0 +1,148 @@ +from random import randint, random + +""" +This algorithm simulate the evolution of a highway with only one road +The highway is divided in cells, each cell can have at most one car in it +The highway make a loop. That means that when a car comes to one end, they will + come out of the other +A car is represented by it's speed (from 0 to 5) + +Some information about speed: + -1 means that the cell on the highway is empty + 0 to 5 are the speed of the cars with 0 being the lowest and 5 the highest + +More information here: https://en.wikipedia.org/wiki/Nagel%E2%80%93Schreckenberg_model + +Examples for doctest: +>>>highway = construct_highway(6, 3, 0) +>>>simulate(highway, 2, 0, 2) +[[0, -1, -1, 0, -1, -1], [-1, 1, -1, -1, 1, -1], [-1, -1, 1, -1, -1, 1]] +>>>highway = construct_highway(5, 2, -2) +>>>simulate(highway, 3, 0, 2) +[[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]] +""" + +PROBABILITY = 0.1 # The probability that a driver will slow down +SPEED_START = 1 # The speed of the cars a the start +FREQUENCY = 5 # How many cells there are between two cars at the start +MAX_SPEED = 5 # The maximum speed a car will go to +NUMBER_OF_CELLS = 1024 # How many cell are there in the highway +NUMBER_OF_UPDATE = 2048 # How many times will the position be updated +highway = [] # Where every position and speed of every car will be stored + + +def construct_highway( + NUMBER_OF_CELLS: int, + FREQUENCY: int, + SPEED_START: int, + random_frequency: bool = False, + random_speed: bool = False, + MAX_SPEED: int = 5, +) -> list: + """ + Build the highway following the parameters given + >>> construct_highway(10, 2, 6) + [[6, -1, 6, -1, 6, -1, 6, -1, 6, -1]] + >>> construct_highway(10, 10, 2) + [[2, -1, -1, -1, -1, -1, -1, -1, -1, -1]] + """ + + highway = [[-1] * NUMBER_OF_CELLS] # Create a highway without any car + i = 0 + if SPEED_START < 0: + SPEED_START = 0 + while i < NUMBER_OF_CELLS: + if random_speed: + highway[0][i] = randint(0, MAX_SPEED) + else: + highway[0][i] = SPEED_START # Place the cars + if random_frequency: + i += randint(1, MAX_SPEED * 2) # Arbitrary number, may need tuning + else: + i += FREQUENCY + return highway + + +def get_distance(highway_now: list, car_index: int) -> int: + """ + Get the distance between a car (at index car_index) and the next car + >>> get_distance([6,-1,6,-1,6], 2) + 1 + >>> get_distance([2,-1,-1,-1,3,1,0,1,3,2], 0) + 3 + >>> get_distance([-1,-1,-1,-1,2,-1,-1,-1,3], -1) + 4 + """ + + distance = 0 + cells = highway_now[car_index + 1 :] + for cell in range(len(cells)): # May need a better name for this + if cells[cell] != -1: # If the cell is not empty then + return distance # we have the distance we wanted + else: + distance += 1 + # Here if the car is near the end of the highway + distance += get_distance(highway_now, -1) + return distance + + +def update(highway_now: list, PROBABILITY: float, MAX_SPEED: int) -> list: + """ + Update the speed of the cars + >>> update([-1,-1,-1,-1,-1,2,-1,-1,-1,-1,3], 0.0, 5) + [-1, -1, -1, -1, -1, 3, -1, -1, -1, -1, 4] + >>> update([-1,-1,2,-1,-1,-1,-1,3], 0.0, 5) + [-1, -1, 3, -1, -1, -1, -1, 1] + """ + + NUMBER_OF_CELLS = len(highway_now) + # Beforce calculations, the highway is empty + next_highway = [-1] * NUMBER_OF_CELLS + + for car_index in range(NUMBER_OF_CELLS): + if highway_now[car_index] != -1: + # Add 1 to the current speed of the car and cap the speed + next_highway[car_index] = min(highway_now[car_index] + 1, MAX_SPEED) + # Number of empty cell before the next car + dn = get_distance(highway_now, car_index) - 1 + # We can't have the car causing an accident + next_highway[car_index] = min(next_highway[car_index], dn) + if random() < PROBABILITY: + # Randomly, a driver will slow down + next_highway[car_index] = max(next_highway[car_index] - 1, 0) + return next_highway + + +def simulate( + highway: list, NUMBER_OF_UPDATE: int, PROBABILITY: float, MAX_SPEED: int +) -> list: + """ + The main function, it will simulate the evolution of the highway + >>> simulate([[-1,2,-1,-1,-1,3]], 2, 0.0, 3) + [[-1, 2, -1, -1, -1, 3], [-1, -1, -1, 2, -1, 0], [1, -1, -1, 0, -1, -1]] + >>> simulate([[-1,2,-1,3]], 4, 0.0, 3) + [[-1, 2, -1, 3], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0]] + """ + + NUMBER_OF_CELLS = len(highway[0]) + + for i in range(NUMBER_OF_UPDATE): + next_speeds_calculated = update(highway[i], PROBABILITY, MAX_SPEED) + real_next_speeds = [-1] * NUMBER_OF_CELLS + + for car_index in range(NUMBER_OF_CELLS): + speed = next_speeds_calculated[car_index] + if speed != -1: + # Change the position based on the speed (with % to create the loop) + index = (car_index + speed) % NUMBER_OF_CELLS + # Commit the change of position + real_next_speeds[index] = speed + highway.append(real_next_speeds) + + return highway + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From df0c01791bf91084ce695fdef6102ce68b9cf956 Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 21:52:53 +0200 Subject: [PATCH 02/10] Update cellular_automata/nasch.py Co-authored-by: Christian Clauss --- cellular_automata/nasch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellular_automata/nasch.py b/cellular_automata/nasch.py index 2f9c8353ee9f..0f0eeea322f1 100644 --- a/cellular_automata/nasch.py +++ b/cellular_automata/nasch.py @@ -14,7 +14,7 @@ More information here: https://en.wikipedia.org/wiki/Nagel%E2%80%93Schreckenberg_model Examples for doctest: ->>>highway = construct_highway(6, 3, 0) +>>> highway = construct_highway(6, 3, 0) >>>simulate(highway, 2, 0, 2) [[0, -1, -1, 0, -1, -1], [-1, 1, -1, -1, 1, -1], [-1, -1, 1, -1, -1, 1]] >>>highway = construct_highway(5, 2, -2) From 2abdc96a36fb629d4a763f2523265aee24b8f2eb Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 22:54:26 +0200 Subject: [PATCH 03/10] Update nasch.py --- cellular_automata/nasch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cellular_automata/nasch.py b/cellular_automata/nasch.py index 0f0eeea322f1..7f6bfbf762ee 100644 --- a/cellular_automata/nasch.py +++ b/cellular_automata/nasch.py @@ -15,10 +15,10 @@ Examples for doctest: >>> highway = construct_highway(6, 3, 0) ->>>simulate(highway, 2, 0, 2) +>>> simulate(highway, 2, 0, 2) [[0, -1, -1, 0, -1, -1], [-1, 1, -1, -1, 1, -1], [-1, -1, 1, -1, -1, 1]] ->>>highway = construct_highway(5, 2, -2) ->>>simulate(highway, 3, 0, 2) +>>> highway = construct_highway(5, 2, -2) +>>> simulate(highway, 3, 0, 2) [[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]] """ From 9921fdc45d107c1515ddb037cb7d162e38bfc3b5 Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:24:53 +0200 Subject: [PATCH 04/10] Update and rename nasch.py to nagel_schrekenberg.py --- cellular_automata/{nasch.py => nagel_schrekenberg.py} | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename cellular_automata/{nasch.py => nagel_schrekenberg.py} (98%) diff --git a/cellular_automata/nasch.py b/cellular_automata/nagel_schrekenberg.py similarity index 98% rename from cellular_automata/nasch.py rename to cellular_automata/nagel_schrekenberg.py index 7f6bfbf762ee..b15cb60ade52 100644 --- a/cellular_automata/nasch.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -82,8 +82,7 @@ def get_distance(highway_now: list, car_index: int) -> int: else: distance += 1 # Here if the car is near the end of the highway - distance += get_distance(highway_now, -1) - return distance + return distance + get_distance(highway_now, -1) def update(highway_now: list, PROBABILITY: float, MAX_SPEED: int) -> list: From 50905fa2d8d472ff1ed3b529e0b071477072da93 Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:42:18 +0200 Subject: [PATCH 05/10] Update cellular_automata/nagel_schrekenberg.py Co-authored-by: Christian Clauss --- cellular_automata/nagel_schrekenberg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index b15cb60ade52..fae9f8447d42 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -14,7 +14,7 @@ More information here: https://en.wikipedia.org/wiki/Nagel%E2%80%93Schreckenberg_model Examples for doctest: ->>> highway = construct_highway(6, 3, 0) +>>> simulate(construct_highway(6, 3, 0), 2, 0, 2) >>> simulate(highway, 2, 0, 2) [[0, -1, -1, 0, -1, -1], [-1, 1, -1, -1, 1, -1], [-1, -1, 1, -1, -1, 1]] >>> highway = construct_highway(5, 2, -2) From 4df9e0b50dba20bf014fbbf918f6d26b56c0488d Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:47:45 +0200 Subject: [PATCH 06/10] Update nagel_schrekenberg.py --- cellular_automata/nagel_schrekenberg.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index fae9f8447d42..1e559bf208b7 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -15,10 +15,8 @@ Examples for doctest: >>> simulate(construct_highway(6, 3, 0), 2, 0, 2) ->>> simulate(highway, 2, 0, 2) [[0, -1, -1, 0, -1, -1], [-1, 1, -1, -1, 1, -1], [-1, -1, 1, -1, -1, 1]] ->>> highway = construct_highway(5, 2, -2) ->>> simulate(highway, 3, 0, 2) +>>> simulate(construct_highway(5, 2, -2), 3, 0, 2) [[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]] """ @@ -32,7 +30,7 @@ def construct_highway( - NUMBER_OF_CELLS: int, + number_of_cells: int, FREQUENCY: int, SPEED_START: int, random_frequency: bool = False, @@ -52,14 +50,8 @@ def construct_highway( if SPEED_START < 0: SPEED_START = 0 while i < NUMBER_OF_CELLS: - if random_speed: - highway[0][i] = randint(0, MAX_SPEED) - else: - highway[0][i] = SPEED_START # Place the cars - if random_frequency: - i += randint(1, MAX_SPEED * 2) # Arbitrary number, may need tuning - else: - i += FREQUENCY + highway[0][i] = randint(0, MAX_SPEED) if random_speed else SPEED_START # Place the cars + i = i + randint(1, MAX_SPEED * 2) if random_frequency else i + FREQUENCY # Arbitrary number, may need tuning return highway From 926e87ad5075f839b7b0cd405b326fa26d3f1c2c Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:49:22 +0200 Subject: [PATCH 07/10] Update nagel_schrekenberg.py --- cellular_automata/nagel_schrekenberg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index 1e559bf208b7..8e3d8f5142b7 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -51,7 +51,7 @@ def construct_highway( SPEED_START = 0 while i < NUMBER_OF_CELLS: highway[0][i] = randint(0, MAX_SPEED) if random_speed else SPEED_START # Place the cars - i = i + randint(1, MAX_SPEED * 2) if random_frequency else i + FREQUENCY # Arbitrary number, may need tuning + i += randint(1, MAX_SPEED * 2) if random_frequency else FREQUENCY # Arbitrary number, may need tuning return highway From 842dde361daa05f350bab90787d38226e2da8fad Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:56:28 +0200 Subject: [PATCH 08/10] Update nagel_schrekenberg.py --- cellular_automata/nagel_schrekenberg.py | 59 +++++++++++++------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index 8e3d8f5142b7..1c798662eabb 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -21,21 +21,21 @@ """ PROBABILITY = 0.1 # The probability that a driver will slow down -SPEED_START = 1 # The speed of the cars a the start -FREQUENCY = 5 # How many cells there are between two cars at the start -MAX_SPEED = 5 # The maximum speed a car will go to -NUMBER_OF_CELLS = 1024 # How many cell are there in the highway -NUMBER_OF_UPDATE = 2048 # How many times will the position be updated +initial_speed = 1 # The speed of the cars a the start +frequency = 5 # How many cells there are between two cars at the start +max_speed = 5 # The maximum speed a car will go to +number_of_cells = 1024 # How many cell are there in the highway +number_of_update = 2048 # How many times will the position be updated highway = [] # Where every position and speed of every car will be stored def construct_highway( number_of_cells: int, - FREQUENCY: int, - SPEED_START: int, + frequency: int, + initial_speed: int, random_frequency: bool = False, random_speed: bool = False, - MAX_SPEED: int = 5, + max_speed: int = 5, ) -> list: """ Build the highway following the parameters given @@ -45,13 +45,17 @@ def construct_highway( [[2, -1, -1, -1, -1, -1, -1, -1, -1, -1]] """ - highway = [[-1] * NUMBER_OF_CELLS] # Create a highway without any car + highway = [[-1] * number_of_cells] # Create a highway without any car i = 0 - if SPEED_START < 0: - SPEED_START = 0 - while i < NUMBER_OF_CELLS: - highway[0][i] = randint(0, MAX_SPEED) if random_speed else SPEED_START # Place the cars - i += randint(1, MAX_SPEED * 2) if random_frequency else FREQUENCY # Arbitrary number, may need tuning + if initial_speed < 0: + initial_speed = 0 + while i < number_of_cells: + highway[0][i] = ( + randint(0, max_speed) if random_speed else initial_speed + ) # Place the cars + i += ( + randint(1, max_speed * 2) if random_frequency else frequency + ) # Arbitrary number, may need tuning return highway @@ -71,13 +75,12 @@ def get_distance(highway_now: list, car_index: int) -> int: for cell in range(len(cells)): # May need a better name for this if cells[cell] != -1: # If the cell is not empty then return distance # we have the distance we wanted - else: - distance += 1 + distance += 1 # Here if the car is near the end of the highway return distance + get_distance(highway_now, -1) -def update(highway_now: list, PROBABILITY: float, MAX_SPEED: int) -> list: +def update(highway_now: list, PROBABILITY: float, max_speed: int) -> list: """ Update the speed of the cars >>> update([-1,-1,-1,-1,-1,2,-1,-1,-1,-1,3], 0.0, 5) @@ -86,14 +89,14 @@ def update(highway_now: list, PROBABILITY: float, MAX_SPEED: int) -> list: [-1, -1, 3, -1, -1, -1, -1, 1] """ - NUMBER_OF_CELLS = len(highway_now) + number_of_cells = len(highway_now) # Beforce calculations, the highway is empty - next_highway = [-1] * NUMBER_OF_CELLS + next_highway = [-1] * number_of_cells - for car_index in range(NUMBER_OF_CELLS): + for car_index in range(number_of_cells): if highway_now[car_index] != -1: # Add 1 to the current speed of the car and cap the speed - next_highway[car_index] = min(highway_now[car_index] + 1, MAX_SPEED) + next_highway[car_index] = min(highway_now[car_index] + 1, max_speed) # Number of empty cell before the next car dn = get_distance(highway_now, car_index) - 1 # We can't have the car causing an accident @@ -105,7 +108,7 @@ def update(highway_now: list, PROBABILITY: float, MAX_SPEED: int) -> list: def simulate( - highway: list, NUMBER_OF_UPDATE: int, PROBABILITY: float, MAX_SPEED: int + highway: list, number_of_update: int, PROBABILITY: float, max_speed: int ) -> list: """ The main function, it will simulate the evolution of the highway @@ -115,17 +118,17 @@ def simulate( [[-1, 2, -1, 3], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0]] """ - NUMBER_OF_CELLS = len(highway[0]) + number_of_cells = len(highway[0]) - for i in range(NUMBER_OF_UPDATE): - next_speeds_calculated = update(highway[i], PROBABILITY, MAX_SPEED) - real_next_speeds = [-1] * NUMBER_OF_CELLS + for i in range(number_of_update): + next_speeds_calculated = update(highway[i], PROBABILITY, max_speed) + real_next_speeds = [-1] * number_of_cells - for car_index in range(NUMBER_OF_CELLS): + for car_index in range(number_of_cells): speed = next_speeds_calculated[car_index] if speed != -1: # Change the position based on the speed (with % to create the loop) - index = (car_index + speed) % NUMBER_OF_CELLS + index = (car_index + speed) % number_of_cells # Commit the change of position real_next_speeds[index] = speed highway.append(real_next_speeds) From 0fe0715223e9855220d1e41b3fe87f3a90bdcb70 Mon Sep 17 00:00:00 2001 From: henri Date: Mon, 25 Oct 2021 00:07:51 +0200 Subject: [PATCH 09/10] update nagel_schrekenberg.py --- cellular_automata/nagel_schrekenberg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index 1c798662eabb..4c4e291d48c5 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -26,7 +26,7 @@ max_speed = 5 # The maximum speed a car will go to number_of_cells = 1024 # How many cell are there in the highway number_of_update = 2048 # How many times will the position be updated -highway = [] # Where every position and speed of every car will be stored +highway: list[int] = [] # Where every position and speed of every car will be stored def construct_highway( From a00fed8e7d2e7966ac528a011be43983bcbf7f1f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 26 Oct 2021 09:53:01 +0200 Subject: [PATCH 10/10] Update nagel_schrekenberg.py --- cellular_automata/nagel_schrekenberg.py | 50 ++++++++++++------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index 4c4e291d48c5..be44761ecf82 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -1,16 +1,21 @@ -from random import randint, random - """ -This algorithm simulate the evolution of a highway with only one road -The highway is divided in cells, each cell can have at most one car in it -The highway make a loop. That means that when a car comes to one end, they will - come out of the other -A car is represented by it's speed (from 0 to 5) +Simulate the evolution of a highway with only one road that is a loop. +The highway is divided in cells, each cell can have at most one car in it. +The highway is a loop so when a car comes to one end, it will come out on the other. +Each car is represented by its speed (from 0 to 5). Some information about speed: -1 means that the cell on the highway is empty 0 to 5 are the speed of the cars with 0 being the lowest and 5 the highest +highway: list[int] Where every position and speed of every car will be stored +probability The probability that a driver will slow down +initial_speed The speed of the cars a the start +frequency How many cells there are between two cars at the start +max_speed The maximum speed a car can go to +number_of_cells How many cell are there in the highway +number_of_update How many times will the position be updated + More information here: https://en.wikipedia.org/wiki/Nagel%E2%80%93Schreckenberg_model Examples for doctest: @@ -19,14 +24,7 @@ >>> simulate(construct_highway(5, 2, -2), 3, 0, 2) [[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]] """ - -PROBABILITY = 0.1 # The probability that a driver will slow down -initial_speed = 1 # The speed of the cars a the start -frequency = 5 # How many cells there are between two cars at the start -max_speed = 5 # The maximum speed a car will go to -number_of_cells = 1024 # How many cell are there in the highway -number_of_update = 2048 # How many times will the position be updated -highway: list[int] = [] # Where every position and speed of every car will be stored +from random import randint, random def construct_highway( @@ -62,11 +60,11 @@ def construct_highway( def get_distance(highway_now: list, car_index: int) -> int: """ Get the distance between a car (at index car_index) and the next car - >>> get_distance([6,-1,6,-1,6], 2) + >>> get_distance([6, -1, 6, -1, 6], 2) 1 - >>> get_distance([2,-1,-1,-1,3,1,0,1,3,2], 0) + >>> get_distance([2, -1, -1, -1, 3, 1, 0, 1, 3, 2], 0) 3 - >>> get_distance([-1,-1,-1,-1,2,-1,-1,-1,3], -1) + >>> get_distance([-1, -1, -1, -1, 2, -1, -1, -1, 3], -1) 4 """ @@ -80,12 +78,12 @@ def get_distance(highway_now: list, car_index: int) -> int: return distance + get_distance(highway_now, -1) -def update(highway_now: list, PROBABILITY: float, max_speed: int) -> list: +def update(highway_now: list, probability: float, max_speed: int) -> list: """ Update the speed of the cars - >>> update([-1,-1,-1,-1,-1,2,-1,-1,-1,-1,3], 0.0, 5) + >>> update([-1, -1, -1, -1, -1, 2, -1, -1, -1, -1, 3], 0.0, 5) [-1, -1, -1, -1, -1, 3, -1, -1, -1, -1, 4] - >>> update([-1,-1,2,-1,-1,-1,-1,3], 0.0, 5) + >>> update([-1, -1, 2, -1, -1, -1, -1, 3], 0.0, 5) [-1, -1, 3, -1, -1, -1, -1, 1] """ @@ -101,27 +99,27 @@ def update(highway_now: list, PROBABILITY: float, max_speed: int) -> list: dn = get_distance(highway_now, car_index) - 1 # We can't have the car causing an accident next_highway[car_index] = min(next_highway[car_index], dn) - if random() < PROBABILITY: + if random() < probability: # Randomly, a driver will slow down next_highway[car_index] = max(next_highway[car_index] - 1, 0) return next_highway def simulate( - highway: list, number_of_update: int, PROBABILITY: float, max_speed: int + highway: list, number_of_update: int, probability: float, max_speed: int ) -> list: """ The main function, it will simulate the evolution of the highway - >>> simulate([[-1,2,-1,-1,-1,3]], 2, 0.0, 3) + >>> simulate([[-1, 2, -1, -1, -1, 3]], 2, 0.0, 3) [[-1, 2, -1, -1, -1, 3], [-1, -1, -1, 2, -1, 0], [1, -1, -1, 0, -1, -1]] - >>> simulate([[-1,2,-1,3]], 4, 0.0, 3) + >>> simulate([[-1, 2, -1, 3]], 4, 0.0, 3) [[-1, 2, -1, 3], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0]] """ number_of_cells = len(highway[0]) for i in range(number_of_update): - next_speeds_calculated = update(highway[i], PROBABILITY, max_speed) + next_speeds_calculated = update(highway[i], probability, max_speed) real_next_speeds = [-1] * number_of_cells for car_index in range(number_of_cells):