1
0
forked from s444417/ProjektAI

AdamT_Podprojekt_v4

This commit is contained in:
s444496 2020-05-07 18:36:20 +02:00
parent 5ea5322cbc
commit a45ebf5e92
18 changed files with 177 additions and 13 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.7 (venv)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (ProjektAI) (4)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">

View File

@ -3,5 +3,5 @@
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (venv)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (ProjektAI) (4)" project-jdk-type="Python SDK" />
</project>

View File

@ -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)

View File

@ -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)

View File

@ -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