Machine_learning_2023/main.py

137 lines
4.5 KiB
Python
Raw Normal View History

2023-03-29 07:13:08 +02:00
from random import randint
import pygame
2023-04-20 00:15:00 +02:00
import configparser
2023-03-29 07:13:08 +02:00
2023-03-29 11:27:59 +02:00
from domain.commands.random_cat_move_command import RandomCatMoveCommand
from domain.commands.vacuum_move_command import VacuumMoveCommand
2023-03-29 07:13:08 +02:00
from domain.entities.cat import Cat
from domain.entities.entity import Entity
from domain.entities.vacuum import Vacuum
from domain.entities.docking_station import Doc_Station
2023-03-29 07:13:08 +02:00
from domain.world import World
from view.renderer import Renderer
2023-04-20 03:38:55 +02:00
from AI_brain.movement import StateGraphSearchBFS, State
2023-03-29 07:13:08 +02:00
2023-04-20 00:15:00 +02:00
config = configparser.ConfigParser()
config.read("config.ini")
2023-03-29 07:13:08 +02:00
2023-03-30 18:18:46 +02:00
2023-03-29 07:13:08 +02:00
class Main:
def __init__(self):
tiles_x = 10
tiles_y = 10
self.renderer = Renderer(800, 800, tiles_x, tiles_y)
2023-03-29 11:27:59 +02:00
self.world = generate_world(tiles_x, tiles_y)
self.commands = []
2023-03-29 07:13:08 +02:00
self.clock = pygame.time.Clock()
self.running = True
self.fps = 60
def run(self):
while self.running:
self.process_input()
self.update()
self.renderer.render(self.world)
self.clock.tick(self.fps)
pygame.quit()
2023-04-20 03:38:55 +02:00
def run_robot(self):
self.renderer.render(self.world)
start_state = State(self.world.vacuum.x, self.world.vacuum.y)
end_state = State(self.world.doc_station.x, self.world.doc_station.y)
SGS_BFS = StateGraphSearchBFS(self.world, start_state, end_state)
if not SGS_BFS.search():
print("No solution")
exit(0)
SGS_BFS.actions.reverse()
while self.running:
self.renderer.render(self.world)
self.clock.tick(5)
if len(SGS_BFS.actions) > 0:
action_direction = SGS_BFS.actions.pop()
if action_direction == "UP":
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (0, -1))
)
elif action_direction == "DOWN":
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (0, 1))
)
elif action_direction == "LEFT":
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (-1, 0))
)
elif action_direction == "RIGHT":
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (1, 0))
)
self.update()
pygame.quit()
2023-03-29 07:13:08 +02:00
def process_input(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
2023-03-30 18:18:46 +02:00
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (-1, 0))
)
2023-03-29 07:13:08 +02:00
if event.key == pygame.K_RIGHT:
2023-03-30 18:18:46 +02:00
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (1, 0))
)
2023-03-29 07:13:08 +02:00
if event.key == pygame.K_UP:
2023-03-30 18:18:46 +02:00
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (0, -1))
)
2023-03-29 07:13:08 +02:00
if event.key == pygame.K_DOWN:
2023-03-30 18:18:46 +02:00
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (0, 1))
)
2023-03-29 07:13:08 +02:00
def update(self):
2023-04-20 00:15:00 +02:00
if config.getboolean("APP", "cat"):
self.commands.append(RandomCatMoveCommand(self.world, self.world.cat))
2023-03-29 11:27:59 +02:00
for command in self.commands:
command.run()
self.commands.clear()
def generate_world(tiles_x: int, tiles_y: int) -> World:
world = World(tiles_x, tiles_y)
for _ in range(10):
temp_x = randint(0, tiles_x - 1)
temp_y = randint(0, tiles_y - 1)
world.add_entity(Entity(temp_x, temp_y, "PEEL"))
world.vacuum = Vacuum(1, 1)
world.doc_station = Doc_Station(9, 8)
2023-04-20 00:15:00 +02:00
if config.getboolean("APP", "cat"):
world.cat = Cat(7, 8)
world.add_entity(world.cat)
2023-03-29 11:27:59 +02:00
world.add_entity(Entity(2, 8, "PLANT1"))
world.add_entity(Entity(4, 1, "PLANT1"))
world.add_entity(Entity(3, 4, "PLANT2"))
world.add_entity(Entity(8, 8, "PLANT2"))
world.add_entity(Entity(9, 3, "PLANT3"))
return world
2023-03-29 07:13:08 +02:00
2023-03-12 01:39:08 +01:00
2023-03-29 07:13:08 +02:00
if __name__ == "__main__":
app = Main()
2023-04-20 03:38:55 +02:00
if config["APP"]["movement"] == "human":
app.run()
elif config["APP"]["movement"] == "robot":
app.run_robot()