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

add move and colour histories, plot colour history in static but disable #15

Merged
merged 2 commits into from
Apr 3, 2024
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 src/pyturmite/constants.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
22 changes: 16 additions & 6 deletions src/pyturmite/turmites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -147,17 +149,25 @@ 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)
Expand Down Expand Up @@ -187,10 +197,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])
Expand Down
13 changes: 13 additions & 0 deletions src/pyturmite/utils/plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def static_plot(
self,
turmite,
n_steps,
plot_history=False,
):
for _ in range(n_steps):
turmite.update()
Expand All @@ -40,6 +41,18 @@ 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()

Expand Down
Loading