From 80e497cc593f206042a96c749f5f22181960d35d Mon Sep 17 00:00:00 2001 From: Kacper Kalinowski Date: Fri, 10 Jun 2022 11:11:31 +0200 Subject: [PATCH] Finished TSP, cleaning, refactoring whole project --- decision_tree/tree_as_txt.txt | 83 +++++++------------------------- decision_tree/tree_model | Bin 5556 -> 5556 bytes genetic_algorithm/TSP.py | 19 -------- last_map.nparr | Bin 1728 -> 2628 bytes main.py | 19 ++------ map/map.py | 11 ----- mapa.py | 11 ----- path_search_algorthms/a_star.py | 3 -- settings.py | 8 +-- 9 files changed, 27 insertions(+), 127 deletions(-) diff --git a/decision_tree/tree_as_txt.txt b/decision_tree/tree_as_txt.txt index aecad7f..7fd0886 100644 --- a/decision_tree/tree_as_txt.txt +++ b/decision_tree/tree_as_txt.txt @@ -4,73 +4,46 @@ | | | |--- class: 0 | | |--- feature_0 > 1.50 | | | |--- feature_3 <= 3.50 -<<<<<<< HEAD | | | | |--- feature_4 <= 2.50 | | | | | |--- class: 1 | | | | |--- feature_4 > 2.50 | | | | | |--- feature_2 <= 2.50 | | | | | | |--- class: 1 | | | | | |--- feature_2 > 2.50 -======= -| | | | |--- feature_2 <= 2.50 -| | | | | |--- class: 1 -| | | | |--- feature_2 > 2.50 -| | | | | |--- feature_4 <= 2.50 -| | | | | | |--- class: 1 -| | | | | |--- feature_4 > 2.50 ->>>>>>> a16ec52642067a2be0b41a5c3bcab24193122343 | | | | | | |--- class: 0 | | | |--- feature_3 > 3.50 | | | | |--- feature_3 <= 4.50 | | | | | |--- feature_1 <= 2.50 | | | | | | |--- feature_0 <= 2.50 | | | | | | | |--- feature_1 <= 1.50 -| | | | | | | | |--- feature_4 <= 2.50 -| | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_4 > 2.50 -| | | | | | | | | |--- feature_2 <= 2.00 -| | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_2 > 2.00 -| | | | | | | | | | |--- class: 0 -| | | | | | | |--- feature_1 > 1.50 -| | | | | | | | |--- class: 0 -| | | | | | |--- feature_0 > 2.50 -<<<<<<< HEAD -| | | | | | | |--- feature_2 <= 2.50 -| | | | | | | | |--- class: 1 -| | | | | | | |--- feature_2 > 2.50 -| | | | | | | | |--- feature_4 <= 2.50 -| | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_4 > 2.50 -======= -| | | | | | | |--- feature_4 <= 2.50 -| | | | | | | | |--- class: 1 -| | | | | | | |--- feature_4 > 2.50 -| | | | | | | | |--- feature_2 <= 2.50 -| | | | | | | | | |--- class: 1 -| | | | | | | | |--- feature_2 > 2.50 ->>>>>>> a16ec52642067a2be0b41a5c3bcab24193122343 -| | | | | | | | | |--- class: 0 -| | | | | |--- feature_1 > 2.50 -| | | | | | |--- feature_1 <= 3.50 -| | | | | | | |--- feature_0 <= 3.50 -| | | | | | | | |--- class: 0 -| | | | | | | |--- feature_0 > 3.50 -<<<<<<< HEAD | | | | | | | | |--- feature_2 <= 2.50 | | | | | | | | | |--- class: 1 | | | | | | | | |--- feature_2 > 2.50 | | | | | | | | | |--- feature_4 <= 2.00 | | | | | | | | | | |--- class: 1 | | | | | | | | | |--- feature_4 > 2.00 -======= +| | | | | | | | | | |--- class: 0 +| | | | | | | |--- feature_1 > 1.50 +| | | | | | | | |--- class: 0 +| | | | | | |--- feature_0 > 2.50 +| | | | | | | |--- feature_2 <= 2.50 +| | | | | | | | |--- class: 1 +| | | | | | | |--- feature_2 > 2.50 | | | | | | | | |--- feature_4 <= 2.50 | | | | | | | | | |--- class: 1 | | | | | | | | |--- feature_4 > 2.50 -| | | | | | | | | |--- feature_2 <= 2.00 +| | | | | | | | | |--- class: 0 +| | | | | |--- feature_1 > 2.50 +| | | | | | |--- feature_1 <= 3.50 +| | | | | | | |--- feature_0 <= 3.50 +| | | | | | | | |--- class: 0 +| | | | | | | |--- feature_0 > 3.50 +| | | | | | | | |--- feature_2 <= 2.50 +| | | | | | | | | |--- class: 1 +| | | | | | | | |--- feature_2 > 2.50 +| | | | | | | | | |--- feature_4 <= 2.00 | | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- feature_2 > 2.00 ->>>>>>> a16ec52642067a2be0b41a5c3bcab24193122343 +| | | | | | | | | |--- feature_4 > 2.00 | | | | | | | | | | |--- class: 0 | | | | | | |--- feature_1 > 3.50 | | | | | | | |--- class: 0 @@ -86,7 +59,6 @@ | | | | | | |--- class: 1 | | | | |--- feature_3 > 3.50 | | | | | |--- feature_1 <= 2.50 -<<<<<<< HEAD | | | | | | |--- feature_0 <= 2.50 | | | | | | | |--- class: 0 | | | | | | |--- feature_0 > 2.50 @@ -94,15 +66,6 @@ | | | | | | | | |--- class: 1 | | | | | | | |--- feature_3 > 4.50 | | | | | | | | |--- class: 0 -======= -| | | | | | |--- feature_3 <= 4.50 -| | | | | | | |--- feature_0 <= 2.50 -| | | | | | | | |--- class: 0 -| | | | | | | |--- feature_0 > 2.50 -| | | | | | | | |--- class: 1 -| | | | | | |--- feature_3 > 4.50 -| | | | | | | |--- class: 0 ->>>>>>> a16ec52642067a2be0b41a5c3bcab24193122343 | | | | | |--- feature_1 > 2.50 | | | | | | |--- class: 0 | | | |--- feature_4 > 4.50 @@ -113,7 +76,6 @@ | |--- feature_1 <= 1.50 | | |--- feature_4 <= 1.50 | | | |--- feature_2 <= 4.50 -<<<<<<< HEAD | | | | |--- feature_0 <= 1.50 | | | | | |--- class: 0 | | | | |--- feature_0 > 1.50 @@ -121,15 +83,6 @@ | | | | | | |--- class: 1 | | | | | |--- feature_3 > 4.50 | | | | | | |--- class: 0 -======= -| | | | |--- feature_3 <= 4.50 -| | | | | |--- feature_0 <= 1.50 -| | | | | | |--- class: 0 -| | | | | |--- feature_0 > 1.50 -| | | | | | |--- class: 1 -| | | | |--- feature_3 > 4.50 -| | | | | |--- class: 0 ->>>>>>> a16ec52642067a2be0b41a5c3bcab24193122343 | | | |--- feature_2 > 4.50 | | | | |--- class: 0 | | |--- feature_4 > 1.50 diff --git a/decision_tree/tree_model b/decision_tree/tree_model index 94453c27c027b40902bc1add5b03b97246877741..ae04602d38e3aa1bbd0fe76df41e959e8e61602d 100644 GIT binary patch delta 24 gcmdm@y+wP2FZ<-9tOAo`*%Kx&W#Qj^hW!OE0CrIcO#lD@ delta 24 gcmdm@y+wP2FZ<+BR)NW}>natdrd%l@1f0B-yU(*OVf diff --git a/genetic_algorithm/TSP.py b/genetic_algorithm/TSP.py index 199979d..aaeea82 100644 --- a/genetic_algorithm/TSP.py +++ b/genetic_algorithm/TSP.py @@ -190,30 +190,11 @@ def nextGeneration(currentGen, eliteSize, mutationRate, distanceArray): return nextGeneration -def geneticAlgorithm(population, popSize, eliteSize, mutationRate, generations, distanceArray): - pop = initialPopulation(popSize, population) - print("Initial distance: " + str(1 / rankRoutes(pop, distanceArray)[0][1])) - - for i in range(0, generations): - pop = nextGeneration(pop, eliteSize, mutationRate, distanceArray) - - print("Final distance: " + str(1 / rankRoutes(pop, distanceArray)[0][1])) - bestRouteIndex = rankRoutes(pop, distanceArray)[0][0] - bestRoute = pop[bestRouteIndex] - return bestRoute - - # tutaj ma być lista kordów potencjalnych śmietników z drzewa decyzyjnego cityList = [] -# for i in range(0,25): -# cityList.append(City(x=int(random.random() * 200), y=int(random.random() * 200))) - -# geneticAlgorithm(population=cityList, popSize=100, eliteSize=20, mutationRate=0.01, generations=1000) - - # plotting the progress def distanceFromCityToCity(cityFrom, city, array): diff --git a/last_map.nparr b/last_map.nparr index 086faa106e8ad21c327003bab051525af64a1f13..671ab21ac8586a153f0c033e9d4421f31010cb63 100644 GIT binary patch literal 2628 zcmb`Dy-LJT5QVdrK1I4HY_+(JSlcN!7FM#j5e0E25gYL-d|`8t4-Ui4y}QX?NM`2z z%}su9?{DrN7S&hv-E7t(%KpD*j5dg3{)hI69Vo~NXZ{9?$Bn6Db+z^NrqO6MNkL(iNu*FR@BUcQt6dgz_- zlc$=($c3Z2j~*>^PAiS;UF$trRC!W9$Izx~^h?7&GMMpe7MH@*snOB3>0JkQXwBv4vi9-mep8R_4Q@+ox^WN_P zp2EbXGSRAfU&?}4#phVAFX{a*Ub*n(ITm}(I(Vr6=(IN(zW)z>Y#dDh literal 1728 zcmbV|y=nqc5QSIEJjLyFO^Q@ui5n6SmEB+?d5XMnoZ^GwFne8gLC&4? zH-mq>*XNhFS#_zddEfRQU4BY=^Zl6Xls`}1PuCpZPu;#Xe%l=S_Qv;L&AGkJi}k8b z>(#POziHz4P!+zJXmgppt{PVFfg8feJJf3(CC%>`t+V+{nUWTD*SbL>~Y&C1~g z{Z4x`M>*q6rx!0>V3bAkNFMasn2sn%2YK&-!!re~kKdHM&-mLs&l`PcAG-PdJDTYP Qy`TTZbNJAhLE}~X2Lr<#3IG5A diff --git a/main.py b/main.py index e74759e..6fa8a15 100644 --- a/main.py +++ b/main.py @@ -49,9 +49,6 @@ class Game(): pg.display.set_caption("Trashmaster") self.load_data() self.init_game() - # because dont work without data.txt - # self.init_bfs() - # self.init_a_star() self.t = aiPlayer.aiPlayer(self.player, game=self) def init_game(self): @@ -101,10 +98,10 @@ class Game(): atrrs_container = i.get_attributes() x, y = i.get_coords() dec = decisionTree.decision(getTree(), *atrrs_container) - # if dec[0] == 1: - self.positive_decision.append(i) # zmiana po to by losowało wszystkie smietniki a nie poprawne tylko, zeby ladniej bylo widac algorytm genetyczny - # else: - # self.negative_decision.append(i) + if dec[0] == 1: + self.positive_decision.append(i) # zmiana po to by losowało wszystkie smietniki a nie poprawne tylko, zeby ladniej bylo widac algorytm genetyczny + else: + self.negative_decision.append(i) print('positive actions') print(len(self.positive_decision)) @@ -121,9 +118,7 @@ class Game(): print('action') - # trash_x, trash_y = i.get_coords() - # for ii in self.tsp_list: temp_tsp = str(self.tsp_list[i]) temp_tsp = temp_tsp.strip("()") temp_tsp = temp_tsp.split(",") @@ -161,19 +156,15 @@ class Game(): # print(self.positive_actions[0]) - # self.t.startAiController(self.positive_actions[0]) def init_TSP(self): city_list =[] for i in self.positive_decision: trash_x, trash_y = i.get_coords() - # city_list.append(TSP.City(x=int(trash_x), y=int(trash_y), array=self.mapArray)) city_list.append(TSP.City(x=trash_x, y=trash_y, array=self.mapArray)) - - # dist = a_star.get_cost - self.tsp_list = TSP.geneticAlgorithmPlot(population=city_list, popSize=100, eliteSize=20, mutationRate=0.01, generations=300, array=self.mapArray) + self.tsp_list = TSP.geneticAlgorithmPlot(population=city_list, popSize=100, eliteSize=20, mutationRate=0.01, generations=500, array=self.mapArray) print(self.tsp_list) def load_data(self): diff --git a/map/map.py b/map/map.py index b4c332c..68f0424 100644 --- a/map/map.py +++ b/map/map.py @@ -5,17 +5,6 @@ from settings import * def get_tiles(): array = map_utils.generate_map() - # array = map_utils.get_blank_map_array() - - # array[1][1] = 1 - # array[1][2] = 1 - # array[1][3] = 1 - # array[1][4] = 1 - # array[1][5] = 1 - # array[1][6] = 1 - - # array[2][5] = 1 - pattern = map_pattern.get_pattern() tiles = map_utils.get_sprites(array, pattern) return tiles, array diff --git a/mapa.py b/mapa.py index 07581b1..5ad3528 100644 --- a/mapa.py +++ b/mapa.py @@ -2,17 +2,6 @@ import pygame as pg import pytmx -# config -# TILE_SIZE = 16 - -# def preparedMap(screenSize): -# tileImage = pg.image.load('tile1.png') -# surface = pg.Surface(screenSize) - -# for x in range(0, screenSize[0], TILE_SIZE): -# for y in range(0, screenSize[1], TILE_SIZE): -# surface.blit(tileImage, (x, y)) -# return surface class TiledMap: # loading file diff --git a/path_search_algorthms/a_star.py b/path_search_algorthms/a_star.py index 0ee756a..dd9345e 100644 --- a/path_search_algorthms/a_star.py +++ b/path_search_algorthms/a_star.py @@ -4,11 +4,8 @@ from path_search_algorthms import a_star_utils as utils def get_cost(start_x: int, start_y: int, target_x: int, target_y: int, array): actions = search_path(start_x, start_y, utils.Rotation.NONE, target_x, target_y, array) - print('length') if actions is None: - print('0') return 1 - print(len(actions)) return len(actions) diff --git a/settings.py b/settings.py index a4fa55d..b493ad6 100644 --- a/settings.py +++ b/settings.py @@ -27,12 +27,12 @@ PLAYER_WIDTH = 64 PLAYER_HEIGHT = 32 #map settings x 16 y 12 -MAP_WIDTH = 20 -MAP_HEIGHT = 20 +MAP_WIDTH = 25 +MAP_HEIGHT = 25 TILE_SIZE_PX = 64 MAP_WIDTH_PX = MAP_WIDTH * TILE_SIZE_PX MAP_HEIGHT_PX = MAP_HEIGHT * TILE_SIZE_PX -TRASHBIN_NUMBER = 50 -WALL_NUMBER = 40 \ No newline at end of file +TRASHBIN_NUMBER = 70 +WALL_NUMBER = 50 \ No newline at end of file