diff --git a/gui_with_pygame.py b/gui_with_pygame.py index 979ce48..f3b2880 100644 --- a/gui_with_pygame.py +++ b/gui_with_pygame.py @@ -8,83 +8,8 @@ import src.utils.config as conf import pickle -import math import src.utils.utils as util -class Graph: - def __init__(self, surface, position, size, data=None, bg_color=(255, 255, 255), line_color=(0, 0, 255), axis_color=(0, 0, 0)): - """ - Initializes the Graph object. - - :param surface: The pygame surface where the graph will be drawn - :param position: The (x, y) position of the top-left corner of the graph - :param size: A tuple (width, height) for the size of the graph - :param data: A list of data points to plot - :param bg_color: The background color of the graph area - :param line_color: The color of the graph line - :param axis_color: The color of the graph axes - """ - self.surface = surface - self.position = position - self.width, self.height = size - self.bg_color = bg_color - self.line_color = line_color - self.axis_color = axis_color - self.data = data if data is not None else [] - - self.padding = 20 # Padding for axes - - def add_data(self, point): - """ - Add a single data point to the graph. - """ - self.data.append(point) - - def draw_axes(self): - """ - Draws the X and Y axes of the graph. - """ - # X-axis - pygame.draw.line(self.surface, self.axis_color, - (self.position[0], self.position[1] + self.height - self.padding), - (self.position[0] + self.width, self.position[1] + self.height - self.padding), 2) - - # Y-axis - pygame.draw.line(self.surface, self.axis_color, - (self.position[0] + self.padding, self.position[1]), - (self.position[0] + self.padding, self.position[1] + self.height), 2) - - def draw(self): - """ - Draw the graph onto the provided surface. - """ - # Fill the graph area with background color - pygame.draw.rect(self.surface, self.bg_color, (*self.position, self.width, self.height)) - - # Draw the axes - self.draw_axes() - - # If there's no data to plot, return - if len(self.data) == 0: - return - - # Determine the scaling factor based on data range - max_data_value = max(self.data) - min_data_value = min(self.data) - y_range = max_data_value - min_data_value if max_data_value != min_data_value else 1 - - # Calculate scaling for x and y axes - x_scale = (self.width - 2 * self.padding) / max(1, len(self.data) - 1) - y_scale = (self.height - 2 * self.padding) / y_range - - # Plot the data points - for i in range(1, len(self.data)): - x1 = self.position[0] + self.padding + (i - 1) * x_scale - y1 = self.position[1] + self.height - self.padding - (self.data[i - 1] - min_data_value) * y_scale - x2 = self.position[0] + self.padding + i * x_scale - y2 = self.position[1] + self.height - self.padding - (self.data[i] - min_data_value) * y_scale - - pygame.draw.line(self.surface, self.line_color, (x1, y1), (x2, y2), 2) class GenomeManager: @@ -710,10 +635,6 @@ def update_screen(self): except: pass self.fitness_graph.draw(self.screen) - gen_data,best_fitness_data,avg_fitness_data,min_fitness_data = util.read_fitness_file("data/fitness/fitness_values.txt") - #self.new_fitness_graph = Graph(self.screen, (700, 100), (400, 400), best_fitness_data) - - #self.new_fitness_graph.draw() elif st.sc_selector == 2: diff --git a/main.py b/main.py index c39e3f7..afb7502 100644 --- a/main.py +++ b/main.py @@ -10,27 +10,27 @@ warnings.filterwarnings("ignore", category=UserWarning, message=".*Gym version v0.24.1.*") def play_genome(args): + neat_name = "latest" + if args.neat_name != '': + neat_name = args.neat_name + if args.to_gen is not None: to_gen = args.to_gen if args.from_gen is not None: from_gen = args.from_gen else: from_gen = 0 - test_genome(from_gen, to_gen) + test_genome(from_gen, to_gen, neat_name) - neat_name = "latest" - if args.neat_name != '': - neat_name = args.neat_name - generation_num = args.generation if args.generation is not None else -1 genome = load_best_genome(generation_num, neat_name) env, state = env_debug_init() run_game_debug(env, state, genome, 0, visualize=False) -def test_genome(from_gen: int, to_gen: int): +def test_genome(from_gen: int, to_gen: int, neat_name: str): for i in range(from_gen, to_gen + 1): print(f"Testing genome {i}...") - genome = load_best_genome(i) + genome = load_best_genome(i, neat_name) env, state = env_debug_init() fitness = run_game_debug(env, state, genome, i) print(fitness) diff --git a/src/genetics/traverse.py b/src/genetics/traverse.py index bd08ed4..17d3f5c 100644 --- a/src/genetics/traverse.py +++ b/src/genetics/traverse.py @@ -67,12 +67,8 @@ def kahns_algorithm(self) -> List[Node]: queue.append(connection.out_node) # If the topological sort includes all nodes, return it - final_order_of_traversal = [] if len(order_of_traversal) == len(self.genome.nodes): - for node in order_of_traversal: - if len(node.connections_to_output) > 0 or node.type == "output": - final_order_of_traversal.append(node) - return final_order_of_traversal + return order_of_traversal else: # If not all nodes are processed, there's a cycle # print("loop") diff --git a/src/visualization/visualize_genome.py b/src/visualization/visualize_genome.py index 5a3734d..d6f15c7 100644 --- a/src/visualization/visualize_genome.py +++ b/src/visualization/visualize_genome.py @@ -88,10 +88,10 @@ def visualize_genome(genome: Genome, frame_number: int): plt.xlim(GRAPH_XMIN, GRAPH_XMAX) plt.ylim(GRAPH_YMIN, GRAPH_YMAX) - directory = "./genome_frames" + directory = "./data/genome_frames" if not os.path.exists(directory): os.makedirs(directory) - plt.savefig(f'./genome_frames/genome_{frame_number}.png') + plt.savefig(f'./data/genome_frames/genome_{frame_number}.png') plt.close() \ No newline at end of file