This commit is contained in:
andrzej 2020-05-11 15:06:08 +02:00
commit 5a80cc1966
3 changed files with 30 additions and 42 deletions

View File

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

13
main.py
View File

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