Skip to content

Commit

Permalink
feat: initialize all the genomes
Browse files Browse the repository at this point in the history
  • Loading branch information
ludvigovrevik committed Sep 26, 2024
1 parent 7b3e2d5 commit 9ed697c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
8 changes: 3 additions & 5 deletions src/environments/run_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def env_init() -> Tuple[MarioJoypadSpace, np.ndarray]:
env = MarioJoypadSpace(env, SIMPLE_MOVEMENT) # Select available actions for AI
env.metadata['render_modes'] = "human"
env.metadata['render_fps'] = 144
env.unwrapped._lives = 1
# env.unwrapped._lives = 1 # Have mario have one life

state = env.reset() # Good practice to reset the env before using it.
return env, state
Expand All @@ -35,13 +35,11 @@ def insert_input(genome:Genome, state: list) -> None:

def run_game(env: MarioJoypadSpace, genome: Genome):
forward = Traverse(genome)
life2 = 0
for i in range(1000): # Simulate 200 frames.

for i in range(200): # Simulate 200 frames.
action = forward.traverse()
if action == -1:
quit()
print([node.value for node in genome.output_nodes])
# print([node.value for node in genome.output_nodes])
# print(f"Chosen action: {action}. This is {SIMPLE_MOVEMENT[action]} in SIMPLE_MOVEMENT")
# move = SIMPLE_MOVEMENT[simple_movement_dict[action]] # Choose to go in the direction NN chooses. BE CAREFUL WITH THE ID OF OUTPUT NODES
sr = env.step(action) # State, Reward, Done, Info
Expand Down
7 changes: 4 additions & 3 deletions src/genetics/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from src.genetics.connection_gene import ConnectionGene
from typing import TYPE_CHECKING
from typing import List

if TYPE_CHECKING:
from src.genetics.genome import Genome
Expand All @@ -13,7 +14,7 @@ class Node:
def __init__(self, id: int, type: str, value: float = 0.0):
self.id = id
self.type = type
self.connected_nodes = [] #hmm
self.connected_nodes: List[int] = [] #hmm
self.connections_to_output = []
self.value = value

Expand All @@ -30,8 +31,8 @@ def set_value(self, value: float):
def update_value(self, value: float):
self.value += value

def add_node_connection(self, node:'Node'):
self.connected_nodes.append(node)
def add_node_connection(self, nodeId: int):
self.connected_nodes.append(nodeId)

def add_connection_connection(self, connection: 'ConnectionGene'):
self.connections_to_output.append(connection)
Expand Down
31 changes: 26 additions & 5 deletions src/genetics/species.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
from src.genetics.genome import Genome
from src.genetics.node import Node

class Species: # Made by Copilot
def __init__(self, representative):
self.representative = representative
self.members = [representative]
def __init__(self):
self.genomes = self.initialize_genomes()


def initialize_genomes(self, number_of_genomes=10):
genomes = []
for i in range(number_of_genomes):
genomes.append(Genome(i))

for i in range(number_of_genomes):
genome = genomes[i]

# Create output nodes
for i in range(7):
genome.add_node(Node(i, 'output'))

# Create input nodes
for i in range(7, 207):
genome.add_node(Node(i, 'input'))

return genomes

def add_member(self, member):
self.members.append(member)
def add_genome(self, member):
self.genomes.append(member)

def calculate_fitness(self):
# Calculate the fitness of each member in the species
Expand Down
37 changes: 26 additions & 11 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
from src.genetics.species import *
from src.genetics.traverse import Traverse
from src.environments.run_env import env_init, run_game
from src.genetics.species import Species
import random

genome = Genome(1)
# Create output nodes
for i in range(7):
genome.add_node(Node(i, 'output'))

# Create input nodes
for i in range(7, 207):
genome.add_node(Node(i, 'input'))
def test_generate_genome():
for i in range(300):
genome = Genome(i)

# Create output nodes
for i in range(7):
genome.add_node(Node(i, 'output'))

def test():
# Create input nodes
for i in range(7, 207):
genome.add_node(Node(i, 'input'))


def test(genome: Genome):
rand1 = random.randint(0, len(genome.nodes)-1)
rand2 = random.randint(0, len(genome.nodes)-1)
global_inovation_number = 0
Expand All @@ -26,10 +32,21 @@ def test():
genome.add_node_mutation(genome.connections[rand3], len(genome.nodes)+1, global_inovation_number)


test()
species = Species()
genomes = species.initialize_genomes()


for genome in genomes:
test(genome)
env, _ = env_init()
run_game(env=env, genome=genome)
# test(genome)


forward = Traverse(genome)
something = forward.traverse()


# while something != None:
# test()
# print(something)
Expand All @@ -41,5 +58,3 @@ def test():
# print(connection)
# print(genome)

env, _ = env_init()
run_game(env=env, genome=genome)

0 comments on commit 9ed697c

Please sign in to comment.