-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.py
63 lines (55 loc) · 2.87 KB
/
ga.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from random import random
import numpy as np
import matris
def cal_pop_fitness(pop, gen, num_gen,max_lines_cleared= False):
# Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its corresponding weight.
# Every child should run multiple times
fitness = []
for idx, user in enumerate(pop):
info = [gen+1,num_gen,idx+1, len(pop)]
avg_fitness = run_child(user, info,max_lines_cleared)
fitness.append(avg_fitness)
return fitness
def run_child(user,info,max_lines_cleared= False):
runs = 1
fitness_child = []
for i in range(0,runs):
matris.start_game()
info_child = info + [i+1, runs]
fitness_child.append(matris.start_round_GA(user, info_child, max_lines_cleared))
return np.average(fitness_child)
def select_mating_pool(pop, fitness, num_parents):
# Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
parents = np.empty((num_parents, pop.shape[1]))
for parent_num in range(num_parents):
max_fitness_idx = np.where(fitness == np.max(fitness))
max_fitness_idx = max_fitness_idx[0][0]
parents[parent_num, :] = pop[max_fitness_idx, :]
fitness[max_fitness_idx] = -99999999999
return parents
def crossover(parents, offspring_size):
offspring = np.empty(offspring_size)
# The point at which crossover takes place between two parents. Usually, it is at the center.
crossover_point = np.uint8(offspring_size[1]/2)
for k in range(offspring_size[0]):
# Index of the first parent to mate.
parent1_idx = k%parents.shape[0]
# Index of the second parent to mate.
parent2_idx = (k+1)%parents.shape[0]
# The new offspring will have its first half of its genes taken from the first parent.
offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]
# The new offspring will have its second half of its genes taken from the second parent.
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
return offspring
def mutation(offspring_crossover,mutate_percentage, num_mutations=1):
mutations_counter = np.uint8(offspring_crossover.shape[1] / num_mutations)
# Mutation changes a number of genes as defined by the num_mutations argument. The changes are random.
for idx in range(offspring_crossover.shape[0]):
gene_idx = mutations_counter - 1
for mutation_num in range(num_mutations):
# The random value to be added to the gene.
random_value = np.random.uniform(-mutate_percentage, mutate_percentage, 1)
offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value
gene_idx = gene_idx + mutations_counter
return offspring_crossover