Zaktualizuj 'src/decisionTree.py'

This commit is contained in:
Dominik Zawadzki 2020-06-05 12:16:19 +00:00
parent d89e37e6eb
commit 2abfdf6239

View File

@ -2,6 +2,7 @@ import numpy as np
import pandas as pd import pandas as pd
import pprint import pprint
from .matrix import Matrix
from src.graphics import * from src.graphics import *
from .waiter import Waiter from .waiter import Waiter
@ -9,15 +10,17 @@ eps = np.finfo(float).eps
tasksList = [] tasksList = []
tasksQueue = [] tasksQueue = []
class DecisionTree: class DecisionTree:
def __init__(self): def __init__(self):
graphics = Graphics() graphics = Graphics()
self.waiter = Waiter(graphics) self.waiter = Waiter(graphics)
self.matrix = Matrix(graphics=graphics)
def BuildDf(self): 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(',') 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(',') 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(',') 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} dataset ={'actionName':actionName,'distance':distance,'priority':priority}
df = pd.DataFrame(dataset,columns=['actionName','distance','priority']) df = pd.DataFrame(dataset,columns=['actionName','distance','priority'])
@ -81,18 +84,40 @@ class DecisionTree:
return tree return tree
#Dodawanie zadan do listy zadan #Dodawanie zadan do listy zadan
def TasksList(self, name, coordinate): def TasksList(self, name, coordinate):
waiterNode = self.waiter.Node() distance = []
distance = abs(waiterNode[0] - coordinate[0]) + abs(waiterNode[1] - coordinate[1]) 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]])
tasksList.append([name, distance])
#Kolejkowanie zadan #Kolejkowanie zadan
def Queue(self, tasksList): def Queue(self, tasksList):
df = self.BuildDf() df = self.BuildDf()
tree = self.BuildTree(df) tree = self.BuildTree(df)
winnerNode = self.FindWinner(df) winnerNode = self.FindWinner(df)
for i in tasksList: for i in tasksList:
@ -100,26 +125,34 @@ class DecisionTree:
subtable = tree[winnerNode][i[0]] subtable = tree[winnerNode][i[0]]
if subtable in ['0','1','2','3']: if subtable in ['0','1','2','3']:
tasksQueue.append([i[0], i[1], subtable]) tasksQueue.append([i[0], i[1], i[2], subtable])
else: else:
tasksQueue.append([i[0], i[1], tree[winnerNode][i[0]]['distance'][str(i[1])]]) tasksQueue.append([i[0], i[1], i[2], tree[winnerNode][i[0]]['distance'][str(i[1])]])
elif winnerNode is "distance": elif winnerNode is "distance":
subtable = tree[winnerNode][i[1]] subtable = tree[winnerNode][i[1]]
if subtable in ['0','1','2','3']: if subtable in ['0','1','2','3']:
tasksQueue.append([i[0], i[1], subtable]) tasksQueue.append([i[0], i[1], i[2], subtable])
else: else:
tasksQueue.append([i[0], i[1], tree[winnerNode][i[1]]['actionName'][str(i[0])]]) tasksQueue.append([i[0], i[1], i[2], tree[winnerNode][i[1]]['actionName'][str(i[0])]])
tasksQueue.sort(key=lambda x: x[2]) tasksQueue.sort(key=lambda x: x[3])
print(tasksQueue) 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): 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) print(tasksList)
self.Queue(tasksList)