Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
3691634fb3 | |||
ac61d57af1 | |||
69f1936be9 |
2
.idea/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
# Default ignored files
|
||||
/workspace.xml
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -1,6 +0,0 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||
</project>
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/AI_PRO2.iml" filepath="$PROJECT_DIR$/.idea/AI_PRO2.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
18
Board.py
@ -1,18 +0,0 @@
|
||||
import random
|
||||
|
||||
from Field import Field
|
||||
from constants import *
|
||||
|
||||
states = ['toPlow', 'toSeed', 'toFertilize', 'toWater', 'toCut']
|
||||
|
||||
|
||||
def generate():
|
||||
board = []
|
||||
for i in range(0, int(HORIZONTAL_TILES_NUMBER)):
|
||||
board.append([])
|
||||
for j in range(0, int(VERTICAL_TILES_NUMBER)):
|
||||
board[i].append(Field(int(i * TILE_SIZE), int(j * TILE_SIZE), random.choice(states)))
|
||||
|
||||
board[0][0] = Field(int(0 * TILE_SIZE), int(0 * TILE_SIZE), "TOOLS_FIELD")
|
||||
board[1][0] = Field(int(1 * TILE_SIZE), int(0 * TILE_SIZE), "FUEL_FIELD")
|
||||
return board
|
38
Field.py
@ -1,38 +0,0 @@
|
||||
class Field:
|
||||
def __init__(self, horizontal_index, vertical_index, state):
|
||||
self.__horizontal_index = horizontal_index
|
||||
self.__vertical_index = vertical_index
|
||||
self.__state = state
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self.__state
|
||||
|
||||
@property
|
||||
def horizontal_index(self):
|
||||
return self.__horizontal_index
|
||||
|
||||
@property
|
||||
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 3
|
||||
elif self.state == 'toWater':
|
||||
return 4
|
||||
elif self.state == 'toCut':
|
||||
return 100
|
||||
else:
|
||||
return 0
|
||||
|
||||
@state.setter
|
||||
def state(self, state):
|
||||
if state == "toPlow" or state == "toWater" or state == "toSeed" or \
|
||||
state == "toFertilize" or state == "toCut" or state == "TOOLS_FIELD" or state == "FUEL_FIELD":
|
||||
self.__state = state
|
30
FindPath.py
@ -1,30 +0,0 @@
|
||||
from constants import *
|
||||
|
||||
def whichStateLookingFor(tractor, TillageUnit):
|
||||
searching_field = "none"
|
||||
if tractor.header and tractor.hitch == "Crop Trailer":
|
||||
searching_field = "toCut"
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Nothing":
|
||||
searching_field = "toPlow"
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Water":
|
||||
searching_field = "toWater"
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Seeds":
|
||||
searching_field = "toSeed"
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Fertilizer":
|
||||
searching_field = "toFertilize"
|
||||
return searching_field
|
||||
|
||||
|
||||
def nearestLookingField(board, tractor, TillageUnit):
|
||||
end_horizontal_index = 0
|
||||
end_vertical_index = 0
|
||||
searching_field = whichStateLookingFor(tractor, TillageUnit)
|
||||
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 / TILE_SIZE
|
||||
end_vertical_index = field.vertical_index / TILE_SIZE
|
||||
return end_horizontal_index, end_vertical_index
|
||||
|
||||
return end_horizontal_index, end_vertical_index
|
174
Graphsearch.py
@ -1,174 +0,0 @@
|
||||
from operator import itemgetter
|
||||
|
||||
import constants
|
||||
|
||||
|
||||
class Istate:
|
||||
def __init__(self, direction, horizontal_index, vertical_index):
|
||||
self.__direction = direction
|
||||
self.__horizontal_index = horizontal_index
|
||||
self.__vertical_index = vertical_index
|
||||
|
||||
@property
|
||||
def direction(self):
|
||||
return self.__direction
|
||||
|
||||
@property
|
||||
def horizontal_index(self):
|
||||
return self.__horizontal_index
|
||||
|
||||
@property
|
||||
def vertical_index(self):
|
||||
return self.__vertical_index
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, action, direction, parent, horizontal_index, vertical_index):
|
||||
self.__action = action
|
||||
self.__direction = direction
|
||||
self.__parent = parent
|
||||
self.__horizontal_index = horizontal_index
|
||||
self.__vertical_index = vertical_index
|
||||
|
||||
@property
|
||||
def action(self):
|
||||
return self.__action
|
||||
|
||||
@property
|
||||
def direction(self):
|
||||
return self.__direction
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
return self.__parent
|
||||
|
||||
@property
|
||||
def horizontal_index(self):
|
||||
return self.__horizontal_index
|
||||
|
||||
@property
|
||||
def vertical_index(self):
|
||||
return self.__vertical_index
|
||||
|
||||
|
||||
def goal_test(elem, goaltest):
|
||||
if elem.horizontal_index == goaltest[0] and elem.vertical_index == goaltest[1]:
|
||||
return True
|
||||
else:
|
||||
# print("Traktor wartosc horizontal_index: ", elem.horizontal_index)
|
||||
# print("Traktor wartosc vertical_index: ", elem.vertical_index)
|
||||
return False
|
||||
|
||||
|
||||
def print_moves(elem):
|
||||
moves_list = []
|
||||
while elem.parent is not None:
|
||||
moves_list.append(elem.action)
|
||||
elem = elem.parent
|
||||
moves_list.reverse()
|
||||
return moves_list
|
||||
|
||||
|
||||
def cost(board, node):
|
||||
cost_value = 0
|
||||
while node.parent is not None:
|
||||
field = board[node.horizontal_index][node.vertical_index]
|
||||
cost_value = cost_value + field.cost + 1
|
||||
node = node.parent
|
||||
return cost_value
|
||||
|
||||
|
||||
def heuristic(node, goaltest):
|
||||
return abs(node.horizontal_index - goaltest[0]) + abs(node.vertical_index - goaltest[1])
|
||||
|
||||
|
||||
def f(board, node, goaltest):
|
||||
cost_value = cost(board, node)
|
||||
return cost_value + heuristic(node, goaltest)
|
||||
|
||||
|
||||
def succ(elem):
|
||||
actions_list = []
|
||||
direction = elem.direction
|
||||
if direction == "UP":
|
||||
direction = "RIGHT"
|
||||
elif direction == "RIGHT":
|
||||
direction = "DOWN"
|
||||
elif direction == "DOWN":
|
||||
direction = "LEFT"
|
||||
elif direction == "LEFT":
|
||||
direction = "UP"
|
||||
actions_list.append(("Right_Rotation", (direction, elem.horizontal_index, elem.vertical_index)))
|
||||
|
||||
direction = elem.direction
|
||||
if direction == "UP":
|
||||
direction = "LEFT"
|
||||
elif direction == "LEFT":
|
||||
direction = "DOWN"
|
||||
elif direction == "DOWN":
|
||||
direction = "RIGHT"
|
||||
elif direction == "RIGHT":
|
||||
direction = "UP"
|
||||
actions_list.append(("Left_Rotation", (direction, elem.horizontal_index, elem.vertical_index)))
|
||||
|
||||
horizontal_change = 0
|
||||
vertical_change = 0
|
||||
|
||||
if elem.direction == "RIGHT" and elem.horizontal_index < constants.HORIZONTAL_TILES_NUMBER - 1:
|
||||
horizontal_change = 1
|
||||
elif elem.direction == "LEFT" and elem.horizontal_index > 0:
|
||||
horizontal_change = -1
|
||||
elif elem.direction == "UP" and elem.vertical_index > 0:
|
||||
vertical_change = -1
|
||||
elif elem.direction == "DOWN" and elem.vertical_index < constants.VERTICAL_TILES_NUMBER - 1:
|
||||
vertical_change = 1
|
||||
|
||||
actions_list.append(("Move", (elem.direction, elem.horizontal_index + horizontal_change,
|
||||
elem.vertical_index + vertical_change)))
|
||||
|
||||
return actions_list
|
||||
|
||||
|
||||
def graphsearch(fringe, explored, istate, goaltest, board):
|
||||
node = Node(None, istate.direction, None, istate.horizontal_index, istate.vertical_index)
|
||||
fringe.append((node, 0))
|
||||
while True:
|
||||
if not fringe:
|
||||
return False
|
||||
|
||||
elem = fringe.pop(0)
|
||||
temp = elem[0]
|
||||
|
||||
if goal_test(elem[0], goaltest) is True:
|
||||
return print_moves(elem[0])
|
||||
|
||||
explored.append(elem)
|
||||
|
||||
for (action, state) in succ(temp):
|
||||
fringe_list = []
|
||||
fringe_list_priority = []
|
||||
explored_list = []
|
||||
|
||||
for (x, y) in fringe:
|
||||
fringe_list.append((x.direction, x.horizontal_index, x.vertical_index))
|
||||
fringe_list_priority.append(((x.direction, x.horizontal_index, x.vertical_index), y))
|
||||
|
||||
for (x, y) in explored:
|
||||
explored_list.append((x.direction, x.horizontal_index, x.vertical_index))
|
||||
|
||||
x = Node(action, state[0], elem[0], state[1], state[2])
|
||||
p = f(board, x, goaltest)
|
||||
|
||||
if state not in fringe_list and state not in explored_list:
|
||||
fringe.append((x, p))
|
||||
fringe = sorted(fringe, key=itemgetter(1))
|
||||
elif state in fringe_list:
|
||||
index = 0
|
||||
for (state_priority, r) in fringe_list_priority:
|
||||
if state_priority == state:
|
||||
if r > p:
|
||||
fringe.insert(index, (x, p))
|
||||
fringe.pop(index + 1)
|
||||
fringe = sorted(fringe, key=itemgetter(1))
|
||||
break
|
||||
index = index + 1
|
12
Main/Board.py
Normal file
@ -0,0 +1,12 @@
|
||||
import random
|
||||
from constants import *
|
||||
|
||||
board = []
|
||||
|
||||
|
||||
def generate():
|
||||
for i in range(int(HORIZONTAL_TILES_NUMBER)):
|
||||
board.append([random.randint(0, 3)])
|
||||
for j in range(int(VERTICAL_TILES_NUMBER)):
|
||||
board[i].append(random.randint(0, 3))
|
||||
return board
|
38
Main/constants.py
Normal file
@ -0,0 +1,38 @@
|
||||
#constants
|
||||
|
||||
|
||||
# display size in pixels
|
||||
DISPLAY_SIZE_HORIZONTAL = 600
|
||||
DISPLAY_SIZE_VERTICAL = 600
|
||||
|
||||
#TILE DIVIDER
|
||||
TILE_DIVIDER = 100
|
||||
|
||||
# number of tiles
|
||||
HORIZONTAL_TILES_NUMBER = DISPLAY_SIZE_HORIZONTAL/TILE_DIVIDER
|
||||
VERTICAL_TILES_NUMBER = DISPLAY_SIZE_VERTICAL/TILE_DIVIDER
|
||||
|
||||
#TILE_SIZE
|
||||
TILE_SIZE = DISPLAY_SIZE_HORIZONTAL/HORIZONTAL_TILES_NUMBER
|
||||
|
||||
#colors
|
||||
WHITE = (255, 255, 255) #SUPER UPRAWA
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0) #DO ZASIANIA
|
||||
YELLOW = (255, 255, 0) #DO PODLANIA
|
||||
GREEN = (0, 255, 0) #DO SCIECIA
|
||||
|
||||
|
||||
#tractor const
|
||||
TRACTOR_HORIZONTAL_LOCATION = DISPLAY_SIZE_HORIZONTAL/2
|
||||
TRACTOR_VERTICAL_LOCATION = DISPLAY_SIZE_VERTICAL/2
|
||||
|
||||
TRACTOR_WIDTH = TILE_SIZE
|
||||
TRACTOR_HEIGHT = TILE_SIZE
|
||||
|
||||
#FRAMES PER SECOND
|
||||
FPS = 144
|
||||
|
||||
|
||||
|
||||
|
80
Main/main.py
Normal file
@ -0,0 +1,80 @@
|
||||
import pygame
|
||||
# wersja 1.05
|
||||
|
||||
import Board
|
||||
from constants import *
|
||||
|
||||
pygame.init()
|
||||
|
||||
# display size in pixels
|
||||
display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICAL))
|
||||
# program name
|
||||
pygame.display.set_caption('Tryryryry')
|
||||
|
||||
working = True
|
||||
|
||||
tractor_horizontal_location = TRACTOR_HORIZONTAL_LOCATION
|
||||
tractor_vertical_location = TRACTOR_VERTICAL_LOCATION
|
||||
|
||||
horizontal_change = 0
|
||||
vertical_change = 0
|
||||
|
||||
change_tile = False
|
||||
board = Board.generate()
|
||||
color = BLACK
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
while working:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
working = False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
print(tractor_horizontal_location, " ", tractor_vertical_location)
|
||||
if event.key == pygame.K_LEFT and tractor_horizontal_location > 0:
|
||||
horizontal_change = -TRACTOR_WIDTH
|
||||
vertical_change = 0
|
||||
elif event.key == pygame.K_RIGHT and tractor_horizontal_location < DISPLAY_SIZE_HORIZONTAL - TRACTOR_WIDTH:
|
||||
horizontal_change = TRACTOR_WIDTH
|
||||
vertical_change = 0
|
||||
elif event.key == pygame.K_UP and tractor_vertical_location > 0:
|
||||
vertical_change = -TRACTOR_HEIGHT
|
||||
horizontal_change = 0
|
||||
elif event.key == pygame.K_DOWN and tractor_vertical_location < DISPLAY_SIZE_VERTICAL - TRACTOR_HEIGHT:
|
||||
vertical_change = TRACTOR_HEIGHT
|
||||
horizontal_change = 0
|
||||
elif event.key == pygame.K_SPACE:
|
||||
change_tile = True
|
||||
|
||||
tractor_horizontal_location += horizontal_change
|
||||
tractor_vertical_location += vertical_change
|
||||
display.fill(WHITE)
|
||||
for i in range(int(HORIZONTAL_TILES_NUMBER)):
|
||||
for j in range(int(VERTICAL_TILES_NUMBER)):
|
||||
if change_tile and i * TILE_SIZE == tractor_horizontal_location and j * TILE_SIZE == tractor_vertical_location:
|
||||
board[i][j] = 4
|
||||
change_tile = False
|
||||
if board[i][j] == 0:
|
||||
color = WHITE
|
||||
elif board[i][j] == 1:
|
||||
color = RED
|
||||
elif board[i][j] == 2:
|
||||
color = YELLOW
|
||||
elif board[i][j] == 3:
|
||||
color = GREEN
|
||||
elif board[i][j] == 4:
|
||||
color = BLACK
|
||||
|
||||
pygame.draw.rect(display, color,
|
||||
[i * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, TILE_SIZE])
|
||||
|
||||
pygame.draw.rect(display, BLACK,
|
||||
[tractor_horizontal_location, tractor_vertical_location, TRACTOR_WIDTH, TRACTOR_HEIGHT])
|
||||
|
||||
pygame.display.update()
|
||||
clock.tick(FPS)
|
||||
horizontal_change = 0
|
||||
vertical_change = 0
|
||||
# commit test 2
|
||||
|
||||
pygame.quit()
|
||||
quit()
|
142
Tractor.py
@ -1,142 +0,0 @@
|
||||
from TractorLoad import TillageUnit
|
||||
from constants import HORIZONTAL_TILES_NUMBER, VERTICAL_TILES_NUMBER
|
||||
|
||||
|
||||
class Tractor:
|
||||
def __init__(self, horizontal_index, vertical_index, hitch, header, autodrive, direction):
|
||||
self.__horizontal_index = horizontal_index
|
||||
self.__vertical_index = vertical_index
|
||||
self.__hitch = hitch
|
||||
self.__header = header
|
||||
self.__autodrive = autodrive
|
||||
self.__fuel_tank = 100
|
||||
self.__engineWorking = False
|
||||
self.__direction = direction
|
||||
|
||||
@property
|
||||
def horizontal_index(self):
|
||||
return self.__horizontal_index
|
||||
|
||||
def __horizontal_index(self, horizontal_index):
|
||||
if 1 <= horizontal_index < HORIZONTAL_TILES_NUMBER:
|
||||
if self.__engineWorking:
|
||||
self.__horizontal_index = horizontal_index
|
||||
|
||||
@property
|
||||
def vertical_index(self):
|
||||
return self.__vertical_index
|
||||
|
||||
def __vertical_index(self, vertical_index):
|
||||
if 1 <= self.__vertical_index < VERTICAL_TILES_NUMBER-1:
|
||||
if self.__engineWorking:
|
||||
self.__vertical_index = vertical_index
|
||||
|
||||
@property
|
||||
def hitch(self):
|
||||
return self.__hitch
|
||||
|
||||
@hitch.setter
|
||||
def hitch(self, hitch):
|
||||
if hitch == "Crop Trailer" or isinstance(hitch, TillageUnit) or hitch == "Nothing":
|
||||
self.__hitch = hitch
|
||||
|
||||
@property
|
||||
def fuel_tank(self):
|
||||
return self.__fuel_tank
|
||||
|
||||
def __fuel_tank(self, fuel_tank):
|
||||
if 0 < fuel_tank < 100:
|
||||
self.__fuel_tank = fuel_tank
|
||||
|
||||
def fill_tank(self):
|
||||
self.__fuel_tank = 100
|
||||
|
||||
def reduce_fuel(self):
|
||||
if 0 < self.fuel_tank <= 100:
|
||||
self.__fuel_tank = self.__fuel_tank - 0.1
|
||||
if self.__fuel_tank <= 0:
|
||||
self.__engineWorking = False
|
||||
|
||||
@property
|
||||
def header(self):
|
||||
return self.__header
|
||||
|
||||
@header.setter
|
||||
def header(self, header):
|
||||
if header is True or header is False:
|
||||
self.__header = header
|
||||
|
||||
@property
|
||||
def engineWorking(self):
|
||||
return self.__engineWorking
|
||||
|
||||
def turnOnEngine(self):
|
||||
if self.__fuel_tank > 0:
|
||||
self.__engineWorking = True
|
||||
else:
|
||||
print("noFuel")
|
||||
|
||||
def turnOffEngine(self):
|
||||
self.__engineWorking = False
|
||||
|
||||
@property
|
||||
def autodrive(self):
|
||||
return self.__autodrive
|
||||
|
||||
@autodrive.setter
|
||||
def autodrive(self, autodrive):
|
||||
if isinstance(autodrive, bool):
|
||||
self.__autodrive = autodrive
|
||||
|
||||
@property
|
||||
def direction(self):
|
||||
return self.__direction
|
||||
|
||||
@direction.setter
|
||||
def direction(self, direction):
|
||||
self.__direction = direction
|
||||
|
||||
|
||||
def drive(self):
|
||||
mRange = 1
|
||||
if not self.__engineWorking:
|
||||
return
|
||||
if self.__direction == "UP" and self.vertical_index > 0:
|
||||
self.__vertical_index += - mRange
|
||||
elif self.__direction == "DOWN" and self.vertical_index < VERTICAL_TILES_NUMBER-1:
|
||||
self.__vertical_index += mRange
|
||||
elif self.__direction == "RIGHT" and self.horizontal_index < HORIZONTAL_TILES_NUMBER-1:
|
||||
self.__horizontal_index += mRange
|
||||
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()
|
||||
|
@ -1,45 +0,0 @@
|
||||
from TractorLoad import TillageUnit
|
||||
|
||||
|
||||
def changeFieldState(field, tractor):
|
||||
if tractor.header and tractor.hitch == "Crop Trailer" and field.state == "toCut":
|
||||
return "toPlow"
|
||||
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Nothing" and field.state == "toPlow":
|
||||
return "toWater"
|
||||
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Water" and field.state == "toWater":
|
||||
return "toSeed"
|
||||
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Seeds" and field.state == "toSeed":
|
||||
return "toFertilize"
|
||||
|
||||
elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Fertilizer" and field.state == "toFertilize":
|
||||
return "toCut"
|
||||
|
||||
|
||||
def autoToolsChange(tractor, tillageUnit, toolCounter):
|
||||
if tractor.horizontal_index == 0 and tractor.vertical_index == 0:
|
||||
toolCounter = (toolCounter + 1) % 5
|
||||
tractor, tillageUnit = chooseToolset(tractor, tillageUnit, toolCounter)
|
||||
tractor.fill_tank()
|
||||
return tractor, tillageUnit, toolCounter
|
||||
|
||||
def chooseToolset(tractor, tillageUnit, set):
|
||||
if set == 0:
|
||||
tractor.hitch = tillageUnit
|
||||
tractor.hitch.load = "Nothing"
|
||||
if set == 1:
|
||||
tractor.hitch = tillageUnit
|
||||
tractor.hitch.load = "Water"
|
||||
if set == 2:
|
||||
tractor.hitch = tillageUnit
|
||||
tractor.hitch.load = "Seeds"
|
||||
if set == 3:
|
||||
tractor.hitch = tillageUnit
|
||||
tractor.hitch.load = "Fertilizer"
|
||||
if set == 4:
|
||||
tractor.header = True
|
||||
tractor.hitch = "Crop Trailer"
|
||||
|
||||
return tractor, tillageUnit
|
@ -1,12 +0,0 @@
|
||||
class TillageUnit:
|
||||
def __init__(self, __load):
|
||||
self.__load = __load
|
||||
|
||||
@property
|
||||
def load(self):
|
||||
return self.__load
|
||||
|
||||
@load.setter
|
||||
def load(self, load):
|
||||
if load == "Fertilizer" or load == "Seeds" or load == "Water" or load == "Nothing":
|
||||
self.__load = load
|
@ -1,10 +0,0 @@
|
||||
import random
|
||||
|
||||
def checkConditions():
|
||||
weather = random.choice(['Clear Sky', 'Cloudy', 'Rainy', 'Hail'])
|
||||
day_time = random.choice(['Day', 'Night'])
|
||||
temperature = random.choice(['Freezing', 'Cold', 'Mild', 'Hot'])
|
||||
wind = random.choice(['Windless', 'Strong Wind', 'Gale'])
|
||||
humidy = random.choice(['Low', 'High'])
|
||||
|
||||
return weather, day_time, temperature, wind, humidy
|
@ -1,7 +0,0 @@
|
||||
from constants import ANIMATION_PART
|
||||
|
||||
def animationsOn():
|
||||
ANIMATION_PART = 11
|
||||
|
||||
def animationsOff():
|
||||
ANIMATION_PART = 1
|
@ -1,23 +0,0 @@
|
||||
# Pythono3 code to rename multiple
|
||||
# files in a directory or folder
|
||||
|
||||
# importing os module
|
||||
import os
|
||||
|
||||
|
||||
path = 'neural_network\\images\\potatoes\\'
|
||||
def main():
|
||||
for count, filename in enumerate(os.listdir(path)):
|
||||
dst = 'potato' + str(count) + ".jpg"
|
||||
src = path + filename
|
||||
dst = path + dst
|
||||
|
||||
# rename() function will
|
||||
# rename all the files
|
||||
os.rename(src, dst)
|
||||
|
||||
|
||||
# Driver Code
|
||||
if __name__ == '__main__':
|
||||
# Calling main() function
|
||||
main()
|
42
constants.py
@ -1,42 +0,0 @@
|
||||
#constants
|
||||
|
||||
# display size in pixels
|
||||
from math import ceil, floor
|
||||
|
||||
DISPLAY_SIZE_HORIZONTAL = 1600
|
||||
DISPLAY_SIZE_VERTICAL = 800
|
||||
|
||||
#TILE DIVIDER = TILE SIZE
|
||||
TILE_SIZE = 100
|
||||
|
||||
# number of tiles
|
||||
HORIZONTAL_TILES_NUMBER = floor((DISPLAY_SIZE_HORIZONTAL)/TILE_SIZE)
|
||||
VERTICAL_TILES_NUMBER = floor((DISPLAY_SIZE_VERTICAL-200)/TILE_SIZE)
|
||||
|
||||
#TILE_SIZE
|
||||
|
||||
#colors
|
||||
WHITE = (255, 255, 255) #SUPER UPRAWA
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0) #DO ZASIANIA
|
||||
YELLOW = (255, 255, 0) #DO PODLANIA
|
||||
GREEN = (0, 255, 0) #DO SCIECIA
|
||||
|
||||
|
||||
#tractor const
|
||||
|
||||
TRACTOR_WIDTH = TILE_SIZE
|
||||
TRACTOR_HEIGHT = TILE_SIZE
|
||||
|
||||
#FRAMES PER SECOND
|
||||
FPS = 5
|
||||
|
||||
#ANIMATION_PART
|
||||
|
||||
ANIMATION_PART = 1
|
||||
|
||||
|
||||
TRACTOR_DIRECTION_RIGHT = 2
|
||||
TRACTOR_DIRECTION_UP = 1
|
||||
TRACTOR_DIRECTION_DOWN = 3
|
||||
TRACTOR_DIRECTION_LEFT = 4
|
@ -1,55 +0,0 @@
|
||||
def convert(field_state, weather, day_time, temperature, wind, humidy):
|
||||
field_value = 0
|
||||
weather_value = 0
|
||||
day_time_value = 0
|
||||
temperature_value = 0
|
||||
wind_value = 0
|
||||
humidy_value = 0
|
||||
|
||||
if field_state == "toPlow":
|
||||
field_value = 0
|
||||
elif field_state == "toWater":
|
||||
field_value = 1
|
||||
elif field_state == "toSeed":
|
||||
field_value = 2
|
||||
elif field_state == "toFertilize":
|
||||
field_value = 3
|
||||
elif field_state == "toCut":
|
||||
field_value = 4
|
||||
|
||||
if weather == "Clear Sky":
|
||||
weather_value = 0
|
||||
elif weather == "Cloudy":
|
||||
weather_value = 1
|
||||
elif weather == "Rainy":
|
||||
weather_value = 2
|
||||
elif weather == "Hail":
|
||||
weather_value = 3
|
||||
|
||||
if day_time == "Night":
|
||||
day_time_value = 0
|
||||
elif day_time == "Day":
|
||||
day_time_value = 1
|
||||
|
||||
if temperature == "Freezing":
|
||||
temperature_value = 0
|
||||
elif temperature == "Cold":
|
||||
temperature_value = 1
|
||||
elif temperature == "Mild":
|
||||
temperature_value = 2
|
||||
elif temperature == "Hot":
|
||||
temperature_value = 3
|
||||
|
||||
if wind == "Windless":
|
||||
wind_value = 0
|
||||
elif wind == "Strong Wind":
|
||||
wind_value = 1
|
||||
elif wind == "Gale":
|
||||
wind_value = 2
|
||||
|
||||
if humidy == "Low":
|
||||
humidy_value = 0
|
||||
elif humidy == "High":
|
||||
humidy_value = 1
|
||||
|
||||
return field_value, weather_value, day_time_value, temperature_value, wind_value, humidy_value
|
@ -1,55 +0,0 @@
|
||||
import pandas
|
||||
from sklearn import tree
|
||||
import pydotplus
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.image as pltimg
|
||||
import joblib
|
||||
|
||||
df = pandas.read_csv("treedata\\data.csv")
|
||||
|
||||
#Map text values to number values
|
||||
d = {'toPlow' : 0, 'toWater' : 1, 'toSeed' : 2, 'toFertilize' : 3, 'toCut' : 4}
|
||||
df['Field'] = df['Field'].map(d)
|
||||
|
||||
d = {'Night' : 0, 'Day' : 1}
|
||||
df['Day Time'] = df['Day Time'].map(d)
|
||||
|
||||
d = {'Clear Sky' : 0, 'Cloudy' : 1, 'Rainy' : 2, 'Hail': 3}
|
||||
df['Weather'] = df['Weather'].map(d)
|
||||
|
||||
d = {'Freezing' : 0, 'Cold' : 1, 'Mild': 2, 'Hot': 3}
|
||||
df['Temperature'] = df['Temperature'].map(d)
|
||||
|
||||
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
|
||||
df['Wind'] = df['Wind'].map(d)
|
||||
|
||||
d = {'Low': 0, 'High': 1}
|
||||
df['Humidy'] = df['Humidy'].map(d)
|
||||
|
||||
d = {'Wait' : 0, 'Make Action' : 1}
|
||||
df['Decision'] = df['Decision'].map(d)
|
||||
|
||||
#Separate the feature columns from targert columns
|
||||
|
||||
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
||||
|
||||
X = df[features]
|
||||
y = df['Decision']
|
||||
|
||||
dtree = DecisionTreeClassifier()
|
||||
dtree = dtree.fit(X, y)
|
||||
joblib.dump(dtree, 'treedata\\DecisionTree.pkl')
|
||||
|
||||
|
||||
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
|
||||
graph = pydotplus.graph_from_dot_data(data)
|
||||
graph.write_png('treedata\\mydecisiontree.png')
|
||||
|
||||
img = pltimg.imread('treedata\\mydecisiontree.png')
|
||||
imgplot = plt.imshow(img)
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
#print(dtree.predict([[0, 1, 0, 0, 0, 1]]))
|
@ -1,48 +0,0 @@
|
||||
import os
|
||||
import cv2
|
||||
import matplotlib
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
from matplotlib.pyplot import imshow
|
||||
|
||||
path_potatoes = 'neural_network\\images\\potatoes'
|
||||
path_beetroot = 'neural_network\\images\\beetroot'
|
||||
size = 100
|
||||
|
||||
#POTATOES
|
||||
image_data = []
|
||||
label_data = []
|
||||
for img in os.listdir(path_potatoes):
|
||||
pic = cv2.imread(os.path.join(path_potatoes,img))
|
||||
pic = cv2.cvtColor(pic,cv2.COLOR_BGR2RGB)
|
||||
pic = cv2.resize(pic,(size,size))
|
||||
image_data.append([pic])
|
||||
label_data.append(1)
|
||||
|
||||
#np.save(os.path.join('neural_network','potatoes-dataset'),np.array(training_data_potatoes))
|
||||
|
||||
#saved_potatoes = np.load(os.path.join('neural_network','potatoes-dataset.npy'))
|
||||
|
||||
#BEETROOT
|
||||
for img in os.listdir(path_beetroot):
|
||||
pic = cv2.imread(os.path.join(path_beetroot,img))
|
||||
pic = cv2.cvtColor(pic,cv2.COLOR_BGR2RGB)
|
||||
pic = cv2.resize(pic,(size,size))
|
||||
image_data.append([pic])
|
||||
label_data.append(0)
|
||||
|
||||
#np.save(os.path.join('neural_network','beetroot-dataset'),np.array(training_data_beetroot))
|
||||
|
||||
#saved_potatoes = np.load(os.path.join('neural_network','beetroot-dataset.npy'))
|
||||
|
||||
np.save(os.path.join('neural_network','image-dataset'),np.array(image_data))
|
||||
np.save(os.path.join('neural_network','label-dataset'),np.array(label_data))
|
||||
|
||||
saved_images = np.load(os.path.join('neural_network','image-dataset.npy'))
|
||||
|
||||
print(saved_images.shape)
|
||||
|
||||
plt.imshow(saved_images[0].reshape(size,size,3))
|
||||
plt.imshow(np.array(image_data[0]).reshape(size,size,3))
|
||||
plt.show()
|
@ -1,122 +0,0 @@
|
||||
from itertools import product
|
||||
import csv
|
||||
|
||||
# stan pola: toCut, toPlow, toWater, toSeed, toFertilize
|
||||
# pora dnia: dzień, noc
|
||||
# pogoda: sunny, cloudy, rainy, hail
|
||||
# temperatura: freezing, cold, mild, hot
|
||||
# wiatr: windless, strong wind, gale
|
||||
# humidy: low, high
|
||||
|
||||
|
||||
field_states = ['toPlow', 'toWater', 'toSeed', 'toFertilize', 'toCut']
|
||||
day_time = ['Day', 'Night']
|
||||
weather = ['Clear Sky', 'Cloudy', 'Rainy', 'Hail']
|
||||
temperature = ['Freezing', 'Cold', 'Mild', 'Hot']
|
||||
wind = ['Windless', 'Strong Wind', 'Gale']
|
||||
humidy = ['Low', 'High']
|
||||
|
||||
output = list(product(field_states, day_time, weather, temperature, wind, humidy))
|
||||
|
||||
dict = []
|
||||
|
||||
for x in range(len(output)):
|
||||
if x % 3 == 0:
|
||||
while True:
|
||||
mField = output[x][0]
|
||||
mDay_time = output[x][1]
|
||||
mWeather = output[x][2]
|
||||
mTemperature = output[x][3]
|
||||
mWind = output[x][4]
|
||||
mHumidy = output[x][5]
|
||||
mDecision = 'null'
|
||||
|
||||
# pora dnia: dzień 2, noc -2
|
||||
# pogoda: sunny+3, cloudy+3, rainy-2, hail-5
|
||||
# temperatura: freezing -3, cold-1, mild+4, hot+2
|
||||
# wiatr: windless +2, strong wind-1, gale-3
|
||||
# humidy: low+2, high-3
|
||||
|
||||
if mDay_time == 'Day':
|
||||
valDay_time = 2
|
||||
else:
|
||||
valDay_time = -3
|
||||
|
||||
if mWeather == 'Clear Sky' or 'Cloudy':
|
||||
valWeather = 3
|
||||
elif mWeather == 'Rainy':
|
||||
valWeather = -2
|
||||
else:
|
||||
valWeather = -5
|
||||
|
||||
if mTemperature == 'Freezing':
|
||||
valTemperature = -3
|
||||
elif mTemperature == 'Cold':
|
||||
valTemperature = -1
|
||||
elif mTemperature == 'Mild':
|
||||
valTemperature = 4
|
||||
else:
|
||||
valTemperature = 2
|
||||
|
||||
if mWind == 'Windless':
|
||||
valWind = +2
|
||||
elif mWind == 'Strong Wind':
|
||||
valWind = -1
|
||||
else:
|
||||
valWind = -3
|
||||
|
||||
if humidy == 'Low':
|
||||
valHumidy = 2
|
||||
else:
|
||||
valHumidy = -2
|
||||
|
||||
result = valDay_time + valWeather + valTemperature + valWind + valHumidy
|
||||
if result >= 0:
|
||||
mDecision = "Make Action"
|
||||
else:
|
||||
mDecision = "Wait"
|
||||
|
||||
#Special conditions
|
||||
if mDay_time == 'Night' and (mField == 'toWater' or mField == 'toCut'):
|
||||
mDecision = "Make Action"
|
||||
break
|
||||
else:
|
||||
mDecision = "Wait"
|
||||
|
||||
if mWeather == 'Rainy' and (mField == 'toPlow' or mField == 'toSeed'):
|
||||
mDecision = "Make Action"
|
||||
break
|
||||
else:
|
||||
mDecision = 'Wait'
|
||||
|
||||
if mWeather == 'Hail':
|
||||
mDecision = 'Wait'
|
||||
break
|
||||
|
||||
if mTemperature == 'Freezing':
|
||||
mDecision = 'Wait'
|
||||
break
|
||||
|
||||
if mWind == 'Gale':
|
||||
mDecision = 'Wait'
|
||||
break
|
||||
|
||||
if mHumidy == 'High' and mField == 'toCut':
|
||||
mDecision = "Wait"
|
||||
break
|
||||
else:
|
||||
mDecision = "Make Action"
|
||||
|
||||
break
|
||||
|
||||
dict.append({'Field': mField, 'Day Time': mDay_time, 'Weather': mWeather,
|
||||
'Temperature': mTemperature, 'Wind': mWind, 'Humidy': mHumidy, 'Decision': mDecision})
|
||||
|
||||
fields = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy', 'Decision']
|
||||
|
||||
filename = "treedata\\data.csv"
|
||||
|
||||
with open(filename, 'w') as csvfile:
|
||||
writer = csv.DictWriter(csvfile, fieldnames=fields)
|
||||
writer.writeheader()
|
||||
writer.writerows(dict)
|
102
drawUI.py
@ -1,102 +0,0 @@
|
||||
from TractorLoad import TillageUnit
|
||||
from constants import *
|
||||
from images import *
|
||||
import pygame
|
||||
|
||||
|
||||
def drawUI(board, display, tractor, direction, tillageUnit, field, animationSpeed, weather, day_time, temperature, wind, humidy, decision):
|
||||
if animationSpeed == 1:
|
||||
display.fill(WHITE)
|
||||
makeField(board, display)
|
||||
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction, 0)
|
||||
drawInfo(display, tractor, tillageUnit, field, direction, weather, day_time, temperature, wind, humidy, decision)
|
||||
pygame.display.update()
|
||||
for i in range(1, animationSpeed):
|
||||
display.fill(WHITE)
|
||||
makeField(board, display)
|
||||
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction, i)
|
||||
drawInfo(display, tractor, tillageUnit, field, direction)
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
def drawInfo(display, tractor, tillageUnit, field, direction, weather, day_time, temperature, wind, humidy, decision):
|
||||
myfont = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
hitches = tractor.hitch
|
||||
if isinstance(tractor.hitch, TillageUnit):
|
||||
hitches = "Tillage Unit"
|
||||
if decision == [0]:
|
||||
decision = "Wait"
|
||||
else:
|
||||
decision = "Make Action"
|
||||
|
||||
text = f"(Q)Hitches: {hitches} (W)Header: {tractor.header} (R)Engine working: {tractor.engineWorking} (T)Autodrive: {tractor.autodrive} Fuel: {round(tractor.fuel_tank,1)}"
|
||||
textsurface = myfont.render(text, False, (0, 0, 0))
|
||||
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 200)))
|
||||
|
||||
text = f"(E)Tillage Unit Load: {tillageUnit.load} (^) Direction: {direction}"
|
||||
textsurface = myfont.render(text, False, (0, 0, 0))
|
||||
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 150)))
|
||||
|
||||
text = f"Current field: {field.horizontal_index/100, field.vertical_index/100, field.state} Action: {decision}"
|
||||
textsurface = myfont.render(text, False, (0, 0, 0))
|
||||
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 100)))
|
||||
|
||||
text = f"Weather: {weather} Day Time: {day_time} Temperature: {temperature} Wind: {wind} Humidy: {humidy}"
|
||||
textsurface = myfont.render(text, False, (0, 0, 0))
|
||||
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 50)))
|
||||
|
||||
|
||||
def makeField(board, screen: pygame.Surface):
|
||||
for i in range(int(HORIZONTAL_TILES_NUMBER)):
|
||||
for j in range(int(VERTICAL_TILES_NUMBER)):
|
||||
field = board[i][j]
|
||||
|
||||
pos_x = i * TILE_SIZE + TILE_SIZE // 2
|
||||
pos_y = j * TILE_SIZE + TILE_SIZE // 2
|
||||
|
||||
if field.state == "toWater":
|
||||
to_water_rect.center = (pos_x, pos_y)
|
||||
screen.blit(to_water, to_water_rect)
|
||||
elif field.state == "toPlow":
|
||||
to_plow_rect.center = (pos_x, pos_y)
|
||||
screen.blit(to_plow, to_plow_rect)
|
||||
elif field.state == "toSeed":
|
||||
to_seed_rect.center = (pos_x, pos_y)
|
||||
screen.blit(to_seed, to_seed_rect)
|
||||
elif field.state == "toCut":
|
||||
to_cut_rect.center = (pos_x, pos_y)
|
||||
screen.blit(to_cut, to_cut_rect)
|
||||
elif field.state == "toFertilize":
|
||||
to_fertilizer_rect.center = (pos_x, pos_y)
|
||||
screen.blit(to_fertilizer, to_fertilizer_rect)
|
||||
|
||||
elif field.state == "TOOLS_FIELD":
|
||||
tools_rect.center = (pos_x, pos_y)
|
||||
screen.blit(tools, tools_rect)
|
||||
|
||||
elif field.state == "FUEL_FIELD":
|
||||
fuel_rect.center = (pos_x, pos_y)
|
||||
screen.blit(fuel, fuel_rect)
|
||||
|
||||
|
||||
def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertical_index, direction, i):
|
||||
tractor_pic = tractor_up
|
||||
horizontal = tractor_horizontal_index * TILE_SIZE
|
||||
vertical = tractor_vertical_index * TILE_SIZE
|
||||
|
||||
i = i/ANIMATION_PART
|
||||
|
||||
if direction == "UP":
|
||||
tractor_pic = tractor_up
|
||||
vertical = vertical - (TILE_SIZE * i)
|
||||
if direction == "DOWN":
|
||||
tractor_pic = tractor_down
|
||||
vertical = vertical + (TILE_SIZE * i)
|
||||
if direction == "LEFT":
|
||||
tractor_pic = tractor_left
|
||||
horizontal = horizontal - (TILE_SIZE * i)
|
||||
if direction == "RIGHT":
|
||||
tractor_pic = tractor_right
|
||||
horizontal = horizontal + (TILE_SIZE * i)
|
||||
|
||||
screen.blit(tractor_pic, (horizontal, vertical))
|
30
driving.py
@ -1,30 +0,0 @@
|
||||
from drawUI import *
|
||||
import pygame
|
||||
|
||||
|
||||
# def autodrive(tractor, direction, comeback):
|
||||
# if tractor.autodrive:
|
||||
# if not comeback:
|
||||
# if direction == "RIGHT" and tractor.horizontal_index == HORIZONTAL_TILES_NUMBER - 1:
|
||||
# direction = "DOWN"
|
||||
# elif direction == "DOWN" and tractor.horizontal_index == HORIZONTAL_TILES_NUMBER - 1:
|
||||
# direction = "LEFT"
|
||||
# elif direction == "LEFT" and tractor.horizontal_index == 0:
|
||||
# direction = "DOWN"
|
||||
# elif direction == "DOWN" and tractor.horizontal_index == 0:
|
||||
# direction = "RIGHT"
|
||||
# else:
|
||||
# direction = "UP"
|
||||
# return direction
|
||||
|
||||
#
|
||||
# def isComebackTime(tractor, direction, comeback):
|
||||
# if tractor.vertical_index == 5 and tractor.horizontal_index == 0:
|
||||
# comeback = True
|
||||
# print(comeback)
|
||||
# direction = "UP"
|
||||
# if tractor.vertical_index == 0 and tractor.horizontal_index == 0:
|
||||
# comeback = False
|
||||
# print(comeback)
|
||||
# direction = "RIGHT"
|
||||
# return comeback, direction
|
48
images.py
@ -1,48 +0,0 @@
|
||||
import pygame
|
||||
|
||||
from constants import TILE_SIZE
|
||||
|
||||
tractor_up_image = pygame.image.load("resources/tractor_up.png")
|
||||
tractor_up = pygame.transform.scale(tractor_up_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_right_image = pygame.image.load("resources/tractor_right.png")
|
||||
tractor_right = pygame.transform.scale(tractor_right_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_down_image = pygame.image.load("resources/tractor_down.png")
|
||||
tractor_down = pygame.transform.scale(tractor_down_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_left_image = pygame.image.load("resources/tractor_left.png")
|
||||
tractor_left = pygame.transform.scale(tractor_left_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
to_water_image = pygame.image.load("resources/to_water.png")
|
||||
to_water = pygame.transform.scale(to_water_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
to_plow_image = pygame.image.load("resources/to_plow.png")
|
||||
to_plow = pygame.transform.scale(to_plow_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
to_seed_image = pygame.image.load("resources/to_seed.png")
|
||||
to_seed = pygame.transform.scale(to_seed_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
to_cut_image = pygame.image.load("resources/to_cut.png")
|
||||
to_cut = pygame.transform.scale(to_cut_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
to_fertilizer_image = pygame.image.load("resources/to_fertilizer.png")
|
||||
to_fertilizer = pygame.transform.scale(to_fertilizer_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
fuel_image = pygame.image.load("resources/fuel.png")
|
||||
fuel = pygame.transform.scale(fuel_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tools_image = pygame.image.load("resources/tools.png")
|
||||
tools = pygame.transform.scale(tools_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_up_rect = tractor_up.get_rect()
|
||||
tractor_right_rect = tractor_right.get_rect()
|
||||
tractor_down_rect = tractor_down.get_rect()
|
||||
tractor_left_rect = tractor_left.get_rect()
|
||||
to_water_rect = to_water.get_rect()
|
||||
to_plow_rect = to_plow.get_rect()
|
||||
to_seed_rect = to_seed.get_rect()
|
||||
to_cut_rect = to_cut.get_rect()
|
||||
to_fertilizer_rect = to_fertilizer.get_rect()
|
||||
fuel_rect = fuel.get_rect()
|
||||
tools_rect = tools.get_rect()
|
93
main.py
@ -1,93 +0,0 @@
|
||||
import Board
|
||||
import FindPath
|
||||
import TractorAction
|
||||
import WeatherConditions
|
||||
import convertToPrediction
|
||||
import drawUI
|
||||
import Graphsearch as graph
|
||||
from Tractor import Tractor
|
||||
from TractorLoad import TillageUnit
|
||||
from constants import *
|
||||
from manualSteering import manualSteeringDriver
|
||||
import joblib
|
||||
import pygame
|
||||
|
||||
pygame.init()
|
||||
|
||||
|
||||
display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICAL))
|
||||
pygame.display.set_caption('Tractor')
|
||||
|
||||
working = True
|
||||
autoAction = True
|
||||
|
||||
animationSpeed = ANIMATION_PART
|
||||
|
||||
hitchCounter = 0
|
||||
loadCounter = 0
|
||||
toolCounter = - 1
|
||||
|
||||
#środowisko
|
||||
board = Board.generate()
|
||||
#agent
|
||||
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()
|
||||
|
||||
#zjawiska pogodowe
|
||||
weather, day_time, temperature, wind, humidy = WeatherConditions.checkConditions()
|
||||
|
||||
weatherTimer = 0
|
||||
|
||||
#pętla główna działania programu
|
||||
while working:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
working = False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
hitchCounter, loadCounter = \
|
||||
manualSteeringDriver(event, board, tractor, hitchCounter, tillageUnit, loadCounter)
|
||||
|
||||
field = board[tractor.horizontal_index][tractor.vertical_index]
|
||||
|
||||
if weatherTimer == 5:
|
||||
weatherTimer = 0
|
||||
weather, day_time, temperature, wind, humidy = WeatherConditions.checkConditions()
|
||||
|
||||
#sformulowanie informacji dla drzewa decyzyjnego / lista informacji o aktualnej pogodzie i polu
|
||||
question = [convertToPrediction.convert(field.state, weather, day_time, temperature, wind, humidy)]
|
||||
|
||||
dtree = joblib.load('treedata\\DecisionTree.pkl')
|
||||
|
||||
#przekazanie informacji do drzewa
|
||||
decision = dtree.predict(question)
|
||||
|
||||
if tractor.autodrive:
|
||||
# A*************
|
||||
if not move_list and decision == 1:
|
||||
field.state = TractorAction.changeFieldState(field, tractor)
|
||||
istate = graph.Istate(tractor.direction, tractor.horizontal_index, tractor.vertical_index)
|
||||
move_list = graph.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 field.horizontal_index == 0 and field.vertical_index == 0 and not move_list:
|
||||
tractor, tillageUnit, toolCounter = TractorAction.autoToolsChange(tractor, tillageUnit, toolCounter)
|
||||
|
||||
drawUI.drawUI(board, display, tractor, tractor.direction, tillageUnit, field, animationSpeed, weather, day_time,
|
||||
temperature, wind, humidy, decision)
|
||||
|
||||
clock.tick(FPS)
|
||||
weatherTimer = weatherTimer + 1
|
||||
|
||||
pygame.quit()
|
||||
quit()
|
@ -1,62 +0,0 @@
|
||||
from TractorAction import changeFieldState
|
||||
from constants import *
|
||||
import pygame
|
||||
|
||||
|
||||
def manualSteeringDriver(event, board, tractor, hitchCounter, tillageUnit, loadCounter):
|
||||
if event.key == pygame.K_SPACE:
|
||||
field = board[tractor.horizontal_index][tractor.vertical_index]
|
||||
field.state = changeFieldState(field, tractor)
|
||||
if event.key == pygame.K_q:
|
||||
hitchCounter = (hitchCounter + 1) % 3
|
||||
if hitchCounter == 0:
|
||||
tractor.hitch = "Crop Trailer"
|
||||
if hitchCounter == 1:
|
||||
tractor.hitch = tillageUnit
|
||||
if hitchCounter == 2:
|
||||
tractor.hitch = "Nothing"
|
||||
if event.key == pygame.K_w:
|
||||
if tractor.header:
|
||||
tractor.header = False
|
||||
else:
|
||||
tractor.header = True
|
||||
if event.key == pygame.K_e:
|
||||
loadCounter = (loadCounter + 1) % 4
|
||||
if loadCounter == 0:
|
||||
tillageUnit.load = "Nothing"
|
||||
elif loadCounter == 1:
|
||||
tillageUnit.load = "Seeds"
|
||||
elif loadCounter == 2:
|
||||
tillageUnit.load = "Water"
|
||||
elif loadCounter == 3:
|
||||
tillageUnit.load = "Fertilizer"
|
||||
|
||||
if event.key == pygame.K_m:
|
||||
tractor.drive()
|
||||
|
||||
if event.key == pygame.K_r:
|
||||
if tractor.engineWorking:
|
||||
tractor.turnOffEngine()
|
||||
else:
|
||||
tractor.turnOnEngine()
|
||||
if event.key == pygame.K_t:
|
||||
if tractor.autodrive:
|
||||
tractor.autodrive = False
|
||||
|
||||
else:
|
||||
tractor.autodrive = True
|
||||
|
||||
manualTurning(event, tractor)
|
||||
return hitchCounter, loadCounter
|
||||
|
||||
|
||||
def manualTurning(event, tractor):
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT and tractor.horizontal_index > 0:
|
||||
tractor.direction = "LEFT"
|
||||
elif event.key == pygame.K_RIGHT and tractor.horizontal_index < HORIZONTAL_TILES_NUMBER - 1:
|
||||
tractor.direction = "RIGHT"
|
||||
elif event.key == pygame.K_UP and tractor.vertical_index > 0:
|
||||
tractor.direction = "UP"
|
||||
elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1:
|
||||
tractor.direction = "DOWN"
|
@ -1,54 +0,0 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.optim as optim
|
||||
from matplotlib.pyplot import imshow
|
||||
import numpy as np
|
||||
from matplotlib.pyplot import imshow
|
||||
import matplotlib.pyplot as ppl
|
||||
|
||||
def plotdigit(image):
|
||||
img = np.reshape(image, (-250, 250))
|
||||
imshow(img, cmap='Greys', vmin=0, vmax=255)
|
||||
ppl.show()
|
||||
|
||||
train_images = np.load('neural_network\\image-dataset.npy')
|
||||
print(train_images.shape)
|
||||
|
||||
train_labels = np.load('neural_network\\label-dataset.npy')
|
||||
|
||||
train_images = train_images / 255
|
||||
train_labels = train_labels / 255
|
||||
|
||||
train_images = [torch.tensor(image, dtype=torch.float32) for image in train_images]
|
||||
print(train_images[0].shape)
|
||||
|
||||
train_labels = [torch.tensor(label, dtype=torch.long) for label in train_labels]
|
||||
|
||||
|
||||
input_dim = 100*100*3
|
||||
output_dim = 2
|
||||
|
||||
model = nn.Sequential(
|
||||
nn.Linear(input_dim, output_dim),
|
||||
nn.LogSoftmax()
|
||||
)
|
||||
|
||||
def train(model, n_iter):
|
||||
|
||||
criterion = nn.NLLLoss()
|
||||
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
||||
|
||||
for epoch in range(n_iter):
|
||||
for image, label in zip(train_images, train_labels):
|
||||
optimizer.zero_grad()
|
||||
|
||||
output = model(image)
|
||||
loss = criterion(output.unsqueeze(0), label.unsqueeze(0))
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
print(f'epoch: {epoch:03}')
|
||||
|
||||
|
||||
train(model, 100)
|
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 93 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 103 KiB |
Before Width: | Height: | Size: 32 KiB |
Before Width: | Height: | Size: 25 KiB |
Before Width: | Height: | Size: 50 KiB |
Before Width: | Height: | Size: 151 KiB |
Before Width: | Height: | Size: 51 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 3.1 MiB |
Before Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 116 KiB |
Before Width: | Height: | Size: 25 KiB |
Before Width: | Height: | Size: 98 KiB |
Before Width: | Height: | Size: 35 KiB |
Before Width: | Height: | Size: 118 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 170 KiB |
Before Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 128 KiB |
Before Width: | Height: | Size: 9.8 KiB |
Before Width: | Height: | Size: 195 KiB |
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 53 KiB |
Before Width: | Height: | Size: 138 KiB |
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 172 KiB |
Before Width: | Height: | Size: 33 KiB |
Before Width: | Height: | Size: 257 KiB |
Before Width: | Height: | Size: 70 KiB |
Before Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 117 KiB |
Before Width: | Height: | Size: 7.3 KiB |
Before Width: | Height: | Size: 25 KiB |
Before Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 7.4 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 91 KiB |
Before Width: | Height: | Size: 59 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 2.0 MiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 550 KiB |
Before Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 156 KiB |
Before Width: | Height: | Size: 96 KiB |
Before Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 254 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 252 KiB |
Before Width: | Height: | Size: 90 KiB |
Before Width: | Height: | Size: 50 KiB |
Before Width: | Height: | Size: 343 KiB |
Before Width: | Height: | Size: 233 KiB |
Before Width: | Height: | Size: 194 KiB |
Before Width: | Height: | Size: 130 KiB |
Before Width: | Height: | Size: 171 KiB |
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 265 KiB |