działający algorytm, do połączenia z resztą
This commit is contained in:
parent
9a07af72f5
commit
3ff8b95999
@ -1,13 +1,16 @@
|
|||||||
import random
|
import random
|
||||||
|
import math
|
||||||
|
import statistics
|
||||||
|
import time
|
||||||
|
|
||||||
### prawdopodobieństwo mutacji
|
### prawdopodobieństwo mutacji
|
||||||
mutation_prob = 0.02
|
mutation_prob = 0.02
|
||||||
### ilość osobników w pokoleniu
|
### ilość osobników w pokoleniu, powinna być parzysta
|
||||||
generation_size = 4
|
generation_size = 30
|
||||||
### liczba pokoleń
|
### liczba pokoleń
|
||||||
number_of_generations = 4
|
number_of_generations = 100
|
||||||
### liczba paczek
|
### liczba paczek
|
||||||
number_of_packages = 10
|
number_of_packages = 15
|
||||||
### liczba regałów
|
### liczba regałów
|
||||||
number_of_racks = 5
|
number_of_racks = 5
|
||||||
### jak bardzo promowane są osobniki wykorzystujące całą pojemność regału
|
### jak bardzo promowane są osobniki wykorzystujące całą pojemność regału
|
||||||
@ -49,10 +52,10 @@ def roulette(generation):
|
|||||||
for i in range(generation_size):
|
for i in range(generation_size):
|
||||||
individual_fitness = evaluation(generation[i])
|
individual_fitness = evaluation(generation[i])
|
||||||
evaluations.append(individual_fitness)
|
evaluations.append(individual_fitness)
|
||||||
# print("tablica niedopasowań: ", evaluations)
|
# print("tablica dopasowań: ", evaluations)
|
||||||
minimum = min(evaluations)
|
maximum = min(evaluations)
|
||||||
# dodaję tą 1 żeby nie wywalać najgorszego osobnika
|
# dodaję tą 1 żeby nie wywalać najgorszego osobnika
|
||||||
normalized = [x+(-1*minimum)+1 for x in evaluations]
|
normalized = [x+(-1*maximum)+1 for x in evaluations]
|
||||||
# print(normalized)
|
# print(normalized)
|
||||||
sum_of_normalized = sum(normalized)
|
sum_of_normalized = sum(normalized)
|
||||||
roulette_tab = [x/sum_of_normalized for x in normalized]
|
roulette_tab = [x/sum_of_normalized for x in normalized]
|
||||||
@ -87,24 +90,60 @@ def crossover(individual1, individual2):
|
|||||||
return new1, new2
|
return new1, new2
|
||||||
|
|
||||||
def mutation(individual):
|
def mutation(individual):
|
||||||
print(individual)
|
# print(individual)
|
||||||
locus = random.randint(0,number_of_packages-1)
|
locus = random.randint(0,number_of_packages-1)
|
||||||
individual[locus] = random.randint(0,number_of_racks-1)
|
individual[locus] = random.randint(0,number_of_racks-1)
|
||||||
return individual
|
return individual
|
||||||
|
|
||||||
### lista paczek, indeks to id paczki, wartość w liście to jej waga
|
### lista paczek, indeks to id paczki, wartość w liście to jej waga
|
||||||
packages = [random.randint(2,10) for i in range(number_of_packages)]
|
packages = [random.randint(2,9) for i in range(number_of_packages)]
|
||||||
### lista regałów, indeks to id regału, wartość w liście to jego pojemność
|
### lista regałów, indeks to id regału, wartość w liście to jego pojemność
|
||||||
racks = [random.randint(10,20) for i in range(number_of_racks)]
|
racks = [random.randint(10,20) for i in range(number_of_racks)]
|
||||||
# print(packages)
|
# print(packages)
|
||||||
# print(racks)
|
# print(racks)
|
||||||
first_generation = first_gen()
|
|
||||||
# crossover(first_generation[0],first_generation[1])
|
### WŁAŚCIWY ALGORYTM
|
||||||
# descendants = []
|
generation = first_gen()
|
||||||
# for i in range(0,generation_size,2):
|
global_maximum = -math.inf
|
||||||
# pair = crossover(first_generation[i],first_generation[i+1])
|
|
||||||
# for each in pair:
|
# pętla znajdująca najlepszy fitness w pierwszym pokoleniu
|
||||||
# descendants.append(each)
|
for i in range(generation_size):
|
||||||
# print(descendants)
|
evaluation_of_individual = evaluation(generation[i])
|
||||||
print("first gen: ",first_generation)
|
if evaluation_of_individual > global_maximum:
|
||||||
roulette(first_generation)
|
global_maximum = evaluation_of_individual
|
||||||
|
best_individual = generation[i].copy()
|
||||||
|
|
||||||
|
#właściwa pętla programu
|
||||||
|
for generation_index in range(number_of_generations):
|
||||||
|
print('pokolenie numer: ', generation_index)
|
||||||
|
# print(generation)
|
||||||
|
|
||||||
|
### RULETKA
|
||||||
|
survivors = roulette(generation)
|
||||||
|
# print('przetrwali: ',survivors)
|
||||||
|
|
||||||
|
### KRZYŻOWANIE
|
||||||
|
descendants = []
|
||||||
|
for individual in range(0,generation_size,2):
|
||||||
|
pair = crossover(survivors[individual],survivors[individual+1])
|
||||||
|
for each in pair:
|
||||||
|
descendants.append(each)
|
||||||
|
# print('potomkowie: ', descendants)
|
||||||
|
|
||||||
|
### MUTACJA
|
||||||
|
for individual in range(generation_size):
|
||||||
|
if random.random() <= mutation_prob:
|
||||||
|
mutation(descendants[individual])
|
||||||
|
# print('potomkowie po mutacji: ', descendants)
|
||||||
|
|
||||||
|
### NAJLEPSZE DOPASOWANIE
|
||||||
|
local_maximum = -math.inf
|
||||||
|
for each in range(generation_size):
|
||||||
|
specific_fitness = evaluation(descendants[each])
|
||||||
|
if specific_fitness > local_maximum:
|
||||||
|
local_maximum = specific_fitness
|
||||||
|
print('maximum w pokoleniu: ',local_maximum)
|
||||||
|
if local_maximum > global_maximum:
|
||||||
|
global_maximum = local_maximum
|
||||||
|
generation = descendants
|
||||||
|
print('maximum globalne: ', global_maximum)
|
||||||
|
Loading…
Reference in New Issue
Block a user