automatyczny_kelner/src/obj/Waiter.py

163 lines
6.0 KiB
Python
Raw Normal View History

import copy
from src.obj.Object import Object
class Waiter(Object):
def __init__(self, position, orientation, square_size, screen_size, basket=[], memory=[], battery=300):
2023-05-14 14:23:37 +02:00
super().__init__("waiter", position, orientation, square_size, screen_size)
self.battery = battery
self.basket_size = 4
self.memory_size = 4
self.basket = basket
self.memory = memory
self.prev_position = copy.deepcopy(self.position)
self.prev_orientation = copy.copy(self.orientation)
2023-05-14 14:23:37 +02:00
def changeState(self, state):
self.position = copy.deepcopy(state.position)
self.orientation = copy.copy(state.orientation)
self.basket = copy.copy(state.basket)
self.battery -= state.cost
2023-05-26 03:26:14 +02:00
# self.calcTree()
2023-05-14 14:23:37 +02:00
return state
def dish_in_basket(self, table) -> bool:
return table in self.basket
2023-05-14 14:23:37 +02:00
2023-05-26 03:26:14 +02:00
'''
2023-05-26 01:29:28 +02:00
def calcTree(self):
from sklearn import tree
import pandas as pd # for manipulating the csv data
2023-05-26 01:29:28 +02:00
import numpy as np
import os
# importing the dataset from the disk
train_data_m = np.genfromtxt(
"dataset/converted_dataset.csv", delimiter=",", skip_header=1)
2023-05-26 01:29:28 +02:00
X_train = [data[:-1] for data in train_data_m]
y_train = [data[-1] for data in train_data_m]
# Create the decision tree classifier using the ID3 algorithm
clf = tree.DecisionTreeClassifier(criterion='entropy')
# Train the decision tree on the training data
clf.fit(X_train, y_train)
# Visualize the trained decision tree
tree_text = tree.export_text(clf, feature_names=[
'Battery Charge', 'Fullness', 'Ready orders', 'Waiting tables', 'Availability', 'Cleanliness', 'Error'])
2023-05-26 01:29:28 +02:00
with open('decision_tree.txt', 'w') as f:
f.write(tree_text) # Save the visualization as a text file
2023-05-26 01:29:28 +02:00
# Test the decision tree with a new example
# Battery Charge,Fullness,Ready orders,Waiting tables,Availability,Cleanliness,Error
new_example = [self.battery, 0, self.orderReadiness,
self.waitingTables, self.availability, self.cleanliness, self.error]
2023-05-26 01:29:28 +02:00
predicted_label = clf.predict([new_example])
if predicted_label[0] > 0:
result = "YES"
2023-05-26 01:29:28 +02:00
else:
result = "NO"
2023-05-26 01:29:28 +02:00
print("Predicted Label:", result)
2023-05-26 01:29:28 +02:00
def calcTreePDF(self):
from sklearn import tree
import pandas as pd # for manipulating the csv data
2023-05-26 01:29:28 +02:00
import numpy as np
import graphviz
import os
os.environ["PATH"] += os.pathsep + \
'C:/Program Files (x86)/Graphviz/bin/'
2023-05-26 01:29:28 +02:00
# importing the dataset from the disk
train_data_m = np.genfromtxt(
"dataset/converted_dataset.csv", delimiter=",", skip_header=1)
2023-05-26 01:29:28 +02:00
X_train = [data[:-1] for data in train_data_m]
y_train = [data[-1] for data in train_data_m]
# Create the decision tree classifier using the ID3 algorithm
clf = tree.DecisionTreeClassifier(criterion='entropy')
# Train the decision tree on the training data
clf.fit(X_train, y_train)
# Visualize the trained decision tree
tree_text = tree.export_text(clf, feature_names=[
'Battery Charge', 'Fullness', 'Ready orders', 'Waiting tables', 'Availability', 'Cleanliness', 'Error'])
2023-05-26 01:29:28 +02:00
with open('decision_tree.txt', 'w') as f:
f.write(tree_text) # Save the visualization as a text file
dot_data = tree.export_graphviz(clf, out_file=None, feature_names=[
'Battery Charge', 'Fullness', 'Ready orders', 'Waiting tables', 'Availability', 'Cleanliness', 'Error'], class_names=['NO', 'YES'], filled=True, rounded=True)
2023-05-26 01:29:28 +02:00
graph = graphviz.Source(dot_data)
graph.render("decision_tree") # Save the visualization as a PDF file
# Test the decision tree with a new example
# Battery Charge,Fullness,Ready orders,Waiting tables,Availability,Cleanliness,Error
new_example = [self.battery, 0, self.orderReadiness,
self.waitingTables, self.availability, self.cleanliness, self.error]
2023-05-26 01:29:28 +02:00
predicted_label = clf.predict([new_example])
if predicted_label[0] > 0:
result = "YES"
2023-05-26 01:29:28 +02:00
else:
result = "NO"
2023-05-26 01:29:28 +02:00
print("Predicted Label:", result)
2023-05-26 03:26:14 +02:00
'''
def basket_is_full(self) -> bool:
return self.basket_size == 0
def combine_orders(self):
while not self.basket_is_full() and not self.memory_is_empty():
dish = self.memory.pop()
dish.set_done()
self.basket.append(dish)
self.basket_size -= 1
self.memory_size += 1
def deliver_dish(self, table):
if table in self.basket:
table.reset()
self.basket.remove(table)
self.basket_size += 1
def order_in_memory(self, table) -> bool:
return table in self.memory
def memory_is_empty(self) -> bool:
return not self.memory
def memory_is_full(self) -> bool:
return self.memory_size == 0
def collect_order(self, table):
if self.memory_is_full():
return
if table.agent_role == "order":
table.set_wait()
self.memory.append(table)
self.memory_size -= 1
def battary_status(self) -> str:
if self.battery >= 200:
return "hight"
elif self.battery >= 100:
return "medium"
else:
return "low"
def recharge(self):
self.battery = 300
def left(self):
self.orientation = (self.orientation + 1) % 4
def right(self):
self.orientation = (self.orientation - 1) % 4
def front(self):
if self.orientation % 2: # x (1 or 3)
self.position[0] += self.orientation - 2 # x (-1 or +1)
else: # y (0 or 2)
self.position[1] += self.orientation - 1 # y (-1 or +1)