SI_Traktor/Kamila.py

229 lines
6.7 KiB
Python
Raw Normal View History

2020-05-15 13:54:18 +02:00
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
import numpy
2020-05-18 17:13:09 +02:00
header = ["hydration", "weeds", "empty", "ready", "TODO"]
work = ["Podlac", "Odchwascic", "Zasadzic", "Zebrac"]
2020-05-15 13:54:18 +02:00
def check(field):
if field == 0:
2020-05-18 17:13:09 +02:00
return [[0, 0, 1, 0, "Zasadzic"], [0, 0, 1, 0, "Podlac"]]
2020-05-15 13:54:18 +02:00
elif field == 1:
2020-05-18 17:13:09 +02:00
return [[0, 1, 1, 0, "Odchwascic"], [0, 1, 1, 0, "Podlac"], [0, 1, 1, 0, "Zasadzic"]]
2020-05-15 13:54:18 +02:00
elif field == 2:
2020-05-17 20:24:15 +02:00
return [[0, 0, 0, 0, "Podlac"]]
2020-05-15 13:54:18 +02:00
elif field == 3:
2020-05-18 17:13:09 +02:00
return [[0, 1, 0, 0, "Odchwascic"], [0, 1, 0, 0, "Podlac"]]
2020-05-15 13:54:18 +02:00
elif field == 4:
2020-05-18 17:13:09 +02:00
return [[1, 0, 1, 0, "Zasadzic"]]
2020-05-15 13:54:18 +02:00
elif field == 5:
2020-05-18 17:13:09 +02:00
return [[1, 1, 1, 0, "Odchwascic"], [1, 1, 1, 0, "Zasadzic"]]
2020-05-15 13:54:18 +02:00
elif field == 6:
2020-05-17 20:24:15 +02:00
return []
2020-05-15 13:54:18 +02:00
elif field == 7:
2020-05-18 17:13:09 +02:00
return [[1, 1, 0, 0, "Odchwascic"]]
2020-05-15 13:54:18 +02:00
elif field == 8:
2020-05-18 17:13:09 +02:00
return [[0, 0, 0, 1, "Zebrac"], [0, 0, 0, 1, "Potem podlac"], [0, 0, 0, 1, "Potem zasadzic"]]
2020-05-15 13:54:18 +02:00
else:
print("wrong field number")
2020-05-18 17:13:09 +02:00
# liczenie ilości prac do wykonania
2020-05-15 13:54:18 +02:00
def class_counts(rows):
counts = {}
for row in rows:
label = row[-1]
if label not in counts:
counts[label] = 0
counts[label] += 1
return counts
2020-05-18 17:13:09 +02:00
# sprawdzenie czy wartość jest liczbą
2020-05-15 13:54:18 +02:00
def is_numeric(value):
return isinstance(value, int) or isinstance(value, float)
2020-05-18 17:13:09 +02:00
# klasa tworząca zapytanie do podziału danych
2020-05-15 13:54:18 +02:00
class Question():
def __init__(self, column, value):
self.column = column
self.value = value
def match(self, example):
val = example[self.column]
if is_numeric(val):
return val == self.value
2020-05-18 17:13:09 +02:00
# wyświetlenie pytania
2020-05-15 13:54:18 +02:00
def __repr__(self):
if is_numeric(self.value):
condition = "=="
2020-05-18 17:13:09 +02:00
return "Is %s %s %s?" % (
2020-05-15 13:54:18 +02:00
header[self.column], condition, str(self.value)
)
2020-05-18 17:13:09 +02:00
# podział danych na spełnione i niespełnione wiersze
2020-05-15 13:54:18 +02:00
def partition(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
2020-05-18 17:13:09 +02:00
# funkcja implementująca indeks gini
2020-05-15 13:54:18 +02:00
def gini(rows):
counts = class_counts(rows)
impurity = 1
for lbl in counts:
2020-05-18 17:13:09 +02:00
prob_of_lbl = counts[lbl] / float(len(rows))
impurity -= prob_of_lbl ** 2
2020-05-15 13:54:18 +02:00
return impurity
def info_gain(left, right, current_uncertainty):
2020-05-18 17:13:09 +02:00
p = float(len(left)) / (len(left) + len(right))
return current_uncertainty - p * gini(left) - (1 - p) * gini(right)
2020-05-15 13:54:18 +02:00
2020-05-18 17:13:09 +02:00
# znalezienie najlepszego "miejsca" na podział danych
2020-05-15 13:54:18 +02:00
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 = partition(rows, question)
if len(true_rows) == 0 or len(false_rows) == 0:
continue
2020-05-18 17:13:09 +02:00
gain = info_gain(true_rows, false_rows, current_uncertainty)
2020-05-15 13:54:18 +02:00
if gain >= best_gain:
best_gain, best_question = gain, question
return best_gain, best_question
class Leaf:
def __init__(self, rows):
self.predictions = class_counts(rows)
class DecisionNode:
def __init__(self, question, true_branch, false_branch):
self.question = question
self.true_branch = true_branch
self.false_branch = false_branch
2020-05-18 17:13:09 +02:00
# funkcja budująca drzewo
2020-05-15 13:54:18 +02:00
def build_tree(rows):
gain, question = find_best_split(rows)
if gain == 0:
return Leaf(rows)
true_rows, false_rows = partition(rows, question)
true_branch = build_tree(true_rows)
false_branch = build_tree(false_rows)
return DecisionNode(question, true_branch, false_branch)
2020-05-18 17:13:09 +02:00
# funcka wypisująca drzewo
2020-05-15 13:54:18 +02:00
def print_tree(node, spacing=""):
if isinstance(node, Leaf):
print(spacing + "Predict", node.predictions)
return
print(spacing + str(node.question))
print(spacing + '--> True: ')
print_tree(node.true_branch, spacing + " ")
print(spacing + '--> False: ')
print_tree(node.false_branch, spacing + " ")
2020-05-15 14:03:52 +02:00
class main():
2020-05-18 17:13:09 +02:00
def __init__(self, traktor, field, ui, path):
self.traktor = traktor
self.field = field
self.ui = ui
self.path = path
2020-05-17 20:24:15 +02:00
self.best_action = 0
2020-05-18 17:13:09 +02:00
2020-05-17 20:24:15 +02:00
def main(self):
2020-05-18 17:13:09 +02:00
# dane testowe
2020-05-15 13:54:18 +02:00
array = ([[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[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]])
2020-05-18 17:13:09 +02:00
while (True):
2020-05-17 20:24:15 +02:00
self.find_best_action()
2020-05-18 17:13:09 +02:00
if self.best_action == -1:
break
2020-05-17 20:24:15 +02:00
self.do_best_action()
print("Koniec roboty")
2020-05-15 13:54:18 +02:00
2020-05-17 20:24:15 +02:00
def find_best_action(self):
testing_data = []
matrix = self.field.get_matrix()
matrix_todo = []
2020-05-18 17:13:09 +02:00
# print(self.field)
2020-05-15 13:54:18 +02:00
for i in range(10):
2020-05-17 20:24:15 +02:00
matrix_todo.append([])
verse = matrix[i]
for j in range(len(verse)):
2020-05-15 13:54:18 +02:00
coord = (i, j)
2020-05-18 17:13:09 +02:00
current_field = check(verse[j]) # czynnosci ktore trzeba jeszcze zrobic na kazdym polu
2020-05-17 20:24:15 +02:00
matrix_todo[i].append([])
for action in current_field:
matrix_todo[i][j].append(action[-1])
testing_data.extend(current_field)
2020-05-18 17:13:09 +02:00
# testing_data.append(current_field)
2020-05-17 20:24:15 +02:00
if len(testing_data) > 0:
x = build_tree(testing_data)
print_tree(x)
if isinstance(x, Leaf):
self.best_action = self.find_remaining_action(matrix_todo)
return
self.best_action = x.question.column
print(header[x.question.column])
print(x.question.value)
else:
self.best_action = self.find_remaining_action(matrix_todo)
return
def do_best_action(self):
2020-05-18 17:13:09 +02:00
self.traktor.set_mode(self.best_action)
while self.path.pathfinding(self.traktor, self.field, self.ui) != 0:
2020-05-17 20:24:15 +02:00
pass
2020-05-18 17:13:09 +02:00
2020-05-17 20:24:15 +02:00
def find_remaining_action(self, matrix_todo):
for row in matrix_todo:
for field in row:
for action in field:
print(action)
return work.index(action)
2020-05-18 17:13:09 +02:00
return -1