Skip to content

Commit

Permalink
refactor: optimize and fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
maiahi committed Apr 29, 2024
1 parent afa8615 commit 1611858
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/agents/geneticAlgAgentJon.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def play_game(self, agg_height, max_height, lines_cleared, bumpiness, holes):

move += 1
# Advance the game by one frame
board.doAction(Action.SOFT_DROP)
board.doAction(Action.SOFT_DROP)
if board.blockHasLanded:
board.updateBoard()
#board.printBoard()

total_cleared += board.rowsRemoved
Expand All @@ -103,7 +105,8 @@ def replace_30_percent(self, pop_list: list[list[list[float], float]]) -> list[l
# TODO create method for fetching a random 10%, and finds the two with highest lines cleared, and makes a child (with 5% chance of mutation)
def paring_pop(self, pop_list: list[list[list[float], float]]) -> list[list[float], float]:
# Gets the number of pops to select
num_pops_to_select = int(len(pop_list) * 0.1)
# num_pops_to_select = int(len(pop_list) * 0.1)
num_pops_to_select = int(len(pop_list) * 0.5)

# Get a sample of pops based on the previous number
random_pop_sample = random.sample(pop_list, num_pops_to_select)
Expand All @@ -116,7 +119,7 @@ def paring_pop(self, pop_list: list[list[list[float], float]]) -> list[list[floa
norm = np.linalg.norm(new_pop[0])
if norm == 0:
norm = 1e-10 # or some small constant
new_pop[0] = (new_pop[0] / norm).tolist()
new_pop[0] = [i / norm for i in new_pop[0]]

# Mutate 5% of children pops
if random.randrange(0,1000)/1000 < 0.05:
Expand Down

0 comments on commit 1611858

Please sign in to comment.