Merge pull request 'drzewo impl klasy do podlewania' (#6) from tree_classes into master

Reviewed-on: #6
This commit is contained in:
Zuzanna Myszczuk 2023-06-26 21:06:21 +02:00
commit abee4a4a6c
8 changed files with 51 additions and 9 deletions

View File

@ -1,8 +1,7 @@
{
"ExpandedNodes": [
"",
"\\decisionTree"
""
],
"SelectedNode": "\\decisionTree\\decisionTree.sav",
"SelectedNode": "\\C:\\Users\\zmysz\\Desktop\\nowy-inteligentny-traktor",
"PreviewInSolutionExplorer": false
}

Binary file not shown.

Binary file not shown.

View File

@ -8,6 +8,7 @@ class Field:
self.fertilizedTime = fertilizedTime # number
class Plant:
def __init__(self, plantType, growthState):
self.plantType = plantType # wheat/carrot/cabbage
@ -23,3 +24,15 @@ class Player:
x = 0
y = 0
rotation = 0
class Watering:
def __init__(self, rain, planted, temperature, sunny, snowy, moist, rotten, dayTime ):
self.rain = rain # yes/no
self.planted = planted # yes/no
self.temperature = temperature # good/bad
self.sunny = sunny
self.snowy = snowy # yes/no
self.moist = moist # yes/no
self.rotten = rotten # yes/no
self.dayTime = dayTime # 1 2 3 4

View File

@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
# from sklearn.datasets import load_iris
from sklearn.tree import export_text
@ -6,7 +9,7 @@ import joblib
X1 = []
view = []
with open("decisionTree/database.txt", 'r') as f:
with open("database.txt", 'r') as f:
for line in f:
line = line.strip()
test_list = [int(i) for i in line]
@ -50,14 +53,14 @@ with open("decisionTree/database.txt", 'r') as f:
view.append(x)
X1.append(test_list)
f = open("decisionTree/learning_set.txt", "w") # zapisuje atrybuty s³ownie
f = open("learning_set.txt", "w") # zapisuje atrybuty s³ownie
for i in view:
f.write(str(i)+"\n")
f.close()
Y1 = []
with open("decisionTree/decissions.txt", 'r') as f: # czyta decyzje
with open("decissions.txt", 'r') as f: # czyta decyzje
for line in f:
line = line.strip()
test = int(line)
@ -67,7 +70,7 @@ dataset = X1
decision = Y1
labels = ['Rain', 'Plant', 'Temperature', 'Sun', 'Snow', 'Moisture', 'Rotten', 'Time']
model = DecisionTreeClassifier(random_state=0, max_depth=20).fit(dataset, decision)
filename = 'decisionTree/decisionTree.sav'
filename = 'decisionTree.sav'
print("Model trained")
print("Decision tree:")
print(export_text(model, feature_names=labels))

31
main.py
View File

@ -1,3 +1,5 @@
import joblib
import numpy as np
import pygame
import random
@ -10,7 +12,7 @@ from torchvision.transforms import Lambda
from PIL import Image
import astar
from classes import Field, Player
from classes import Field, Player, Watering
from bfs import Istate, succ
from bfs import graphsearch
from board import Grid, Box, Obstacle, getGridBoxes, gridObjects
@ -406,6 +408,26 @@ def eventHandler(kbdObj, mouseObj):
goalNode = [int(posX/50), int(posY/50)]
# drzewo decyzyjne:
W = np.random.randint(2, size=(10, 10, 8))
# Wczytywanie modelu z pliku
labels = ['Rain', 'Planted', 'Temperature', 'Sun', 'Snow', 'Moisture', 'Rotten', 'Time']
loaded_model = joblib.load('decisionTree/decisionTree.sav')
sample = W[goalNode[0]-1][goalNode[1]-1]
# Klasyfikacja przy użyciu wczytanego modelu
predicted_class = loaded_model.predict([sample])
print(labels)
print(sample)
print('Predicted class:', predicted_class)
# Decyzja dotycząca podlania grządek na podstawie przewidzianej etykiety
if predicted_class == [1]:
print('Podlej grządkę')
else:
print('Nie podlewaj grządki')
print('goalNode x = ', goalNode[0], 'goalNode y = ', goalNode[1])
# Delay to avoid multiple spawning of objects
@ -694,6 +716,7 @@ T = [[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,2,1,0,0,0),Field(1,3,0,0,0,0
[Field(1,0,0,0,0,0),Field(0,2,0,0,0,0),Field(1,1,0,0,0,0),Field(1,0,1,0,0,0),Field(0,2,1,0,0,0),Field(0,3,0,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
[Field(1,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,1,1,0,0,0),Field(1,0,0,0,0,0),Field(0,1,1,0,0,0),Field(0,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,2,1,0,0,0)]]
# =========================================================================================
# no i tutaj mamy główna pętlę programu
@ -753,6 +776,10 @@ while running:
j = j + 1
i = i + 1
font = pygame.font.SysFont('comicsans', 22)
labelx = font.render('temp:22 |rain:none |snow:none |sun:cloudy |time:evening', True, (0, 0, 0))
SCREEN.blit(labelx, (10, 10))
i = 0
while i < len(T)+1:
pygame.draw.line(SCREEN, (0, 0, 0), (50 + i * 50, 50), (50 + i * 50, 50 + len(T) * 50), 1)
@ -775,7 +802,7 @@ while running:
# player seen at the beginning
SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
font = pygame.font.SysFont('comicsans', 22)
label = font.render('F - cel | X - drzewo', True, (0, 0, 0))
label1 = font.render('ARROWS - ręczne poruszanie', True, (0, 0, 0))
label2 = font.render('A - lewo | D - prawo | W - ruch', True, (0, 0, 0))