Zaktualizuj 'src/decisionTree.py'
This commit is contained in:
parent
d89e37e6eb
commit
2abfdf6239
@ -1,125 +1,158 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
from src.graphics import *
|
from .matrix import Matrix
|
||||||
from .waiter import Waiter
|
from src.graphics import *
|
||||||
|
from .waiter import Waiter
|
||||||
eps = np.finfo(float).eps
|
|
||||||
tasksList = []
|
eps = np.finfo(float).eps
|
||||||
tasksQueue = []
|
tasksList = []
|
||||||
|
tasksQueue = []
|
||||||
class DecisionTree:
|
|
||||||
def __init__(self):
|
|
||||||
graphics = Graphics()
|
class DecisionTree:
|
||||||
self.waiter = Waiter(graphics)
|
def __init__(self):
|
||||||
|
graphics = Graphics()
|
||||||
def BuildDf(self):
|
self.waiter = Waiter(graphics)
|
||||||
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(',')
|
self.matrix = Matrix(graphics=graphics)
|
||||||
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(',')
|
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(',')
|
||||||
dataset ={'actionName':actionName,'distance':distance,'priority':priority}
|
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(',')
|
||||||
df = pd.DataFrame(dataset,columns=['actionName','distance','priority'])
|
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(',')
|
||||||
return df
|
|
||||||
|
dataset ={'actionName':actionName,'distance':distance,'priority':priority}
|
||||||
#Obliczanie entropii dla calego zestawu
|
df = pd.DataFrame(dataset,columns=['actionName','distance','priority'])
|
||||||
def FindPriorityEntropy(self,df):
|
return df
|
||||||
entropyNode = 0
|
|
||||||
values = df.priority.unique()
|
#Obliczanie entropii dla calego zestawu
|
||||||
for value in values:
|
def FindPriorityEntropy(self,df):
|
||||||
propability = df.priority.value_counts()[value]/len(df.priority)
|
entropyNode = 0
|
||||||
entropyNode += -propability*np.log2(propability)
|
values = df.priority.unique()
|
||||||
return entropyNode
|
for value in values:
|
||||||
|
propability = df.priority.value_counts()[value]/len(df.priority)
|
||||||
#Obliczanie entropii dla wszystkich atrybut<75>w
|
entropyNode += -propability*np.log2(propability)
|
||||||
def FindAttributesEntropy(self, df, attribute):
|
return entropyNode
|
||||||
targetVariables = df.priority.unique()
|
|
||||||
variables = df[attribute].unique()
|
#Obliczanie entropii dla wszystkich atrybut<75>w
|
||||||
|
def FindAttributesEntropy(self, df, attribute):
|
||||||
entropy2 = 0
|
targetVariables = df.priority.unique()
|
||||||
for variable in variables:
|
variables = df[attribute].unique()
|
||||||
entropy = 0
|
|
||||||
for targetVariable in targetVariables:
|
entropy2 = 0
|
||||||
num = len(df[attribute][df[attribute]==variable][df.priority == targetVariable])
|
for variable in variables:
|
||||||
den = len(df[attribute][df[attribute]==variable])
|
entropy = 0
|
||||||
propability = num/(den + eps)
|
for targetVariable in targetVariables:
|
||||||
entropy += propability*np.log2(propability+eps)
|
num = len(df[attribute][df[attribute]==variable][df.priority == targetVariable])
|
||||||
propability2 = den/len(df)
|
den = len(df[attribute][df[attribute]==variable])
|
||||||
entropy2 += -propability2*entropy
|
propability = num/(den + eps)
|
||||||
return abs(entropy2)
|
entropy += propability*np.log2(propability+eps)
|
||||||
|
propability2 = den/len(df)
|
||||||
#Znajdowanie wierzcholka o najwyzszym info Gain
|
entropy2 += -propability2*entropy
|
||||||
def FindWinner(self, df):
|
return abs(entropy2)
|
||||||
infoGain = []
|
|
||||||
for key in df.keys()[:-1]:
|
#Znajdowanie wierzcholka o najwyzszym info Gain
|
||||||
infoGain.append(self.FindPriorityEntropy(df) - self.FindAttributesEntropy(df, key))
|
def FindWinner(self, df):
|
||||||
return df.keys()[:-1][np.argmax(infoGain)]
|
infoGain = []
|
||||||
|
for key in df.keys()[:-1]:
|
||||||
def GetSubtable(self, df, node, value):
|
infoGain.append(self.FindPriorityEntropy(df) - self.FindAttributesEntropy(df, key))
|
||||||
return df[df[node] == value].reset_index(drop=True)
|
return df.keys()[:-1][np.argmax(infoGain)]
|
||||||
|
|
||||||
#Budowanie drzewa
|
def GetSubtable(self, df, node, value):
|
||||||
def BuildTree(self, df, tree=None):
|
return df[df[node] == value].reset_index(drop=True)
|
||||||
node = self.FindWinner(df)
|
|
||||||
|
#Budowanie drzewa
|
||||||
attValues = np.unique(df[node])
|
def BuildTree(self, df, tree=None):
|
||||||
|
node = self.FindWinner(df)
|
||||||
if tree is None:
|
|
||||||
tree = {}
|
attValues = np.unique(df[node])
|
||||||
tree[node] = {}
|
|
||||||
|
if tree is None:
|
||||||
for value in attValues:
|
tree = {}
|
||||||
subtable = self.GetSubtable(df, node, value)
|
tree[node] = {}
|
||||||
clValue,counts = np.unique(subtable['priority'],return_counts=True)
|
|
||||||
|
for value in attValues:
|
||||||
if len(counts) == 1:
|
subtable = self.GetSubtable(df, node, value)
|
||||||
tree[node][value] = clValue[0]
|
clValue,counts = np.unique(subtable['priority'],return_counts=True)
|
||||||
else:
|
|
||||||
tree[node][value] = self.BuildTree(subtable)
|
if len(counts) == 1:
|
||||||
|
tree[node][value] = clValue[0]
|
||||||
return tree
|
else:
|
||||||
|
tree[node][value] = self.BuildTree(subtable)
|
||||||
|
|
||||||
#Dodawanie zadan do listy zadan
|
return tree
|
||||||
def TasksList(self, name, coordinate):
|
|
||||||
waiterNode = self.waiter.Node()
|
|
||||||
distance = abs(waiterNode[0] - coordinate[0]) + abs(waiterNode[1] - coordinate[1])
|
|
||||||
|
#Dodawanie zadan do listy zadan
|
||||||
tasksList.append([name, distance])
|
def TasksList(self, name, coordinate):
|
||||||
|
distance = []
|
||||||
#Kolejkowanie zadan
|
waiterNode = [self.waiter.X, self.waiter.Y]
|
||||||
def Queue(self, tasksList):
|
if name != "goToBar":
|
||||||
df = self.BuildDf()
|
if self.matrix.matrix[coordinate[0] - 1][coordinate[1] - 1].walk_through == 1:
|
||||||
tree = self.BuildTree(df)
|
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:
|
||||||
winnerNode = self.FindWinner(df)
|
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:
|
||||||
for i in tasksList:
|
distance.append([abs(coordinate[0] - (coordinate[0] + 1)) + abs(waiterNode[1] - coordinate[1]), [coordinate[0] + 1, coordinate[1] ]])
|
||||||
if winnerNode is "actionName":
|
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]])
|
||||||
subtable = tree[winnerNode][i[0]]
|
if self.matrix.matrix[coordinate[0] - 1][coordinate[1] + 1].walk_through == 1:
|
||||||
if subtable in ['0','1','2','3']:
|
distance.append([abs(waiterNode[0] - (coordinate[0] - 1)) + abs(waiterNode[1] - (coordinate[1] + 1)), [coordinate[0] - 1, coordinate[1] + 1]])
|
||||||
tasksQueue.append([i[0], i[1], subtable])
|
|
||||||
else:
|
else:
|
||||||
tasksQueue.append([i[0], i[1], tree[winnerNode][i[0]]['distance'][str(i[1])]])
|
distance.append([abs(waiterNode[0] - 0) + abs(waiterNode[1] - 12), [0, 12]])
|
||||||
elif winnerNode is "distance":
|
distance.append([abs(waiterNode[0] - 1) + abs(waiterNode[1] - 12), [1, 12]])
|
||||||
|
distance.append([abs(waiterNode[0] - 2) + abs(waiterNode[1] - 12), [2, 12]])
|
||||||
subtable = tree[winnerNode][i[1]]
|
distance.append([abs(waiterNode[0] - 3) + abs(waiterNode[1] - 12), [3, 12]])
|
||||||
if subtable in ['0','1','2','3']:
|
distance.append([abs(waiterNode[0] - 4) + abs(waiterNode[1] - 12), [4, 12]])
|
||||||
tasksQueue.append([i[0], i[1], subtable])
|
distance.append([abs(waiterNode[0] - 5) + abs(waiterNode[1] - 13), [5, 13]])
|
||||||
else:
|
distance.append([abs(waiterNode[0] - 5) + abs(waiterNode[1] - 14), [5, 14]])
|
||||||
tasksQueue.append([i[0], i[1], tree[winnerNode][i[1]]['actionName'][str(i[0])]])
|
|
||||||
|
distance.sort(key=lambda x: x[0])
|
||||||
|
tasksList.append([name, distance[0][0], distance[0][1]])
|
||||||
tasksQueue.sort(key=lambda x: x[2])
|
|
||||||
print(tasksQueue)
|
|
||||||
|
#Kolejkowanie zadan
|
||||||
def print(self):
|
def Queue(self, tasksList):
|
||||||
df = self.BuildDf()
|
df = self.BuildDf()
|
||||||
#a_entropy = {k:self.FindAttributesEntropy(df,k) for k in df.keys()[:-1]}
|
tree = self.BuildTree(df)
|
||||||
#print(a_entropy)
|
winnerNode = self.FindWinner(df)
|
||||||
#print('\n Info Gain: ', self.FindWinner(df))
|
|
||||||
print(tasksList)
|
for i in tasksList:
|
||||||
self.Queue(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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user