updated main, repaired astar, added method for get actions from coords, expected to be used in GA #31

Merged
s464978 merged 1 commits from astar_fix into master 2022-06-10 01:12:24 +02:00
10 changed files with 141 additions and 153 deletions

View File

@ -12,7 +12,7 @@ def getPrediction(img_path, network_name):
img = Image.open(img_path)
transform_tensor = transforms.ToTensor()(img).unsqueeze_(0)
classes = ['glass', 'metal', 'paper', 'plastic']
neural_net.load_state_dict(torch.load(PATH + network_name))
neural_net.load_state_dict(torch.load(PATH + network_name, map_location='cpu'))
neural_net.eval()
outputs = neural_net(transform_tensor)

View File

View File

@ -4,40 +4,18 @@
| | | |--- class: 0
| | |--- feature_0 > 1.50
| | | |--- feature_3 <= 3.50
| | | | |--- feature_2 <= 2.50
| | | | |--- feature_4 <= 2.50
| | | | | |--- class: 1
| | | | |--- feature_2 > 2.50
| | | | | |--- feature_4 <= 2.50
| | | | |--- feature_4 > 2.50
| | | | | |--- feature_2 <= 2.50
| | | | | | |--- class: 1
| | | | | |--- feature_4 > 2.50
| | | | | |--- feature_2 > 2.50
| | | | | | |--- class: 0
| | | |--- feature_3 > 3.50
| | | | |--- feature_3 <= 4.50
| | | | | |--- feature_1 <= 2.50
| | | | | | |--- feature_0 <= 2.50
| | | | | | | |--- feature_1 <= 1.50
| | | | | | | | |--- 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_4 <= 2.50
| | | | | | | | |--- class: 1
| | | | | | | |--- feature_4 > 2.50
| | | | | | | | |--- feature_2 <= 2.50
| | | | | | | | | |--- class: 1
| | | | | | | | |--- feature_2 > 2.50
| | | | | | | | | |--- class: 0
| | | | | |--- feature_1 > 2.50
| | | | | | |--- feature_0 <= 3.50
| | | | | | | |--- class: 0
| | | | | | |--- feature_0 > 3.50
| | | | | | | |--- feature_1 <= 3.50
| | | | | | | | |--- feature_4 <= 2.50
| | | | | | | | | |--- class: 1
| | | | | | | | |--- feature_4 > 2.50
@ -45,8 +23,30 @@
| | | | | | | | | | |--- class: 1
| | | | | | | | | |--- feature_2 > 2.00
| | | | | | | | | | |--- class: 0
| | | | | | | |--- feature_1 > 3.50
| | | | | | | |--- 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
| | | | | | | | | |--- class: 0
| | | | | |--- feature_1 > 2.50
| | | | | | |--- feature_1 <= 3.50
| | | | | | | |--- feature_0 <= 3.50
| | | | | | | | |--- class: 0
| | | | | | | |--- feature_0 > 3.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 > 3.50
| | | | | | | |--- class: 0
| | | | |--- feature_3 > 4.50
| | | | | |--- class: 0
| |--- feature_4 > 3.50
@ -73,8 +73,8 @@
| | |--- feature_2 > 1.50
| | | |--- class: 0
|--- feature_2 > 3.50
| |--- feature_4 <= 1.50
| | |--- feature_1 <= 1.50
| |--- feature_1 <= 1.50
| | |--- feature_4 <= 1.50
| | | |--- feature_2 <= 4.50
| | | | |--- feature_0 <= 1.50
| | | | | |--- class: 0
@ -85,7 +85,7 @@
| | | | | | |--- class: 0
| | | |--- feature_2 > 4.50
| | | | |--- class: 0
| | |--- feature_1 > 1.50
| | |--- feature_4 > 1.50
| | | |--- class: 0
| |--- feature_4 > 1.50
| |--- feature_1 > 1.50
| | |--- class: 0

Binary file not shown.

BIN
last_map.nparr Normal file

Binary file not shown.

100
main.py
View File

@ -1,25 +1,24 @@
from asyncio import sleep
from calendar import c
from random import randint
import time
import os
from game_objects.player import Player
import pygame as pg
import sys
from os import path
from random import randint
import math
from map import *
from settings import *
from map import map
from map import map_utils
from path_search_algorthms import bfs
from path_search_algorthms import a_star, a_star_utils
from decision_tree import decisionTree
from NeuralNetwork import prediction
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 game_objects import aiPlayer
import itertools
from map import map
from map import map_utils
from path_search_algorthms import bfs
from path_search_algorthms import a_star_controller
from decision_tree import decisionTree
from NeuralNetwork import prediction
from settings import *
def getTree():
@ -51,17 +50,7 @@ class Game():
# because dont work without data.txt
# self.init_bfs()
# self.init_a_star()
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
self.t = aiPlayer(self.player, game=self)
def init_game(self):
# initialize all variables and do all the setup for a new game
@ -70,6 +59,12 @@ class Game():
# sprite groups and map array for calculations
(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.agentSprites = pg.sprite.Group()
# player obj
@ -80,7 +75,6 @@ class Game():
# other
self.debug_mode = False
def init_bfs(self):
start_node = (0, 0)
target_node = (18, 18)
@ -96,17 +90,8 @@ class Game():
nextNode = node[1]
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):
# logika pracy z drzewem
# logika pracy z drzewem
self.positive_decision = []
self.negative_decision = []
@ -118,7 +103,7 @@ class Game():
self.positive_decision.append(i)
else:
self.negative_decision.append(i)
# print('positive actions')
# for i in self.positive_actions:
# print('----')
@ -129,8 +114,10 @@ class Game():
print(len(self.positive_decision))
for i in self.positive_decision:
# print(i.get_coords())
print('action')
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)
print('')
@ -140,32 +127,31 @@ class Game():
for i in range(0, 10):
random = randint(0, 48)
file = files[random]
result = prediction.getPrediction(dir + '/' +file, 'trained_nn_20.pth')
img = pg.image.load(dir + '/' +file).convert_alpha()
result = prediction.getPrediction(dir + '/' + file, 'trained_nn_20.pth')
img = pg.image.load(dir + '/' + file).convert_alpha()
img = pg.transform.scale(img, (128, 128))
trash = Trash(img, 0, 0, 128, 128)
self.trashDisplay.add(trash)
self.text_display = result
self.draw()
print(result + ' ' + file)
pg.time.wait(1000)
# print(result + ' ' + file)
pg.time.wait(100)
self.text_display = ''
self.draw()
# print(self.positive_actions[0])
# self.t.startAiController(self.positive_actions[0])
def load_data(self):
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'resources/textures')
game_folder = os.path.dirname(__file__)
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))
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.init_decision_tree()
while self.playing:
@ -195,8 +181,8 @@ class Game():
map.render_tiles(self.trashDisplay, self.screen, self.camera)
# draw text
text_surface = pg.font.SysFont('Comic Sans MS', 30).render(self.text_display, False, (0,0,0))
self.screen.blit(text_surface, (0,128))
text_surface = pg.font.SysFont('Comic Sans MS', 30).render(self.text_display, False, (0, 0, 0))
self.screen.blit(text_surface, (0, 128))
# rerender additional sprites
for sprite in self.agentSprites:
@ -221,20 +207,16 @@ class Game():
pos = pg.mouse.get_pos()
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)
# print(actions)
actions = a_star_controller.get_actions_by_coords(clicked_coords[0], clicked_coords[1], self)
if (actions != None):
self.t.startAiController(actions)
# create the game object
if __name__ == "__main__":
g = Game()
g.run()
g.show_go_screen()

10
main_test.py Normal file
View File

@ -0,0 +1,10 @@
from map import map_utils
from path_search_algorthms import a_star, a_star_utils
import numpy
file = open('last_map.nparr', 'rb')
array = numpy.load(file)
file.close
actions = a_star.search_path(12, 0, a_star_utils.Rotation.UP, 1, 5, array)
print(actions)

View File

@ -1,8 +1,13 @@
from data_structures.heap import Heap
from path_search_algorthms import a_star_utils as utils
def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array):
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)
return len(actions)
def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array):
start_node = utils.Node(start_x, start_y, agent_rotation)
target_node = utils.Node(target_x, target_y, utils.Rotation.NONE)
@ -38,51 +43,10 @@ def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, targ
neighbour.h_cost = utils.get_h_cost(neighbour, target_node)
neighbour.parent = node
# add to search
if(not search_list.contains(neighbour)):
# add to search
if (not search_list.contains(neighbour)):
search_list.append(neighbour, neighbour.f_cost())
# array version
# nodes for check
# search_list = [start_node]
# checked nodes
# searched_list: list[(int, int)] = []
# while (len(search_list) > 0):
# node = search_list[0]
# # find cheapest node in search_list
# for i in range(1, len(search_list)):
# if (search_list[i].f_cost() <= node.f_cost()):
# if(search_list[i].h_cost < node.h_cost):
# node = search_list[i]
# search_list.remove(node)
# searched_list.append((node.x, node.y))
# # check for target node
# if ((node.x, node.y) == (target_x, target_y)):
# return trace_path(node)
# # neightbours processing
# neighbours = utils.get_neighbours(node, searched_list, array)
# for neighbour in neighbours:
# # calculate new g cost for neightbour (start -> node -> neightbour)
# new_neighbour_cost = node.g_cost + utils.get_neighbour_cost(node, neighbour)
# if (new_neighbour_cost < neighbour.g_cost or neighbour not in search_list):
# # replace cost and set parent node
# neighbour.g_cost = new_neighbour_cost
# neighbour.h_cost = utils.get_h_cost(neighbour, target_node)
# neighbour.parent = node
# # add to search
# if(neighbour not in search_list):
# search_list.append(neighbour)
def trace_path(end_node: utils.Node):
path = []
@ -91,9 +55,11 @@ def trace_path(end_node: utils.Node):
# set final rotation of end_node because we don't do it before
node.rotation = utils.get_needed_rotation(node.parent, node)
while (node.parent != 0):
move = utils.get_move(node.parent, node)
path += move
while (node.parent != False):
if (node.parent == utils.Rotation.NONE):
path += "forward"
else:
path += utils.get_move(node.parent, node)
node = node.parent
# delete move on initial tile
@ -107,6 +73,3 @@ def trace_path(end_node: utils.Node):
return path

View File

@ -0,0 +1,26 @@
import math
from path_search_algorthms import a_star
from settings import *
def get_actions_by_coords(x, y, game):
# print('get_actions_by_coords')
# print(x, y, x/TILESIZE, y/TILESIZE)
offset_x, offset_y = game.camera.offset()
# print('offset ' + str(self.camera.offset()))
clicked_coords = [math.floor(x / TILESIZE) - offset_x, math.floor(y / TILESIZE) - offset_y]
# print(self.player.pos[0], self.player.pos[1], clicked_coords)
actions = a_star.search_path(math.floor(game.player.pos[0] / TILESIZE),
math.floor(game.player.pos[1] / TILESIZE), game.player.rotation(),
clicked_coords[0], clicked_coords[1], game.mapArray)
return actions
def get_actions_for_target_coords(x, y, game):
actions = a_star.search_path(
math.floor(game.player.pos[0] / TILESIZE),
math.floor(game.player.pos[1] / TILESIZE),
game.player.rotation(),
x / TILESIZE,
y / TILESIZE,
game.mapArray
)
return actions

View File

@ -3,6 +3,7 @@ from enum import Enum
from map import map_utils
from settings import *
class Rotation(Enum):
UP = 0
RIGHT = 1
@ -13,34 +14,37 @@ class Rotation(Enum):
def __int__(self):
return self.value
class Node:
def __init__(self, x: int, y: int, rotation: Rotation):
self.x = x
self.y = y
self.g_cost = 0
self.h_cost = 0
self.parent = 0
self.rotation = rotation
self.parent: Node | bool = False
self.rotation: Rotation = rotation
def f_cost(self):
return self.g_cost + self.h_cost
def get_neighbours(node, searched_list, array):
neighbours = []
for offset_x in range (-1, 2):
for offset_y in range (-1, 2):
for offset_x in range(-1, 2):
for offset_y in range(-1, 2):
# don't look for cross neighbours
if(abs(offset_x) + abs(offset_y) == 1):
if (abs(offset_x) + abs(offset_y) == 1):
x = node.x + offset_x
y = node.y + offset_y
# prevent out of map coords
if (x >= 0 and x < MAP_WIDTH and y >= 0 and y < MAP_HEIGHT):
if(map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list):
if (map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list):
neighbour = Node(x, y, Rotation.NONE)
neighbour.rotation = get_needed_rotation(node, neighbour)
neighbours.append(neighbour)
return neighbours
# move cost schema:
# - move from tile to tile: 10
# - add extra 10 (1 rotation) if it exists
@ -49,11 +53,12 @@ def get_h_cost(start_node: Node, target_node: Node):
distance_y = abs(start_node.y - target_node.y)
cost = (distance_x + distance_y) * 10
if(distance_x > 0 and distance_y > 0):
if (distance_x > 0 and distance_y > 0):
cost += 10
return cost
# move cost schema:
# - move from tile to tile: 10
# - every rotation 90*: 10
@ -67,6 +72,7 @@ def get_neighbour_cost(start_node: Node, target_node: Node) -> int:
else:
return 30
# translate rotation change to move
def get_move(start_node: Node, target_node: Node):
rotate_change = get_rotate_change(start_node.rotation, target_node.rotation)
@ -79,10 +85,12 @@ def get_move(start_node: Node, target_node: Node):
else:
return ["left", "forward"]
# simple calc func
def get_rotate_change(rotationA: Rotation, rotationB: Rotation) -> int:
return int(rotationA) - int(rotationB)
# get new rotation for target_node as neighbour of start_node
def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation:
if (start_node.x - target_node.x > 0):
@ -92,10 +100,9 @@ def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation:
if (start_node.y - target_node.y > 0):
return Rotation.UP
if (start_node.y - target_node.y < 0):
return Rotation.DOWN
return Rotation.DOWN