Add decision trees and some refactor

This commit is contained in:
mkielar 2021-06-10 21:23:58 +02:00
parent 1d717acefa
commit bb970960d0
53 changed files with 1205 additions and 505 deletions

340
.gitignore vendored Normal file
View File

@ -0,0 +1,340 @@
# Created by https://www.toptal.com/developers/gitignore/api/python,venv,intellij,pycharm
# Edit at https://www.toptal.com/developers/gitignore?templates=python,venv,intellij,pycharm
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/
# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml
# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/
# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$
# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml
### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
# Generated files
# Sensitive or high-churn files
# Gradle
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
# Mongo Explorer plugin
# File-based project format
# IntelliJ
# mpeltonen/sbt-idea plugin
# JIRA plugin
# Cursive Clojure plugin
# Crashlytics plugin (for Android Studio and IntelliJ)
# Editor-based Rest Client
# Android studio 3.1+ serialized cache file
### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
doc/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
#poetry.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
# .env
.env/
.venv/
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# operating system-related files
# file properties cache/storage on macOS
*.DS_Store
# thumbnail cache on Windows
Thumbs.db
# profiling data
.prof
### venv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json
# End of https://www.toptal.com/developers/gitignore/api/python,venv,intellij,pycharm
.idea/**

View File

@ -1,8 +1,15 @@
required pygame library installed required pygame library installed
change img directory in main.py line 13 change img directory in main.py line 13
board is represented as 2D lists of square objects, where first index of the list points to the row and second to the column board is represented as 2D lists of square objects, where first index of the list points to the row and second to the column
of the square(piece object) location of the square(piece object) location
each square(piece object) contains own x and y identifiers and isSbThere atribute which stores info if detective is standing on each square(piece object) contains own x and y identifiers and isSbThere atribute which stores info if detective is standing on
piece location piece location
a_star algorithm:
-goal: nearest mushroom without posion, x,y finded by bfs
-get_cost: return summary cost of parent node and cost of current node (predictable heuristic cost and cost of field)
-heuristic function: summary of differences between node location and goal location

View File

@ -1,87 +0,0 @@
from container import board
from container.node import Node
from container.mushroom import Mushroom
from container.tree import Tree
from operator import attrgetter
def a_star(board, fringe, explored, istate, successor, get_cost, goaltest):
agent_x = istate[0]
agent_y = istate[1]
agent_angle = istate[2]
fringe: List[Node] = [Node(agent_x, agent_y, agent_angle)]
while fringe:
if not fringe:
return False, False
node = min(fringe, key=lambda x: x.cost)
fringe.remove(node)
if (node.x, node.y) == goaltest:
actions = []
while node.parent != None:
actions.append(node.action)
node = node.parent
actions.reverse()
return actions
explored.append(node)
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
next_node = Node(x, y, angle)
next_node.parent = node
next_node.action = action
next_node.cost = get_cost(next_node, goaltest,board) + heuristic_function(node, goaltest)
if next_node not in fringe and next_node not in explored:
fringe.append(next_node)
else:
for checked_node in fringe:
if(checked_node == next_node and (next_node.cost) < (checked_node.cost)):
fringe[fringe.index(checked_node)] = next_node
def successor(board,x,y,angle):
result = []
result.append(("rotate_left", x, y, (angle + 1) % 4))
result.append(("rotate_right", x, y, (angle - 1) % 4))
if angle == 1:
if x > 0:
result.append(("move", x-1, y, angle))
elif angle == 0:
if y > 0:
result.append(("move", x, y-1, angle))
elif angle == 3:
if x < board.col - 1:
result.append(("move", x+1, y, angle))
elif angle == 2:
if y < board.row - 1:
result.append(("move", x, y+1, angle))
return result
def get_cost(node, goal: Mushroom, board):
if (node.parent.x, node.parent.y) == (node.x, node.y):
sum = 1
elif(isinstance((board.board[node.x][node.y]),Tree)):
sum = 1000
elif(isinstance((board.board[node.x][node.y]),Mushroom) and (board.board[node.x][node.y].poison) == True):
sum = 1000
else:
sum = 2
return sum + node.parent.cost
def heuristic_function(node, goal):
return abs(node.x - goal[0]) + abs(node.y - goal[1])

View File

@ -1,25 +0,0 @@
from container.piece import Piece
import pygame
class Mushroom(Piece):
def __init__(self,x_y, img = 0, poison = False, grow = 100):
self.name = "Mushroom"
self.col = x_y[0]
self.row = x_y[1]
self.poison = poison
self.grow = grow
self.img = pygame.image.load(r'container\mushrooms\m'+str(img)+'.png')
if poison: self.img = pygame.image.load(r'container\mushrooms\pm'+str(img)+'.png')
self.points = img+1
if poison: self.points *= -1
def draw(self, win, square_size):
x = (self.col+0.5*(100-self.grow)/100)*square_size[0]
y = (self.row+0.5*(100-self.grow)/100)*square_size[1]
win.blit(pygame.transform.scale(self.img, (int(square_size[0]/100*self.grow), int(square_size[1]/100*self.grow))), ((x, y)))

View File

@ -1,13 +0,0 @@
from container.piece import Piece
import pygame
class Tree(Piece):
def __init__(self,x_y, img = 0):
self.name = "Tree"
self.col = x_y[0]
self.row = x_y[1]
self.img = pygame.image.load(r'container\trees\tree'+str(img)+'.png')

122
core/a_star.py Normal file
View File

@ -0,0 +1,122 @@
from typing import List
from core.mushroom import Mushroom
from core.node import Node
from core.tree import Tree
def a_star(board, fringe, explored, istate, successor, get_cost, goaltest):
agent_x = istate[0]
agent_y = istate[1]
agent_angle = istate[2]
fringe: List[Node] = [Node(agent_x, agent_y, agent_angle)]
while fringe:
if not fringe:
return False, False
node = min(fringe, key=lambda x: x.cost)
fringe.remove(node)
if (node.x, node.y) == goaltest:
actions = []
while node.parent is not None:
actions.append(node.action)
node = node.parent
actions.reverse()
return actions
explored.append(node)
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
next_node = Node(x, y, angle)
next_node.parent = node
next_node.action = action
next_node.cost = get_cost(next_node, goaltest, board) + heuristic_function(next_node, goaltest, board)
if next_node not in fringe and next_node not in explored:
fringe.append(next_node)
else:
for checked_node in fringe:
if checked_node == next_node and next_node.cost < checked_node.cost:
fringe[fringe.index(checked_node)] = next_node
def successor(board, x, y, angle):
result = []
result.append(("rotate_left", x, y, (angle + 1) % 4))
result.append(("rotate_right", x, y, (angle - 1) % 4))
if angle == 1:
if x > 0:
result.append(("move", x - 1, y, angle))
elif angle == 0:
if y > 0:
result.append(("move", x, y - 1, angle))
elif angle == 3:
if x < board.col - 1:
result.append(("move", x + 1, y, angle))
elif angle == 2:
if y < board.row - 1:
result.append(("move", x, y + 1, angle))
return result
def get_cost(node, goal: Mushroom, board):
if (node.parent.x, node.parent.y) == (node.x, node.y):
weight = 1
elif isinstance((board.board[node.x][node.y]), Tree):
weight = 1000
elif isinstance((board.board[node.x][node.y]), Mushroom) and board.dt.predict(board.board[node.x][node.y]):
weight = 1000
else:
weight = 2
return weight + node.parent.cost
def heuristic_function(node, goal, board):
try:
return abs(node.x - goal[0]) + abs(node.y - goal[1])
except:
print("No more edible mushrooms on the board. Score: " + str(board.agent.points))
exit(0)
def bfs(istate, successor, board):
fringe = []
explored = []
fringe_states = set()
explored_states = set()
agent_x = istate[0]
agent_y = istate[1]
agent_angle = istate[2]
fringe.append(Node(agent_x, agent_y, agent_angle))
fringe_states.add((agent_x, agent_y, agent_angle))
while fringe:
if not fringe:
return False, False
node = fringe.pop(0)
fringe_states.remove((node.x, node.y, node.angle))
if isinstance((board.board[node.x][node.y]), Mushroom):
if not board.dt.predict(board.board[node.x][node.y]):
return node.x, node.y
explored.append(node)
explored_states.add((node.x, node.y, node.angle))
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
if ((x, y, angle) not in explored_states and (x, y, angle) not in fringe_states):
next_node = Node(x, y, angle)
next_node.parent = node
next_node.action = action
fringe.append(next_node)
fringe_states.add((x, y, angle))

View File

@ -1,96 +1,104 @@
import pygame import os
from container.piece import Piece
import pygame
from core.piece import Piece
class Agent(Piece):
def __init__(self,x_y, square_size):
self.name = "Agent" class Agent(Piece):
self.col = x_y[0] def __init__(self,x_y, square_size):
self.row = x_y[1] self.name = "Agent"
self.angle = 0 self.col = x_y[0]
self.moving = 0 self.row = x_y[1]
self.rotating = 0 self.angle = 0
self.move_to = (self.row, self.col) self.current_angle = 0
self.moving = 0
self.points = 0 self.rotating = 0
self.img = pygame.image.load(r'container\new_detective.png') self.move_to = (self.row, self.col)
self.square_size = square_size
self.img = pygame.transform.scale(self.img, (int(self.square_size[0]), int(self.square_size[1]))) self.points = 0
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep +
'agent' + os.path.sep + 'new_detective.png')
def draw(self, win): self.square_size = square_size
x = (self.col + 0.5 + (self.move_to[0] - self.col)/10*self.moving)*self.square_size[0] self.img = pygame.transform.scale(self.img, (int(self.square_size[0]), int(self.square_size[1])))
y = (self.row + 0.5 + (self.move_to[1] - self.row)/10*self.moving)*self.square_size[1]
#win.blit(self.img, (x, y))
self.blitRotate(win, self.img, (x, y), (self.square_size[0]/2, self.square_size[1]/2), self.angle*90 + self.rotating*90/10) def draw(self, win):
def rotate(self, angle): x = (self.col + 0.5 + (self.move_to[0] - self.col)/10*self.moving)*self.square_size[0]
if self.moving == self.rotating == 0: y = (self.row + 0.5 + (self.move_to[1] - self.row)/10*self.moving)*self.square_size[1]
self.rotating += angle
#win.blit(self.img, (x, y))
def move(self):
if self.moving == self.rotating == 0: self.blitRotate(win, self.img, (x, y), (self.square_size[0]/2, self.square_size[1]/2), self.angle*90 + self.rotating*90/10)
move_to = (0,-1)
if self.angle == 1: move_to = (-1,0) def rotate(self, angle):
if self.angle == 2: move_to = (0,1) if self.moving == self.rotating == 0:
if self.angle == 3: move_to = (1,0) self.rotating += angle
#print(self.rotating)
self.move_to = (self.col + move_to[0], self.row + move_to[1])
def move(self):
self.moving += 1 if self.moving == self.rotating == 0:
move_to = (0,-1)
def update_animation(self): if self.angle == 1: move_to = (-1,0)
if self.moving > 0: if self.angle == 2: move_to = (0,1)
self.moving += 1 if self.angle == 3: move_to = (1,0)
if self.moving >= 10:
self.moving = 0 self.move_to = (self.col + move_to[0], self.row + move_to[1])
self.col, self.row = self.move_to
self.moving += 1
if self.rotating > 0:
self.rotating += 1 def update_animation(self):
if self.rotating >= 10: if self.moving > 0:
self.rotating = 0 self.moving += 1
self.angle = (self.angle + 1) % 4 if self.moving >= 10:
if self.rotating < 0: self.moving = 0
self.rotating -= 1 self.col, self.row = self.move_to
if self.rotating <= -10:
self.rotating = 0 if self.rotating > 0:
self.angle = (self.angle - 1) % 4 self.rotating += 1
if self.rotating >= 10:
self.rotating = 0
def blitRotate(self, surf, image, pos, originPos, angle): self.angle = (self.angle + 1) % 4
if self.rotating < 0:
# calcaulate the axis aligned bounding box of the rotated image self.rotating -= 1
w, h = image.get_size() if self.rotating <= -10:
box = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]] self.rotating = 0
box_rotate = [p.rotate(angle) for p in box] self.angle = (self.angle - 1) % 4
min_box = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
max_box = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
def blitRotate(self, surf, image, pos, originPos, angle):
# calculate the translation of the pivot
pivot = pygame.math.Vector2(originPos[0], -originPos[1]) # calcaulate the axis aligned bounding box of the rotated image
pivot_rotate = pivot.rotate(angle) w, h = image.get_size()
pivot_move = pivot_rotate - pivot box = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
box_rotate = [p.rotate(angle) for p in box]
# calculate the upper left origin of the rotated image min_box = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1]) max_box = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
# get a rotated image # calculate the translation of the pivot
rotated_image = pygame.transform.rotate(image, angle) pivot = pygame.math.Vector2(originPos[0], -originPos[1])
pivot_rotate = pivot.rotate(angle)
# rotate and blit the image pivot_move = pivot_rotate - pivot
surf.blit(rotated_image, origin)
# calculate the upper left origin of the rotated image
# draw rectangle around the image origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])
#pygame.draw.rect(surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)
# get a rotated image
rotated_image = pygame.transform.rotate(image, angle)
# rotate and blit the image
surf.blit(rotated_image, origin)
# draw rectangle around the image
#pygame.draw.rect(surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)

View File

@ -1,156 +1,158 @@
import pygame import random
import random
from core.constans import *
from container.constans import * from core.decision_tree import DecisionTree
from container.tree import Tree from core.tree import Tree
from container.mushroom import Mushroom from core.mushroom import Mushroom
from container.agent import Agent from core.agent import Agent
from container.a_star import a_star,successor, get_cost from core.a_star import a_star, bfs, successor, get_cost
class Board:
def __init__(self, x = 0, y = 0, width = WIDTH, height = HEIGHT, row = ROWS, col = COLUMNS): class Board:
self.x = x def __init__(self, x=0, y=0, width=WIDTH, height=HEIGHT, row=ROWS, col=COLUMNS):
self.y = y self.x = x
self.width = width self.y = y
self.height = height self.width = width
self.row = row self.height = height
self.col = col self.row = row
self.square_size = (self.width / self.col, self.height / self.row) self.col = col
self.board = [[False for j in range(self.row)] for i in range(self.col)] self.square_size = (self.width / self.col, self.height / self.row)
self.free_spaces = [] self.board = [[False for j in range(self.row)] for i in range(self.col)]
self.update_free_spaces() self.free_spaces = []
self.update_free_spaces()
self.agent = Agent(self.free_spaces.pop(), self.square_size) self.dt = DecisionTree(0.05)
self.dt.learn()
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row}) {self.agent.angle}")
self.agent = Agent(self.free_spaces.pop(), self.square_size)
self.surface = pygame.Surface((self.width, self.height))
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row}) {self.agent.angle}")
for i in range(TREES):
x_y = self.free_spaces.pop() self.surface = pygame.Surface((self.width, self.height))
self.board[x_y[0]][x_y[1]] = Tree(x_y, random.randint(0, 8))
for i in range(TREES):
x_y = self.free_spaces.pop()
for i in range(MUSHROOMS_START): self.board[x_y[0]][x_y[1]] = Tree(x_y, random.randint(0, 8))
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2)) for i in range(MUSHROOMS_START):
x_y = self.free_spaces.pop()
for i in range(POISON_MUSHROOMS_START): self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True) for i in range(POISON_MUSHROOMS_START):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
self.grow_next = 0
self.grow_next = 0
self.path = []
self.actions = [] self.path = []
self.actions = []
def update_free_spaces(self):
self.free_spaces = [] def update_free_spaces(self):
self.free_spaces = []
for x in range(self.col):
for y in range(self.row): for x in range(self.col):
if self.board[x][y] == False: self.free_spaces.append((x,y)) for y in range(self.row):
if self.board[x][y] == False: self.free_spaces.append((x, y))
random.shuffle(self.free_spaces)
random.shuffle(self.free_spaces)
def draw_squares(self,win):
self.surface.fill(GREEN_2) def draw_squares(self, win):
for x in range(self.col): self.surface.fill(GREEN_2)
for y in range(self.row): for x in range(self.col):
if (x+y) % 2 == 0: for y in range(self.row):
pygame.draw.rect(self.surface,GREEN,(x*self.square_size[0], y*self.square_size[1], self.square_size[0], self.square_size[1])) if (x + y) % 2 == 0:
pygame.draw.rect(self.surface, GREEN, (
win.blit(self.surface, (self.x, self.y)) x * self.square_size[0], y * self.square_size[1], self.square_size[0], self.square_size[1]))
def draw_agent(self,win): win.blit(self.surface, (self.x, self.y))
self.agent.draw(self.surface)
win.blit(self.surface, (self.x, self.y)) def draw_agent(self, win):
self.agent.draw(self.surface)
def draw_pieces(self,win): win.blit(self.surface, (self.x, self.y))
for x in range(self.col):
for y in range(self.row): def draw_pieces(self, win):
if self.board[x][y] != False: for x in range(self.col):
self.board[x][y].draw(self.surface, self.square_size) for y in range(self.row):
win.blit(self.surface, (self.x, self.y)) if self.board[x][y] != False:
self.board[x][y].draw(self.surface, self.square_size)
def draw_info(self,win): win.blit(self.surface, (self.x, self.y))
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render(str(self.agent.points), False, (0, 0, 0)) def draw_info(self, win):
win.blit(textsurface,(self.x+self.width-textsurface.get_width()-5,self.y-40)) myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render(str(self.agent.points), False, (0, 0, 0))
# kolumna agneta win.blit(textsurface, (self.x + self.width - textsurface.get_width() - 5, self.y - 40))
textsurface = myfont.render("c: "+str(self.agent.col), False, (0, 0, 0))
win.blit(textsurface,(self.x+1,self.y-40)) # kolumna agneta
textsurface = myfont.render("c: " + str(self.agent.col), False, (0, 0, 0))
# wiersz agneta win.blit(textsurface, (self.x + 1, self.y - 40))
textsurface = myfont.render("r: "+str(self.agent.row), False, (0, 0, 0))
win.blit(textsurface,(self.x+100,self.y-40)) # wiersz agneta
textsurface = myfont.render("r: " + str(self.agent.row), False, (0, 0, 0))
# kierunek agenta win.blit(textsurface, (self.x + 100, self.y - 40))
textsurface = myfont.render("a: "+str(self.agent.angle), False, (0, 0, 0))
win.blit(textsurface,(self.x+200,self.y-40)) # kierunek agenta
textsurface = myfont.render("a: " + str(self.agent.angle), False, (0, 0, 0))
win.blit(textsurface, (self.x + 200, self.y - 40))
def update_agent(self): def update_agent(self):
self.agent.update_animation() self.agent.update_animation()
if self.actions and self.agent.moving == self.agent.rotating == 0:
if self.actions and self.agent.moving == self.agent.rotating == 0: action = self.actions.pop(0)
action = self.actions.pop(0)
if action == "move":
if action == "move": self.agent.move() self.agent.move()
elif action == "rotate_left": self.agent.rotate(1) elif action == "rotate_left":
else: self.agent.rotate(-1) self.agent.rotate(1)
else:
self.agent.rotate(-1)
#zebranie grzyba
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100: # zebranie grzyba
self.agent.points += self.board[self.agent.col][self.agent.row].points if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][
self.board[self.agent.col][self.agent.row] = False self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100:
self.free_spaces.append((self.agent.col, self.agent.row)) self.agent.points += self.board[self.agent.col][self.agent.row].points
self.board[self.agent.col][self.agent.row] = False
def grow_mushrooms(self): self.free_spaces.append((self.agent.col, self.agent.row))
numer = 0
numer_poison = 0 def grow_mushrooms(self):
numer = 0
for x in range(self.col): # numer_poison = 0
for y in range(self.row):
if self.board[x][y] != False and self.board[x][y].name == "Mushroom": for x in range(self.col):
if self.board[x][y].grow < 100: self.board[x][y].grow += 1 for y in range(self.row):
if self.board[x][y] != False and self.board[x][y].name == "Mushroom":
if self.board[x][y].poison: numer_poison += 1 if self.board[x][y].grow < 100: self.board[x][y].grow += 1
else: numer += 1
# if self.board[x][y].poison:
if self.grow_next < 100: self.grow_next += 1 # numer_poison += 1
elif random.random() > 0.5 and numer < MUSHROOMS_MAX: # else:
self.grow_next = 0 numer += 1
self.update_free_spaces()
x_y = self.free_spaces.pop() if self.grow_next < 100:
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), False, 0) self.grow_next += 1
elif numer_poison < POISON_MUSHROOMS_MAX: elif random.random() > 0.5 and numer < MUSHROOMS_MAX:
self.grow_next = 0 self.grow_next = 0
self.update_free_spaces() self.update_free_spaces()
x_y = self.free_spaces.pop() x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True, 0) self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), 0)
# elif numer_poison < POISON_MUSHROOMS_MAX:
def a_starxd(self): # self.grow_next = 0
print(self.agent.angle) # self.update_free_spaces()
fringe = [] # x_y = self.free_spaces.pop()
explored = [] # self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), 0)
self.actions = a_star(self,
fringe, def a_starxd(self):
explored, print(self.agent.angle)
(self.agent.col,self.agent.row, self.agent.angle), fringe = []
successor, explored = []
get_cost, self.actions = a_star(self,
(bfs((self.agent.col,self.agent.row,self.agent.angle),successor,self)) fringe,
) explored,
(self.agent.col, self.agent.row, self.agent.angle),
print(self.actions) successor,
get_cost,
return self.actions (bfs((self.agent.col, self.agent.row, self.agent.angle), successor, self))
)
print(self.actions)
return self.actions

View File

@ -1,21 +1,21 @@
import pygame import pygame
#board size variables #board size variables
WIDTH = 1000 WIDTH = 1000
HEIGHT = 700 HEIGHT = 700
COLUMNS = 20 COLUMNS = 20
ROWS = 14 ROWS = 14
#number of mushrooms #amount of mushrooms on the board
MUSHROOMS_START = 5 MUSHROOMS_START = 5
MUSHROOMS_MAX = 10 MUSHROOMS_MAX = 10
POISON_MUSHROOMS_START = 5 POISON_MUSHROOMS_START = 5
POISON_MUSHROOMS_MAX = 40 POISON_MUSHROOMS_MAX = 40
TREES = 80 TREES = 80
#colors in game in rgb #colors of board in game in rgb
GREEN = (0,230,0) GREEN = (0,230,0)
GREEN_2 = (0,200,0) GREEN_2 = (0,200,0)
WHITE = (255,255,255) #WHITE = (255,255,255)

32
core/decision_tree.py Normal file
View File

@ -0,0 +1,32 @@
import os
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
class DecisionTree:
def __init__(self, precision):
self.model = DecisionTreeClassifier(criterion='gini')
self.learningDataSize = precision
def learn(self):
dataset = pd.read_csv(r'core' + os.path.sep + 'resources' + os.path.sep + 'data' +
os.path.sep + 'data_tree.csv')
input_x = dataset.iloc[:, 0:8]
input_y = dataset.iloc[:, 8]
x_train, x_test, y_train, y_test = train_test_split(input_x, input_y, test_size=(1-self.learningDataSize))
self.model.fit(x_train, y_train)
def predict(self, mushroom):
return bool(self.model.predict([[
mushroom.isRed,
mushroom.hasRing,
mushroom.hasEmptyCore,
mushroom.hasDots,
mushroom.hasLamella,
mushroom.isShiftingColor,
mushroom.hasUnpleasantSmell,
mushroom.hasSlime,
]])[0])

42
core/mushroom.py Normal file
View File

@ -0,0 +1,42 @@
import os
import random
from core.piece import Piece
import pygame
class Mushroom(Piece):
def __init__(self, x_y, img=0, grow=100):
self.name = "Mushroom"
self.col = x_y[0]
self.row = x_y[1]
self.grow = grow
self.isRed = bool(random.getrandbits(1))
self.hasRing = bool(random.getrandbits(1))
self.hasEmptyCore = bool(random.getrandbits(1))
self.hasDots = bool(random.getrandbits(1))
self.hasLamella = bool(random.getrandbits(1))
self.isShiftingColor = bool(random.getrandbits(1))
self.hasUnpleasantSmell = bool(random.getrandbits(1))
self.hasSlime = bool(random.getrandbits(1))
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep + 'mushrooms' +
os.path.sep + 'm' + str(img) + '.png')
poison = (int(self.isRed) + int(self.hasRing) + int(self.hasEmptyCore) + int(self.hasDots) + int(
self.hasLamella) + int(self.isShiftingColor) + int(self.hasUnpleasantSmell) + int(self.hasSlime)) > 4
if poison:
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep + 'mushrooms' +
os.path.sep + 'pm' + str(img) + '.png')
self.points = img + 1
if poison:
self.points *= -1
def draw(self, win, square_size):
x = (self.col + 0.5 * (100 - self.grow) / 100) * square_size[0]
y = (self.row + 0.5 * (100 - self.grow) / 100) * square_size[1]
win.blit(pygame.transform.scale(self.img,
(int(square_size[0] / 100 * self.grow), int(square_size[1] / 100 * self.grow))),
(x, y))

View File

@ -1,15 +1,15 @@
class Node: class Node:
def __init__(self, x, y, angle = None, parent = None, action = 0, cost=0): def __init__(self, x, y, angle = None, parent = None, action = 0, cost=0):
self.x = x self.x = x
self.y = y self.y = y
self.angle = angle self.angle = angle
self.parent = parent self.parent = parent
self.action = action self.action = action
self.cost = cost self.cost = cost
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, Node) and (self.x == other.x and self.y == other.y and self.angle == other.angle): if isinstance(other, Node) and (self.x == other.x and self.y == other.y and self.angle == other.angle):
return True return True
else: else:
return False return False

View File

@ -1,15 +1,18 @@
import pygame import pygame
class Piece: class Piece:
def __init__(self,x_y): def __init__(self,x_y,weight = 0 ,mushroom = 0):
self.name = "" self.name = ""
self.col = x_y[0] self.col = x_y[0]
self.row = x_y[1] self.row = x_y[1]
self.img = '' self.img = ''
self.weight = weight
def draw(self, win, square_size): self.mushroom = mushroom
win.blit(pygame.transform.scale(self.img, (int(square_size[0]), int(square_size[1]))), (self.col*square_size[0], self.row*square_size[1]))
def draw(self, win, square_size):
win.blit(pygame.transform.scale(self.img, (int(square_size[0]), int(square_size[1]))), (self.col*square_size[0], self.row*square_size[1]))

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -0,0 +1,257 @@
Czy jest czerwony,Czy ma pierścień,Czy ma pusty trzpień,Czy ma kropki,Czy ma blaszki,Czy zmienia kolor po przełamaniu,Czy ma nieprzyjemny zapach,Czy ma śluz,Czy jest trujący
0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0
0,1,0,0,0,0,0,0,0
1,1,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0
1,0,1,0,0,0,0,0,0
0,1,1,0,0,0,0,0,0
1,1,1,0,0,0,0,0,0
0,0,0,1,0,0,0,0,0
1,0,0,1,0,0,0,0,0
0,1,0,1,0,0,0,0,0
1,1,0,1,0,0,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1,1,0,1,1,1,1
0,0,0,0,1,1,1,1,0
1,0,0,0,1,1,1,1,1
0,1,0,0,1,1,1,1,1
1,1,0,0,1,1,1,1,1
0,0,1,0,1,1,1,1,1
1,0,1,0,1,1,1,1,1
0,1,1,0,1,1,1,1,1
1,1,1,0,1,1,1,1,1
0,0,0,1,1,1,1,1,1
1,0,0,1,1,1,1,1,1
0,1,0,1,1,1,1,1,1
1,1,0,1,1,1,1,1,1
0,0,1,1,1,1,1,1,1
1,0,1,1,1,1,1,1,1
0,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1 Czy jest czerwony Czy ma pierścień Czy ma pusty trzpień Czy ma kropki Czy ma blaszki Czy zmienia kolor po przełamaniu Czy ma nieprzyjemny zapach Czy ma śluz Czy jest trujący
2 0 0 0 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0
4 0 1 0 0 0 0 0 0 0
5 1 1 0 0 0 0 0 0 0
6 0 0 1 0 0 0 0 0 0
7 1 0 1 0 0 0 0 0 0
8 0 1 1 0 0 0 0 0 0
9 1 1 1 0 0 0 0 0 0
10 0 0 0 1 0 0 0 0 0
11 1 0 0 1 0 0 0 0 0
12 0 1 0 1 0 0 0 0 0
13 1 1 0 1 0 0 0 0 0
14 0 0 1 1 0 0 0 0 0
15 1 0 1 1 0 0 0 0 0
16 0 1 1 1 0 0 0 0 0
17 1 1 1 1 0 0 0 0 0
18 0 0 0 0 1 0 0 0 0
19 1 0 0 0 1 0 0 0 0
20 0 1 0 0 1 0 0 0 0
21 1 1 0 0 1 0 0 0 0
22 0 0 1 0 1 0 0 0 0
23 1 0 1 0 1 0 0 0 0
24 0 1 1 0 1 0 0 0 0
25 1 1 1 0 1 0 0 0 0
26 0 0 0 1 1 0 0 0 0
27 1 0 0 1 1 0 0 0 0
28 0 1 0 1 1 0 0 0 0
29 1 1 0 1 1 0 0 0 0
30 0 0 1 1 1 0 0 0 0
31 1 0 1 1 1 0 0 0 0
32 0 1 1 1 1 0 0 0 0
33 1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
238 0 0 1 1 0 1 1 1 1
239 1 0 1 1 0 1 1 1 1
240 0 1 1 1 0 1 1 1 1
241 1 1 1 1 0 1 1 1 1
242 0 0 0 0 1 1 1 1 0
243 1 0 0 0 1 1 1 1 1
244 0 1 0 0 1 1 1 1 1
245 1 1 0 0 1 1 1 1 1
246 0 0 1 0 1 1 1 1 1
247 1 0 1 0 1 1 1 1 1
248 0 1 1 0 1 1 1 1 1
249 1 1 1 0 1 1 1 1 1
250 0 0 0 1 1 1 1 1 1
251 1 0 0 1 1 1 1 1 1
252 0 1 0 1 1 1 1 1 1
253 1 1 0 1 1 1 1 1 1
254 0 0 1 1 1 1 1 1 1
255 1 0 1 1 1 1 1 1 1
256 0 1 1 1 1 1 1 1 1
257 1 1 1 1 1 1 1 1 1

View File

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 9.9 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

15
core/tree.py Normal file
View File

@ -0,0 +1,15 @@
import os
import pygame
from core.piece import Piece
class Tree(Piece):
def __init__(self, x_y, img=0):
super().__init__(x_y)
self.name = "Tree"
self.col = x_y[0]
self.row = x_y[1]
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep + 'trees' +
os.path.sep + 'tree' + str(img) + '.png')

129
main.py
View File

@ -1,71 +1,58 @@
import pygame import pygame
from container.constans import WIDTH, HEIGHT, ROWS, COLUMNS, GREEN
from container.board import Board from core.board import Board
from core.constans import WIDTH, HEIGHT, GREEN
FPS = 18 from core.decision_tree import DecisionTree
from core.mushroom import Mushroom
#creating game window FPS = 40
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
#setting name # creating game window
pygame.display.set_caption('Forest') WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.font.init() # setting name
pygame.display.set_caption('Forest')
#detective = pygame.image.load(r'container\detective.png') pygame.font.init()
# detective = pygame.image.load(r'core\detective.png')
def main():
run = True def main():
clock = pygame.time.Clock() #for fps run = True
board = Board(0,40,WIDTH,HEIGHT-40) clock = pygame.time.Clock() # for fps
board = Board(0, 40, WIDTH, HEIGHT - 40)
while run: while run:
clock.tick(FPS) clock.tick(FPS)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
# managing arrow click
#managing arrow click key_input = pygame.key.get_pressed()
if key_input[pygame.K_LEFT]: board.agent.rotate(1)
key_input = pygame.key.get_pressed() if key_input[pygame.K_UP]: board.agent.move()
if key_input[pygame.K_LEFT]: board.agent.rotate(1) if key_input[pygame.K_RIGHT]: board.agent.rotate(-1)
if key_input[pygame.K_UP]: board.agent.move()
if key_input[pygame.K_RIGHT]: board.agent.rotate(-1) # drawing map and detective
WIN.fill(GREEN)
board.update_agent() # update moves and collecting mushrooms
board.grow_mushrooms()
#drawing map and detective
WIN.fill(GREEN) board.draw_squares(WIN)
board.update_agent() #update moves and collecting mushrooms board.draw_pieces(WIN)
board.grow_mushrooms() board.draw_agent(WIN)
board.draw_info(WIN)
board.draw_squares(WIN)
board.draw_pieces(WIN) pygame.display.update()
board.draw_agent(WIN)
board.draw_info(WIN) if key_input[pygame.K_SPACE]:
board.a_starxd()
pygame.display.update()
if board.agent.rotating == 0 and board.agent.moving == 0:
board.a_starxd()
if key_input[pygame.K_SPACE]: pygame.quit()
board.a_starxd()
if(board.agent.rotating==0 and board.agent.moving==0): main()
board.a_starxd()
pygame.quit()
main()

10
requirements.txt Normal file
View File

@ -0,0 +1,10 @@
joblib==1.0.1
numpy==1.20.3
pandas==1.2.4
pygame==2.0.1
python-dateutil==2.8.1
pytz==2021.1
scikit-learn==0.24.2
scipy==1.6.3
six==1.16.0
threadpoolctl==2.1.0