poprawka wyboru

Signed-off-by: Neerka <kuba.markil0220@gmail.com>
This commit is contained in:
Neerka 2024-06-10 14:14:19 +02:00
parent a692108116
commit 21e54bb946
3 changed files with 16 additions and 5 deletions

Binary file not shown.

View File

@ -33,13 +33,24 @@ def mutate(individual):
individual[idx1], individual[idx2] = individual[idx2], individual[idx1]
def roulette_selection(population, fitnesses):
total_fitness = sum(fitnesses)
selection_point = random.uniform(0, total_fitness)
current = 0
for individual, fitness in zip(population, fitnesses):
current += fitness
if current <= selection_point:
return individual
def genetic_algorithm(points, population_size, generations):
population = [random.sample(range(len(points)), len(points)) for _ in range(population_size)]
for _ in range(generations):
population.sort(key=lambda individual: calculate_fitness(individual, points), reverse=True)
next_generation = population[:population_size // 10]
fitnesses = [calculate_fitness(individual, points) for individual in population]
next_generation = []
while len(next_generation) < population_size:
parent1, parent2 = random.choices(population[:population_size // 2], k=2)
parent1 = roulette_selection(population, fitnesses)
parent2 = roulette_selection(population, fitnesses)
child = crossover(parent1, parent2)
if random.random() < 0.01:
mutate(child)
@ -64,4 +75,4 @@ for i in range(1, 29):
if i % 2 == 1 and i % 4 != 1:
point_list.append((i, 10))
print(genetic_algorithm(point_list, 100, 1000))
print(genetic_algorithm(point_list, 100, 10000))

View File

@ -18,7 +18,7 @@ houses = householdGenerator(MULT)
garbagetruck = Garbagetruck(MULT).setHouses(houses).setTrashcans(trashcans)
garbagetruck.setFirstTarget()
# print("Kolejność danych do drzewa:")
# print("Pora roku - Pora dnia - Typ śmieci - Zapełnienie kosza - Zapełnienie śmieciarki - Zapłacone - Ostatnio zabrane "
# print("Pora roku - Pora dnia - Typ śmieci - Zapełnienie kosza - Zapełnienie śmieciarki - Zapłacone - Ostatnio zabrane"
# "- Pogoda")
running = True