import copy from src.obj.Object import Object class Waiter(Object): def __init__(self, position, orientation, square_size, screen_size, basket=[], memory=[], battery=300): 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) 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 return state def dish_in_basket(self, table) -> bool: return table in self.basket self.calcTree() return state def calcTree(self): from sklearn import tree import pandas as pd # for manipulating the csv data 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) 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']) with open('decision_tree.txt', 'w') as f: f.write(tree_text) # Save the visualization as a text 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] predicted_label = clf.predict([new_example]) if predicted_label[0] > 0: result = "YES" else: result = "NO" print("Predicted Label:", result) def calcTreePDF(self): from sklearn import tree import pandas as pd # for manipulating the csv data import numpy as np import graphviz import os os.environ["PATH"] += os.pathsep + \ 'C:/Program Files (x86)/Graphviz/bin/' # importing the dataset from the disk train_data_m = np.genfromtxt( "dataset/converted_dataset.csv", delimiter=",", skip_header=1) 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']) 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) 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] predicted_label = clf.predict([new_example]) if predicted_label[0] > 0: result = "YES" else: result = "NO" print("Predicted Label:", result) def dampState(self): self.prev_position = copy.deepcopy(self.position) self.prev_orientation = copy.copy(self.orientation) 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)