algorytm genetyczny połączony z resztą

This commit is contained in:
magdabiadala 2020-05-11 14:54:33 +02:00
parent 5dcaefe983
commit 3984da7585
3 changed files with 30 additions and 42 deletions

View File

@ -1,23 +1,17 @@
import random
import math
# packs_coords
# racks_coords
### prawdopodobieństwo mutacji
mutation_prob = 0.03
### ilość osobników w pokoleniu, powinna być parzysta
generation_size = 20
generation_size = 40
### liczba pokoleń
number_of_generations = 30
### liczba paczek
number_of_packages = 45
### liczba regałów
number_of_racks = 70
### jak bardzo promowane są osobniki wykorzystujące całą pojemność regału
amount_of_promotion = 3
amount_of_promotion = 0
def first_gen():
def first_gen(number_of_packages, number_of_racks):
first_generation = []
for individual in range(generation_size):
individual = []
@ -27,7 +21,7 @@ def first_gen():
first_generation.append(individual)
return first_generation
def evaluation(individual):
def evaluation(individual, packages, racks, number_of_packages, number_of_racks):
# im większy fitness tym lepszy osobnik
# print("regały: ",racks)
rest_of_capacity = racks.copy()
@ -46,11 +40,11 @@ def evaluation(individual):
### tu dodaj to co zrobi Andrzej
return fitness
def roulette(generation):
def roulette(generation, packages, racks, number_of_packages, number_of_racks):
# print('pokolenie: ', generation)
evaluations = []
for i in range(generation_size):
individual_fitness = evaluation(generation[i])
individual_fitness = evaluation(generation[i], packages, racks, number_of_packages, number_of_racks)
evaluations.append(individual_fitness)
# print("tablica dopasowań: ", evaluations)
maximum = min(evaluations)
@ -76,7 +70,7 @@ def roulette(generation):
# print('przetrwali: ',survivors)
return survivors
def crossover(individual1, individual2):
def crossover(individual1, individual2, number_of_packages):
cut = random.randint(1,number_of_packages-1)
new1 = individual1[:cut]
new2 = individual2[:cut]
@ -89,21 +83,24 @@ def crossover(individual1, individual2):
# print(cut)
return new1, new2
def mutation(individual):
def mutation(individual, number_of_packages, number_of_racks):
# print(individual)
locus = random.randint(0,number_of_packages-1)
individual[locus] = random.randint(0,number_of_racks-1)
return individual
def gen_alg(number_of_generations, generation_size, mutation_prob, amount_of_promotion):
def gen_alg(packages, racks, number_of_generations, generation_size, mutation_prob, amount_of_promotion):
number_of_packages = len(packages)
number_of_racks = len(racks)
### WŁAŚCIWY ALGORYTM
generation = first_gen()
generation = first_gen(number_of_packages, number_of_racks)
global_maximum = -math.inf
# pętla znajdująca najlepszy fitness w pierwszym pokoleniu
for i in range(generation_size):
evaluation_of_individual = evaluation(generation[i])
evaluation_of_individual = evaluation(generation[i], packages, racks, number_of_packages, number_of_racks)
if evaluation_of_individual > global_maximum:
global_maximum = evaluation_of_individual
best_individual = generation[i].copy()
@ -114,13 +111,13 @@ def gen_alg(number_of_generations, generation_size, mutation_prob, amount_of_pro
# print(generation)
### RULETKA
survivors = roulette(generation)
survivors = roulette(generation, packages, racks, number_of_packages, number_of_racks)
# print('przetrwali: ',survivors)
### KRZYŻOWANIE
descendants = []
for individual in range(0,generation_size,2):
pair = crossover(survivors[individual],survivors[individual+1])
pair = crossover(survivors[individual],survivors[individual+1], number_of_packages)
for each in pair:
descendants.append(each)
# print('potomkowie: ', descendants)
@ -128,25 +125,21 @@ def gen_alg(number_of_generations, generation_size, mutation_prob, amount_of_pro
### MUTACJA
for individual in range(generation_size):
if random.random() <= mutation_prob:
mutation(descendants[individual])
mutation(descendants[individual], number_of_packages, number_of_racks)
# print('potomkowie po mutacji: ', descendants)
### NAJLEPSZE DOPASOWANIE
local_maximum = -math.inf
for each in range(generation_size):
specific_fitness = evaluation(descendants[each])
specific_fitness = evaluation(descendants[each], packages, racks, number_of_packages, number_of_racks)
if specific_fitness > local_maximum:
local_maximum = specific_fitness
print('maximum w pokoleniu: ',local_maximum)
generation_best_individual = descendants[each].copy()
print('maksimum w pokoleniu: ',local_maximum)
if local_maximum > global_maximum:
global_maximum = local_maximum
best_individual = generation_best_individual.copy()
generation = descendants
print('maximum globalne: ', global_maximum)
### lista paczek, indeks to id paczki, wartość w liście to jej waga
packages = [random.randint(1,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(15,18) for i in range(number_of_racks)]
# print(packages)
# print(racks)
gen_alg(number_of_generations, generation_size, mutation_prob, amount_of_promotion)
print('maksimum globalne: ', global_maximum)
print("jeśli maksimum globalne wynosi 0, każda paczka ma swój regał")
print("najlepsze dopasowanie: ", best_individual)

13
main.py
View File

@ -6,11 +6,6 @@ import random
import sys
from attributes import PackStatus, COLORS, DIRECTION_ANGLES
mutation_prob = 0.03
generation_size = 20
number_of_generations = 30
amount_of_promotion = 3
WINDOW_SIZE = (640, 640)
COLOR_OF_FIELD = {
'Floor': 'gray',
@ -25,7 +20,7 @@ TILE_HEIGHT = 32
CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2)
class MainGameFrame:
def __init__(self):
def __init__(self, mutation_prob = 0.03, generation_size = 40, number_of_generations = 30, amount_of_promotion = 0):
pygame.font.init()
self.display = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Smart ForkLift")
@ -40,12 +35,12 @@ class MainGameFrame:
list_of_racks = self.warehouse_map.get_all_racks(True)
racks_coords = [(line.x_position, line.y_position) for line in list_of_racks]
packs_sizes = [pack.size for pack in self.warehouse_map.packages]
# racks_capacities = [pack.size for pack in self.warehouse_map.packages]
racks_capacities = [rack.capacity for rack in list_of_racks]
print("koordynaty paczek: ",packs_coords)
print("koordynaty regałów: ",racks_coords)
print("wagi paczek: ",packs_sizes)
# print("pojemności regałów: ",racks_capacities)
gen_alg(number_of_generations, generation_size, mutation_prob, amount_of_promotion)
print("pojemności regałów: ",racks_capacities)
gen_alg(packs_sizes, racks_capacities, number_of_generations, generation_size, mutation_prob, amount_of_promotion)
def run(self):
while True: