Drobne poprawki

This commit is contained in:
andrzej 2020-06-15 15:03:47 +02:00
parent 6322d823fa
commit 7f49117134
2 changed files with 38 additions and 13 deletions

View File

@ -3,14 +3,14 @@ import math
### 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 = 40 # generation_size = 40
### liczba pokoleń ### liczba pokoleń
number_of_generations = 30 # number_of_generations = 30
### 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 = 5 amount_of_promotion = 5
def first_gen(number_of_packages, number_of_racks): def first_gen(number_of_packages, number_of_racks, generation_size):
first_generation = [] first_generation = []
for individual in range(generation_size): for individual in range(generation_size):
individual = [] individual = []
@ -20,17 +20,43 @@ def first_gen(number_of_packages, number_of_racks):
first_generation.append(individual) first_generation.append(individual)
return first_generation return first_generation
# def evaluation(individual, packages, racks, number_of_packages, number_of_racks, tree_predictor):
# # im większy fitness tym lepszy osobnik
# # print("regały: ",racks)
# rest_of_capacity = [rack.capacity for rack in racks]
# # print("początkowa pojemność: ",rest_of_capacity)
# for i in range(number_of_packages):
# can_place = tree_predictor.check_if_can_place(packages[i], racks[i])
# if not can_place:
# rest_of_capacity[individual[i]] -= packages[i].size * 5
# else:
# rest_of_capacity[individual[i]] -= packages[i].size
# # print("pozostała pojemność: ",rest_of_capacity)
# # pdb.set_trace()
# 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 += 2
# else:
# fitness += 1
# return fitness
def evaluation(individual, packages, racks, number_of_packages, number_of_racks, tree_predictor): def evaluation(individual, packages, racks, number_of_packages, number_of_racks, tree_predictor):
# 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 = [rack.capacity for rack in racks] rest_of_capacity = [rack.capacity for rack in racks]
# print("początkowa pojemność: ",rest_of_capacity) # print("początkowa pojemność: ",rest_of_capacity)
bad_placed = 0
for i in range(number_of_packages): for i in range(number_of_packages):
rest_of_capacity[individual[i]] -= packages[i].size
can_place = tree_predictor.check_if_can_place(packages[i], racks[i]) can_place = tree_predictor.check_if_can_place(packages[i], racks[i])
if not can_place: if not can_place:
rest_of_capacity[individual[i]] -= packages[i].size * 5 bad_placed +=1
else:
rest_of_capacity[individual[i]] -= packages[i].size
# print("pozostała pojemność: ",rest_of_capacity) # print("pozostała pojemność: ",rest_of_capacity)
# pdb.set_trace() # pdb.set_trace()
fitness = 0 fitness = 0
@ -40,12 +66,11 @@ def evaluation(individual, packages, racks, number_of_packages, number_of_racks,
fitness += rest_of_capacity[i] fitness += rest_of_capacity[i]
# delikane promowanie osobników wykorzystujących regały w pełni # delikane promowanie osobników wykorzystujących regały w pełni
elif rest_of_capacity[i] == 0: elif rest_of_capacity[i] == 0:
fitness += 2 fitness += amount_of_promotion
else: fitness -= 5*bad_placed
fitness += 1
return fitness return fitness
def roulette(generation, packages, racks, number_of_packages, number_of_racks, tree_predictor): def roulette(generation, packages, generation_size ,racks, number_of_packages, number_of_racks, tree_predictor):
# print('pokolenie: ', generation) # print('pokolenie: ', generation)
evaluations = [] evaluations = []
for i in range(generation_size): for i in range(generation_size):
@ -100,7 +125,7 @@ def gen_alg(packages, racks, number_of_generations, generation_size, mutation_pr
number_of_racks = len(racks) number_of_racks = len(racks)
### WŁAŚCIWY ALGORYTM ### WŁAŚCIWY ALGORYTM
generation = first_gen(number_of_packages, number_of_racks) generation = first_gen(number_of_packages, number_of_racks, generation_size)
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
@ -116,7 +141,7 @@ def gen_alg(packages, racks, number_of_generations, generation_size, mutation_pr
# print(generation) # print(generation)
### RULETKA ### RULETKA
survivors = roulette(generation, packages, racks, number_of_packages, number_of_racks, tree_predictor) survivors = roulette(generation, packages, generation_size, racks, number_of_packages, number_of_racks, tree_predictor)
# print('przetrwali: ',survivors) # print('przetrwali: ',survivors)
### KRZYŻOWANIE ### KRZYŻOWANIE

View File

@ -20,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, mutation_prob=0.03, generation_size=40, number_of_generations=100, amount_of_promotion=0): def __init__(self, mutation_prob=0.05, generation_size=30, number_of_generations=100, 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")