AI_PROJEKT_2021/main.py
2021-06-22 19:02:44 +02:00

135 lines
4.3 KiB
Python

import time
import pygame
import sys
from random import seed
from random import random
from astar import RoutePlanning, astar
from world import set_world, World, Agent
from genetic_algorithm import *
from decision_tree import *
from neural_network2 import *
from neural_network_vers2 import *
start_point = (0, 0)
position = list(start_point)
window = pygame.display.set_mode((800, 560))
cell_size = 40
final_point = (1, 0)
world_map_file = set_world('worldmap.txt')
pygame.init()
world = World(world_map_file, cell_size)
sprites = pygame.sprite.Group()
agent = Agent(world.get_world_cell_size(), world)
sprites.add(agent)
window.fill((0, 0, 0))
roads = []
houses = [
(5,1)],[(11,2)],[(6,6)],[(12,6)],[(3,9)],[(7,9)],[(17,9)],[(6,12)
]
for w in range(0, len(world_map_file[0])):
for h in range(0, len(world_map_file)):
if int(world_map_file[h][w]) != 0:
roads.append((w, h))
def move(actions):
for x in actions:
if x == "Go right":
while agent.get_directions()[0] != "E":
agent.turn_right()
agent.move()
if x == "Go left":
while agent.get_directions()[0] != "W":
agent.turn_left()
agent.move()
if x == "Go down":
while agent.get_directions()[0] != "S":
agent.turn_left()
agent.move()
if x == "Go up":
while agent.get_directions()[0] != "N":
agent.turn_right()
agent.move()
world.draw(window)
sprites.update()
sprites.draw(window)
time.sleep(0.2)
pygame.display.update()
while agent.get_directions()[0] != "E":
agent.turn_right()
world.draw(window)
sprites.update()
sprites.draw(window)
time.sleep(0.1)
pygame.display.update()
if final_point[0] == 19 and final_point[1] == 8:
agent.change_image2()
for i in range(0, 8):
if (houses[i][0] == (final_point[0],final_point[1])):
print("----------drzewo decyzyjne---------------")
t = buildTree()
seed(1)
trash = [[random.random(),random.random(),random.random(),random.random(),random.random(),random.random(),random.random(),random.random()],[random.random(),random.random(),random.random(),random.random(),random.random(),random.random(),random.random(),random.random()]]
print(trash) # prints data array
typeOfTrashes = predictTypeOfTrash(t, trash) # prints recognised trash
print(typeOfTrashes)
print("-----------sieci neuronowe--------------")
b()
agent.change_image()
world.draw(window)
sprites.update()
sprites.draw(window)
time.sleep(0.1)
pygame.display.update()
while True:
world.draw(window)
sprites.update()
sprites.draw(window)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
agent.move()
if event.key == pygame.K_a:
agent.turn_left()
if event.key == pygame.K_d:
agent.turn_right()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
actions = k((agent.get_second(), agent.get_first()))
move(actions)
if event.key == pygame.K_KP_ENTER:
for i in range(0, 8):
if (houses[i][0] == (agent.get_first(), agent.get_second())):
predict()
elif event.type == pygame.MOUSEBUTTONDOWN:
start_point = (agent.get_first(), agent.get_second())
pos_x = int(pygame.mouse.get_pos()[1] / cell_size)
pos_y = int(pygame.mouse.get_pos()[0] / cell_size)
if int(world_map_file[pos_x][pos_y]) != 0:
final_point = (pos_y, pos_x)
route = RoutePlanning(start_point, final_point, roads)
actions = astar(route).reconstruct_path()
move(actions)
pygame.display.update()