Added new fields and reconfigured map

This commit is contained in:
Tomasz Torchalski 2023-06-10 23:53:24 +02:00
parent ebcecf4279
commit 3b2342a6b4
13 changed files with 106 additions and 34 deletions

BIN
Tiles/Base.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

BIN
Tiles/Bend.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

BIN
Tiles/End.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

BIN
Tiles/Intersection.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 KiB

BIN
Tiles/Junction.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

BIN
Tiles/Straight.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

View File

Before

Width:  |  Height:  |  Size: 9.3 KiB

After

Width:  |  Height:  |  Size: 9.3 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 9.8 KiB

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

@ -1,3 +1,2 @@
def heuristicfn(startx, starty, goalx, goaly): def heuristicfn(startx, starty, goalx, goaly):
return abs(startx - goalx) + abs(starty - goaly) return abs(startx - goalx) + abs(starty - goaly)
# return pow(((startx//50)-(starty//50)),2) + pow(((goalx//50)-(goaly//50)),2)

10
main.py
View File

@ -1,8 +1,6 @@
import pygame import pygame
from treelearn import treelearn from treelearn import treelearn
import loadmodel import loadmodel
from astar import astar from astar import astar
from state import State from state import State
import time import time
@ -10,6 +8,7 @@ from garbage_truck import GarbageTruck
from heuristicfn import heuristicfn from heuristicfn import heuristicfn
from map import randomize_map from map import randomize_map
pygame.init() pygame.init()
WIDTH, HEIGHT = 800, 800 WIDTH, HEIGHT = 800, 800
window = pygame.display.set_mode((WIDTH, HEIGHT)) window = pygame.display.set_mode((WIDTH, HEIGHT))
@ -19,9 +18,9 @@ AGENT = pygame.transform.scale(AGENT_IMG, (50, 50))
FPS = 10 FPS = 10
FIELDCOUNT = 16 FIELDCOUNT = 16
FIELDWIDTH = 50 FIELDWIDTH = 50
BASE_IMG = pygame.image.load("Tiles/Base.jpg")
BASE = pygame.transform.scale(BASE_IMG, (50, 50))
GRASS_IMG = pygame.image.load("grass.png")
GRASS = pygame.transform.scale(GRASS_IMG, (50, 50))
def draw_window(agent, fields, flip): def draw_window(agent, fields, flip):
if flip: if flip:
direction = pygame.transform.flip(AGENT, True, False) direction = pygame.transform.flip(AGENT, True, False)
@ -74,11 +73,10 @@ def main():
garbage_type = loadmodel.classify(imgpath_array[agent.rect.x // 50][agent.rect.y // 50]) garbage_type = loadmodel.classify(imgpath_array[agent.rect.x // 50][agent.rect.y // 50])
low_space = agent.collect(garbage_type) low_space = agent.collect(garbage_type)
fields[agent.rect.x//50][agent.rect.y//50] = GRASS fields[agent.rect.x//50][agent.rect.y//50] = BASE
priority_array[agent.rect.x//50][agent.rect.y//50] = 1 priority_array[agent.rect.x//50][agent.rect.y//50] = 1
time.sleep(0.5) time.sleep(0.5)
pygame.quit() pygame.quit()

125
map.py
View File

@ -1,14 +1,37 @@
import pygame, random import pygame as pg
import random
from request import Request from request import Request
DIRT_IMG = pygame.image.load("dirt.jpg")
DIRT = pygame.transform.scale(DIRT_IMG, (50, 50)) STRAIGHT_IMG = pg.image.load("Tiles/Straight.jpg")
GRASS_IMG = pygame.image.load("grass.png") STRAIGHT_VERTICAL = pg.transform.scale(STRAIGHT_IMG, (50, 50))
GRASS = pygame.transform.scale(GRASS_IMG, (50, 50)) STRAIGHT_HORIZONTAL = pg.transform.rotate(STRAIGHT_VERTICAL, 270)
SAND_IMG = pygame.image.load("sand.jpeg") BASE_IMG = pg.image.load("Tiles/Base.jpg")
SAND = pygame.transform.scale(SAND_IMG, (50, 50)) BASE = pg.transform.scale(BASE_IMG, (50, 50))
COBBLE_IMG = pygame.image.load("cobble.jpeg") BEND_IMG = pg.image.load("Tiles/Bend.jpg")
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50)) BEND1 = pg.transform.scale(BEND_IMG, (50, 50))
BEND2 = pg.transform.rotate(BEND1, 90)
BEND3 = pg.transform.rotate(pg.transform.flip(pg.transform.rotate(BEND1, 180), True, True), 180)
BEND4 = pg.transform.rotate(BEND1, -90)
INTERSECTION_IMG = pg.image.load("Tiles/Intersection.jpg")
INTERSECTION = pg.transform.scale(INTERSECTION_IMG, (50, 50))
JUNCTION_IMG = pg.image.load("Tiles/Junction.jpg")
JUNCTION_SOUTH = pg.transform.scale(JUNCTION_IMG, (50, 50))
JUNCTION_NORTH = pg.transform.rotate(pg.transform.flip(JUNCTION_SOUTH, True, False), 180)
JUNCTION_EAST = pg.transform.rotate(JUNCTION_SOUTH, -90)
JUNCTION_WEST = pg.transform.rotate(JUNCTION_SOUTH, 90)
END_IMG = pg.image.load("Tiles/End.jpg")
END1 = pg.transform.flip(pg.transform.rotate(pg.transform.scale(END_IMG, (50, 50)), 180), False, True)
END2 = pg.transform.rotate(END1, 90)
DIRT_IMG = pg.image.load("Tiles/dirt.jpg")
DIRT = pg.transform.scale(DIRT_IMG, (50, 50))
GRASS_IMG = pg.image.load("Tiles/grass.png")
GRASS = pg.transform.scale(GRASS_IMG, (50, 50))
SAND_IMG = pg.image.load("Tiles/sand.jpeg")
SAND = pg.transform.scale(SAND_IMG, (50, 50))
COBBLE_IMG = pg.image.load("Tiles/cobble.jpeg")
COBBLE = pg.transform.scale(COBBLE_IMG, (50, 50))
def randomize_map(): # tworzenie mapy z losowymi polami def randomize_map(): # tworzenie mapy z losowymi polami
request_list = [] request_list = []
@ -16,36 +39,88 @@ def randomize_map(): # tworzenie mapy z losowymi polami
field_array_2 = [] field_array_2 = []
imgpath_array = [[0 for x in range(16)] for x in range(16)] imgpath_array = [[0 for x in range(16)] for x in range(16)]
field_priority = [] field_priority = []
map_array = [['b', 'sh', 'sh', 'sh', 'sh', 'jw', 'sh', 'sh', 'sh', 'sh', 'jw', 'sh', 'sh', 'sh', 'b3', 'g'],
['sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'sv', 'g'],
['sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'gr', 'g', 'sv', 'g'],
['js', 'sh', 'sh', 'sh', 'sh', 'i', 'sh', 'sh', 'sh', 'sh', 'jn', 'g', 'gr', 'g', 'sv', 'g'],
['sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'gr', 'g', 'sv', 'g'],
['sv', 'g', 'gr', 'gr', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'sv', 'g'],
['sv', 'g', 'gr', 'gr', 'g', 'js', 'sh', 'sh', 'sh', 'sh', 'i', 'sh', 'sh', 'sh', 'jn', 'g'],
['sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'sv', 'g'],
['b1', 'sh', 'jw', 'sh', 'sh', 'jn', 'g', 'gr', 'gr', 'g', 'sv', 'g', 'gr', 'g', 'sv', 'g'],
['g', 'g', 'sv', 'g', 'g', 'sv', 'g', 'gr', 'gr', 'g', 'sv', 'g', 'g', 'g', 'sv', 'g'],
['gr', 'g', 'sv', 'g', 'g', 'sv', 'g', 'gr', 'gr', 'g', 'js', 'sh', 'sh', 'sh', 'jn', 'g'],
['gr', 'g', 'sv', 'g', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'sv', 'g'],
['gr', 'g', 'js', 'sh', 'sh', 'i', 'sh', 'sh', 'sh', 'sh', 'jn', 'g', 'gr', 'g', 'sv', 'g'],
['gr', 'g', 'sv', 'g', 'g', 'sv', 'g', ' g', 'g', 'g', 'sv', 'g', 'gr', 'g', 'sv', 'g'],
['gr', 'g', 'sv', 'g', 'g', 'sv', 'g', 'g', 'g', 'g', 'sv', 'g', 'g', 'g', 'sv', 'g'],
['gr', 'g', 'b1', 'sh', 'sh', 'je', 'sh', 'sh', 'sh', 'sh', 'je', 'sh', 'sh', 'sh', 'b4', 'g'],
]
for i in range(16): for i in range(16):
temp_priority = [] temp_priority = []
for j in range(16): for j in range(16):
if i in (0, 1) and j in (0, 1): if map_array[i][j] == 'b':
field_array_2.append(GRASS) field_array_2.append(BASE)
temp_priority.append(1) temp_priority.append(1)
elif map_array[i][j] == 'b3':
field_array_2.append(BEND3)
temp_priority.append(1)
elif map_array[i][j] == 'b4':
field_array_2.append(BEND4)
temp_priority.append(1)
elif map_array[i][j] == 'b1':
field_array_2.append(BEND1)
temp_priority.append(1)
elif map_array[i][j] == 'sh':
field_array_2.append(STRAIGHT_VERTICAL)
temp_priority.append(1)
elif map_array[i][j] == 'sv':
field_array_2.append(STRAIGHT_HORIZONTAL)
temp_priority.append(1)
elif map_array[i][j] == 'i':
field_array_2.append(INTERSECTION)
temp_priority.append(1)
elif map_array[i][j] == 'je':
field_array_2.append(JUNCTION_EAST)
temp_priority.append(1)
elif map_array[i][j] == 'jw':
field_array_2.append(JUNCTION_WEST)
temp_priority.append(1)
elif map_array[i][j] == 'js':
field_array_2.append(JUNCTION_SOUTH)
temp_priority.append(1)
elif map_array[i][j] == 'jn':
field_array_2.append(JUNCTION_NORTH)
temp_priority.append(1)
elif map_array[i][j] == 'gr':
field_array_2.append(BASE)
temp_priority.append(1000)
else: else:
prob = random.uniform(0, 100) prob = random.uniform(0, 100)
if 0 <= prob <= 12: if 0 <= prob <= 20:
garbage_type = random.choice(['glass', 'mixed', 'paper', 'plastic']) garbage_type = random.choice(['glass', 'mixed', 'paper', 'plastic'])
garbage_image_number = random.randrange(1, 100) garbage_image_number = random.randrange(1, 100)
GARBAGE_IMG = pygame.image.load(f"./model_training/test_dataset/{garbage_type}/{garbage_type} ({str(garbage_image_number)}).jpg") GARBAGE_IMG = pg.image.load(
GARBAGE = pygame.transform.scale(GARBAGE_IMG, (50, 50)) f"./model_training/test_dataset/{garbage_type}/{garbage_type} ({str(garbage_image_number)}).jpg")
GARBAGE = pg.transform.scale(GARBAGE_IMG, (50, 50))
field_array_2.append(GARBAGE) field_array_2.append(GARBAGE)
#field_array_2.append(COBBLE) imgpath_array[i][j] = (
imgpath_array[i][j] = (f"./model_training/test_dataset/{garbage_type}/{garbage_type} ({str(garbage_image_number)}).jpg") f"./model_training/test_dataset/{garbage_type}/{garbage_type} ({str(garbage_image_number)}).jpg")
temp_priority.append(100) temp_priority.append(100)
request_list.append(Request( request_list.append(Request(
i*50,j*50, #lokacja i * 50, j * 50, # lokacja
random.randint(0,3), #typ śmieci random.randint(0, 3), # typ śmieci
random.random(), #objętość śmieci random.random(), # objętość śmieci
random.randint(0,30), #ostatni odbiór random.randint(0, 30), # ostatni odbiór
random.randint(0,1), #czy opłacone w terminie random.randint(0, 1), # czy opłacone w terminie
random.random() * 10, #intensywność odoru random.random() * 10, # intensywność odoru
random.random() * 50 #waga śmieci random.random() * 50 # waga śmieci
)) ))
else: else:
field_array_2.append(GRASS) field_array_2.append(BASE)
temp_priority.append(1) temp_priority.append(1000)
field_array_1.append(field_array_2) field_array_1.append(field_array_2)
field_array_2 = [] field_array_2 = []
field_priority.append(temp_priority) field_priority.append(temp_priority)