scalenie modułów

This commit is contained in:
s464840 2022-06-07 21:21:42 +02:00
parent aae5ad9491
commit 0186e21290
25 changed files with 125 additions and 79 deletions

8
.idea/.gitignore vendored
View File

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,6 +0,0 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

View File

@ -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>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/SI-projekt-smieciarka2.iml" filepath="$PROJECT_DIR$/.idea/SI-projekt-smieciarka2.iml" />
</modules>
</component>
</project>

View File

@ -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>

View File

@ -4,10 +4,10 @@ import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
DATASET_PATH = Path('../dataset')
DATASET_PATH = pathlib.Path('../dataset')
learn = load_learner(DATASET_PATH/'export.pkl')
path = Path(DATASET_PATH/'glass/glass1.jpg')
path = pathlib.Path(DATASET_PATH / 'others/trash1.jpg')
def getPredict(learner, path):
prediction = learner.predict(path)

Binary file not shown.

Binary file not shown.

View File

@ -1,51 +1,58 @@
from queue import PriorityQueue
import numpy as np
def heuristic(xy1, xy2):
return abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1])
def neighbours(point):
x, y = point
list=((x+1,y), (x,y+1), (x,y-1), (x-1,y))
return list
#determining the cost of a specific field in the grid
def neighbours(point, collisionsMap):
x, y = point
list = [(x + 1, y), (x, y + 1), (x, y - 1), (x - 1, y)]
return [(x, y) for x, y in list if 0 <= x <= 14 and 0 <= y <= 14 and not collisionsMap[x][y]]
# determining the cost of a specific field in the grid
def checkCost(grid, xy):
x, y = xy
cost = grid[x][y]
return cost
def aStar(grid, start, goal):
def aStar(grid, collisionsMap, start, goal):
openlist = PriorityQueue()
openlist.put(start, 0)
fScore = {}
gScore = {}
origin = {start: None}
fScore[start] = 0
closedlist=[]
fScore[start] = heuristic(start, goal)
gScore[start] = 0
while openlist!= {} :
while not openlist.empty():
current = openlist.get()
if current == goal:
path = []
#following from the succesors to the root our starting point
while current != start:
# following from the succesors to the root our starting point
while current is not None:
path.append(current)
current = origin[current]
path.reverse()
break
return path
# successor function
for succ in neighbours(current):
#checking if didn't go out of the maze
if(succ[0] < 0 or succ[1] < 0 or succ[0] > 14 or succ[1] > 14):
for succ in neighbours(current, collisionsMap):
# checking if didn't go out of the maze
if succ[0] < 0 or succ[1] < 0 or succ[0] > 14 or succ[1] > 14:
continue
gScore = fScore[current[0],current[1]] + checkCost(grid, current)
if succ not in closedlist or gScore < fScore[succ[0],succ[1]]:
closedlist.append(succ)
origin[succ[0],succ[1]] = current
fScore[succ[0],succ[1]] = gScore
priority = gScore + heuristic(goal, succ)
tentiative_gScore = gScore.get(current, np.inf) + checkCost(grid, succ)
if tentiative_gScore < gScore.get(succ, np.inf):
origin[succ] = current
priority = tentiative_gScore + heuristic(goal, succ)
fScore[succ] = priority
gScore[succ] = tentiative_gScore
openlist.put(succ, priority)
return path
raise RuntimeError("No path found")

View File

@ -1,10 +1,32 @@
from pathlib import Path
import numpy as np
import astar
import pygame
import snn
import joblib
import os
screen = []
objectArray = []
collisionsMap = []
decision_tree = joblib.load(Path('../tree/decisionTreeClassifier'))
type_map = {
'glass': 2,
'others': 0,
'paper': 4,
'plastic': 3
}
testset_path = Path('../testset')
testset = [Path(f'{testset_path}/{file}') for file in os.listdir(testset_path)]
season = 2
truck_full = 0
truck_working = 0
weightsMap = ([1, 2, 1, 4, 5, 2, 7, 8, 5, 4, 15, 3, 4, 5, 8],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 1],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 3],
@ -65,10 +87,8 @@ class Agent(Object):
truck = pygame.transform.scale(truck, (square, square))
screen.blit(truck, (circleX, circleY))
def move(self, gridLength, path):
for (x, y) in path:
newPos = self.pos.get_moved(x, y)
self.move_if_possible(newPos, gridLength)
def move(self, newPos):
self.pos = newPos
def move_if_possible(self, newPos, gridLength):
if movement_allowed(newPos, gridLength):
@ -76,13 +96,13 @@ class Agent(Object):
class House(Object):
def __init__(self, name, pos):
def __init__(self, name, pos, **kwargs):
super().__init__(name, pos)
self.trash_cans = {
"paper": False,
"glass": False,
"plastic": False,
"others": False
self.trash_can = {
"type": kwargs.get('type', np.random.choice(testset)),
'animal': kwargs.get('animal', np.random.randint(0, 2)),
'quantity': kwargs.get('quantity', np.random.random() * 0.7 + 0.3),
'time': kwargs.get('time', np.random.randint(0, 6))
}
def draw(self, square):
@ -167,7 +187,8 @@ if __name__ == '__main__':
(7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (13, 14), (6, 9)
]])]
holes = [Hole(f'dziura-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
(4, 9), (5, 11), (11, 7), (13, 8)
(4, 9), (5, 11), (11, 7), (13, 8), (0, 1), (1, 1), (2, 1), (14, 13),
(13, 13), (4, 8), (5, 9), (7, 9), (6, 8), (3, 5), (4, 6), (5, 5)
]])]
objectArray.append(agent)
objectArray.append(junkyard)
@ -175,7 +196,7 @@ if __name__ == '__main__':
objectArray += holes
collisionsMap = [[False] * gridSize for _ in range(gridSize)]
for object in objectArray[1:]:
for object in holes:
collisionsMap[object.pos.x][object.pos.y] = True
width = 610
@ -184,12 +205,51 @@ if __name__ == '__main__':
startPos = (0, 0)
finalPos = (14, 14)
astarPath = astar.aStar(weightsMap, startPos, finalPos)
astarPath = astar.aStar(weightsMap, collisionsMap, startPos, finalPos)
checkpoints = [startPos]
for house in houses:
checkpoints.append((house.pos.x, house.pos.y))
checkpoints.append(finalPos)
astarPath = []
for i in range(len(checkpoints) - 1):
path = astar.aStar(weightsMap, collisionsMap, checkpoints[i], checkpoints[i + 1])
if i == 0:
astarPath += path
else:
astarPath += path[1:]
print(astarPath)
pathPos = 0
nextCheckpoint = 1
while True:
agent_x, agent_y = astarPath[pathPos]
checkpoint_x, checkpoint_y = checkpoints[nextCheckpoint]
agent.pos = Position(agent_x, agent_y)
for house in houses:
if house.pos.x == agent_x and house.pos.y == agent_y \
and agent_x == checkpoint_x and agent_y == checkpoint_y:
nextCheckpoint += 1
house.trash_can['evaluated_type'] = type_map[snn.getPredict(house.trash_can['type'])[0]]
print('House:', house.name, 'pos:', astarPath[pathPos], 'type:', house.trash_can['type'],
'evaluated_type:', house.trash_can['evaluated_type'])
tree_input = np.array([
house.trash_can['evaluated_type'],
season,
house.trash_can['animal'],
truck_full,
house.trash_can['quantity'],
house.trash_can['time'],
truck_working
]).reshape(1, -1)
should_get = decision_tree.predict(tree_input)
print('Desicion tree input:', tree_input, 'result:', should_get)
pathPos = pathPos + 1 if pathPos < len(astarPath) - 1 else pathPos
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
screen.fill(c)
draw(gridSize, objectArray)
kb_listen(objectArray, gridSize, astarPath)
pygame.display.update() # by krata pojawiła się w okienku - update powierzc
pygame.time.wait(100)

16
src/snn.py Normal file
View File

@ -0,0 +1,16 @@
from fastai.vision.all import *
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
DATASET_PATH = pathlib.Path('../dataset')
learn = load_learner(DATASET_PATH/'export.pkl')
path = pathlib.Path(DATASET_PATH / 'others/trash1.jpg')
def getPredict(path):
prediction = learn.predict(path)
predictionName = learn.predict(path)[0]
# print(prediction, '\n\n','prediction:', predictionName, 'path:', path)
return prediction

BIN
testset/glass0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
testset/glass1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
testset/glass2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
testset/others0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
testset/others1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
testset/others2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
testset/paper0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

BIN
testset/paper1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
testset/plastic0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

BIN
testset/plastic1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
testset/plastic2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
tree/decisionTreeClassifier Normal file

Binary file not shown.

View File

@ -1,5 +1,6 @@
import pandas as pd
from sklearn import tree
import joblib
df = pd.read_csv('data.csv')
print(df.head())
@ -19,6 +20,8 @@ print(pred)
print('Jedynki: ', len(df[df['Y'] == 1]))
print('Zera: ', len(df[df['Y'] == 0]))
joblib.dump(clf, 'decisionTreeClassifier')
#Legenda
#czy wywiezc zmieci 1 tak 0 nie