Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kapi0okapi committed Oct 24, 2024
2 parents 50059a8 + 09f5c97 commit 82e934b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 93 deletions.
79 changes: 0 additions & 79 deletions gui_with_pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/genetics/traverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/visualization/visualize_genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


0 comments on commit 82e934b

Please sign in to comment.