SI_2020/genetic_algorithm.py

111 lines
3.8 KiB
Python

import random
### prawdopodobieństwo mutacji
mutation_prob = 0.02
### ilość osobników w pokoleniu
generation_size = 4
### liczba pokoleń
number_of_generations = 4
### liczba paczek
number_of_packages = 10
### liczba regałów
number_of_racks = 5
### jak bardzo promowane są osobniki wykorzystujące całą pojemność regału
amount_of_promotion = 3
def first_gen():
first_generation = []
for individual in range(generation_size):
individual = []
for pack in range(number_of_packages):
r = random.randint(0,number_of_racks-1)
individual.append(r)
first_generation.append(individual)
return first_generation
def evaluation(individual):
# im większy fitness tym lepszy osobnik
# print("regały: ",racks)
rest_of_capacity = racks.copy()
# print("początkowa pojemność: ",rest_of_capacity)
for i in range(number_of_packages):
rest_of_capacity[individual[i]] -= packages[i]
# print("pozostała pojemność: ",rest_of_capacity)
fitness = 0
for i in range(number_of_racks):
# jak regał jest przepełniony, zmniejsza fitness osobnika
if rest_of_capacity[i] < 0:
fitness += rest_of_capacity[i]
# delikane promowanie osobników wykorzystujących regały w pełni
elif rest_of_capacity[i] == 0:
fitness += amount_of_promotion
### tu dodaj to co zrobi Andrzej
return fitness
def roulette(generation):
# print('pokolenie: ', generation)
evaluations = []
for i in range(generation_size):
individual_fitness = evaluation(generation[i])
evaluations.append(individual_fitness)
# print("tablica niedopasowań: ", evaluations)
minimum = min(evaluations)
# dodaję tą 1 żeby nie wywalać najgorszego osobnika
normalized = [x+(-1*minimum)+1 for x in evaluations]
# print(normalized)
sum_of_normalized = sum(normalized)
roulette_tab = [x/sum_of_normalized for x in normalized]
# print(roulette_tab)
for i in range(1,generation_size-1):
roulette_tab[i] += roulette_tab[i-1]
# wpisuję 1 ręcznie, bo czasem liczby nie sumowały się idealnie do 1
#(niedokładność komputera)
roulette_tab[generation_size-1] = 1
# print("ruletka: ", roulette_tab)
survivors = []
for individual in range(generation_size):
random_number = random.random()
interval_number = 0
while random_number > roulette_tab[interval_number]:
interval_number += 1
survivors.append(generation[interval_number])
# print('przetrwali: ',survivors)
return survivors
def crossover(individual1, individual2):
cut = random.randint(1,number_of_packages-1)
new1 = individual1[:cut]
new2 = individual2[:cut]
new1 = new1 + individual2[cut:]
new2 = new2 + individual1[cut:]
# print(individual1)
# print(individual2)
# print(new1)
# print(new2)
# print(cut)
return new1, new2
def mutation(individual):
print(individual)
locus = random.randint(0,number_of_packages-1)
individual[locus] = random.randint(0,number_of_racks-1)
return individual
### 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)]
### 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)]
# print(packages)
# print(racks)
first_generation = first_gen()
# crossover(first_generation[0],first_generation[1])
# descendants = []
# for i in range(0,generation_size,2):
# pair = crossover(first_generation[i],first_generation[i+1])
# for each in pair:
# descendants.append(each)
# print(descendants)
print("first gen: ",first_generation)
roulette(first_generation)