forked from s452751/AI_PRO
A* and corrected graphsearch
This commit is contained in:
parent
8098a258f2
commit
5109d10d8c
30
Field.py
30
Field.py
@ -8,21 +8,6 @@ class Field:
|
||||
def state(self):
|
||||
return self.__state
|
||||
|
||||
@property
|
||||
def cost(self):
|
||||
if self.state == 'toPlow':
|
||||
return 1
|
||||
elif self.state == 'toSeed':
|
||||
return 2
|
||||
elif self.state == 'toFertilize':
|
||||
return 3
|
||||
elif self.state == 'toWater':
|
||||
return 4
|
||||
elif self.state == 'toCut':
|
||||
return 5
|
||||
else:
|
||||
return 0
|
||||
|
||||
@property
|
||||
def horizontal_index(self):
|
||||
return self.__horizontal_index
|
||||
@ -31,6 +16,21 @@ class Field:
|
||||
def vertical_index(self):
|
||||
return self.__vertical_index
|
||||
|
||||
@property
|
||||
def cost(self):
|
||||
if self.state == 'toPlow':
|
||||
return 1
|
||||
elif self.state == 'toSeed':
|
||||
return 2
|
||||
elif self.state == 'toFertilize':
|
||||
return 300
|
||||
elif self.state == 'toWater':
|
||||
return 4
|
||||
elif self.state == 'toCut':
|
||||
return 5000
|
||||
else:
|
||||
return 0
|
||||
|
||||
@state.setter
|
||||
def state(self, state):
|
||||
if state == "toPlow" or state == "toWater" or state == "toSeed" or \
|
||||
|
100
FindPath.py
100
FindPath.py
@ -1,21 +1,7 @@
|
||||
from constants import *
|
||||
from queue import Queue
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, parent: (int, int), index: (int, int)):
|
||||
self.__index = index
|
||||
self.__parent = parent
|
||||
|
||||
def get_index(self):
|
||||
return self.__index
|
||||
|
||||
def get_parent(self):
|
||||
return self.__parent
|
||||
|
||||
|
||||
def whichStateLookingFor(tractor, TillageUnit):
|
||||
searching_field = "toPlow"
|
||||
searching_field = "none"
|
||||
if tractor.header and tractor.hitch == "Crop Trailer":
|
||||
searching_field = "toCut"
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Nothing":
|
||||
@ -30,87 +16,15 @@ def whichStateLookingFor(tractor, TillageUnit):
|
||||
|
||||
|
||||
def nearestLookingField(board, tractor, TillageUnit):
|
||||
horizontal_tiles_number = int(HORIZONTAL_TILES_NUMBER)
|
||||
vertical_tiles_number = int(VERTICAL_TILES_NUMBER)
|
||||
|
||||
print(horizontal_tiles_number, vertical_tiles_number)
|
||||
a = input()
|
||||
end_horizontal_index = 0
|
||||
end_vertical_index = 0
|
||||
searching_field = whichStateLookingFor(tractor, TillageUnit)
|
||||
for i in range(horizontal_tiles_number):
|
||||
for j in range(vertical_tiles_number):
|
||||
for i in range(0, int(HORIZONTAL_TILES_NUMBER)):
|
||||
for j in range(0, int(VERTICAL_TILES_NUMBER)):
|
||||
field = board[i][j]
|
||||
if searching_field == field.state:
|
||||
end_horizontal_index = field.horizontal_index
|
||||
end_vertical_index = field.vertical_index
|
||||
break
|
||||
end_horizontal_index = field.horizontal_index / TILE_SIZE
|
||||
end_vertical_index = field.vertical_index / TILE_SIZE
|
||||
return end_horizontal_index, end_vertical_index
|
||||
|
||||
return end_horizontal_index, end_vertical_index
|
||||
|
||||
|
||||
def graphsearch(tractor, board, TillageUnit, fringe: Queue, explored):
|
||||
start_horizontal_index = tractor.horizontal_index
|
||||
start_vertical_index = tractor.vertical_index
|
||||
start_state = (start_horizontal_index, start_vertical_index)
|
||||
end_state = nearestLookingField(board, tractor, TillageUnit)
|
||||
print(start_state)
|
||||
print(end_state)
|
||||
|
||||
fringe.put(start_state)
|
||||
|
||||
while True:
|
||||
if fringe.empty():
|
||||
return False
|
||||
|
||||
elem = fringe.get()
|
||||
|
||||
if goaltest(elem, end_state):
|
||||
break
|
||||
#TODO
|
||||
#return droga ktora musi pokonac traktor
|
||||
else:
|
||||
explored.append(elem)
|
||||
elem = succ(start_state, end_state, tractor)
|
||||
fringe.put(elem)
|
||||
|
||||
|
||||
def goaltest(elem, end_state):
|
||||
print('element:', elem, 'state:', end_state)
|
||||
if elem == end_state:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def succ(start_state, end_state, tractor):
|
||||
print(tractor.horizontal_index, ' ', tractor.vertical_index)
|
||||
a = input()
|
||||
if start_state[1] < end_state[1]:
|
||||
if tractor.direction == "RIGHT":
|
||||
tractor.drive()
|
||||
print("przesunalem sie w prawo")
|
||||
else:
|
||||
tractor.direction = "RIGHT"
|
||||
|
||||
elif start_state[1] > end_state[1]:
|
||||
if tractor.direction == "LEFT":
|
||||
tractor.drive()
|
||||
print("przesunalem sie w lewo")
|
||||
else:
|
||||
tractor.direction = "LEFT"
|
||||
|
||||
elif start_state[0] < end_state[0]:
|
||||
if tractor.direction == "DOWN":
|
||||
tractor.drive()
|
||||
print("przesunalem sie w dol")
|
||||
else:
|
||||
tractor.direction = "DOWN"
|
||||
elif start_state[0] > end_state[0]:
|
||||
if tractor.direction == "UP":
|
||||
tractor.drive()
|
||||
print("przesunalem sie w gore")
|
||||
else:
|
||||
tractor.direction = "UP"
|
||||
else:
|
||||
return tractor.horizontal_index, tractor.horizontal_index
|
||||
return end_horizontal_index, end_vertical_index
|
33
Tractor.py
33
Tractor.py
@ -94,8 +94,7 @@ class Tractor:
|
||||
|
||||
@direction.setter
|
||||
def direction(self, direction):
|
||||
if direction == "UP" or direction == "DOWN" or direction == "RIGHT" or direction == "LEFT":
|
||||
self.__direction = direction
|
||||
self.__direction = direction
|
||||
|
||||
|
||||
def drive(self):
|
||||
@ -111,3 +110,33 @@ class Tractor:
|
||||
elif self.__direction == "LEFT" and self.horizontal_index > 0:
|
||||
self.__horizontal_index += -mRange
|
||||
self.reduce_fuel()
|
||||
|
||||
def left_rotation(self):
|
||||
if self.__direction == "UP":
|
||||
self.__direction = "LEFT"
|
||||
elif self.__direction == "LEFT":
|
||||
self.__direction = "DOWN"
|
||||
elif self.__direction == "DOWN":
|
||||
self.__direction = "RIGHT"
|
||||
elif self.__direction == "RIGHT":
|
||||
self.__direction = "UP"
|
||||
|
||||
|
||||
def right_rotation(self):
|
||||
if self.__direction == "UP":
|
||||
self.__direction = "RIGHT"
|
||||
elif self.__direction == "RIGHT":
|
||||
self.__direction = "DOWN"
|
||||
elif self.__direction == "DOWN":
|
||||
self.__direction = "LEFT"
|
||||
elif self.__direction == "LEFT":
|
||||
self.__direction = "UP"
|
||||
|
||||
def auto_movement(self, move):
|
||||
if move == "Move":
|
||||
self.drive()
|
||||
elif move == "Left_Rotation":
|
||||
self.left_rotation()
|
||||
elif move == "Right_Rotation":
|
||||
self.right_rotation()
|
||||
|
||||
|
@ -29,8 +29,8 @@ TRACTOR_WIDTH = TILE_SIZE
|
||||
TRACTOR_HEIGHT = TILE_SIZE
|
||||
|
||||
#FRAMES PER SECOND
|
||||
FPS = 10
|
||||
FPS = 5
|
||||
|
||||
#ANIMATION_PART
|
||||
|
||||
ANIMATION_PART = 11
|
||||
ANIMATION_PART = 1
|
||||
|
30
main.py
30
main.py
@ -1,15 +1,14 @@
|
||||
import pygame
|
||||
|
||||
import Board
|
||||
import FindPath
|
||||
import TractorAction
|
||||
import drawUI
|
||||
from FindPath import graphsearch
|
||||
import Graphsearch
|
||||
from Tractor import Tractor
|
||||
from TractorLoad import TillageUnit
|
||||
from animations import animationsOn, animationsOff
|
||||
from constants import *
|
||||
from manualSteering import manualSteeringDriver
|
||||
from queue import Queue
|
||||
|
||||
pygame.init()
|
||||
|
||||
@ -27,11 +26,13 @@ toolCounter = - 1
|
||||
|
||||
board = Board.generate()
|
||||
|
||||
tractor = Tractor(horizontal_index=-0, vertical_index=0, hitch="nothing", header=False, autodrive=False,
|
||||
direction='RIGHT')
|
||||
tractor = Tractor(horizontal_index=-0, vertical_index=0, hitch="nothing", header=False, autodrive=True,
|
||||
direction="RIGHT")
|
||||
|
||||
tillageUnit = TillageUnit("Nothing")
|
||||
|
||||
tractor.turnOnEngine()
|
||||
move_list = []
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
@ -45,23 +46,28 @@ while working:
|
||||
|
||||
field = board[tractor.horizontal_index][tractor.vertical_index]
|
||||
|
||||
print("curr field cost", field.cost)
|
||||
print("tractor dir", tractor.direction)
|
||||
if not move_list:
|
||||
field.state = TractorAction.changeFieldState(field, tractor)
|
||||
istate = Graphsearch.Istate(tractor.direction, tractor.horizontal_index, tractor.vertical_index)
|
||||
move_list = Graphsearch.graphsearch([], [], istate, FindPath.nearestLookingField(board, tractor, TillageUnit), board)
|
||||
print(move_list)
|
||||
|
||||
elif move_list:
|
||||
tractor.auto_movement(move_list.pop(0))
|
||||
TractorAction.changeFieldState(field, tractor)
|
||||
|
||||
if tractor.autodrive and tractor.engineWorking:
|
||||
animationSpeed = ANIMATION_PART
|
||||
else:
|
||||
animationSpeed = 1
|
||||
print(ANIMATION_PART)
|
||||
|
||||
if tractor.autodrive:
|
||||
if field.horizontal_index == 0 and field.vertical_index == 0 and not move_list:
|
||||
tractor, tillageUnit, toolCounter = TractorAction.autoToolsChange(tractor, tillageUnit, toolCounter)
|
||||
field.state = TractorAction.changeFieldState(field, tractor)
|
||||
|
||||
drawUI.drawUI(board, display, tractor, tractor.direction, tillageUnit, field, animationSpeed)
|
||||
|
||||
clock.tick(FPS)
|
||||
|
||||
#graphsearch(tractor, board, TillageUnit, Queue(), list())
|
||||
|
||||
pygame.quit()
|
||||
quit()
|
||||
quit()
|
@ -51,13 +51,12 @@ def manualSteeringDriver(event, board, tractor, hitchCounter, tillageUnit, loadC
|
||||
|
||||
|
||||
def manualTurning(event, tractor):
|
||||
tractor.direction = "STOP"
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT and tractor.horizontal_index > 0:
|
||||
tractor.direction = "LEFT"
|
||||
tractor.direction = TRACTOR_DIRECTION_LEFT
|
||||
elif event.key == pygame.K_RIGHT and tractor.horizontal_index < HORIZONTAL_TILES_NUMBER - 1:
|
||||
tractor.direction = "RIGHT"
|
||||
tractor.direction = TRACTOR_DIRECTION_RIGHT
|
||||
elif event.key == pygame.K_UP and tractor.vertical_index > 0:
|
||||
tractor.direction = "UP"
|
||||
tractor.direction = TRACTOR_DIRECTION_UP
|
||||
elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1:
|
||||
tractor.direction = "DOWN"
|
||||
tractor.direction = TRACTOR_DIRECTION_DOWN
|
||||
|
Loading…
Reference in New Issue
Block a user