import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def visualize_path(start, end, boardsize, path_str='', save_string=''):
grid_size = boardsize
# You can adjust this based on the size of the grid you want
fig, ax = plt.subplots(figsize=(7, 7))
plt.xticks([i + 0.5 for i in range(grid_size)], minor=True) # Add minor ticks
plt.yticks([i + 0.5 for i in range(grid_size)], minor=True) # Add minor ticks
ax.grid(which='minor', linestyle='-', linewidth=0.5, color='black') # Show minor gridlines
# Create a custom colormap that transitions from red to green
cmap = plt.cm.get_cmap('RdYlGn')
norm = plt.Normalize(0, len(path_str))
# Function to update the position and draw the path movement in each frame
def update(frame):
ax.clear()
ax.set_xticks([i + 0.5 for i in range(grid_size)], minor=True) # Add minor ticks
ax.set_yticks([i + 0.5 for i in range(grid_size)], minor=True) # Add minor ticks
ax.grid(which='minor', linestyle='-', linewidth=0.5, color='black') # Show minor gridlines
ax.set_title('Visualization of Path')
ax.text(start[0], start[1], 'S', ha='center', va='center', fontsize=20, fontweight='bold', color='red')
ax.text(end[0], end[1], 'E', ha='center', va='center', fontsize=20, fontweight='bold', color='green')
# Initialize the position to the starting point
x, y = start
# Plot the path up to the current frame
for i in range(frame + 1):
if i < len(path_str):
move = path_str[i]
color = cmap(norm(i))
if move == 'u':
if y - 1 < 0:
ax.plot([x, x], [y-1, y], 'o-', color=color)
y = (y - 1) % grid_size
ax.plot([x, x], [y+1, y], 'o-', color=color)
elif move == 'd':
if y + 1 > grid_size-1:
ax.plot([x, x], [y, y+1], 'o-', color=color)
y = (y + 1) % grid_size
ax.plot([x, x], [y-1, y], 'o-', color=color)
elif move == 'l':
if x - 1 < 0:
ax.plot([x-1, x], [y, y], 'o-', color=color)
x = (x - 1) % grid_size
ax.plot([x+1, x], [y, y], 'o-', color=color)
elif move == 'r':
if x + 1 > grid_size-1:
ax.plot([x, x+1], [y, y], 'o-', color=color)
x = (x + 1) % grid_size
ax.plot([x, x-1], [y, y], 'o-', color=color)
elif move == 'n':
pass # Do nothing if 'n' is encountered in the path
ax.set_xlim(-0.5, grid_size - 0.5)
ax.set_ylim(-0.5, grid_size - 0.5)
ax.set_aspect('equal', adjustable='box')
ax.invert_yaxis()
# Animate the plot
ani = FuncAnimation(fig, update, frames=len(path_str), interval=1000, repeat=False)
# Save animation as a GIF using Pillow
save_path = f'/Users/jan-philipppollmeier/Desktop/MA_Fred/Gifs/best_path_gen{save_string}.gif'
ani.save(save_path, writer='pillow', fps=1)
def visualize_generation(start, end, boardsize, generation, save_string=''):
grid_size = boardsize
fig, ax = plt.subplots(figsize=(7, 7))
plt.xticks([i for i in range(grid_size + 1)], minor=True) # Add minor ticks
plt.yticks([i for i in range(grid_size + 1)], minor=True) # Add minor ticks
ax.grid(which='minor', linestyle='-', linewidth=0.5, color='black') # Show minor gridlines
cmap = plt.cm.get_cmap('RdYlGn_r')
num_paths = len(generation.paths)
colors = [cmap(i / num_paths) for i in range(num_paths)]
def update(frame):
ax.clear()
ax.set_xticks([i + 0.5 for i in range(grid_size)], minor=True) # Add minor ticks
ax.set_yticks([i + 0.5 for i in range(grid_size)], minor=True) # Add minor ticks
ax.grid(which='minor', linestyle='-', linewidth=0.5, color='black') # Show minor gridlines
ax.set_title('Visualization of Generation of Paths')
ax.text(start[0], start[1], 'S', ha='center', va='center', fontsize=20, fontweight='bold', color='red')
ax.text(end[0], end[1], 'E', ha='center', va='center', fontsize=20, fontweight='bold', color='green')
x = []
y = []
off = []
for i in range(num_paths):
x.append(start[0])
y.append(start[1])
off.append(0.8/num_paths*i)
for i in range(frame + 1):
for j, path in enumerate(generation.paths):
path_str = path.string
if i < len(path_str):
move = path_str[i]
color = colors[j]
alpha = 1.0 - (i / (len(path_str)+1)) # Decrease alpha with each segment
color_with_alpha = list(color[:3]) + [alpha]
if move == 'u':
if y[j] - 1 < 0:
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]], [y[j]-0.4+off[j]-1, y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
y[j]= (y[j]- 1) % grid_size
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]], [y[j]-0.4+off[j]+1, y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
elif move == 'd':
if y[j]+ 1 > grid_size-1:
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]], [y[j]-0.4+off[j], y[j]-0.4+off[j]+1], 'o-', color=color_with_alpha)
y[j]= (y[j]+ 1) % grid_size
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]], [y[j]-0.4+off[j]-1, y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
elif move == 'l':
if x[j] - 1 < 0:
ax.plot([x[j]-0.4+off[j]-1, x[j]-0.4+off[j]], [y[j]-0.4+off[j], y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
x[j] = (x[j] - 1) % grid_size
ax.plot([x[j]-0.4+off[j]+1, x[j]-0.4+off[j]], [y[j]-0.4+off[j], y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
elif move == 'r':
if x[j] + 1 > grid_size-1:
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]+1], [y[j]-0.4+off[j], y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
x[j] = (x[j] + 1) % grid_size
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]-1], [y[j]-0.4+off[j], y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
elif move == 'n':
ax.plot([x[j]-0.4+off[j], x[j]-0.4+off[j]], [y[j]-0.4+off[j], y[j]-0.4+off[j]], 'o-', color=color_with_alpha)
pass # Do nothing if 'n' is encountered in the path
# Add the segment number to the current dot
#ax.text(x[j], y[j], str(i), color='white', fontsize=8, ha='center', va='center')
ax.set_xlim(-0.5, grid_size - 0.5)
ax.set_ylim(-0.5, grid_size - 0.5)
ax.set_aspect('equal', adjustable='box')
ax.invert_yaxis()
num_frames = len(generation.paths[0].string)
ani = FuncAnimation(fig, update, frames=num_frames, interval=1000, repeat=False)
# Save animation as a GIF using Pillow
save_path = f'/Users/jan-philipppollmeier/Desktop/MA_Fred/Gifs/visualization_gen{save_string}.gif'
ani.save(save_path, writer='pillow', fps=1)