From b315d69bda175ff1d29022c39c35e533612ccc9e Mon Sep 17 00:00:00 2001 From: James Lawlor Date: Wed, 3 Apr 2024 19:35:54 +0300 Subject: [PATCH 1/2] add move and colour histories, plot colour history in static but disable --- src/pyturmite/constants.py | 2 +- src/pyturmite/main.py | 4 ++-- src/pyturmite/turmites.py | 20 ++++++++++++++------ src/pyturmite/utils/plotters.py | 13 ++++++++++++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/pyturmite/constants.py b/src/pyturmite/constants.py index 1070f83..afea2ee 100644 --- a/src/pyturmite/constants.py +++ b/src/pyturmite/constants.py @@ -1,5 +1,5 @@ CANVAS_SIZE = 8 -N_STEPS = 12000 +N_STEPS = 20000 PLOT_MODE = "animate" # animate | static CMAP = "Oranges" RULESET = "classic" # 'classic' or 'stateful' diff --git a/src/pyturmite/main.py b/src/pyturmite/main.py index 35e1422..558f697 100644 --- a/src/pyturmite/main.py +++ b/src/pyturmite/main.py @@ -1,7 +1,8 @@ from pyturmite.turmites import ClassicTurmite, StatefulTurmite from pyturmite.utils.plotters import Plotter from pyturmite.utils.input_handling import parse_args - +import numpy as np +import matplotlib def main(): args = parse_args() @@ -14,6 +15,5 @@ def main(): plotter = Plotter(mode=args.plot_mode) plotter.plot(turmite=turmite, n_steps=args.n_steps) - if __name__ == "__main__": main() diff --git a/src/pyturmite/turmites.py b/src/pyturmite/turmites.py index 177eec1..0a6027d 100644 --- a/src/pyturmite/turmites.py +++ b/src/pyturmite/turmites.py @@ -35,6 +35,8 @@ def __init__( self.cmap = plt.get_cmap(CMAP) self.n_colours = 0 self.parse_instructions(instructions) + self.movement_history = [] + self.colour_history = [] def __str__(self): return str(vars(self)) @@ -147,17 +149,23 @@ def parse_instructions(self, instructions): colours = self.cmap(np.linspace(0.0, 1, len(instructions))) self.n_colours = len(colours) - self.instructions = { + self.colour_to_instruction_mappings = { ix: { "colour": colours[ix], - "instruction": self.instruction_to_func(instruction), + "instruction": instruction, + "instruction_function": self.instruction_to_func(instruction), } for ix, instruction in enumerate(instructions) } def turn(self, colour): - turn_instruction = self.instructions[colour]["instruction"] - turn_instruction() + self.colour_history.append(colour) + + colour_to_instruction_mapping = self.colour_to_instruction_mappings[colour] + instruction = colour_to_instruction_mapping["instruction"] + self.movement_history.append(instruction) + turn_instruction_function = colour_to_instruction_mapping["instruction_function"] + turn_instruction_function() def update(self): colour = self.check_square_colour(self.x, self.y) @@ -187,10 +195,10 @@ def parse_instructions(self, input_instructions): colours = self.cmap(np.linspace(0.0, 1, len(input_instructions[0]))) self.n_colours = len(colours) self.n_states = len(input_instructions) - self.instructions = np.array(input_instructions) + self.instruction_mappings = np.array(input_instructions) def turn(self, colour, state): - colour_state_tuple = self.instructions[state][colour] + colour_state_tuple = self.instruction_mappings[state][colour] # change state self.state = int(colour_state_tuple[-1]) diff --git a/src/pyturmite/utils/plotters.py b/src/pyturmite/utils/plotters.py index b421dd7..e59dd39 100644 --- a/src/pyturmite/utils/plotters.py +++ b/src/pyturmite/utils/plotters.py @@ -24,10 +24,11 @@ def static_plot( self, turmite, n_steps, + plot_history=False, ): for _ in range(n_steps): turmite.update() - + data = np.array(turmite.grid) data[turmite.x][turmite.y] = turmite.n_colours + 1 plt.figure() @@ -40,6 +41,16 @@ def static_plot( ) plt.show() + if plot_history: + colour_history = turmite.colour_history + step_list = range(1,len(colour_history)) + cumulative_average_of_colours_visited = [np.mean(colour_history[:_]) for _ in step_list] + plt.plot(step_list, cumulative_average_of_colours_visited) + plt.xscale('log') + plt.ylabel("Cumulative average of visited square colour") + plt.xlabel("Number of steps") + plt.show() + def animate(self, turmite, n_steps): fig = plt.figure() From 955772ab413f6f06b4b75137d8bc763b41dd2913 Mon Sep 17 00:00:00 2001 From: James Lawlor Date: Wed, 3 Apr 2024 19:37:14 +0300 Subject: [PATCH 2/2] lint --- src/pyturmite/main.py | 4 ++-- src/pyturmite/turmites.py | 4 +++- src/pyturmite/utils/plotters.py | 10 ++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/pyturmite/main.py b/src/pyturmite/main.py index 558f697..35e1422 100644 --- a/src/pyturmite/main.py +++ b/src/pyturmite/main.py @@ -1,8 +1,7 @@ from pyturmite.turmites import ClassicTurmite, StatefulTurmite from pyturmite.utils.plotters import Plotter from pyturmite.utils.input_handling import parse_args -import numpy as np -import matplotlib + def main(): args = parse_args() @@ -15,5 +14,6 @@ def main(): plotter = Plotter(mode=args.plot_mode) plotter.plot(turmite=turmite, n_steps=args.n_steps) + if __name__ == "__main__": main() diff --git a/src/pyturmite/turmites.py b/src/pyturmite/turmites.py index 0a6027d..4e40308 100644 --- a/src/pyturmite/turmites.py +++ b/src/pyturmite/turmites.py @@ -164,7 +164,9 @@ def turn(self, colour): colour_to_instruction_mapping = self.colour_to_instruction_mappings[colour] instruction = colour_to_instruction_mapping["instruction"] self.movement_history.append(instruction) - turn_instruction_function = colour_to_instruction_mapping["instruction_function"] + turn_instruction_function = colour_to_instruction_mapping[ + "instruction_function" + ] turn_instruction_function() def update(self): diff --git a/src/pyturmite/utils/plotters.py b/src/pyturmite/utils/plotters.py index e59dd39..a6e44f9 100644 --- a/src/pyturmite/utils/plotters.py +++ b/src/pyturmite/utils/plotters.py @@ -28,7 +28,7 @@ def static_plot( ): for _ in range(n_steps): turmite.update() - + data = np.array(turmite.grid) data[turmite.x][turmite.y] = turmite.n_colours + 1 plt.figure() @@ -43,10 +43,12 @@ def static_plot( if plot_history: colour_history = turmite.colour_history - step_list = range(1,len(colour_history)) - cumulative_average_of_colours_visited = [np.mean(colour_history[:_]) for _ in step_list] + step_list = range(1, len(colour_history)) + cumulative_average_of_colours_visited = [ + np.mean(colour_history[:_]) for _ in step_list + ] plt.plot(step_list, cumulative_average_of_colours_visited) - plt.xscale('log') + plt.xscale("log") plt.ylabel("Cumulative average of visited square colour") plt.xlabel("Number of steps") plt.show()