GA END
- BUGfix - comments - stop var with Michał Malinowski
This commit is contained in:
parent
ef5c5556ef
commit
361a733102
@ -39,10 +39,11 @@ def genetic_algorithm_setup(field):
|
|||||||
|
|
||||||
best_outputs = []
|
best_outputs = []
|
||||||
num_generations = 10
|
num_generations = 10
|
||||||
num_parents = 2
|
num_parents = 4
|
||||||
|
|
||||||
# iterative var
|
# iterative var
|
||||||
generation = 0
|
generation = 0
|
||||||
|
stop = 0
|
||||||
# TODO WARUNEK STOPU
|
# TODO WARUNEK STOPU
|
||||||
while generation < num_generations and stop < 3:
|
while generation < num_generations and stop < 3:
|
||||||
if keyboard.is_pressed('space'):
|
if keyboard.is_pressed('space'):
|
||||||
@ -55,7 +56,7 @@ def genetic_algorithm_setup(field):
|
|||||||
fitness = []
|
fitness = []
|
||||||
|
|
||||||
for i in range(0, population_size):
|
for i in range(0, population_size):
|
||||||
print(len(population_text), i)
|
print('POP_TEXT', len(population_text), i)
|
||||||
fitness.append((i, population_fitness(population_text[i], field, population_size)))
|
fitness.append((i, population_fitness(population_text[i], field, population_size)))
|
||||||
|
|
||||||
print("Fitness")
|
print("Fitness")
|
||||||
@ -99,12 +100,20 @@ def genetic_algorithm_setup(field):
|
|||||||
for row in offspring_mutation[i]]))
|
for row in offspring_mutation[i]]))
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
population_text_copy = copy.deepcopy(population_text)
|
||||||
|
unused_indexes = [i for i in range(0, population_size) if i not in [j[0] for j in best]]
|
||||||
# Creating next generation
|
# Creating next generation
|
||||||
population_text = []
|
population_text = []
|
||||||
for k in range(0, len(parents)):
|
for k in range(0, len(parents)):
|
||||||
population_text.append(parents)
|
population_text.append(parents[k])
|
||||||
for k in range(0, len(offspring_mutation)):
|
for k in range(0, len(offspring_mutation)):
|
||||||
population_text.append(offspring_mutation[k])
|
population_text.append(offspring_mutation[k])
|
||||||
|
while len(population_text) < population_size:
|
||||||
|
x = random.choice(unused_indexes)
|
||||||
|
population_text.append(population_text_copy[x])
|
||||||
|
unused_indexes.remove(x)
|
||||||
|
|
||||||
|
print("END LEN", len(population_text))
|
||||||
|
|
||||||
# TODO WARUNEK STOPU
|
# TODO WARUNEK STOPU
|
||||||
stop = 0
|
stop = 0
|
||||||
|
@ -2,6 +2,7 @@ import copy
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
import matplotlib
|
import matplotlib
|
||||||
|
import matplotlib.pyplot
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import src.dimensions as D
|
import src.dimensions as D
|
||||||
@ -28,7 +29,7 @@ def local_fitness(field, x, y, plants_case):
|
|||||||
plant_value = 1
|
plant_value = 1
|
||||||
|
|
||||||
neighbour_bonus = 1
|
neighbour_bonus = 1
|
||||||
print(x, y)
|
|
||||||
if x - 1 >= 0:
|
if x - 1 >= 0:
|
||||||
if plants_case[x][y] == plants_case[x - 1][y]:
|
if plants_case[x][y] == plants_case[x - 1][y]:
|
||||||
neighbour_bonus += 1
|
neighbour_bonus += 1
|
||||||
@ -47,11 +48,12 @@ def local_fitness(field, x, y, plants_case):
|
|||||||
return local_fitness_value
|
return local_fitness_value
|
||||||
|
|
||||||
|
|
||||||
def population_fitness(population_text, field, population_size):
|
def population_fitness(population_text_local, field, population_size):
|
||||||
# Calculating the fitness value of each solution in the current population.
|
# 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.
|
# The fitness function calulates the sum of products between each input and its corresponding weight.
|
||||||
fitness = []
|
fitness = []
|
||||||
|
|
||||||
|
print('LOCAL', len(population_text_local))
|
||||||
for k in range(population_size):
|
for k in range(population_size):
|
||||||
population_values_single = []
|
population_values_single = []
|
||||||
population_values_single_row = []
|
population_values_single_row = []
|
||||||
@ -59,7 +61,7 @@ def population_fitness(population_text, field, population_size):
|
|||||||
|
|
||||||
for i in range(0, D.GSIZE):
|
for i in range(0, D.GSIZE):
|
||||||
for j in range(0, D.GSIZE):
|
for j in range(0, D.GSIZE):
|
||||||
population_values_single_row.append(local_fitness(field, i, j, population_text))
|
population_values_single_row.append(local_fitness(field, i, j, population_text_local))
|
||||||
population_values_single.append(population_values_single_row)
|
population_values_single.append(population_values_single_row)
|
||||||
|
|
||||||
for i in range(D.GSIZE):
|
for i in range(D.GSIZE):
|
||||||
@ -73,7 +75,7 @@ def crossover(parents):
|
|||||||
for i in range(0, len(parents)):
|
for i in range(0, len(parents)):
|
||||||
child = copy.deepcopy(parents[i])
|
child = copy.deepcopy(parents[i])
|
||||||
# Vertical randomization
|
# Vertical randomization
|
||||||
width = random.randint(1, D.GSIZE / len(parents)) # width of stripes
|
width = random.randint(1, D.GSIZE // len(parents)) # width of stripes
|
||||||
indexes_parents = numpy.random.permutation(range(0, len(parents))) # sorting of stripes
|
indexes_parents = numpy.random.permutation(range(0, len(parents))) # sorting of stripes
|
||||||
beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from
|
beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from
|
||||||
for x in indexes_parents:
|
for x in indexes_parents:
|
||||||
@ -92,15 +94,6 @@ def mutation(population_units, offspring_crossover, num_mutants, num_mutations=1
|
|||||||
offspring_crossover[case][mutation_x][mutation_y] = mutation_value
|
offspring_crossover[case][mutation_x][mutation_y] = mutation_value
|
||||||
num_mutants -= 1
|
num_mutants -= 1
|
||||||
|
|
||||||
while num_mutants > 0:
|
|
||||||
case = random.randint(0, len(offspring_crossover))
|
|
||||||
for mutation in range(0, num_mutations):
|
|
||||||
mutation_x = random.randint(0, D.GSIZE - 1)
|
|
||||||
mutation_y = random.randint(0, D.GSIZE - 1)
|
|
||||||
mutation_value = random.choice(population_units)
|
|
||||||
offspring_crossover[case][mutation_x][mutation_y] = mutation_value
|
|
||||||
num_mutants -= 1
|
|
||||||
|
|
||||||
return offspring_crossover
|
return offspring_crossover
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user