Gotowy podprojekt
This commit is contained in:
parent
3666bd3079
commit
89ac7e6da6
96
Kamila.py
96
Kamila.py
@ -4,61 +4,34 @@ from sklearn.model_selection import train_test_split
|
|||||||
from sklearn import metrics
|
from sklearn import metrics
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
header = ["ready", "hydration", "weeds", "empty", "TODO"]
|
header = ["hydration", "weeds", "empty", "ready", "TODO"]
|
||||||
work = ["Zebrac","Podlac","Odchwascic","Zasadzic"]
|
work = ["Podlac", "Odchwascic", "Zasadzic", "Zebrac"]
|
||||||
#0 - 3
|
|
||||||
#1 - 0
|
|
||||||
#2 - 1
|
|
||||||
#3 - 2
|
|
||||||
def check_p(field):
|
|
||||||
if field == 0:
|
|
||||||
return [0, 0, 0, 0, "Zasadzic"]
|
|
||||||
elif field == 1:
|
|
||||||
return [0, 0, 1, 0, "Odchwascic"]
|
|
||||||
elif field == 2:
|
|
||||||
return [0, 0, 0, 1, "Podlac"]
|
|
||||||
elif field == 3:
|
|
||||||
return [0, 0, 1, 1, "Odchwascic"]
|
|
||||||
elif field == 4:
|
|
||||||
return [0, 1, 0, 0, "Zasadzic"]
|
|
||||||
elif field == 5:
|
|
||||||
return [0, 1, 1, 0, "Odchwascic"]
|
|
||||||
elif field == 6:
|
|
||||||
return [0, 1, 0, 1, "Ignoruj"]
|
|
||||||
elif field == 7:
|
|
||||||
return [0, 1, 1, 1, "Odchwascic"]
|
|
||||||
elif field == 8:
|
|
||||||
return [1, 0, 0, 1, "Zebrac"]
|
|
||||||
else:
|
|
||||||
print("wrong field number")
|
|
||||||
|
|
||||||
|
|
||||||
def check(field):
|
def check(field):
|
||||||
if field == 0:
|
if field == 0:
|
||||||
return [[0, 0, 0, 1, "Zasadzic"],[0,0,0,1,"Podlac"]]
|
return [[0, 0, 1, 0, "Zasadzic"], [0, 0, 1, 0, "Podlac"]]
|
||||||
elif field == 1:
|
elif field == 1:
|
||||||
return [[0, 0, 1, 1, "Odchwascic"], [0,0,1,1,"Podlac"], [0,0,1,1,"Zasadzic"]]
|
return [[0, 1, 1, 0, "Odchwascic"], [0, 1, 1, 0, "Podlac"], [0, 1, 1, 0, "Zasadzic"]]
|
||||||
elif field == 2:
|
elif field == 2:
|
||||||
return [[0, 0, 0, 0, "Podlac"]]
|
return [[0, 0, 0, 0, "Podlac"]]
|
||||||
elif field == 3:
|
elif field == 3:
|
||||||
return [[0, 0, 1, 0, "Odchwascic"],[0,0,1,0,"Podlac"]]
|
return [[0, 1, 0, 0, "Odchwascic"], [0, 1, 0, 0, "Podlac"]]
|
||||||
elif field == 4:
|
elif field == 4:
|
||||||
return [[0, 1, 0, 1, "Zasadzic"]]
|
return [[1, 0, 1, 0, "Zasadzic"]]
|
||||||
elif field == 5:
|
elif field == 5:
|
||||||
return [[0, 1, 1, 1, "Odchwascic"],[0,1,1,1,"Zasadzic"]]
|
return [[1, 1, 1, 0, "Odchwascic"], [1, 1, 1, 0, "Zasadzic"]]
|
||||||
elif field == 6:
|
elif field == 6:
|
||||||
return []
|
return []
|
||||||
elif field == 7:
|
elif field == 7:
|
||||||
return [[0, 1, 1, 0, "Odchwascic"]]
|
return [[1, 1, 0, 0, "Odchwascic"]]
|
||||||
elif field == 8:
|
elif field == 8:
|
||||||
return [[1, 0, 0, 0, "Zebrac"],[1, 0, 0, 0, "Potem podlac"],[1, 0, 0, 0, "Potem zasadzic"]]
|
return [[0, 0, 0, 1, "Zebrac"], [0, 0, 0, 1, "Potem podlac"], [0, 0, 0, 1, "Potem zasadzic"]]
|
||||||
else:
|
else:
|
||||||
print("wrong field number")
|
print("wrong field number")
|
||||||
|
|
||||||
def un_values(rows, col):
|
|
||||||
return set([row[col] for row in rows])
|
|
||||||
|
|
||||||
|
|
||||||
|
# liczenie ilości prac do wykonania
|
||||||
def class_counts(rows):
|
def class_counts(rows):
|
||||||
counts = {}
|
counts = {}
|
||||||
for row in rows:
|
for row in rows:
|
||||||
@ -69,10 +42,12 @@ def class_counts(rows):
|
|||||||
return counts
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
# sprawdzenie czy wartość jest liczbą
|
||||||
def is_numeric(value):
|
def is_numeric(value):
|
||||||
return isinstance(value, int) or isinstance(value, float)
|
return isinstance(value, int) or isinstance(value, float)
|
||||||
|
|
||||||
|
|
||||||
|
# klasa tworząca zapytanie do podziału danych
|
||||||
class Question():
|
class Question():
|
||||||
def __init__(self, column, value):
|
def __init__(self, column, value):
|
||||||
self.column = column
|
self.column = column
|
||||||
@ -82,11 +57,9 @@ class Question():
|
|||||||
val = example[self.column]
|
val = example[self.column]
|
||||||
if is_numeric(val):
|
if is_numeric(val):
|
||||||
return val == self.value
|
return val == self.value
|
||||||
else:
|
|
||||||
return val != self.value
|
|
||||||
|
|
||||||
|
# wyświetlenie pytania
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
condition = "!="
|
|
||||||
if is_numeric(self.value):
|
if is_numeric(self.value):
|
||||||
condition = "=="
|
condition = "=="
|
||||||
return "Is %s %s %s?" % (
|
return "Is %s %s %s?" % (
|
||||||
@ -94,6 +67,7 @@ class Question():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# podział danych na spełnione i niespełnione wiersze
|
||||||
def partition(rows, question):
|
def partition(rows, question):
|
||||||
true_rows, false_rows = [], []
|
true_rows, false_rows = [], []
|
||||||
for row in rows:
|
for row in rows:
|
||||||
@ -104,6 +78,7 @@ def partition(rows, question):
|
|||||||
return true_rows, false_rows
|
return true_rows, false_rows
|
||||||
|
|
||||||
|
|
||||||
|
# funkcja implementująca indeks gini
|
||||||
def gini(rows):
|
def gini(rows):
|
||||||
counts = class_counts(rows)
|
counts = class_counts(rows)
|
||||||
impurity = 1
|
impurity = 1
|
||||||
@ -118,6 +93,7 @@ def info_gain(left, right, current_uncertainty):
|
|||||||
return current_uncertainty - p * gini(left) - (1 - p) * gini(right)
|
return current_uncertainty - p * gini(left) - (1 - p) * gini(right)
|
||||||
|
|
||||||
|
|
||||||
|
# znalezienie najlepszego "miejsca" na podział danych
|
||||||
def find_best_split(rows):
|
def find_best_split(rows):
|
||||||
best_gain = 0
|
best_gain = 0
|
||||||
best_question = None
|
best_question = None
|
||||||
@ -152,6 +128,7 @@ class DecisionNode:
|
|||||||
self.false_branch = false_branch
|
self.false_branch = false_branch
|
||||||
|
|
||||||
|
|
||||||
|
# funkcja budująca drzewo
|
||||||
def build_tree(rows):
|
def build_tree(rows):
|
||||||
gain, question = find_best_split(rows)
|
gain, question = find_best_split(rows)
|
||||||
if gain == 0:
|
if gain == 0:
|
||||||
@ -164,6 +141,7 @@ def build_tree(rows):
|
|||||||
return DecisionNode(question, true_branch, false_branch)
|
return DecisionNode(question, true_branch, false_branch)
|
||||||
|
|
||||||
|
|
||||||
|
# funcka wypisująca drzewo
|
||||||
def print_tree(node, spacing=""):
|
def print_tree(node, spacing=""):
|
||||||
if isinstance(node, Leaf):
|
if isinstance(node, Leaf):
|
||||||
print(spacing + "Predict", node.predictions)
|
print(spacing + "Predict", node.predictions)
|
||||||
@ -178,23 +156,6 @@ def print_tree(node, spacing=""):
|
|||||||
print_tree(node.false_branch, spacing + " ")
|
print_tree(node.false_branch, spacing + " ")
|
||||||
|
|
||||||
|
|
||||||
def classify(row, node):
|
|
||||||
if isinstance(node, Leaf):
|
|
||||||
return node.predictions
|
|
||||||
if node.question.match(row):
|
|
||||||
return classify(row, node.true_branch)
|
|
||||||
else:
|
|
||||||
return classify(row,node.false_branch)
|
|
||||||
|
|
||||||
|
|
||||||
def print_leaf(counts):
|
|
||||||
total = sum(counts.values()) * 1.0
|
|
||||||
probs = {}
|
|
||||||
for lbl in counts.keys():
|
|
||||||
probs[lbl] = str(int(counts[lbl]/total * 100)) + "%"
|
|
||||||
return probs
|
|
||||||
|
|
||||||
|
|
||||||
class main():
|
class main():
|
||||||
def __init__(self, traktor, field, ui, path):
|
def __init__(self, traktor, field, ui, path):
|
||||||
self.traktor = traktor
|
self.traktor = traktor
|
||||||
@ -202,7 +163,9 @@ class main():
|
|||||||
self.ui = ui
|
self.ui = ui
|
||||||
self.path = path
|
self.path = path
|
||||||
self.best_action = 0
|
self.best_action = 0
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
|
# dane testowe
|
||||||
array = ([[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
|
array = ([[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
|
||||||
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
|
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
|
||||||
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
|
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||||
@ -213,12 +176,14 @@ class main():
|
|||||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||||
[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, 0, 0, 0]])
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
|
||||||
while (self.best_action != -1):
|
|
||||||
|
while (True):
|
||||||
self.find_best_action()
|
self.find_best_action()
|
||||||
|
if self.best_action == -1:
|
||||||
|
break
|
||||||
self.do_best_action()
|
self.do_best_action()
|
||||||
print("Koniec roboty")
|
print("Koniec roboty")
|
||||||
|
|
||||||
|
|
||||||
def find_best_action(self):
|
def find_best_action(self):
|
||||||
testing_data = []
|
testing_data = []
|
||||||
matrix = self.field.get_matrix()
|
matrix = self.field.get_matrix()
|
||||||
@ -247,20 +212,13 @@ class main():
|
|||||||
else:
|
else:
|
||||||
self.best_action = self.find_remaining_action(matrix_todo)
|
self.best_action = self.find_remaining_action(matrix_todo)
|
||||||
return
|
return
|
||||||
#for row in testing_data:
|
|
||||||
# print("Actual: %s. Predicted %s" %
|
|
||||||
# (row[-1], print_leaf(classify(row, x))))
|
|
||||||
#for row in matrix_todo:
|
|
||||||
# print(row)
|
|
||||||
|
|
||||||
def do_best_action(self):
|
def do_best_action(self):
|
||||||
self.traktor.set_mode((self.best_action+3) % 4)
|
self.traktor.set_mode(self.best_action)
|
||||||
while self.path.pathfinding(self.traktor, self.field, self.ui) != 0:
|
while self.path.pathfinding(self.traktor, self.field, self.ui) != 0:
|
||||||
pass
|
pass
|
||||||
# 0 - 3
|
|
||||||
# 1 - 0
|
|
||||||
# 2 - 1
|
|
||||||
# 3 - 2
|
|
||||||
def find_remaining_action(self, matrix_todo):
|
def find_remaining_action(self, matrix_todo):
|
||||||
for row in matrix_todo:
|
for row in matrix_todo:
|
||||||
for field in row:
|
for field in row:
|
||||||
|
Loading…
Reference in New Issue
Block a user