storage #4

Merged
s473603 merged 2 commits from storage into master 2023-05-26 12:10:07 +02:00
2 changed files with 77 additions and 49 deletions

122
field.py
View File

@ -1,9 +1,12 @@
import asyncio
import random import random
import time import time
from heapq import * from heapq import *
from enum import Enum, IntEnum from enum import Enum, IntEnum
from queue import PriorityQueue from queue import PriorityQueue
from collections import deque from collections import deque
from threading import Thread
from IC3 import tree from IC3 import tree
import pygame import pygame
@ -22,8 +25,7 @@ WATER_TANK_CAPACITY = 10
GAS_TANK_CAPACITY = 250 GAS_TANK_CAPACITY = 250
SPAWN_POINT = (0, 0) SPAWN_POINT = (0, 0)
SKLEP_POINT = (14, 14) SKLEP_POINT = (14, 14)
TIMEOUT = 1
tractor_image = pygame.transform.scale(pygame.image.load("images/tractor_image.png"), (BLOCK_SIZE, BLOCK_SIZE)) tractor_image = pygame.transform.scale(pygame.image.load("images/tractor_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
rock_image = pygame.transform.scale(pygame.image.load("images/rock_image.png"), (BLOCK_SIZE, BLOCK_SIZE)) rock_image = pygame.transform.scale(pygame.image.load("images/rock_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
@ -32,7 +34,10 @@ carrot_image = pygame.transform.scale(pygame.image.load("images/carrot.png"), (B
broccoli_image = pygame.transform.scale(pygame.image.load("images/broccoli.png"), (BLOCK_SIZE, BLOCK_SIZE)) broccoli_image = pygame.transform.scale(pygame.image.load("images/broccoli.png"), (BLOCK_SIZE, BLOCK_SIZE))
onion_image = pygame.transform.scale(pygame.image.load("images/onion.png"), (BLOCK_SIZE, BLOCK_SIZE)) onion_image = pygame.transform.scale(pygame.image.load("images/onion.png"), (BLOCK_SIZE, BLOCK_SIZE))
gas_station_image = pygame.transform.scale(pygame.image.load("images/gas_station.png"), (BLOCK_SIZE, BLOCK_SIZE)) gas_station_image = pygame.transform.scale(pygame.image.load("images/gas_station.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_station_image = pygame.transform.scale(pygame.image.load("images/storage.png"), (BLOCK_SIZE, BLOCK_SIZE)) gas_station_closed_image = pygame.transform.scale(pygame.image.load("images/gas_station_closed.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_station_image = pygame.transform.scale(pygame.image.load("images/storage_open.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_closed_station_image = pygame.transform.scale(pygame.image.load("images/storage_closed.png"),
(BLOCK_SIZE, BLOCK_SIZE))
font = pygame.font.Font('freesansbold.ttf', BLOCK_SIZE // 2) font = pygame.font.Font('freesansbold.ttf', BLOCK_SIZE // 2)
@ -55,7 +60,6 @@ def get_click_mouse_pos():
return (grid_x, grid_y, Direction.RIGHT) if click[0] else False return (grid_x, grid_y, Direction.RIGHT) if click[0] else False
def draw_interface(): def draw_interface():
def returnFun(): def returnFun():
for y, row in enumerate(grid.grid): for y, row in enumerate(grid.grid):
@ -86,9 +90,9 @@ def draw_interface():
grid = Grid(BOARD_SIZE, BOARD_SIZE, BLOCK_SIZE) grid = Grid(BOARD_SIZE, BOARD_SIZE, BLOCK_SIZE)
graph1 = Graph(grid) graph1 = Graph(grid)
graph1.initialize_graph(grid) t2 = Thread(target=close_open, args=(grid,))
t2.setDaemon(True)
t2.start()
fl_running = True fl_running = True
while fl_running: while fl_running:
draw_grid() draw_grid()
@ -115,6 +119,11 @@ def draw_interface():
# movement(tractor, grid, b) # movement(tractor, grid, b)
updateDisplay(tractor, grid) updateDisplay(tractor, grid)
# graph1.initialize_graph(grid)
class Direction(IntEnum): class Direction(IntEnum):
UP = 0 UP = 0
@ -146,6 +155,8 @@ class Grid:
self.block_size = block_size self.block_size = block_size
self.grid = [[types.EMPTY for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)] self.grid = [[types.EMPTY for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)]
self.initialize_grid() self.initialize_grid()
self.is_gas_station_closed = False
self.is_storage_closed = False
def add_object(self, x, y, type_of_object: types): def add_object(self, x, y, type_of_object: types):
if self.grid[x][y] == types.EMPTY: if self.grid[x][y] == types.EMPTY:
@ -179,15 +190,15 @@ class Grid:
class Graph: class Graph:
def __init__(self, grid: Grid): def __init__(self, grid: Grid):
self.graph = {} self.graph = {}
self.initialize_graph(grid) # self.initialize_graph(grid)
def initialize_graph(self, grid: Grid): # def initialize_graph(self, grid: Grid):
for y, row in enumerate(grid.grid): # for y, row in enumerate(grid.grid):
for x, col in enumerate(row): # for x, col in enumerate(row):
for direction in Direction: # for direction in Direction:
self.graph[(x, y, direction)] = get_next_nodes(x, y, direction, grid) # self.graph[(x, y, direction)] = get_next_nodes(x, y, direction, grid)
def a_star(self, start, goal): def a_star(self, start, goal, grid: Grid):
# not finished yet https://www.youtube.com/watch?v=abHftC1GU6w # not finished yet https://www.youtube.com/watch?v=abHftC1GU6w
queue = PriorityQueue() queue = PriorityQueue()
queue.put((0, start)) queue.put((0, start))
@ -195,15 +206,16 @@ class Graph:
visited = {start: None} visited = {start: None}
returnGoal = goal returnGoal = goal
h = lambda start, goal: abs(start[0] - goal[0]) + abs(start[1] - goal[1]) #heuristic function (manhattan distance) h = lambda start, goal: abs(start[0] - goal[0]) + abs(
start[1] - goal[1]) # heuristic function (manhattan distance)
while not queue.empty(): while not queue.empty():
cur_cost, cur_node = queue.get() cur_cost, cur_node = queue.get()
if cur_node[0] == goal[0] and cur_node[1] == goal[1]: if cur_node[0] == goal[0] and cur_node[1] == goal[1]:
returnGoal=cur_node returnGoal = cur_node
break break
next_nodes = self.graph[cur_node] next_nodes = get_next_nodes(cur_node[0], cur_node[1], cur_node[2], grid)
for next_node in next_nodes: for next_node in next_nodes:
neigh_cost, neigh_node = next_node neigh_cost, neigh_node = next_node
@ -258,7 +270,9 @@ class Tractor:
def get_next_nodes(x, y, direction: Direction, grid: Grid): def get_next_nodes(x, y, direction: Direction, grid: Grid):
check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE else False check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE else False
way = [0, -1] if direction == Direction.UP else [1, 0] if direction == Direction.RIGHT else [0, 1] if direction == Direction.DOWN else [-1, 0] way = [0, -1] if direction == Direction.UP else [1, 0] if direction == Direction.RIGHT else [0,
1] if direction == Direction.DOWN else [
-1, 0]
next_nodes = [] next_nodes = []
for new_direction in Direction: for new_direction in Direction:
if new_direction != direction: if new_direction != direction:
@ -291,6 +305,8 @@ def movement(tractor: Tractor, grid: Grid, road):
else: else:
tractor.rot_center(Direction.LEFT) tractor.rot_center(Direction.LEFT)
updateDisplay(tractor, grid) updateDisplay(tractor, grid)
def getCost(tractor: Tractor, grid: Grid, road): def getCost(tractor: Tractor, grid: Grid, road):
n = len(road) n = len(road)
cost = 0 cost = 0
@ -309,11 +325,12 @@ def getCost(tractor: Tractor, grid: Grid, road):
cost += 2 cost += 2
if aA[2] != bB[2]: if aA[2] != bB[2]:
if (bB[2].value - aA[2].value == 1) or (bB[2].value - aA[2].value == -3): if (bB[2].value - aA[2].value == 1) or (bB[2].value - aA[2].value == -3):
cost +=1 cost += 1
else: else:
cost +=1 cost += 1
return cost return cost
def getRoad(start, goal, visited): def getRoad(start, goal, visited):
arr = [] arr = []
aFrom = goal aFrom = goal
@ -340,7 +357,14 @@ def updateDisplay(tractor: Tractor, grid: Grid):
elif grid.grid[x][y] == types.ROCK: elif grid.grid[x][y] == types.ROCK:
sc.blit(rock_image, (x * BLOCK_SIZE, y * BLOCK_SIZE)) sc.blit(rock_image, (x * BLOCK_SIZE, y * BLOCK_SIZE))
sc.blit(gas_station_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE)) sc.blit(gas_station_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
if grid.is_storage_closed:
sc.blit(sklep_closed_station_image, (SKLEP_POINT[0] * BLOCK_SIZE, SKLEP_POINT[1] * BLOCK_SIZE))
else:
sc.blit(sklep_station_image, (SKLEP_POINT[0] * BLOCK_SIZE, SKLEP_POINT[1] * BLOCK_SIZE)) sc.blit(sklep_station_image, (SKLEP_POINT[0] * BLOCK_SIZE, SKLEP_POINT[1] * BLOCK_SIZE))
if grid.is_gas_station_closed:
sc.blit(gas_station_closed_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
else:
sc.blit(gas_station_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
# region text # region text
vegetables_text = font.render( vegetables_text = font.render(
@ -363,16 +387,16 @@ def updateDisplay(tractor: Tractor, grid: Grid):
pygame.time.Clock().tick(60) pygame.time.Clock().tick(60)
def decisionTree(startpoint, endpoint, tractor, grid, graph1):
one="can it get to the next point" def decisionTree(startpoint, endpoint, tractor, grid, graph1):
two="will it be able to get to the gas station" one = "can it get to the next point"
three="will it be able to get to the gas station after arriving at the next point" two = "will it be able to get to the gas station"
four="will it be able to take the next vegetable to the tractor storage" three = "will it be able to get to the gas station after arriving at the next point"
five="will it be able to get to the vegetable warehouse" four = "will it be able to take the next vegetable to the tractor storage"
six="will it be able to get to the gas station after it arrives at the vegetable warehouse" five = "will it be able to get to the vegetable warehouse"
seven="is the vegetable warehouse closed" six = "will it be able to get to the gas station after it arrives at the vegetable warehouse"
eight="is the gas station closed" seven = "is the vegetable warehouse closed"
eight = "is the gas station closed"
arr = [] arr = []
arr.append(one) arr.append(one)
arr.append(two) arr.append(two)
@ -383,28 +407,26 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
arr.append(seven) arr.append(seven)
arr.append(eight) arr.append(eight)
a1, c1 = graph1.a_star(startpoint, endpoint, grid)
a1, c1 = graph1.a_star(startpoint, endpoint)
b1 = getRoad(startpoint, c1, a1) b1 = getRoad(startpoint, c1, a1)
cost1 = getCost(tractor, grid, b1) cost1 = getCost(tractor, grid, b1)
a2, c2 = graph1.a_star(startpoint, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT)) a2, c2 = graph1.a_star(startpoint, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT), grid)
b2 = getRoad(startpoint, c2, a2) b2 = getRoad(startpoint, c2, a2)
cost2 = getCost(tractor, grid, b2) cost2 = getCost(tractor, grid, b2)
a3, c3 = graph1.a_star(startpoint, (SKLEP_POINT[0], SKLEP_POINT[1], Direction.RIGHT)) a3, c3 = graph1.a_star(startpoint, (SKLEP_POINT[0], SKLEP_POINT[1], Direction.RIGHT), grid)
b3 = getRoad(startpoint, c3, a3) b3 = getRoad(startpoint, c3, a3)
cost3 = getCost(tractor, grid, b3) cost3 = getCost(tractor, grid, b3)
a4, c4 = graph1.a_star(c1, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT)) a4, c4 = graph1.a_star(c1, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT), grid)
b4 = getRoad(c1, c4, a4) b4 = getRoad(c1, c4, a4)
cost4 = getCost(tractor, grid, b4) cost4 = getCost(tractor, grid, b4)
a5, c5 = graph1.a_star(c3, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT)) a5, c5 = graph1.a_star(c3, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT), grid)
b5 = getRoad(c3, c5, a5) b5 = getRoad(c3, c5, a5)
cost5 = getCost(tractor, grid, b5) cost5 = getCost(tractor, grid, b5)
if tractor.gas - cost1 > 0: if tractor.gas - cost1 > 0:
can_it_get_to_the_next_point = 1 can_it_get_to_the_next_point = 1
else: else:
@ -429,18 +451,17 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
will_it_be_able_to_take_the_next_vegetable_to_the_tractor_storage = 1 will_it_be_able_to_take_the_next_vegetable_to_the_tractor_storage = 1
if tractor.gas - cost3 > 0: if tractor.gas - cost3 > 0:
will_it_be_able_to_get_to_the_vegetable_warehouse=1 will_it_be_able_to_get_to_the_vegetable_warehouse = 1
else: else:
will_it_be_able_to_get_to_the_vegetable_warehouse=0 will_it_be_able_to_get_to_the_vegetable_warehouse = 0
if tractor.gas - cost3 - cost5 > 0: if tractor.gas - cost3 - cost5 > 0:
will_it_be_able_to_get_to_the_gas_station_after_it_arrives_at_the_vegetable_warehouse=1 will_it_be_able_to_get_to_the_gas_station_after_it_arrives_at_the_vegetable_warehouse = 1
else: else:
will_it_be_able_to_get_to_the_gas_station_after_it_arrives_at_the_vegetable_warehouse = 0 will_it_be_able_to_get_to_the_gas_station_after_it_arrives_at_the_vegetable_warehouse = 0
is_the_vegetable_warehouse_closed=0 is_the_vegetable_warehouse_closed = grid.is_storage_closed
is_the_gas_station_closed=0 is_the_gas_station_closed = grid.is_gas_station_closed
brr = [] brr = []
brr.append(can_it_get_to_the_next_point) brr.append(can_it_get_to_the_next_point)
@ -462,16 +483,23 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
return predict(tree[root_node][feature_value]) return predict(tree[root_node][feature_value])
else: else:
return None return None
decision = predict(tree) decision = predict(tree)
print(decision) print(decision)
if decision==1: if decision == 1:
movement(tractor, grid, b1) movement(tractor, grid, b1)
if decision==2: if decision == 2:
movement(tractor, grid, b2) movement(tractor, grid, b2)
if decision==3: if decision == 3:
movement(tractor, grid, b3) movement(tractor, grid, b3)
if decision==4: if decision == 4:
print("waiting") print("waiting")
if decision==5: if decision == 5:
print("GAME OVER") print("GAME OVER")
def close_open(grid: Grid):
while True:
time.sleep(TIMEOUT)
grid.is_gas_station_closed = bool(random.getrandbits(1))
grid.is_storage_closed = bool(random.getrandbits(1))

BIN
images/storage_open.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB