Update 'src/board.py'

łączenie z algorytmem genetycznym
This commit is contained in:
Gabriela Piekarska 2022-06-09 10:12:52 +02:00
parent 3e01a528d3
commit 91693d2b19

View File

@ -7,6 +7,7 @@ import pygame
import snn import snn
import joblib import joblib
import os import os
import gen_algorithms
screen = [] screen = []
objectArray = [] objectArray = []
@ -176,6 +177,7 @@ def kb_listen(objectArray, gridLength, path):
agent = objectArray[0] agent = objectArray[0]
#agent.move(gridLength, path) #agent.move(gridLength, path)
if __name__ == '__main__': if __name__ == '__main__':
pygame.init() # inicjalizacja modułów, na razie niepotrzebna pygame.init() # inicjalizacja modułów, na razie niepotrzebna
gridSize = 15 gridSize = 15
@ -222,6 +224,39 @@ if __name__ == '__main__':
pathPos = 0 pathPos = 0
nextCheckpoint = 1 nextCheckpoint = 1
#od tad proboje
# parametry
num_of_houses = 8 # ilość domków
routes_num = 40 # ilość ścieżek, które będziemy generować
# rate = 0.3 # do mutacji, by liczby były ładniejsze
houses_coordinates = [[7, 4], [3, 10], [8, 10], [4, 5], [1, 2], [10, 4], [13, 14], [6, 9]] # generowanie losowych współrzędnych między 1, a 9
names = np.array(['Dom A', 'Dom B', 'Dom C', 'Dom D', 'Dom E', 'Dom F', 'Dom G', 'Dom H']) # nazwy domów
houses_info = {x: y for x, y in
zip(names, houses_coordinates)} # zawiera nazwę domu i jego współrzędne X, Y - słownik
population_set = gen_algorithms.generate_routes(names, routes_num)
list_of_sums = gen_algorithms.sums_for_all_routes(population_set, houses_info)
progenitor_list = gen_algorithms.selection(population_set, list_of_sums)
new_population_set = gen_algorithms.population_mating(progenitor_list)
final_mutated_population = gen_algorithms.mutate_population(new_population_set)
final_route = [-1, np.inf, np.array([])] # format listy
for i in range(400):
list_of_sums = gen_algorithms.sums_for_all_routes(final_mutated_population, houses_info)
# zapisujemy najlepsze rozwiązanie
if list_of_sums.min() < final_route[1]:
final_route[0] = i
final_route[1] = list_of_sums.min()
final_route[2] = np.array(final_mutated_population)[list_of_sums.min() == list_of_sums]
progenitor_list = gen_algorithms.selection(population_set, list_of_sums)
new_population_set = gen_algorithms.population_mating(progenitor_list)
final_mutated_population = gen_algorithms.mutate_population(new_population_set)
print("tutaj")
print(final_route)
print(houses_info) # tu nazwa domu i lokalizacja
while True: while True:
agent_x, agent_y = astarPath[pathPos] agent_x, agent_y = astarPath[pathPos]
checkpoint_x, checkpoint_y = checkpoints[nextCheckpoint] checkpoint_x, checkpoint_y = checkpoints[nextCheckpoint]