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
mutation_prob = 0.03
### ilość osobników w pokoleniu, powinna być parzysta
generation_size = 40
# generation_size = 40
### liczba pokoleń
number_of_generations = 30
# number_of_generations = 30
### jak bardzo promowane są osobniki wykorzystujące całą pojemność regału
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 = []
for individual in range(generation_size):
individual = []
@ -20,17 +20,43 @@ def first_gen(number_of_packages, number_of_racks):
first_generation.append(individual)
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):
# 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)
bad_placed = 0
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])
if not can_place:
rest_of_capacity[individual[i]] -= packages[i].size * 5
else:
rest_of_capacity[individual[i]] -= packages[i].size
bad_placed +=1
# print("pozostała pojemność: ",rest_of_capacity)
# pdb.set_trace()
fitness = 0
@ -40,12 +66,11 @@ def evaluation(individual, packages, racks, number_of_packages, number_of_racks,
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
fitness += amount_of_promotion
fitness -= 5*bad_placed
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)
evaluations = []
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)
### 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
# 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)
### 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)
### 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)
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()
self.display = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Smart ForkLift")