From 2abfdf623956a6aac5867d0651f4d310ccafa8cc Mon Sep 17 00:00:00 2001 From: Dominik Zawadzki Date: Fri, 5 Jun 2020 12:16:19 +0000 Subject: [PATCH 1/2] Zaktualizuj 'src/decisionTree.py' --- src/decisionTree.py | 281 +++++++++++++++++++++++++------------------- 1 file changed, 157 insertions(+), 124 deletions(-) diff --git a/src/decisionTree.py b/src/decisionTree.py index 88d91dd..1ea08c0 100644 --- a/src/decisionTree.py +++ b/src/decisionTree.py @@ -1,125 +1,158 @@ -import numpy as np -import pandas as pd -import pprint - -from src.graphics import * -from .waiter import Waiter - -eps = np.finfo(float).eps -tasksList = [] -tasksQueue = [] - -class DecisionTree: - def __init__(self): - graphics = Graphics() - self.waiter = Waiter(graphics) - - def BuildDf(self): - actionName = 'order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check'.split(',') - distance = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27'.split(',') - priority = '1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4'.split(',') - - dataset ={'actionName':actionName,'distance':distance,'priority':priority} - df = pd.DataFrame(dataset,columns=['actionName','distance','priority']) - return df - - #Obliczanie entropii dla calego zestawu - def FindPriorityEntropy(self,df): - entropyNode = 0 - values = df.priority.unique() - for value in values: - propability = df.priority.value_counts()[value]/len(df.priority) - entropyNode += -propability*np.log2(propability) - return entropyNode - - #Obliczanie entropii dla wszystkich atrybut�w - def FindAttributesEntropy(self, df, attribute): - targetVariables = df.priority.unique() - variables = df[attribute].unique() - - entropy2 = 0 - for variable in variables: - entropy = 0 - for targetVariable in targetVariables: - num = len(df[attribute][df[attribute]==variable][df.priority == targetVariable]) - den = len(df[attribute][df[attribute]==variable]) - propability = num/(den + eps) - entropy += propability*np.log2(propability+eps) - propability2 = den/len(df) - entropy2 += -propability2*entropy - return abs(entropy2) - - #Znajdowanie wierzcholka o najwyzszym info Gain - def FindWinner(self, df): - infoGain = [] - for key in df.keys()[:-1]: - infoGain.append(self.FindPriorityEntropy(df) - self.FindAttributesEntropy(df, key)) - return df.keys()[:-1][np.argmax(infoGain)] - - def GetSubtable(self, df, node, value): - return df[df[node] == value].reset_index(drop=True) - - #Budowanie drzewa - def BuildTree(self, df, tree=None): - node = self.FindWinner(df) - - attValues = np.unique(df[node]) - - if tree is None: - tree = {} - tree[node] = {} - - for value in attValues: - subtable = self.GetSubtable(df, node, value) - clValue,counts = np.unique(subtable['priority'],return_counts=True) - - if len(counts) == 1: - tree[node][value] = clValue[0] - else: - tree[node][value] = self.BuildTree(subtable) - - return tree - - - #Dodawanie zadan do listy zadan - def TasksList(self, name, coordinate): - waiterNode = self.waiter.Node() - distance = abs(waiterNode[0] - coordinate[0]) + abs(waiterNode[1] - coordinate[1]) - - tasksList.append([name, distance]) - - #Kolejkowanie zadan - def Queue(self, tasksList): - df = self.BuildDf() - tree = self.BuildTree(df) - - winnerNode = self.FindWinner(df) - - for i in tasksList: - if winnerNode is "actionName": - - subtable = tree[winnerNode][i[0]] - if subtable in ['0','1','2','3']: - tasksQueue.append([i[0], i[1], subtable]) - else: - tasksQueue.append([i[0], i[1], tree[winnerNode][i[0]]['distance'][str(i[1])]]) - elif winnerNode is "distance": - - subtable = tree[winnerNode][i[1]] - if subtable in ['0','1','2','3']: - tasksQueue.append([i[0], i[1], subtable]) - else: - tasksQueue.append([i[0], i[1], tree[winnerNode][i[1]]['actionName'][str(i[0])]]) - - - tasksQueue.sort(key=lambda x: x[2]) - print(tasksQueue) - - def print(self): - df = self.BuildDf() - #a_entropy = {k:self.FindAttributesEntropy(df,k) for k in df.keys()[:-1]} - #print(a_entropy) - #print('\n Info Gain: ', self.FindWinner(df)) - print(tasksList) - self.Queue(tasksList) +import numpy as np +import pandas as pd +import pprint + +from .matrix import Matrix +from src.graphics import * +from .waiter import Waiter + +eps = np.finfo(float).eps +tasksList = [] +tasksQueue = [] + + +class DecisionTree: + def __init__(self): + graphics = Graphics() + self.waiter = Waiter(graphics) + self.matrix = Matrix(graphics=graphics) + + def BuildDf(self): + actionName = 'order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,order,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,goToBar,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,eat,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check,check'.split(',') + distance = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27'.split(',') + priority = '1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4'.split(',') + + dataset ={'actionName':actionName,'distance':distance,'priority':priority} + df = pd.DataFrame(dataset,columns=['actionName','distance','priority']) + return df + + #Obliczanie entropii dla calego zestawu + def FindPriorityEntropy(self,df): + entropyNode = 0 + values = df.priority.unique() + for value in values: + propability = df.priority.value_counts()[value]/len(df.priority) + entropyNode += -propability*np.log2(propability) + return entropyNode + + #Obliczanie entropii dla wszystkich atrybut�w + def FindAttributesEntropy(self, df, attribute): + targetVariables = df.priority.unique() + variables = df[attribute].unique() + + entropy2 = 0 + for variable in variables: + entropy = 0 + for targetVariable in targetVariables: + num = len(df[attribute][df[attribute]==variable][df.priority == targetVariable]) + den = len(df[attribute][df[attribute]==variable]) + propability = num/(den + eps) + entropy += propability*np.log2(propability+eps) + propability2 = den/len(df) + entropy2 += -propability2*entropy + return abs(entropy2) + + #Znajdowanie wierzcholka o najwyzszym info Gain + def FindWinner(self, df): + infoGain = [] + for key in df.keys()[:-1]: + infoGain.append(self.FindPriorityEntropy(df) - self.FindAttributesEntropy(df, key)) + return df.keys()[:-1][np.argmax(infoGain)] + + def GetSubtable(self, df, node, value): + return df[df[node] == value].reset_index(drop=True) + + #Budowanie drzewa + def BuildTree(self, df, tree=None): + node = self.FindWinner(df) + + attValues = np.unique(df[node]) + + if tree is None: + tree = {} + tree[node] = {} + + for value in attValues: + subtable = self.GetSubtable(df, node, value) + clValue,counts = np.unique(subtable['priority'],return_counts=True) + + if len(counts) == 1: + tree[node][value] = clValue[0] + else: + tree[node][value] = self.BuildTree(subtable) + + return tree + + + + #Dodawanie zadan do listy zadan + def TasksList(self, name, coordinate): + distance = [] + waiterNode = [self.waiter.X, self.waiter.Y] + if name != "goToBar": + if self.matrix.matrix[coordinate[0] - 1][coordinate[1] - 1].walk_through == 1: + distance.append([abs(waiterNode[0] - (coordinate[0] - 1)) + abs(waiterNode[1] - (coordinate[1] - 1)), [coordinate[0] - 1, coordinate[1] - 1]]) + if self.matrix.matrix[coordinate[0] + 1][coordinate[1] - 1].walk_through == 1: + distance.append([abs(waiterNode[0] - (coordinate[0] + 1)) + abs(waiterNode[1] - (coordinate[1] - 1)), [coordinate[0] + 1, coordinate[1] - 1]]) + if self.matrix.matrix[coordinate[0] + 1][coordinate[1]].walk_through == 1: + distance.append([abs(coordinate[0] - (coordinate[0] + 1)) + abs(waiterNode[1] - coordinate[1]), [coordinate[0] + 1, coordinate[1] ]]) + if self.matrix.matrix[coordinate[0] + 1][coordinate[1] - 1].walk_through == 1: + distance.append([abs(waiterNode[0] - (coordinate[0] + 1)) + abs(waiterNode[1] - (coordinate[1] - 1)), [coordinate[0] + 1, coordinate[1] - 1]]) + if self.matrix.matrix[coordinate[0] - 1][coordinate[1] + 1].walk_through == 1: + distance.append([abs(waiterNode[0] - (coordinate[0] - 1)) + abs(waiterNode[1] - (coordinate[1] + 1)), [coordinate[0] - 1, coordinate[1] + 1]]) + + else: + distance.append([abs(waiterNode[0] - 0) + abs(waiterNode[1] - 12), [0, 12]]) + distance.append([abs(waiterNode[0] - 1) + abs(waiterNode[1] - 12), [1, 12]]) + distance.append([abs(waiterNode[0] - 2) + abs(waiterNode[1] - 12), [2, 12]]) + distance.append([abs(waiterNode[0] - 3) + abs(waiterNode[1] - 12), [3, 12]]) + distance.append([abs(waiterNode[0] - 4) + abs(waiterNode[1] - 12), [4, 12]]) + distance.append([abs(waiterNode[0] - 5) + abs(waiterNode[1] - 13), [5, 13]]) + distance.append([abs(waiterNode[0] - 5) + abs(waiterNode[1] - 14), [5, 14]]) + + distance.sort(key=lambda x: x[0]) + tasksList.append([name, distance[0][0], distance[0][1]]) + + + #Kolejkowanie zadan + def Queue(self, tasksList): + df = self.BuildDf() + tree = self.BuildTree(df) + winnerNode = self.FindWinner(df) + + for i in tasksList: + if winnerNode is "actionName": + + subtable = tree[winnerNode][i[0]] + if subtable in ['0','1','2','3']: + tasksQueue.append([i[0], i[1], i[2], subtable]) + else: + tasksQueue.append([i[0], i[1], i[2], tree[winnerNode][i[0]]['distance'][str(i[1])]]) + elif winnerNode is "distance": + + subtable = tree[winnerNode][i[1]] + if subtable in ['0','1','2','3']: + tasksQueue.append([i[0], i[1], i[2], subtable]) + else: + tasksQueue.append([i[0], i[1], i[2], tree[winnerNode][i[1]]['actionName'][str(i[0])]]) + + + tasksQueue.sort(key=lambda x: x[3]) + return tasksQueue + + + def ReturnQueueList(self): + if tasksQueue == []: + queue = self.Queue(tasksList) + else: + queue = tasksQueue + + while queue[0][3] == '0': + queue.pop(0) + + print(queue) + return queue + + def print(self): + print(tasksList) \ No newline at end of file From 807eb522beec25185e235f6e7fc132f9cad5dd2f Mon Sep 17 00:00:00 2001 From: Dominik Zawadzki Date: Fri, 5 Jun 2020 12:18:45 +0000 Subject: [PATCH 2/2] Zaktualizuj 'main.py' --- main.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 4f17296..25fb91c 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,10 @@ if __name__ == "__main__": # AStar goal = None path = '' + + #Dominik + check = 0 + queue = [] # Marcin Dobrowolski suggestionTreeRoot = SuggestionTree.buildTree(trainingData) @@ -48,13 +52,15 @@ if __name__ == "__main__": break if event.key == pygame.K_s: - tree.TasksList('check', [10, 3]) - tree.TasksList('eat', [5, 12]) - tree.TasksList('order', [12, 4]) - tree.TasksList('goToBar', [10, 10]) - tree.TasksList('check', [3, 4]) - tree.TasksList('eat', [10, 5]) + tree.TasksList('check', [2,3]) + tree.TasksList('eat', [9, 6]) + tree.TasksList('order', [4, 8]) + tree.TasksList('goToBar', [11, 4]) + tree.TasksList('check', [2, 9]) + tree.TasksList('eat', [1, 1]) + queue = tree.ReturnQueueList() tree.print() + check = 1 if event.key == pygame.K_m: @@ -142,6 +148,20 @@ if __name__ == "__main__": goal = (x, y) path = waiter.findPath(goal) path = waiter.translatePath(path) + + # Dominik + if check == 1: + task = queue.pop(0) + goal = (task[2][0], task[2][1]) + print(goal) + path = waiter.findPath(goal) + print(path) + path = waiter.translatePath(path) + print(path) + check = 0 + + if len(queue) != 0 and check == 0 and path == '': + check = 1 # AStar if path != '':