- BUGfix
- comments
- stop var

with Michał Malinowski
This commit is contained in:
Lewy 2021-06-21 04:04:26 +02:00
parent ef5c5556ef
commit 361a733102
2 changed files with 18 additions and 16 deletions

View File

@ -39,10 +39,11 @@ def genetic_algorithm_setup(field):
best_outputs = []
num_generations = 10
num_parents = 2
num_parents = 4
# iterative var
generation = 0
stop = 0
# TODO WARUNEK STOPU
while generation < num_generations and stop < 3:
if keyboard.is_pressed('space'):
@ -55,7 +56,7 @@ def genetic_algorithm_setup(field):
fitness = []
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)))
print("Fitness")
@ -99,12 +100,20 @@ def genetic_algorithm_setup(field):
for row in offspring_mutation[i]]))
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
population_text = []
for k in range(0, len(parents)):
population_text.append(parents)
population_text.append(parents[k])
for k in range(0, len(offspring_mutation)):
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
stop = 0

View File

@ -2,6 +2,7 @@ import copy
import random
import matplotlib
import matplotlib.pyplot
import numpy
import src.dimensions as D
@ -28,7 +29,7 @@ def local_fitness(field, x, y, plants_case):
plant_value = 1
neighbour_bonus = 1
print(x, y)
if x - 1 >= 0:
if plants_case[x][y] == plants_case[x - 1][y]:
neighbour_bonus += 1
@ -47,11 +48,12 @@ def local_fitness(field, x, y, plants_case):
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.
# The fitness function calulates the sum of products between each input and its corresponding weight.
fitness = []
print('LOCAL', len(population_text_local))
for k in range(population_size):
population_values_single = []
population_values_single_row = []
@ -59,7 +61,7 @@ def population_fitness(population_text, field, population_size):
for i 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)
for i in range(D.GSIZE):
@ -73,7 +75,7 @@ def crossover(parents):
for i in range(0, len(parents)):
child = copy.deepcopy(parents[i])
# 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
beginning = random.randint(0, len(parents[0]) - width * len(parents)) # point we start putting the stripes from
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
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