diff --git a/.idea/ProjektAI.iml b/.idea/ProjektAI.iml index 54ef975..fc82b32 100644 --- a/.idea/ProjektAI.iml +++ b/.idea/ProjektAI.iml @@ -2,7 +2,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index ba00af5..2ba285b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/kelner/main.py b/kelner/main.py index 199f2f8..6867422 100644 --- a/kelner/main.py +++ b/kelner/main.py @@ -7,6 +7,7 @@ from kelner.src.managers.MenuManager import MenuManager from kelner.src.managers.TableManager import TableManager from kelner.src.managers.WaiterManager import WaiterManager + # create screen consts Scale = 2 # scale for all images used within project CellSize = round(50 * Scale) # pixel size of 1 square cell in the grid @@ -25,12 +26,15 @@ drawableManager = DrawableCollection() # initialize menu manager menuManager = MenuManager() + + # initialize waiter component waiter1 = Waiter(0, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset) waiter2 = Waiter(0, GridCountY - 1, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset) waiter3 = Waiter(GridCountX - 1, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset) waiter4 = Waiter(GridCountX - 1, GridCountY - 1, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset) + # adds waiter to drawable collection drawableManager.add(waiter1) drawableManager.add(waiter2) diff --git a/kelner/src/algorithms/DecisionTree/Tree_Builder.py b/kelner/src/algorithms/DecisionTree/Tree_Builder.py new file mode 100644 index 0000000..7bbcd03 --- /dev/null +++ b/kelner/src/algorithms/DecisionTree/Tree_Builder.py @@ -0,0 +1,140 @@ +import random + +training_db = [ + [1, 0, 0, 'A'], + [1, 1, 0,'B'], +[0, 1, 0, 'C'], +[0, 1, 1, 'B'], +[0, 0, 1, 'D'], +[1, 1, 0, 'B'], + + +] + + +kolumny = ["sport", "czytanie", "lucznictwo", "jedzenie"] + + +def kolumny_wartosci(rows, col): + return set([row[col] for row in rows]) + + + +def kolumny_wartosci_ilosc(rows): + wartosci_ilosc = {} + for row in rows: + jedzenie = row[-1] + if jedzenie not in wartosci_ilosc: + wartosci_ilosc[jedzenie] = 0 + wartosci_ilosc[jedzenie] += 1 + return wartosci_ilosc + + + +def czy_numer(value): + return isinstance(value, int) or isinstance(value, float) + + + + +class Question: + def __init__(self, column, value): + self.column = column + self.value = value + + def match(self, example): + val = example[self.column] + if czy_numer(val): + return val >= self.value + else: + return val == self.value + + + +def kolumny_podzial(rows, question): + true_rows, false_rows = [], [] + for row in rows: + if question.match(row): + true_rows.append(row) + else: + false_rows.append(row) + return true_rows, false_rows + + +def gini(rows): + wartosci_ilosc = kolumny_wartosci_ilosc(rows) + impurity = 1 + for lbl in wartosci_ilosc: + prob_of_lbl = wartosci_ilosc[lbl] / float(len(rows)) + impurity -= prob_of_lbl * prob_of_lbl + return impurity + + + +def info_gain(left, right, current_uncertainty): + p = float(len(left)) / (len(left) + len(right)) + return current_uncertainty - p * gini(left) - (1 - p) * gini(right) + + + +def find_best_split(rows): + best_gain = 0 + best_question = None + current_uncertainty = gini(rows) + n_features = len(rows[0]) - 1 + + for col in range(n_features): + values = set([row[col] for row in rows]) + for val in values: + question = Question(col, val) + true_rows, false_rows = kolumny_podzial(rows, question) + if len(true_rows) == 0 or len(false_rows) == 0: + continue + gain = info_gain(true_rows, false_rows, current_uncertainty) + if gain >= best_gain: + best_gain, best_question = gain, question + return best_gain, best_question + + + +class Leaf: + def __init__(self, rows): + a = random.randrange(0, len(rows)) + self.predictions = rows[a][-1] + + + +class Decision_Node: + def __init__(self, question, true_branch, false_branch): + self.question = question + self.true_branch = true_branch + self.false_branch = false_branch + + + + +def build_tree(rows): + gain, question = find_best_split(rows) + if gain == 0: + return Leaf(rows) + true_rows, false_rows = kolumny_podzial(rows, question) + true_branch = build_tree(true_rows) + false_branch = build_tree(false_rows) + return Decision_Node(question, true_branch, false_branch) + + + + print(spacing + '--> False:') + print_tree(node.false_branch, spacing + " ") + + +def zgadnij(row, node): + if isinstance(node, Leaf): + return node.predictions + + if node.question.match(row): + return zgadnij(row, node.true_branch) + else: + return zgadnij(row, node.false_branch) + + diff --git a/kelner/src/components/Table.py b/kelner/src/components/Table.py index bde2e96..71c68ee 100644 --- a/kelner/src/components/Table.py +++ b/kelner/src/components/Table.py @@ -3,7 +3,7 @@ from enum import Enum from threading import Lock from kelner.src.components.Drawable import Drawable from kelner.src.managers.ImageCache import ImageCache, Images - +from kelner.src.algorithms.DecisionTree import Tree_Builder # status of the table class Status(Enum): @@ -22,6 +22,26 @@ class Table(Drawable): self.__order = [] self.__guests = self.__getRandomGuests() self.__tableLock = Lock() + self.__zainteresowania = [] + + # Build a Decision Tree + self.Decision_Tree = Tree_Builder.build_tree(Tree_Builder.training_db) + + #Generates Hobbys + for a in range(len(self.__guests)): + sport = random.randint(0,1) + czytanie = random.randint(0,1) + lucznictwo = random.randint(0,1) + jedzenie = random.randint(0,1) + self.__zainteresowania.append([sport,czytanie,lucznictwo,jedzenie, "A"]) + + def getOrder(self): + # Generates order + order = [] + for a in range(0,len(self.__guests)): + order.append(Tree_Builder.zgadnij(self.__zainteresowania[a], self.Decision_Tree)) + print("Zamowienie: " + str(order)) + return order @staticmethod def __getRandomGuests(): @@ -33,16 +53,16 @@ class Table(Drawable): return guests # waiter collects orders from table - def getOrder(self): - order = None - if self.__tableLock.acquire(False): - try: - if self.isStatus(Status.Ready) and self.hasOrder(): - order = self.__order - self.setOrder([]) - finally: - self.__tableLock.release() - return order + ##def getOrder(self): + ## order = None + ## if self.__tableLock.acquire(False): + ## try: + ## if self.isStatus(Status.Ready) and self.hasOrder(): + ## order = self.__order + ## self.setOrder([]) + ## finally: + ## self.__tableLock.release() + ## return order def setOrder(self, order): self.__order = order diff --git a/venv/Lib/site-packages/pygame/__pycache__/__init__.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/__init__.cpython-37.pyc index 7d4aa3c..d2719ec 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/__init__.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/__init__.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/_numpysndarray.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/_numpysndarray.cpython-37.pyc index 4034371..6ae0fee 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/_numpysndarray.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/_numpysndarray.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/_numpysurfarray.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/_numpysurfarray.cpython-37.pyc index d3fc87b..b977947 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/_numpysurfarray.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/_numpysurfarray.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/colordict.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/colordict.cpython-37.pyc index d847682..d641439 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/colordict.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/colordict.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/compat.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/compat.cpython-37.pyc index 2bd0e86..5f711bd 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/compat.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/compat.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/cursors.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/cursors.cpython-37.pyc index 82c8b64..1c3614f 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/cursors.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/cursors.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/pkgdata.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/pkgdata.cpython-37.pyc index 482fe5b..fa4b69d 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/pkgdata.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/pkgdata.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/sndarray.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/sndarray.cpython-37.pyc index 3ff5000..b9aa6c0 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/sndarray.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/sndarray.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/sprite.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/sprite.cpython-37.pyc index 9a18126..e5671eb 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/sprite.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/sprite.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/surfarray.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/surfarray.cpython-37.pyc index 3a443a6..bac2122 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/surfarray.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/surfarray.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/sysfont.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/sysfont.cpython-37.pyc index 3520213..bddba74 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/sysfont.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/sysfont.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/__pycache__/version.cpython-37.pyc b/venv/Lib/site-packages/pygame/__pycache__/version.cpython-37.pyc index 3f47f84..d5d9397 100644 Binary files a/venv/Lib/site-packages/pygame/__pycache__/version.cpython-37.pyc and b/venv/Lib/site-packages/pygame/__pycache__/version.cpython-37.pyc differ diff --git a/venv/Lib/site-packages/pygame/threads/__pycache__/__init__.cpython-37.pyc b/venv/Lib/site-packages/pygame/threads/__pycache__/__init__.cpython-37.pyc index 0f7d672..a3da368 100644 Binary files a/venv/Lib/site-packages/pygame/threads/__pycache__/__init__.cpython-37.pyc and b/venv/Lib/site-packages/pygame/threads/__pycache__/__init__.cpython-37.pyc differ