From 3984da7585a1719599d93bf504af6ac5acc93973 Mon Sep 17 00:00:00 2001 From: magdabiadala Date: Mon, 11 May 2020 14:54:33 +0200 Subject: [PATCH] =?UTF-8?q?algorytm=20genetyczny=20po=C5=82=C4=85czony=20z?= =?UTF-8?q?=20reszt=C4=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- genetic_algorithm.py | 55 +++++++++++++++++++------------------------- main.py | 13 ++++------- warehouse.py | 4 ++-- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/genetic_algorithm.py b/genetic_algorithm.py index c4b5d7b..b36a3d2 100644 --- a/genetic_algorithm.py +++ b/genetic_algorithm.py @@ -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) diff --git a/main.py b/main.py index d828dfb..8b5ac91 100644 --- a/main.py +++ b/main.py @@ -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: diff --git a/warehouse.py b/warehouse.py index ef75911..1e50857 100644 --- a/warehouse.py +++ b/warehouse.py @@ -81,7 +81,7 @@ class Warehouse: self.create_fridge(8) # print([row[0].air_temperature for row in self.tiles]) self.packages = self.place_packages(no_of_packages) - self.tiles[1][1] = Tile('floor', 1, 1) + self.tiles[1][1] = Tile('floor', 1, 1) def __str__(self): return "Magazyn {}x{}".format(self.width, self.height) @@ -299,4 +299,4 @@ class Tile: return "Tile type: {} on position ({},{})".format(self.category, self.x_position, self.y_position) def __repr__(self): - return "Tile type: {} on position ({},{})".format(self.category, self.x_position, self.y_position) \ No newline at end of file + return "Tile type: {} on position ({},{})".format(self.category, self.x_position, self.y_position)