Team Ai
Apppublic

SvenSommer/genetic_algorithm_playground

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
plot.py71 linesDownload Raw Back to root
1import random2import matplotlib.pyplot as plt3import numpy as np4from io import BytesIO5 6def plot_strategie_value_over_generations(generation_data, generations):7    plt.figure(figsize=(10, 6))8 9    for generation in range(generations):10        plt.scatter(11            [generation] * len(generation_data[generation]),12            generation_data[generation],13            label=f"Gen {generation + 1}",14        )15 16    plt.title("Evolution of Strategie Value Across Generations")17    plt.xlabel("Generation")18    plt.ylabel("Strategie Value")19    plt.legend()20    plt.show()21 22def smooth_data(data, window_size):23    """ Smooths the data using a moving average. """24    return np.convolve(data, np.ones(window_size)/window_size, mode='valid')25 26def plot_average_variation(generation_data, generations, attempts_data, window_size=5):27    # Calculate the average Strategie Values for each generation28    average_strategie_values = [sum(generation) / len(generation) for generation in generation_data]29    # Calculate the standard deviation of Strategie Values for each generation30    std_strategie_values = [np.std(generation) for generation in generation_data]31    # Calculate the average of attempts for each generation32    average_attempts = [sum(attempts) / len(attempts) for attempts in attempts_data]33    # Smoothing the average attempts34    smoothed_attempts = smooth_data(average_attempts, window_size)35 36    fig, ax1 = plt.subplots(figsize=(12, 6))37 38    color = 'tab:blue'39    ax1.set_xlabel('Generation')40    ax1.set_ylabel('Strategie Value', color=color)41    ax1.plot(range(generations), average_strategie_values, label="Average Strategie Value", color=color)42    ax1.fill_between(range(generations), 43                     [average - std for average, std in zip(average_strategie_values, std_strategie_values)], 44                     [average + std for average, std in zip(average_strategie_values, std_strategie_values)], 45                     color="lightblue", alpha=0.5)46    ax1.tick_params(axis='y', labelcolor=color)47 48    ax2 = ax1.twinx()  # Second y-axis for the average attempts49    color = 'tab:red'50    ax2.set_ylabel('Average Attempts (Smoothed)', color=color)  51    ax2.plot(range(window_size - 1, generations), smoothed_attempts, label="Average Attempts (Smoothed)", color=color)52    ax2.tick_params(axis='y', labelcolor=color)53 54    fig.tight_layout()  # For a clean layout55    plt.title("Average and Variation of Strategie Value Over Generations with Average of Attempts (Smoothed)")56    plt.show()57 58    return fig59 60def plot_randomness(target_numbers):61    fig = plt.figure(figsize=(12, 6))62    generations = list(range(1, len(target_numbers) + 1))63 64    plt.scatter(generations, target_numbers, alpha=0.7)65    plt.title("Target Numbers per Generation")66    plt.xlabel("Generation")67    plt.ylabel("Target Number")68    plt.grid(True)69 70    return fig71