using decisiontree to predict

This commit is contained in:
Kosterix08 2024-06-11 18:14:54 +02:00
parent 5932ba1f93
commit fc75709739
8 changed files with 159 additions and 51 deletions

16
app.py
View File

@ -11,11 +11,16 @@ import threading
import time
import random
from classes.data.klient import Klient
from classes.data.klient import Klient
import xml.etree.ElementTree as ET
from decisiontree import predict_client
pygame.init()
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
pygame.display.set_caption("Game Window")
table_coords = [(4, 4), (4, prefs.GRID_SIZE-5), (prefs.GRID_SIZE-5, 4), (prefs.GRID_SIZE-5, prefs.GRID_SIZE-5)]
from classes.data.data_initializer import clients
chosen_coords = random.choice(table_coords)
chosen_index = random.randint(0, len(table_coords)-1)
@ -117,6 +122,7 @@ def watekDlaSciezkiAgenta():
time.sleep(1)
def watekDlaSciezkiKlienta():
assigned = False
time.sleep(3)
while True:
if len(path2) > 0:
@ -132,10 +138,18 @@ def watekDlaSciezkiKlienta():
x, y = element2
klient.moveto(x, y)
if klient.current_cell == cells[klientx_target][klienty_target]:
if not assigned and klient.current_cell == cells[klientx_target][klienty_target]:
klient.przyStoliku = True
klient.stolik = klient.current_cell
random_client_data = random.choice(clients)
prediction = predict_client(random_client_data)
print("\nClient data:")
print(random_client_data)
print("Prediction (Adult):", prediction)
assigned = True
if assigned:
break
time.sleep(1)

View File

@ -290,3 +290,5 @@ class Agent:

View File

@ -1,9 +1,11 @@
import xml.etree.ElementTree as ET
from posilek import Posilek
from klient import Klient
from kelner import Kelner
from stolik import Stolik
from zamowenie import Zamowienie
from classes.data.posilek import Posilek
from classes.data.klient import KlientCechy
from classes.data.klient import Klient
from classes.data.kelner import Kelner
from classes.data.stolik import Stolik
from classes.data.zamowenie import Zamowienie
with open('database\\meals.xml', 'r') as file:
xml_data = file.read()
tree = ET.ElementTree(ET.fromstring(xml_data))
@ -71,15 +73,47 @@ for person in root.findall('person'):
if favorite_meal in [meal.nazwa for meal in meals]:
favorite_meal = next((meal for meal in meals if meal.nazwa == favorite_meal), None)
wrinkles_element = person.find('Wrinkles')
wrinkles = wrinkles_element.text if wrinkles_element is not None else ''
balding_element = person.find('Balding')
balding = balding_element.text if balding_element is not None else ''
beard_element = person.find('Beard')
beard = beard_element.text if beard_element is not None else ''
outfit_element = person.find('Outfit')
outfit = outfit_element.text if outfit_element is not None else ''
glasses_element = person.find('Glasses')
glasses = glasses_element.text if glasses_element is not None else ''
tattoo_element = person.find('Tattoo')
tattoo = tattoo_element.text if tattoo_element is not None else ''
hair_element = person.find('Hair')
hair = hair_element.text if hair_element is not None else ''
behaviour_element = person.find('Behaviour')
behaviour = behaviour_element.text if behaviour_element is not None else ''
person_data = {
'imie': name,
'nazwisko': surname,
'wiek': age,
'ulubiony_posilek': favorite_meal,
'restrykcje_dietowe': restrictions
'restrykcje_dietowe': restrictions,
'zmarszczki': wrinkles,
'lysienie': balding,
'broda': beard,
'ubior': outfit,
'okulary': glasses,
'tatuaz': tattoo,
'wlosy': hair,
'zachowanie': behaviour
}
clients.append(Klient(**person_data))
clients.append(KlientCechy(**person_data))
# Use the person_instance as needed
@ -109,9 +143,6 @@ for server in root.findall('server'):
servers.append(Kelner(**server_data))
# Prezentacja uzycia przykładowego
import random
stoliki = []
@ -143,7 +174,7 @@ for klient in [k for k in clients if k.stolik is not None]:
print("----------------\n\n")
import logic_test as logic
import classes.data.logic_test as logic
for klient in [k for k in clients if k.stolik is not None]:
for zamownienie in klient.rachunek.zamowienia:

View File

@ -4,6 +4,8 @@ import prefs
import random
import heapq
from collections import deque
from classes.data.rachunek import Rachunek
class Klient:
def __init__(self,x,y,cells):
@ -12,14 +14,7 @@ class Klient:
self.current_cell = cells[x][y]
self.current_x = x
self.current_y = y
# self.imie = imie
# self.nazwisko = nazwisko
# self.wiek = wiek
przyStoliku = False
self.stolik = None
# self.rachunek = Rachunek(random.randint(1,1000))
# self.ulubiony_posilek = ulubiony_posilek
# self.restrykcje_dietowe = restrykcje_dietowe
self.cells = cells
self.X = x
self.Y = y
@ -97,19 +92,6 @@ class Klient:
print(self.direction)
def zloz_zamowienie(self,zamowienie,stolik):
if self.stolik is None:
self.stolik = stolik
stolik.przypisz_kelner(stolik.kelner)
self.rachunek.dodaj_zamowienie(zamowienie)
print(f"Klinet {self.imie} {self.nazwisko} zlozyl zamowienie przy stoliku {stolik.numer_stolika} i przyjal je kelner {stolik.kelner.numer_pracowniczy}.")
else:
print("Klient ma juz przypisany stolik.")
def __str__(self):
return f"Klient: {self.imie} {self.nazwisko} {self.wiek}, ulubione Danie: {self.ulubiony_posilek}, restrykcje diet: {self.restrykcje_dietowe}"
def get_possible_moves(self):
possible_moves = []
@ -199,3 +181,33 @@ class Klient:
dy = abs(current[1] - target[1])
return dx + dy
class KlientCechy:
def __init__(self,imie,nazwisko,wiek,ulubiony_posilek,restrykcje_dietowe,zmarszczki, lysienie, broda, ubior, okulary, tatuaz, wlosy, zachowanie):
self.imie = imie
self.nazwisko = nazwisko
self.wiek = wiek
self.ulubiony_posilek = ulubiony_posilek
self.restrykcje_dietowe = restrykcje_dietowe
self.zmarszczki = zmarszczki
self.lysienie = lysienie
self.broda = broda
self.ubior = ubior
self.okulary = okulary
self.tatuaz = tatuaz
self.wlosy = wlosy
self.zachowanie = zachowanie
self.stolik = None
self.rachunek = Rachunek(random.randint(1,1000))
def zloz_zamowienie(self,zamowienie,stolik):
if self.stolik is None:
self.stolik = stolik
stolik.przypisz_kelner(stolik.kelner)
self.rachunek.dodaj_zamowienie(zamowienie)
print(f"Klinet {self.imie} {self.nazwisko} zlozyl zamowienie przy stoliku {stolik.numer_stolika} i przyjal je kelner {stolik.kelner.numer_pracowniczy}.")
else:
print("Klient ma juz przypisany stolik.")
def __str__(self):
return f"Klient: {self.imie} {self.nazwisko} {self.wiek}, ulubione Danie: {self.ulubiony_posilek}, restrykcje diet: {self.restrykcje_dietowe}. Jego cechy to: zmarszczki: {self.zmarszczki}, lysienie: {self.lysienie}, broda: {self.broda}, ubior: {self.ubior}, okulary: {self.okulary}, tatuaz: {self.tatuaz}, wlosy: {self.wlosy}, zachowanie: {self.zachowanie}"

View File

@ -1,6 +1,6 @@
import pygame
from zamowenie import Zamowienie
from classes.data.zamowenie import Zamowienie
class Rachunek:
def __init__(self,numer_zamowienia):
self.numer_zamowienia = numer_zamowienia

View File

@ -1,7 +1,7 @@
import pygame
from stolik import Stolik
from kelner import Kelner
from posilek import Posilek
from classes.data.stolik import Stolik
from classes.data.kelner import Kelner
from classes.data.posilek import Posilek
class Zamowienie:
def __init__(self,numer_zamowienia,posilek):
self.numer_zamowienia = numer_zamowienia

View File

@ -5,6 +5,14 @@
<age>21</age>
<favoriteMeal>Tatar</favoriteMeal>
<restrictions>Meat</restrictions>
<Wrinkles>No</Wrinkles>
<Balding>Yes</Balding>
<Beard>Yes</Beard>
<Outfit>Messy</Outfit>
<Glasses>No</Glasses>
<Tattoo>No</Tattoo>
<Hair>Color</Hair>
<Behaviour>Energetic</Behaviour>
</person>
<person>
<name>Kamil</name>
@ -12,6 +20,14 @@
<age>17</age>
<favoriteMeal>Pomidorowa z Makaronem</favoriteMeal>
<restrictions>Vegetarian</restrictions>
<Wrinkles>No</Wrinkles>
<Balding>No</Balding>
<Beard>No</Beard>
<Outfit>Messy</Outfit>
<Glasses>No</Glasses>
<Tattoo>No</Tattoo>
<Hair>Color</Hair>
<Behaviour>Energetic</Behaviour>
</person>
<person>
<name>Jon</name>
@ -19,7 +35,14 @@
<age>23</age>
<favoriteMeal>Grochówka</favoriteMeal>
<restrictions>Vegan</restrictions>
<Wrinkles>No</Wrinkles>
<Balding>No</Balding>
<Beard>No</Beard>
<Outfit>Messy</Outfit>
<Glasses>No</Glasses>
<Tattoo>No</Tattoo>
<Hair>Color</Hair>
<Behaviour>Energetic</Behaviour>
</person>
<person>
<name>Andrzej</name>
@ -27,5 +50,13 @@
<age>44</age>
<favoriteMeal>Spaghetti Bolognese</favoriteMeal>
<restrictions>Meat</restrictions>
<Wrinkles>No</Wrinkles>
<Balding>No</Balding>
<Beard>No</Beard>
<Outfit>Messy</Outfit>
<Glasses>No</Glasses>
<Tattoo>No</Tattoo>
<Hair>Color</Hair>
<Behaviour>Energetic</Behaviour>
</person>
</people>

View File

@ -38,13 +38,13 @@ clf_en = DecisionTreeClassifier(criterion='entropy', max_depth=3, random_state=0
clf_en.fit(X_train, y_train)
y_pred_en = clf_en.predict(X_test)
print('Model accuracy score with criterion entropy: {0:0.4f}'.format(accuracy_score(y_test, y_pred_en)))
#print('Model accuracy score with criterion entropy: {0:0.4f}'.format(accuracy_score(y_test, y_pred_en)))
y_pred_train_en = clf_en.predict(X_train)
print('Training-set accuracy score: {0:0.4f}'.format(accuracy_score(y_train, y_pred_train_en)))
#print('Training-set accuracy score: {0:0.4f}'.format(accuracy_score(y_train, y_pred_train_en)))
print('Training set score: {:.4f}'.format(clf_en.score(X_train, y_train)))
print('Test set score: {:.4f}'.format(clf_en.score(X_test, y_test)))
#print('Training set score: {:.4f}'.format(clf_en.score(X_train, y_train)))
#print('Test set score: {:.4f}'.format(clf_en.score(X_test, y_test)))
dot_data = tree.export_graphviz(clf_en, out_file=None,
feature_names=X_train.columns,
@ -53,7 +53,7 @@ dot_data = tree.export_graphviz(clf_en, out_file=None,
special_characters=True)
#nowy klient testowo
new_client = {
"""new_client = {
"Wrinkles": random.choice(['Yes', 'No']),
"Balding": random.choice(['Yes', 'No']),
"Beard": random.choice(['Yes', 'No']),
@ -62,15 +62,33 @@ new_client = {
"Tattoo": random.choice(['Yes', 'No']),
"Hair": random.choice(['Color', 'Grey', 'Natural']),
"Behaviour": random.choice(['Energetic', 'Stressed', 'Calm'])
}
} """
def predict_client(client_data):
new_client = {
"Wrinkles": client_data.zmarszczki,
"Balding": client_data.lysienie,
"Beard": client_data.broda,
"Outfit": client_data.ubior,
"Glasses": client_data.okulary,
"Tattoo": client_data.tatuaz,
"Hair": client_data.wlosy,
"Behaviour": client_data.zachowanie
}
new_client_df = pd.DataFrame(new_client, index=[0])
new_client_df_encoded = encoder.transform(new_client_df)
prediction = clf_en.predict(new_client_df_encoded)
print('Model accuracy score with criterion entropy: {0:0.4f}'.format(accuracy_score(y_test, y_pred_en)))
print('Training-set accuracy score: {0:0.4f}'.format(accuracy_score(y_train, y_pred_train_en)))
print("\nNew client:")
print(new_client_df)
print("Prediction:", prediction[0])
print('Training set score: {:.4f}'.format(clf_en.score(X_train, y_train)))
print('Test set score: {:.4f}'.format(clf_en.score(X_test, y_test)))
return prediction[0]
#print("\nNew client:")
#print(new_client_df)
#print("Prediction:", prediction[0])
#graph = graphviz.Source(dot_data)
#graph.render("decision_tree", format='png')