fix gen alg

This commit is contained in:
s481904 2024-06-10 15:35:08 +02:00
parent e56854690c
commit 084e96ba7d
2 changed files with 125 additions and 50 deletions

View File

@ -19,37 +19,51 @@ class Board:
self.carrot = pygame.image.load("board/carrot.png") self.carrot = pygame.image.load("board/carrot.png")
def generate_board(self): def generate_board(self):
self.board = [[random.choice([0,1,2,3,4,5,6,7,8,9]) for _ in range(rows)] for _ in range(cols)] # Najpierw wypełniamy całą planszę trawą (kod 2)
self.board = [[2 for _ in range(rows)] for _ in range(cols)]
# Losowo wybieramy 5 unikalnych pozycji dla chwastów
weed_positions = random.sample([(row, col) for row in range(rows) for col in range(cols)], 5)
# Umieszczamy chwasty na wylosowanych pozycjach
for row, col in weed_positions:
self.board[row][col] = 1 # 1 oznacza chwast
# Teraz losowo umieszczamy inne elementy, omijając pozycje chwastów
for row in range(rows):
for col in range(cols):
if (row, col) not in weed_positions:
# Losujemy typ terenu, ale pomijamy kod 1 (chwast)
self.board[row][col] = random.choice([0, 2, 3, 4, 5, 6, 7, 8, 9])
def draw_cubes(self, win): def draw_cubes(self, win):
for row in range(rows): for row in range(rows):
for col in range(cols): for col in range(cols):
cube_rect = pygame.Rect(row * size, col * size, size, size) cube_rect = pygame.Rect(row * size, col * size, size, size)
cube=self.board[row][col] cube = self.board[row][col]
if row==4 and col==4: if row == 4 and col == 4:
win.blit(self.grass, cube_rect) win.blit(self.grass, cube_rect)
elif cube == 0: elif cube == 0:
rock_scale = pygame.transform.scale(self.rock, (size, size)) rock_scale = pygame.transform.scale(self.rock, (size, size))
win.blit(self.dirt, cube_rect) win.blit(self.dirt, cube_rect)
win.blit(rock_scale, cube_rect) #win.blit(rock_scale, cube_rect)
elif cube == 1: elif cube == 1:
weed_scale = pygame.transform.scale(self.weeds, (size,size)) weed_scale = pygame.transform.scale(self.weeds, (size, size))
win.blit(self.grass, cube_rect) win.blit(self.grass, cube_rect)
win.blit(weed_scale, cube_rect) win.blit(weed_scale, cube_rect)
elif cube in(2,3,4,5): elif cube in (2, 3, 4, 5):
win.blit(self.grass, cube_rect) win.blit(self.grass, cube_rect)
elif cube == 10: elif cube == 10:
win.blit(self.soil, cube_rect) win.blit(self.soil, cube_rect)
elif cube == 11: elif cube == 11:
carrot_scale = pygame.transform.scale(self.carrot, (size,size)) carrot_scale = pygame.transform.scale(self.carrot, (size, size))
win.blit(self.carrot, cube_rect) win.blit(self.carrot, cube_rect)
win.blit(carrot_scale, cube_rect) win.blit(carrot_scale, cube_rect)
else: else:
win.blit(self.dirt, cube_rect) win.blit(self.dirt, cube_rect)
def load_costs(self): def load_costs(self):
self.costs = { self.costs = {
@ -78,6 +92,7 @@ class Board:
def is_weed(self,row,col): def is_weed(self,row,col):
return self.board[row][col] == 1 return self.board[row][col] == 1
def set_grass(self,row,col): def set_grass(self,row,col):
self.board[row][col]=2 self.board[row][col]=2
@ -93,10 +108,10 @@ class Board:
def set_carrot(self, row, col): def set_carrot(self, row, col):
self.board[row][col] = 11 self.board[row][col] = 11
def get_dirt_positions(self): def get_weed_positions(self):
dirt_positions = [] weed_positions = []
for row in range(rows): for row in range(rows):
for col in range(cols): for col in range(cols):
if self.is_dirt(row, col): if self.is_weed(row, col):
dirt_positions.append([row, col]) weed_positions.append([row, col])
return dirt_positions return weed_positions

View File

@ -5,11 +5,10 @@ from constant import width, height, size, rows, cols
from board import Board from board import Board
from tractor import Tractor from tractor import Tractor
routes_num = 20 # Ilość ścieżek routes_num = 20 # Ilość ścieżek
board = Board() board = Board()
dirt_positions = board.get_dirt_positions() weed_positions = board.get_weed_positions()
dirt_count = len(dirt_positions) weed_count = len(weed_positions)
def manhattan(a, b): def manhattan(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1]) return abs(a[0] - b[0]) + abs(a[1] - b[1])
@ -18,23 +17,20 @@ def find_routes(routes_num):
population_set = [] # zapisujemy trasy - losowe ułóżenia population_set = [] # zapisujemy trasy - losowe ułóżenia
for i in range(routes_num): for i in range(routes_num):
# losowo wygenerowane kolejności na trasie # losowo wygenerowane kolejności na trasie
single_route = np.random.choice(list(range(dirt_count)), dirt_count, replace=False) single_route = np.random.choice(list(range(weed_count)), weed_count, replace=False)
population_set.append(single_route) population_set.append(single_route)
return np.array(population_set) return np.array(population_set) #zwracamy 20 roznych losowych tras
def sum_up_for_route(route_indices): def sum_up_for_route(route_indices):
sum = 0 sum = 0
for i in range(len(route_indices) - 1): for i in range(len(route_indices) - 1):
current_dirt = dirt_positions[route_indices[i]] current_weed = weed_positions[route_indices[i]]
next_dirt = dirt_positions[route_indices[i + 1]] next_weed = weed_positions[route_indices[i + 1]]
sum += manhattan(current_dirt, next_dirt) sum += manhattan(current_weed, next_weed)
return sum return sum #zwracamy odleglosc (ilosc pol) dla danej trasy manhatanem
def routes_sum(population_set): # zapisujemy na liście finalne sumy odległości dla każdej z tras
def routes_sum(population_set): # zapisujemy na liście finalne sumy odległości dla każdej z opcji tras
list_of_sums = np.zeros(routes_num) list_of_sums = np.zeros(routes_num)
for i in range(routes_num): for i in range(routes_num):
list_of_sums[i] = sum_up_for_route(population_set[i]) # wywołujemy dla każdej trasy na liście list_of_sums[i] = sum_up_for_route(population_set[i]) # wywołujemy dla każdej trasy na liście
@ -42,29 +38,30 @@ def routes_sum(population_set): # zapisujemy na liście finalne sumy odległoś
def calculate_fitness(distances): def calculate_fitness(distances):
# Odwrotność odległości jako fitness # odwrotność odległości jako fitness
# Dodajemy małą wartość (np. 1) aby uniknąć dzielenia przez zero # dodajemy małą wartość (np. 1) aby uniknąć dzielenia przez zero
return 1 / (distances + 1) return 1 / (distances + 1)
def selection(population_set, list_of_sums): def selection(population_set, list_of_sums):
# Oblicz wartości fitness dla każdej trasy #RULETKA - czesciowo faworyzuje rozwiaznaia, wiekszy fitness wieksze szanse
fitness_values = calculate_fitness(list_of_sums) # obliczamy wartości fitness (przystosowania) dla każdej trasy
# Normalizuj wartości fitness, aby sumowały się do 1 (wymagane dla np.random.choice) fitness_values = calculate_fitness(list_of_sums)#krotsze trasy maja miec wyzsze wartosci
# normalizujemy wartości fitness, aby sumowały się do 1 (wymagane dla np.random.choice)
probabilities = fitness_values / fitness_values.sum() probabilities = fitness_values / fitness_values.sum()
# Wybierz rodziców na podstawie prawdopodobieństw (wartości fitness) # wybieramy indeksy rodziców na podstawie prawdopodobieństw
progenitor_indices_a = np.random.choice(range(len(population_set)), len(population_set), p=probabilities, replace=True) progenitor_indices_a = np.random.choice(range(len(population_set)), len(population_set), p=probabilities, replace=True)
progenitor_indices_b = np.random.choice(range(len(population_set)), len(population_set), p=probabilities, replace=True) progenitor_indices_b = np.random.choice(range(len(population_set)), len(population_set), p=probabilities, replace=True)
# Wybierz rzeczywiste trasy # finalne trasy
progenitor_a = population_set[progenitor_indices_a] progenitor_a = population_set[progenitor_indices_a]
progenitor_b = population_set[progenitor_indices_b] progenitor_b = population_set[progenitor_indices_b]
return np.array([progenitor_a, progenitor_b]) return np.array([progenitor_a, progenitor_b]) #zwracami listy przodkow-rodzicow
def one_point_crossover(parent_a, parent_b): #krzyzowanie jednopunktowe def one_point_crossover(parent_a, parent_b): #krzyzowanie jednopunktowe
crossover_point = np.random.randint(1, len(parent_a)) crossover_point = np.random.randint(1, len(parent_a))
child = np.concatenate((parent_a[:crossover_point], [x for x in parent_b if x not in parent_a[:crossover_point]])) child = np.concatenate((parent_a[:crossover_point], [x for x in parent_b if x not in parent_a[:crossover_point]]))
return child return child #loosyw punkt przeciecia ktory skleja nam nowa trase, wieksza szans na lepsza tarse
def population_mating(progenitor_list): def population_mating(progenitor_list):
new_population_set = [] new_population_set = []
@ -72,24 +69,17 @@ def population_mating(progenitor_list):
progenitor_a, progenitor_b = progenitor_list[0][i], progenitor_list[1][i] progenitor_a, progenitor_b = progenitor_list[0][i], progenitor_list[1][i]
child = one_point_crossover(progenitor_a, progenitor_b) child = one_point_crossover(progenitor_a, progenitor_b)
new_population_set.append(child) new_population_set.append(child)
return new_population_set return new_population_set # lista potomkow po krzyzowaniu
def mutation_of_child(child):
for i in range(dirt_count): # dla każdego elementu dajemy losową szansę zamiany int *rate
x = np.random.randint(0, dirt_count)
y = np.random.randint(0, dirt_count)
child[x], child[y] = child[y], child[x] # zamiana miejscami
return child def mutation_of_child(child, mutation_rate=0.2):#procent moze pomoc w niezaklucaniu trasy gdy jesy duza trasa ale idk
'''def mutation_of_child(child, mutation_rate=0.1):#procent moze pomoc w niezaklucaniu trasy gdy jesy duza trasa ale idk
num_mutations = int(len(child) * mutation_rate) num_mutations = int(len(child) * mutation_rate)
for _ in range(num_mutations): for _ in range(num_mutations):
x = np.random.randint(0, len(child)) x = np.random.randint(0, len(child))#losowa szansa zamiany - mutacja
y = np.random.randint(0, len(child)) y = np.random.randint(0, len(child))
child[x], child[y] = child[y], child[x] child[x], child[y] = child[y], child[x]
return child''' return child#zwrocenie bardziej roznorodnych potomkow
def mutate_population(new_population_set): def mutate_population(new_population_set):
@ -100,6 +90,23 @@ def mutate_population(new_population_set):
if __name__ == '__main__': if __name__ == '__main__':
pygame.init()
WIN = pygame.display.set_mode((width, height))
pygame.display.set_caption('Trasa Traktora')
clock = pygame.time.Clock()
board = Board()
board.load_images()
weed_positions = [(col, row) for col in range(cols) for row in range(rows) if board.is_weed(col, row)]
weed_count = len(weed_positions)
board.set_grass(9, 9) # pozycja startowa
tractor = Tractor(9, 9) # Start traktora
# Inicjalizacja final_route
final_route = [0, float('inf'), np.array([])]
# [0]: indeks iteracji, [1]: najlepsza suma odległości, [2]: najlepsza trasa
population_set = find_routes(routes_num) population_set = find_routes(routes_num)
list_of_sums = routes_sum(population_set) list_of_sums = routes_sum(population_set)
@ -107,6 +114,7 @@ if __name__ == '__main__':
new_population_set = population_mating(progenitor_list) new_population_set = population_mating(progenitor_list)
final_mutated_population = mutate_population(new_population_set) final_mutated_population = mutate_population(new_population_set)
final_route = [-1, np.inf, np.array([])] # format listy final_route = [-1, np.inf, np.array([])] # format listy
for i in range(20): for i in range(20):
list_of_sums = routes_sum(final_mutated_population) list_of_sums = routes_sum(final_mutated_population)
# zapisujemy najlepsze rozwiązanie # zapisujemy najlepsze rozwiązanie
@ -115,12 +123,64 @@ if __name__ == '__main__':
final_route[1] = list_of_sums.min() final_route[1] = list_of_sums.min()
final_route[2] = np.array(final_mutated_population)[list_of_sums.min() == list_of_sums] final_route[2] = np.array(final_mutated_population)[list_of_sums.min() == list_of_sums]
progenitor_list = selection(population_set, list_of_sums) progenitor_list = selection(population_set, list_of_sums)
new_population_set = population_mating(progenitor_list) new_population_set = population_mating(progenitor_list)
final_mutated_population = mutate_population(new_population_set) final_mutated_population = mutate_population(new_population_set)
print(f"Najlepsza trasa znaleziona w iteracji: {final_route[0]}") print(f"Najlepsza trasa znaleziona w iteracji: {final_route[0]}")
print(f"Minimalna suma odległości: {final_route[1]}") print(f"Minimalna suma odległości: {final_route[1]}")
print(f"Kolejne pola: {final_route[2]}")
run = True
current_target_index = 0
best_routes = final_route[2] #tablica z najlepszymi trasami
visited_fields = []
while run:
clock.tick(2) # FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for route in best_routes:
if current_target_index < len(route):
current_weed = weed_positions[route[current_target_index]]
# ruch w kierunku bieżącego celu
if tractor.col < current_weed[0]:
tractor.col += 1
tractor.direction = "right"
elif tractor.col > current_weed[0]:
tractor.col -= 1
tractor.direction = "left"
elif tractor.row < current_weed[1]:
tractor.row += 1
tractor.direction = "down"
elif tractor.row > current_weed[1]:
tractor.row -= 1
tractor.direction = "up"
current_position = (tractor.col, tractor.row)
if current_position not in visited_fields:
visited_fields.append(current_position)
# Jeśli traktor dotarł do celu
if (tractor.col, tractor.row) == current_weed:
current_target_index += 1
# Aktualizacja planszy
if board.is_weed(tractor.col, tractor.row):
board.set_carrot(tractor.col, tractor.row)
board.draw_cubes(WIN)
tractor.draw(WIN)
pygame.display.update()
print("Odwiedzone pola:")
for field in visited_fields:
print(field)
pygame.quit()