tsp v3
This commit is contained in:
parent
005255ee9b
commit
b1d2d04448
@ -183,10 +183,10 @@ def geneticAlgorithm(population, popSize, eliteSize, mutationRate, generations):
|
|||||||
|
|
||||||
cityList = []
|
cityList = []
|
||||||
|
|
||||||
for i in range(0,25):
|
# for i in range(0,25):
|
||||||
cityList.append(City(x=int(random.random() * 200), y=int(random.random() * 200)))
|
# 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)
|
# geneticAlgorithm(population=cityList, popSize=100, eliteSize=20, mutationRate=0.01, generations=1000)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -197,15 +197,20 @@ def geneticAlgorithmPlot(population, popSize, eliteSize, mutationRate, generatio
|
|||||||
pop = initialPopulation(popSize, population)
|
pop = initialPopulation(popSize, population)
|
||||||
progress = []
|
progress = []
|
||||||
progress.append(1 / rankRoutes(pop)[0][1])
|
progress.append(1 / rankRoutes(pop)[0][1])
|
||||||
|
print("Initial distance: " + str(1 / rankRoutes(pop)[0][1]))
|
||||||
|
|
||||||
for i in range(0, generations):
|
for i in range(0, generations):
|
||||||
pop = nextGeneration(pop, eliteSize, mutationRate)
|
pop = nextGeneration(pop, eliteSize, mutationRate)
|
||||||
progress.append(1 / rankRoutes(pop)[0][1])
|
progress.append(1 / rankRoutes(pop)[0][1])
|
||||||
|
|
||||||
|
print("Final distance: " + str(1 / rankRoutes(pop)[0][1]))
|
||||||
|
bestRouteIndex = rankRoutes(pop)[0][0]
|
||||||
|
bestRoute = pop[bestRouteIndex]
|
||||||
|
|
||||||
plt.plot(progress)
|
plt.plot(progress)
|
||||||
plt.ylabel('Distance')
|
plt.ylabel('Distance')
|
||||||
plt.xlabel('Generation')
|
plt.xlabel('Generation')
|
||||||
plt.show()
|
plt.show()
|
||||||
|
return bestRoute
|
||||||
|
|
||||||
# geneticAlgorithmPlot(population=cityList, popSize=100, eliteSize=20, mutationRate=0.01, generations=1000)
|
# geneticAlgorithmPlot(population=cityList, popSize=100, eliteSize=20, mutationRate=0.01, generations=1000)
|
101
main.py
101
main.py
@ -1,19 +1,20 @@
|
|||||||
from asyncio import sleep
|
|
||||||
from calendar import c
|
|
||||||
from random import randint
|
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
from game_objects.player import Player
|
|
||||||
import pygame as pg
|
|
||||||
import sys
|
import sys
|
||||||
from os import path
|
from random import randint
|
||||||
import math
|
import math
|
||||||
from map import *
|
|
||||||
from settings import *
|
import pygame as pg
|
||||||
|
import numpy
|
||||||
|
|
||||||
|
from game_objects.player import Player
|
||||||
|
from game_objects.aiPlayer import aiPlayer
|
||||||
|
from game_objects.trash import Trash
|
||||||
|
|
||||||
from map import map
|
from map import map
|
||||||
from map import map_utils
|
from map import map_utils
|
||||||
|
from settings import *
|
||||||
from path_search_algorthms import bfs
|
from path_search_algorthms import bfs
|
||||||
from path_search_algorthms import a_star, a_star_utils
|
from path_search_algorthms import a_star_controller, a_star
|
||||||
from decision_tree import decisionTree
|
from decision_tree import decisionTree
|
||||||
from NeuralNetwork import prediction
|
from NeuralNetwork import prediction
|
||||||
from game_objects.trash import Trash
|
from game_objects.trash import Trash
|
||||||
@ -53,16 +54,6 @@ class Game():
|
|||||||
# self.init_a_star()
|
# self.init_a_star()
|
||||||
self.t = aiPlayer.aiPlayer(self.player, game=self)
|
self.t = aiPlayer.aiPlayer(self.player, game=self)
|
||||||
|
|
||||||
|
|
||||||
def get_actions_by_coords(self,x,y):
|
|
||||||
pos = (x,y)
|
|
||||||
offset_x, offset_y = self.camera.offset()
|
|
||||||
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
|
||||||
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
|
||||||
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
|
||||||
clicked_coords[0], clicked_coords[1], self.mapArray)
|
|
||||||
return actions
|
|
||||||
|
|
||||||
def init_game(self):
|
def init_game(self):
|
||||||
# initialize all variables and do all the setup for a new game
|
# initialize all variables and do all the setup for a new game
|
||||||
|
|
||||||
@ -70,6 +61,12 @@ class Game():
|
|||||||
|
|
||||||
# sprite groups and map array for calculations
|
# sprite groups and map array for calculations
|
||||||
(self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles()
|
(self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles()
|
||||||
|
|
||||||
|
# save current map
|
||||||
|
file = open('last_map.nparr', 'wb')
|
||||||
|
numpy.save(file, self.mapArray, allow_pickle=True)
|
||||||
|
file.close
|
||||||
|
|
||||||
self.trashDisplay = pg.sprite.Group()
|
self.trashDisplay = pg.sprite.Group()
|
||||||
self.agentSprites = pg.sprite.Group()
|
self.agentSprites = pg.sprite.Group()
|
||||||
# player obj
|
# player obj
|
||||||
@ -80,7 +77,6 @@ class Game():
|
|||||||
# other
|
# other
|
||||||
self.debug_mode = False
|
self.debug_mode = False
|
||||||
|
|
||||||
|
|
||||||
def init_bfs(self):
|
def init_bfs(self):
|
||||||
start_node = (0, 0)
|
start_node = (0, 0)
|
||||||
target_node = (18, 18)
|
target_node = (18, 18)
|
||||||
@ -96,17 +92,8 @@ class Game():
|
|||||||
nextNode = node[1]
|
nextNode = node[1]
|
||||||
print(realPath)
|
print(realPath)
|
||||||
|
|
||||||
def init_a_star(self):
|
|
||||||
# szukanie sciezki na sztywno i wyprintowanie wyniku (tablica stringow)
|
|
||||||
start_x = 0
|
|
||||||
start_y = 0
|
|
||||||
target_x = 6
|
|
||||||
target_y = 2
|
|
||||||
path = a_star.search_path(start_x, start_y, target_x, target_y, self.mapArray)
|
|
||||||
print(path)
|
|
||||||
|
|
||||||
def init_decision_tree(self):
|
def init_decision_tree(self):
|
||||||
# logika pracy z drzewem
|
# logika pracy z drzewem
|
||||||
self.positive_decision = []
|
self.positive_decision = []
|
||||||
self.negative_decision = []
|
self.negative_decision = []
|
||||||
|
|
||||||
@ -119,18 +106,21 @@ class Game():
|
|||||||
else:
|
else:
|
||||||
self.negative_decision.append(i)
|
self.negative_decision.append(i)
|
||||||
|
|
||||||
|
print('positive actions')
|
||||||
|
print(len(self.positive_decision))
|
||||||
# print('positive actions')
|
# print('positive actions')
|
||||||
# for i in self.positive_actions:
|
# for i in self.positive_actions:
|
||||||
# print('----')
|
# print('----')
|
||||||
# print(i)
|
# print(i)
|
||||||
# print('----')
|
# print('----')
|
||||||
|
def decsion_tree_move(self):
|
||||||
|
|
||||||
print('positive actions')
|
|
||||||
print(len(self.positive_decision))
|
|
||||||
for i in self.positive_decision:
|
for i in self.positive_decision:
|
||||||
# print(i.get_coords())
|
# print(i.get_coords())
|
||||||
|
print('action')
|
||||||
trash_x, trash_y = i.get_coords()
|
trash_x, trash_y = i.get_coords()
|
||||||
action = self.get_actions_by_coords(trash_x, trash_y)
|
action = a_star_controller.get_actions_for_target_coords(trash_x, trash_y, self)
|
||||||
|
print(action)
|
||||||
self.t.startAiController(action)
|
self.t.startAiController(action)
|
||||||
|
|
||||||
print('')
|
print('')
|
||||||
@ -140,15 +130,15 @@ class Game():
|
|||||||
for i in range(0, 10):
|
for i in range(0, 10):
|
||||||
random = randint(0, 48)
|
random = randint(0, 48)
|
||||||
file = files[random]
|
file = files[random]
|
||||||
result = prediction.getPrediction(dir + '/' +file, 'trained_nn_20.pth')
|
result = prediction.getPrediction(dir + '/' + file, 'trained_nn_20.pth')
|
||||||
img = pg.image.load(dir + '/' +file).convert_alpha()
|
img = pg.image.load(dir + '/' + file).convert_alpha()
|
||||||
img = pg.transform.scale(img, (128, 128))
|
img = pg.transform.scale(img, (128, 128))
|
||||||
trash = Trash(img, 0, 0, 128, 128)
|
trash = Trash(img, 0, 0, 128, 128)
|
||||||
self.trashDisplay.add(trash)
|
self.trashDisplay.add(trash)
|
||||||
self.text_display = result
|
self.text_display = result
|
||||||
self.draw()
|
self.draw()
|
||||||
print(result + ' ' + file)
|
# print(result + ' ' + file)
|
||||||
pg.time.wait(1000)
|
pg.time.wait(100)
|
||||||
self.text_display = ''
|
self.text_display = ''
|
||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
@ -156,25 +146,32 @@ class Game():
|
|||||||
|
|
||||||
# self.t.startAiController(self.positive_actions[0])
|
# self.t.startAiController(self.positive_actions[0])
|
||||||
def init_TSP(self):
|
def init_TSP(self):
|
||||||
city_list = self.positive_decision
|
|
||||||
dist = a_star.path_dist
|
|
||||||
|
|
||||||
for i in range(0,25):
|
city_list =[]
|
||||||
city_list.append(TSP.City(x=int(random.random() * 200), y=int(random.random() * 200)))
|
|
||||||
|
|
||||||
tsp_list = TSP.geneticAlgorithm(population=city_list, popSize=100, eliteSize=20, mutationRate=0.01, generations=1000)
|
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)))
|
||||||
|
|
||||||
|
|
||||||
|
# dist = a_star.get_cost
|
||||||
|
tsp_list = TSP.geneticAlgorithmPlot(population=city_list, popSize=100, eliteSize=20, mutationRate=0.01, generations=200)
|
||||||
|
print(tsp_list)
|
||||||
|
|
||||||
def load_data(self):
|
def load_data(self):
|
||||||
game_folder = path.dirname(__file__)
|
game_folder = os.path.dirname(__file__)
|
||||||
img_folder = path.join(game_folder, 'resources/textures')
|
img_folder = os.path.join(game_folder, 'resources/textures')
|
||||||
|
|
||||||
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
self.player_img = pg.image.load(os.path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
||||||
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# game loop - set self.playing = False to end the game
|
# game loop - set self.playing = False to end the game
|
||||||
self.playing = True
|
self.playing = True
|
||||||
self.init_decision_tree()
|
self.init_decision_tree()
|
||||||
|
self.init_TSP()
|
||||||
|
self.decsion_tree_move()
|
||||||
|
|
||||||
while self.playing:
|
while self.playing:
|
||||||
self.dt = self.clock.tick(FPS) / 1000.0
|
self.dt = self.clock.tick(FPS) / 1000.0
|
||||||
self.events()
|
self.events()
|
||||||
@ -202,8 +199,8 @@ class Game():
|
|||||||
map.render_tiles(self.trashDisplay, self.screen, self.camera)
|
map.render_tiles(self.trashDisplay, self.screen, self.camera)
|
||||||
|
|
||||||
# draw text
|
# draw text
|
||||||
text_surface = pg.font.SysFont('Comic Sans MS', 30).render(self.text_display, False, (0,0,0))
|
text_surface = pg.font.SysFont('Comic Sans MS', 30).render(self.text_display, False, (255, 255, 255))
|
||||||
self.screen.blit(text_surface, (0,128))
|
self.screen.blit(text_surface, (0, 128))
|
||||||
|
|
||||||
# rerender additional sprites
|
# rerender additional sprites
|
||||||
for sprite in self.agentSprites:
|
for sprite in self.agentSprites:
|
||||||
@ -211,7 +208,7 @@ class Game():
|
|||||||
if self.debug_mode:
|
if self.debug_mode:
|
||||||
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
||||||
|
|
||||||
self.player.hud_group.draw(self.screen)
|
# self.player.hud_group.draw(self.screen)
|
||||||
# finally update screen
|
# finally update screen
|
||||||
pg.display.flip()
|
pg.display.flip()
|
||||||
|
|
||||||
@ -228,16 +225,12 @@ class Game():
|
|||||||
pos = pg.mouse.get_pos()
|
pos = pg.mouse.get_pos()
|
||||||
offset_x, offset_y = self.camera.offset()
|
offset_x, offset_y = self.camera.offset()
|
||||||
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
||||||
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
actions = a_star_controller.get_actions_by_coords(clicked_coords[0], clicked_coords[1], self)
|
||||||
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
|
||||||
clicked_coords[0], clicked_coords[1], self.mapArray)
|
|
||||||
# print(actions)
|
|
||||||
|
|
||||||
if (actions != None):
|
if (actions != None):
|
||||||
self.t.startAiController(actions)
|
self.t.startAiController(actions)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# create the game object
|
# create the game object
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -21,7 +21,7 @@ def generate_map():
|
|||||||
map[y][x] = 1
|
map[y][x] = 1
|
||||||
|
|
||||||
# generowanie smietnikow
|
# generowanie smietnikow
|
||||||
for i in range(0, 10):
|
for i in range(0, 30):
|
||||||
x = random.randint(0, MAP_WIDTH-1)
|
x = random.randint(0, MAP_WIDTH-1)
|
||||||
y = random.randint(0, MAP_HEIGHT-1)
|
y = random.randint(0, MAP_HEIGHT-1)
|
||||||
map[y][x] = 2
|
map[y][x] = 2
|
||||||
|
Loading…
Reference in New Issue
Block a user