Commit e53993e8 authored by Mathieu Reymond's avatar Mathieu Reymond
Browse files

Merge branch 'master' of https://github.com/Ardillen66/GarbageBot

parents d1937a4d 22b6b774
<mxfile userAgent="Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" version="6.4.6" editor="www.draw.io" type="github"><diagram name="Page-1">7Vxbc+o4Ev41PIayLF8fk5xkZ6rOzKQmU3t5OiVsAZ4xltcWCeyvX8m2jCXLxIAgJJm8BOQrffn6625JE3i/2vyjQPnyFxLjdGJb8WYCv01s23ZCl/3jI9t6xA/semBRJHE9BHYDz8n/cDNoNaPrJMaldCIlJKVJLg9GJMtwRKUxVBTkVT5tTlL5qTla4N7Ac4TS/ui/kpgu69HA9nbjP+FksRRPBl5YH5mh6K9FQdZZ87yJDefVX314hcS9mh9aLlFMXjtD8GEC7wtCaP1ptbnHKZetEFt93ePA0fa9C5zRURdYsHkRuhU/HsdMFs3XjGTs3131izC/xmLflnSVso+AfcSbhP67Geaf/8M/TyH/ltFiWx2a+sAVA/VxC/hi4AkXyQpTXDQ3+RNTum0MAq0pYUOkoEuyIBlKvxOSNw+u35q/6uAvb4ZKsi6i1r4auVNULHBzXuC3gmcGjQl7nWLLzilwimjyIj8ANZa1aM/bSZd9aASsF3bjAy8oXWNhHl5KuUTZ3b0FrX5XPTIn7Gd01eL9d03EgZuyEtAtO8F28s3uoLjL0xKVmB0F9Um/zf7kXsL8kUk6ognJxGNmhfpg9iPqZ/eGuy+pmAzFGyobRkkL8he+JykpdmY0T9JUGUJpssjY14ipjRvB3QsuaMI88bY5sErimD/m7nWZUPyco0qXrwx32hfhl+DNfjvoq7e54MYRNtFgFRTfX3eeD4W/LrteL048xSbgCJsQ2rRrbd6TLMI5V2eKUZEl2WKvNg9SW4GZaaFZdQJ3x5wkGa1+nns3cb9xhTGnrM2vukDoL8VzqtEe5Q57VzKtsdf8o/LeG8eM4kCg6M12x+nNNaA25wC1wVptTwWJ128732dVVzBCXa1qTKvL1ah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Fd0yxyYGngwfj+bfL2bZP32aXtUrVdYPXj1+94kZTcUPmLMkPtrF/6VM7QmNO+HgwMZW9wjMCvWCer7O9h2FculufsKmF0cL3bMft3HLre7TxlCVdZARdodsw73wo4YJ22EdLwBke+P8ByDtpIo763rWx52XbNDO2CBPoICayBBuqpm2Yo1MhVqZFJRtOvzNX9TovLfYUqWEy3nw529++AxGHXCWTcNdX6VoFX6QMaQt4Dlva8T790YJdCF4wrzhjpnwJreIaJ6UJcvd7ti5bgAnW5iGbbJ89MCY593e3mXLvObsts+PB/</diagram></mxfile>
\ No newline at end of file
<mxfile userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0" version="6.5.8" editor="www.draw.io" type="github"><diagram name="Page-1">7V3dc6s2Fv9r/JgMQnw+JrlJ25nbNtPc2e4+3ZGxbNNi5AU5cfavXwkQRkI4si0c56Z+iRFYwPn8nXN0lAm8W21/KtB6+SuZ4WziOrPtBH6ZuK4LQcz+8JHXeiQCoB5YFOmsHuoMPKX/w82g04xu0hkupQspIRlN1/JgQvIcJ1QaQ0VBXuTL5iST77pGC9wbeEpQ1h/9M53RZfMWbrAb/xmni6W4MwiaF56i5O9FQTZ5c7+JC+fVpz69QmKu5kXLJZqRl84QvJ/Au4IQWn9bbe9wxmkryFb/7mHgbPvcBc6p0Q8c2DwIfRUvj2eMFs1hTnL257Z6I8x/47CjJV1l7CtgX/E2pf9uhvn3//Dv15Af5bR4rU5dh8AXA/V5B4Ri4BEX6QpTXDST/IUpfW0EAm0oYUOkoEuyIDnKvhKybm7cf9Hm3UuyKZJWnBoyU1QscHNdFNZj/DU7v2zo8xMm7HGKV3ZBgTNE02dZJFAjWYv2uh112ZeGwHpiu/UUzyjbYCEeQUY5RdnswYJW71WPzAl7ry5bgv9uiDhxVVYEumEXuN56uzspZnlcohKzs6C+6PfpX1xLmD4ySic0Jbm4zbRQb8xeor53b7j7kIrIULylsmCUtCB/4zuSkWInRvM0y5Qhl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QDnTo1147HkarbSRnxbrGM7QltT2o1tfefwRVFNwO5CDBx8arg+yEjwYhPdt6r1ODb9LWAsjIBEpEkamSySRhV/KO6xYoJJBtH8JVGq7EVoqaQyH1p1bodLZOha+ciV1nVuy/bRWw1WaU+K4nxQMRzIa8dlw2x/Vg7vO9BNz2oPhm5weC8rFBlDuUutlLpAX88Wa0GasellsUGe8qIq2upArBv3i4lgV7digO/GiK9rtkhrhdjWr4MaqaMcGmQWcz2743nZc2Kr9IpLJUKOM57SruqseNNHCvb/PO/A6OaCrbt/3ccu6dbuvCcD3ZvKmW+jYsyvQiSnAwJUVJjRsCO9N5KuaJ7qW7a8jb/ce+KeDThHCVossrCKHsVKCu6RV5MDRmAfr2zbKuzZOzrI547vldoHT919/4EVackHlT84EtdO/dPnK0MqInX7SWNYGO91uojte2dbHsq6cLc7ZZcLoYL/bMdv2HNrvNk5awlc2T4k0G2WO1wEHnNP2Pxve1ywMB1DOQdvndFv/Ow10Q6jLyEQKsCR1EotFCqNvlaNAI1+FRjYRTT8z98TgrmR3E7JaIT72EUyvtc3PuOn1IqUp2FL5WzW+Si3QkvU9oL3nfWqmAxuUtq30b5o3z4p5G15mYjsbVze9fdI8XKw0/IaaOk0wUh6u3YropDC/dWT6bWYO3a/C1cDwZg+L94rKe832oKdjpnH5lZoS89VWF4tuDPQD8/uULrmHuTHQonPuma7xTD05GXZK6j56vqaLT5dntKNEJt0fprmy9qCz4UFn50NfxorB/o0PD1U9Hb7zB/DAeTTPVzvqDPeB6usdVIqYwBlT8XSxOy1S/MxXK/dXVooVl84cI7opcGnHx13OBja1juxDlQ50L2aDKHa4+5cn9eW7/ysD7/8P</diagram></mxfile>
\ No newline at end of file
# -*- coding: utf-8 -*-
"""Decision tree implementation for the Garbage Robot."""
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
DIRECTION = 'direction'
RIGHT_STR = 'RIGHT'
LEFT_STR = 'LEFT'
RIGHT = 0
LEFT = 1
COLOR = 'color'
RED_STR = 'RED'
GREEN_STR = 'GREEN'
BLUE_STR = 'BLUE'
RED = 0
GREEN = 1
BLUE = 2
SIZE = 'size'
SMALL_STR = 'SMALL'
MEDIUM_STR = 'MEDIUM'
BIG_STR = 'BIG'
SMALL = 0
MEDIUM = 1
BIG = 2
SHAPE = 'shape'
CUBE_STR = 'CUBE'
SPHERE_STR = 'SPEHRE'
TRIANGLE_STR = 'TRIANGLE'
CUBE = 0
SPHERE = 1
TRIANGLE = 2
map_to_int = {
RIGHT_STR: RIGHT,
LEFT_STR: LEFT,
RED_STR: RED,
GREEN_STR: GREEN,
BLUE_STR: BLUE,
SMALL_STR: SMALL,
MEDIUM_STR: MEDIUM,
BIG_STR: BIG,
CUBE_STR: CUBE,
SPHERE_STR: SPHERE,
TRIANGLE_STR: TRIANGLE
}
class DecisionTreeGR(object):
"""Wrapper class for the SciKit Learn decision tree implementation."""
def __init__(self, training_samples=None):
"""Constructor.
Constructor method for the Decision Tree wraper class. If provided a
list of training samples would be used to train the classifier.
"""
self.__decision_tree = DecisionTreeClassifier(criterion='gini')
self.__training_samples = training_samples
if not self.__training_samples:
self.__training_samples = []
def add_training_sample(self, training_sample):
"""Add a training sample for the classifier.
Expected structure of the training sample in a dictionary form:
{
'color': color_value,
'shape': shape_value,
'size': size_value,
'direction': direction_value
}
where the values are strings with the following possible values:
color_value: 'RED', 'GREEN', 'BLUE'
shape_value: 'SPHERE', 'CUBE', 'TRIANGLE'
size_value: 'SMALL', 'BIG'
direction_value: 'LEFT', 'RIGHT'
"""
self.__training_samples.append(training_sample)
def train(self):
"""Train the tree classifier using the class training samples.
Tree samples used are expected to be provided in the constructor or by
using the add_training_sample method.
"""
df = pd.DataFrame(self.__training_samples)
df = self.__map_string_to_int(df)
features = [COLOR, SIZE, SHAPE]
y = df[DIRECTION]
X = df[features]
self.__decision_tree.fit(X, y)
def __map_string_to_int(self, data_frame):
"""Map string to integer for training dataframe."""
return data_frame.replace(map_to_int)
def classify(self, input):
"""Classify an input.
Input expected to have a dictionary format containing the following
structure:
{
'color': color_value,
'shape': shape_value,
'size': size_value
}
where the values are strings with the following possible values:
color_value: 'RED', 'GREEN', 'BLUE'
shape_value: 'SPHERE', 'CUBE', 'TRIANGLE'
size_value: 'SMALL', 'BIG'
Make sure that the tree is trained before calling this method (Call
train method before)
"""
input = {
key: [map_to_int[value]]
for (key, value) in input.iteritems()
}
res = self.__decision_tree.predict(pd.DataFrame(input))
if res == RIGHT:
return RIGHT_STR
if res == LEFT:
return LEFT_STR
# USAGE EXAMPLE
# dt = DecisionTreeGR()
# ts = {
# COLOR: GREEN_STR,
# SIZE: MEDIUM_STR,
# SHAPE: TRIANGLE_STR,
# DIRECTION: RIGHT_STR
# }
# dt.add_training_sample(ts)
# dt.train()
# inp = {COLOR: RED_STR, SIZE: BIG_STR, SHAPE: CUBE_STR}
# res = dt.classify(inp)
# print(res)
# -*- coding: utf-8 -*-
"""Tests for the decision tree implementation."""
from GarbageBot.decision_api import decision_tree_gr
# Implement proper unit testing here
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment