Podpięcie drzewa do agenta, podpięcie klasyfikatora do agenta

This commit is contained in:
s473632 2023-06-04 22:10:50 +02:00
parent b78f57386e
commit 3a4fcf611e
4 changed files with 159 additions and 31 deletions

View File

@ -1,6 +1,10 @@
import random import random
import pygame as pg import pygame as pg
import heapq import heapq
from decisiontree import decision
from trashtype import trash_type_def
HOUSE_NUMBER = {'House_number': 0}
class Node: class Node:
@ -12,11 +16,13 @@ class Node:
def __lt__(self, other): def __lt__(self, other):
return self.cost < other.cost return self.cost < other.cost
class State: class State:
def __init__(self, position, house_list): def __init__(self, position, house_list):
self.position = position self.position = position
self.house_list = house_list self.house_list = house_list
def vector_to_tuple(vector): def vector_to_tuple(vector):
tup = (int(vector.x), int(vector.y)) tup = (int(vector.x), int(vector.y))
return tup return tup
@ -24,10 +30,109 @@ def vector_to_tuple(vector):
class HousePOI: class HousePOI:
def __init__(self): def __init__(self):
self.bins = None from simulation import Interface
self.season = Interface.season.season
self.day = Interface.day.day
self.truck_fullness = None
self.payment = None
self.bin_fullness = None
self.trash_type = None
self.trash = 1
self.dump_fullness = 0
def discover_bins(self, bins): def discover_bins(self, bin_fullness, trash_type, payment):
self.bins = bins from simulation import Interface
season_autumn = 0
season_spring = 0
season_summer = 0
season_winter = 0
day_friday = 0
day_monday = 0
day_wednesday = 0
trash_types_glass = 0
trash_types_biological = 0
trash_types_paper = 0
trash_types_plastic = 0
truck_fullness = 0
guessed_trash_type = trash_type_def(trash_type)
self.truck_fullness = Interface.truck_fullness.fullness
self.payment = payment
self.bin_fullness = bin_fullness
self.trash_type = guessed_trash_type
if self.season == 'autumn':
season_autumn = 1
if self.season == 'spring':
season_spring = 1
if self.season == 'summer':
season_summer = 1
if self.season == 'winter':
season_winter = 1
if self.day == 'friday':
day_friday = 1
if self.day == 'monday':
day_monday = 1
if self.day == 'wednesday':
day_wednesday = 1
if self.trash_type == 'glass':
trash_types_glass = 1
truck_fullness = self.truck_fullness["glass"]
if self.trash_type == 'biological':
trash_types_biological = 1
truck_fullness = self.truck_fullness["biological"]
if self.trash_type == 'paper':
trash_types_paper = 1
truck_fullness = self.truck_fullness["paper"]
if self.trash_type == 'plastic':
trash_types_plastic = 1
truck_fullness = self.truck_fullness["plastic"]
if truck_fullness < 1:
truck_fullness = 0
if truck_fullness == 1:
truck_fullness = 1
dec_tree = {'dump_fullness': [self.dump_fullness],
'truck_fullness': [truck_fullness],
'trash': [self.trash],
'payment': [payment],
'bin_fullness': [bin_fullness],
'trash_types_glass': [trash_types_glass],
'trash_types_mixed': [trash_types_biological],
'trash_types_paper': [trash_types_paper],
'trash_types_plastic': [trash_types_plastic],
'season_autumn': [season_autumn],
'season_spring': [season_spring],
'season_summer': [season_summer],
'season_winter': [season_winter],
'day_friday': [day_friday],
'day_monday': [day_monday],
'day_wednesday': [day_wednesday]
}
outcome = decision(dec_tree)
if outcome == 1:
Interface.truck_fullness.fullness[trash_type] += 0.25 # 0.25
debug(dec_tree, outcome, trash_type, guessed_trash_type, self.season, self.day, self.bin_fullness,
self.truck_fullness)
def debug(dec_tree, outcome, trash_type, guessed_trash_type, season, day, bin_fullness, truck_fullness):
HOUSE_NUMBER['House_number'] += 1
print("Domek nr: " + str(HOUSE_NUMBER['House_number']))
if outcome == 1:
print("Odebrano śmieci")
if outcome == 0:
print("Nie odebrano śmieci")
print("Pora: " + season)
print("Dzień: " + day)
print("Typ śmieci: " + trash_type)
print("Zaobserwowane: " + guessed_trash_type)
print("Zapełnienie domu: " + str(bin_fullness))
print(truck_fullness)
print(dec_tree)
print("####################################################################################")
class Agent: class Agent:
@ -234,5 +339,7 @@ class Agent:
if entity.entity_type == 'house' and vector_to_tuple(entity.position) == self.current_pos: if entity.entity_type == 'house' and vector_to_tuple(entity.position) == self.current_pos:
current_house = entity current_house = entity
break break
if current_house is not None: if current_house is not None and current_house.visited == False:
self.houses[self.current_pos].discover_bins(current_house.bins) current_house.visited = True
self.houses[self.current_pos].discover_bins(current_house.bins[0], current_house.bins[1],
current_house.bins[2])

View File

@ -1,5 +1,6 @@
from pathlib import Path from pathlib import Path
from random import randint from random import randint
import random
import pygame as pg import pygame as pg
from agent import Agent from agent import Agent
@ -13,9 +14,9 @@ HOUSE_SPRITES = {0: HOUSE_WITHOUT_TRASH_SPRITE,
1: HOUSE_WITH_TRASH_SPRITE} 1: HOUSE_WITH_TRASH_SPRITE}
TRUCK_SPRITE_R = pg.Vector2(2, 0) TRUCK_SPRITE_R = pg.Vector2(2, 0)
TRUCK_SPRITE_D = pg.Vector2(2,1) TRUCK_SPRITE_D = pg.Vector2(2, 1)
TRUCK_SPRITE_L = pg.Vector2(2,2) TRUCK_SPRITE_L = pg.Vector2(2, 2)
TRUCK_SPRITE_U = pg.Vector2(2,3) TRUCK_SPRITE_U = pg.Vector2(2, 3)
PAPER_DUMP_SPRITE = pg.Vector2(3, 0) PAPER_DUMP_SPRITE = pg.Vector2(3, 0)
PLASTIC_DUMP_SPRITE = pg.Vector2(3, 1) PLASTIC_DUMP_SPRITE = pg.Vector2(3, 1)
@ -26,7 +27,24 @@ DUMP_SPRITES = {'paper': PAPER_DUMP_SPRITE,
'plastic': PLASTIC_DUMP_SPRITE, 'plastic': PLASTIC_DUMP_SPRITE,
'mixed': MIXED_DUMP_SPRITE} 'mixed': MIXED_DUMP_SPRITE}
TRASH_TYPES = ['paper', 'plastic', 'glass', 'mixed'] TRASH_TYPES = ['paper', 'plastic', 'glass', 'biological']
DAYS = ['friday', 'monday', 'wednesday']
SEASONS = ['autumn', 'spring', 'summer', 'winter']
class Season:
def __init__(self, season):
self.season = season
class Day:
def __init__(self, day):
self.day = day
class TruckFullness:
def __init__(self, fullness):
self.fullness = fullness
class Entity: class Entity:
@ -40,12 +58,9 @@ class TruckEntity(Entity):
super().__init__(state, position) super().__init__(state, position)
self.entity_type = 'truck' self.entity_type = 'truck'
self.tile = TRUCK_SPRITE_R self.tile = TRUCK_SPRITE_R
self.fullness = {'paper': 0, self.fullness = None
'glass': 0,
'plastic': 0,
'mixed': 0}
self.orientation = 90 self.orientation = 90
def rotate_img(self, orientation): def rotate_img(self, orientation):
self.orientation = orientation self.orientation = orientation
if orientation == 0: if orientation == 0:
@ -76,13 +91,11 @@ class TruckEntity(Entity):
class Bin: class Bin:
def __init__(self, size, trash_type): def __init__(self, items):
self.size = size self.items = items
self.trash_type = trash_type
self.fullness = randint(0, 100)
def __str__(self): def __getitem__(self, index):
return self.trash_type + " bin" return self.items[index]
class HouseEntity(Entity): class HouseEntity(Entity):
@ -90,7 +103,8 @@ class HouseEntity(Entity):
super().__init__(state, position) super().__init__(state, position)
self.tile = HOUSE_SPRITES[0] self.tile = HOUSE_SPRITES[0]
self.entity_type = 'house' self.entity_type = 'house'
self.bins = [Bin(randint(1, 2), trash_type) for trash_type in TRASH_TYPES] self.visited = False
self.bins = Bin([randint(0, 1), random.choice(TRASH_TYPES), randint(0, 1)])
class DumpEntity(Entity): class DumpEntity(Entity):
@ -125,7 +139,7 @@ class SimulationState:
if tile == "R": if tile == "R":
self.road_pos_r.append(pg.Vector2(x, y)) self.road_pos_r.append(pg.Vector2(x, y))
if tile == "G": if tile == "G":
self.road_pos_g.append(pg.Vector2(x,y)) self.road_pos_g.append(pg.Vector2(x, y))
if tile == "H": if tile == "H":
self.houses_pos.append(pg.Vector2(x, y)) self.houses_pos.append(pg.Vector2(x, y))
self.entities.append(HouseEntity(self, pg.Vector2(x, y))) self.entities.append(HouseEntity(self, pg.Vector2(x, y)))
@ -165,7 +179,7 @@ class Layer:
self.sim = sim self.sim = sim
self.texture_atlas = pg.image.load(texture_file) self.texture_atlas = pg.image.load(texture_file)
def renderTile(self, surface, position, tile, orientation = 90, rotate=False): def renderTile(self, surface, position, tile, orientation=90, rotate=False):
# pozycja na ekranie # pozycja na ekranie
sprite_pos = position.elementwise() * self.sim.cell_size sprite_pos = position.elementwise() * self.sim.cell_size
@ -175,7 +189,6 @@ class Layer:
int(pos_in_atlas.y), int(pos_in_atlas.y),
self.sim.cell_size.x, self.sim.cell_size.x,
self.sim.cell_size.y) self.sim.cell_size.y)
# render # render
surface.blit(self.texture_atlas, sprite_pos, texture) surface.blit(self.texture_atlas, sprite_pos, texture)
@ -211,6 +224,13 @@ class StructureLayer(Layer):
class Interface: class Interface:
truck_fullness = TruckFullness({'paper': 0,
'glass': 0,
'plastic': 0,
'biological': 0})
season = Season(random.choice(SEASONS))
day = Day(random.choice(DAYS))
def __init__(self): def __init__(self):
pg.init() pg.init()
@ -224,8 +244,8 @@ class Interface:
# rendering # rendering
self.cell_size = pg.Vector2(64, 64) self.cell_size = pg.Vector2(64, 64)
texture_file = Path(r"..\res\tiles.png") texture_file = Path(r"..\res\tiles.png")
self.layers = [StructureLayer(self, texture_file, self.state, self.state.road_pos_r, ROAD_SPRITE_R ), self.layers = [StructureLayer(self, texture_file, self.state, self.state.road_pos_r, ROAD_SPRITE_R),
StructureLayer(self, texture_file, self.state, self.state.road_pos_g, ROAD_SPRITE_G ), StructureLayer(self, texture_file, self.state, self.state.road_pos_g, ROAD_SPRITE_G),
EntityLayer(self, texture_file, self.state, self.state.entities)] EntityLayer(self, texture_file, self.state, self.state.entities)]
# okno # okno
@ -257,16 +277,16 @@ class Interface:
if self.debug_mode: if self.debug_mode:
if event.key == pg.K_RIGHT: if event.key == pg.K_RIGHT:
self.move_truck.x = 1 self.move_truck.x = 1
if event.key == pg.K_LEFT: if event.key == pg.K_LEFT:
self.move_truck.x = -1 self.move_truck.x = -1
if event.key == pg.K_DOWN: if event.key == pg.K_DOWN:
self.move_truck.y = 1 self.move_truck.y = 1
if event.key == pg.K_UP: if event.key == pg.K_UP:
self.move_truck.y = -1 self.move_truck.y = -1
if event.key == pg.K_d: if event.key == pg.K_d:
self.agent.discover() self.agent.discover()
@ -276,6 +296,7 @@ class Interface:
def update(self): def update(self):
self.state.update(self.move_truck) self.state.update(self.move_truck)
self.agent.update() self.agent.update()
self.agent.discover()
'''if isinstance(self.move_truck, int): '''if isinstance(self.move_truck, int):
self.state.update(self.move_truck) self.state.update(self.move_truck)
self.agent.update()''' self.agent.update()'''
@ -298,5 +319,5 @@ class Interface:
self.processAgentInput() self.processAgentInput()
self.update() self.update()
self.render() self.render()
self.clock.tick(12) self.clock.tick(50)
pg.quit() pg.quit()