Initial commit
commit
d0cb45bce2
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class DecisionTree
|
||||
{
|
||||
public:
|
||||
const char* predict(float (*getObservationValue)(int), bool (*consider)(float, int, float))
|
||||
{
|
||||
if ((consider)((getObservationValue)(3), 3, 1.0)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 0.2)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 0.1)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.8)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.6)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 3.9)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.0)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(1), 1, 2.7)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 5.0)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.5)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 5.1)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.8)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 5.0)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.0)) {
|
||||
if ((consider)((getObservationValue)(1), 1, 3.2)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class DecisionTree
|
||||
{
|
||||
public:
|
||||
const char* predict(float (*getObservationValue)(int), bool (*consider)(float, int, float))
|
||||
{
|
||||
if ((consider)((getObservationValue)(2), 2, 3.3)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 1.4)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 0.1)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.8)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.0)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.9)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.2)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.8)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.7)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.3)) {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class DecisionTree
|
||||
{
|
||||
public:
|
||||
const char* predict(float (*getObservationValue)(int), bool (*consider)(float, int, float))
|
||||
{
|
||||
if ((consider)((getObservationValue)(3), 3, 1.0)) {
|
||||
if ((consider)((getObservationValue)(1), 1, 3.0)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 4.4)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.8)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.5)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.4)) {
|
||||
if ((consider)((getObservationValue)(1), 1, 2.5)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.0)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(1), 1, 3.0)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.2)) {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.3)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 5.1)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.5)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 5.8)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.5)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class DecisionTree
|
||||
{
|
||||
public:
|
||||
const char* predict(float (*getObservationValue)(int), bool (*consider)(float, int, float))
|
||||
{
|
||||
if ((consider)((getObservationValue)(2), 2, 3.5)) {
|
||||
if ((consider)((getObservationValue)(1), 1, 4.0)) {
|
||||
if ((consider)((getObservationValue)(1), 1, 3.0)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 1.4)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.8)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 5.7)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 5.2)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class DecisionTree
|
||||
{
|
||||
public:
|
||||
const char* predict(float (*getObservationValue)(int), bool (*consider)(float, int, float))
|
||||
{
|
||||
if ((consider)((getObservationValue)(2), 2, 3.3)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 5.1)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 0.2)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 0.1)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.8)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.6)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.1)) {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.2)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 5.8)) {
|
||||
if ((consider)((getObservationValue)(1), 1, 3.0)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 5.6)) {
|
||||
if ((consider)((getObservationValue)(3), 3, 1.3)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 5.0)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(1), 1, 3.3)) {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((consider)((getObservationValue)(2), 2, 4.9)) {
|
||||
if ((consider)((getObservationValue)(0), 0, 6.0)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class TreeOfUnease
|
||||
{
|
||||
public:
|
||||
const char* traverse(float (*getObservationValue)(int), bool (*considerWithUnease)(const char*, float, int, float))
|
||||
{
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(3), 3, 1.0)) {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(1), 1, 3.1)) {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(2), 2, 1.3)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(3), 3, 1.8)) {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(2), 2, 5.6)) {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(3), 3, 1.5)) {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(3), 3, 1.3)) {
|
||||
if ((considerWithUnease)("Message", (getObservationValue)(1), 1, 2.2)) {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class TreeOfUnease
|
||||
{
|
||||
public:
|
||||
const char* traverse(float (*getObservationValue)(int), bool (*considerWithUnease)(const char*, float, int, float))
|
||||
{
|
||||
if ((considerWithUnease)("Meise herbarium",(getObservationValue)(3), 3, 1.0)) {
|
||||
if ((considerWithUnease)("About their « living herbarium »",(getObservationValue)(3), 3, 0.2)) {
|
||||
if ((considerWithUnease)("Some of plants there come from Leopold II his garden",(getObservationValue)(3), 3, 0.1)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((considerWithUnease)("About the creation of its herbarium",(getObservationValue)(2), 2, 4.8)) {
|
||||
if ((considerWithUnease)("The basis of the Meise Collection is an herbarium of the plants from Brazil",(getObservationValue)(2), 2, 4.6)) {
|
||||
if ((considerWithUnease)("Leopold 2 bought it to assess whether to make Brazil a colony. But he chose Congo instead",(getObservationValue)(2), 2, 3.9)) {
|
||||
return "?";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((considerWithUnease)("Most of Meise specimens were acquired when you did not need permission from local communities to collect plants",(getObservationValue)(1), 1, 2.7)) {
|
||||
if ((considerWithUnease)("This seems to change with frameworks such as the Nagoya Protocol and the convention on biological diversity",(getObservationValue)(2), 2, 5.0)) {
|
||||
return "?";
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((considerWithUnease)("And there does not seem to be project to give them back to the countries they were collected from",(getObservationValue)(2), 2, 5.1)) {
|
||||
return "?";
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class TreeOfUnease
|
||||
{
|
||||
public:
|
||||
const char* traverse(float (*getObservationValue)(int), bool (*considerWithUnease)(const char*, float, int, float))
|
||||
{
|
||||
if ((considerWithUnease)("Classification",(getObservationValue)(3), 3, 1.0)) {
|
||||
if ((considerWithUnease)("About the need for models",(getObservationValue)(1), 1, 3.0)) {
|
||||
if ((considerWithUnease)("What is simple is always wrong. What is not is unusable. Paul Valéry 1942",(getObservationValue)(0), 0, 4.4)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((considerWithUnease)("About modeling from observations",(getObservationValue)(3), 3, 1.8)) {
|
||||
if ((considerWithUnease)("it seems that we are reducing plants to a few numbered characteristics",(getObservationValue)(3), 3, 1.5)) {
|
||||
if ((considerWithUnease)("Why not apply the same to humans ?",(getObservationValue)(3), 3, 1.4)) {
|
||||
return "?";
|
||||
}
|
||||
else {
|
||||
return "Iris Versicolour";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((considerWithUnease)("Is the practice of categorisation bad in itself ?",(getObservationValue)(1), 1, 3.0)) {
|
||||
return "?";
|
||||
}
|
||||
else {
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Virginica";
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
namespace PublishingHouse
|
||||
{
|
||||
namespace RandomForest
|
||||
{
|
||||
class TreeOfUnease
|
||||
{
|
||||
public:
|
||||
const char* traverse(float (*getObservationValue)(int), bool (*considerWithUnease)(const char*, float, int, float))
|
||||
{
|
||||
if ((considerWithUnease)("Iris Dataset",(getObservationValue)(2), 2, 3.5)) {
|
||||
if ((considerWithUnease)("About the iris Dataset",(getObservationValue)(1), 1, 4.0)) {
|
||||
if ((considerWithUnease)("Its origin are troubling to say the least",(getObservationValue)(1), 1, 3.0)) {
|
||||
if ((considerWithUnease)("It was first published in the Annals of Eugenics… Is it wrong to display it ?",(getObservationValue)(2), 2, 1.4)) {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "Iris Setosa";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,154 @@
|
||||
const int observationCount = 150;
|
||||
const int observationFeatureCount = 4;
|
||||
float observations[observationCount][observationFeatureCount] = {
|
||||
{5.1, 3.5, 1.4, 0.2},
|
||||
{4.9, 3.0, 1.4, 0.2},
|
||||
{4.7, 3.2, 1.3, 0.2},
|
||||
{4.6, 3.1, 1.5, 0.2},
|
||||
{5.0, 3.6, 1.4, 0.2},
|
||||
{5.4, 3.9, 1.7, 0.4},
|
||||
{4.6, 3.4, 1.4, 0.3},
|
||||
{5.0, 3.4, 1.5, 0.2},
|
||||
{4.4, 2.9, 1.4, 0.2},
|
||||
{4.9, 3.1, 1.5, 0.1},
|
||||
{5.4, 3.7, 1.5, 0.2},
|
||||
{4.8, 3.4, 1.6, 0.2},
|
||||
{4.8, 3.0, 1.4, 0.1},
|
||||
{4.3, 3.0, 1.1, 0.1},
|
||||
{5.8, 4.0, 1.2, 0.2},
|
||||
{5.7, 4.4, 1.5, 0.4},
|
||||
{5.4, 3.9, 1.3, 0.4},
|
||||
{5.1, 3.5, 1.4, 0.3},
|
||||
{5.7, 3.8, 1.7, 0.3},
|
||||
{5.1, 3.8, 1.5, 0.3},
|
||||
{5.4, 3.4, 1.7, 0.2},
|
||||
{5.1, 3.7, 1.5, 0.4},
|
||||
{4.6, 3.6, 1.0, 0.2},
|
||||
{5.1, 3.3, 1.7, 0.5},
|
||||
{4.8, 3.4, 1.9, 0.2},
|
||||
{5.0, 3.0, 1.6, 0.2},
|
||||
{5.0, 3.4, 1.6, 0.4},
|
||||
{5.2, 3.5, 1.5, 0.2},
|
||||
{5.2, 3.4, 1.4, 0.2},
|
||||
{4.7, 3.2, 1.6, 0.2},
|
||||
{4.8, 3.1, 1.6, 0.2},
|
||||
{5.4, 3.4, 1.5, 0.4},
|
||||
{5.2, 4.1, 1.5, 0.1},
|
||||
{5.5, 4.2, 1.4, 0.2},
|
||||
{4.9, 3.1, 1.5, 0.1},
|
||||
{5.0, 3.2, 1.2, 0.2},
|
||||
{5.5, 3.5, 1.3, 0.2},
|
||||
{4.9, 3.1, 1.5, 0.1},
|
||||
{4.4, 3.0, 1.3, 0.2},
|
||||
{5.1, 3.4, 1.5, 0.2},
|
||||
{5.0, 3.5, 1.3, 0.3},
|
||||
{4.5, 2.3, 1.3, 0.3},
|
||||
{4.4, 3.2, 1.3, 0.2},
|
||||
{5.0, 3.5, 1.6, 0.6},
|
||||
{5.1, 3.8, 1.9, 0.4},
|
||||
{4.8, 3.0, 1.4, 0.3},
|
||||
{5.1, 3.8, 1.6, 0.2},
|
||||
{4.6, 3.2, 1.4, 0.2},
|
||||
{5.3, 3.7, 1.5, 0.2},
|
||||
{5.0, 3.3, 1.4, 0.2},
|
||||
{7.0, 3.2, 4.7, 1.4},
|
||||
{6.4, 3.2, 4.5, 1.5},
|
||||
{6.9, 3.1, 4.9, 1.5},
|
||||
{5.5, 2.3, 4.0, 1.3},
|
||||
{6.5, 2.8, 4.6, 1.5},
|
||||
{5.7, 2.8, 4.5, 1.3},
|
||||
{6.3, 3.3, 4.7, 1.6},
|
||||
{4.9, 2.4, 3.3, 1.0},
|
||||
{6.6, 2.9, 4.6, 1.3},
|
||||
{5.2, 2.7, 3.9, 1.4},
|
||||
{5.0, 2.0, 3.5, 1.0},
|
||||
{5.9, 3.0, 4.2, 1.5},
|
||||
{6.0, 2.2, 4.0, 1.0},
|
||||
{6.1, 2.9, 4.7, 1.4},
|
||||
{5.6, 2.9, 3.6, 1.3},
|
||||
{6.7, 3.1, 4.4, 1.4},
|
||||
{5.6, 3.0, 4.5, 1.5},
|
||||
{5.8, 2.7, 4.1, 1.0},
|
||||
{6.2, 2.2, 4.5, 1.5},
|
||||
{5.6, 2.5, 3.9, 1.1},
|
||||
{5.9, 3.2, 4.8, 1.8},
|
||||
{6.1, 2.8, 4.0, 1.3},
|
||||
{6.3, 2.5, 4.9, 1.5},
|
||||
{6.1, 2.8, 4.7, 1.2},
|
||||
{6.4, 2.9, 4.3, 1.3},
|
||||
{6.6, 3.0, 4.4, 1.4},
|
||||
{6.8, 2.8, 4.8, 1.4},
|
||||
{6.7, 3.0, 5.0, 1.7},
|
||||
{6.0, 2.9, 4.5, 1.5},
|
||||
{5.7, 2.6, 3.5, 1.0},
|
||||
{5.5, 2.4, 3.8, 1.1},
|
||||
{5.5, 2.4, 3.7, 1.0},
|
||||
{5.8, 2.7, 3.9, 1.2},
|
||||
{6.0, 2.7, 5.1, 1.6},
|
||||
{5.4, 3.0, 4.5, 1.5},
|
||||
{6.0, 3.4, 4.5, 1.6},
|
||||
{6.7, 3.1, 4.7, 1.5},
|
||||
{6.3, 2.3, 4.4, 1.3},
|
||||
{5.6, 3.0, 4.1, 1.3},
|
||||
{5.5, 2.5, 4.0, 1.3},
|
||||
{5.5, 2.6, 4.4, 1.2},
|
||||
{6.1, 3.0, 4.6, 1.4},
|
||||
{5.8, 2.6, 4.0, 1.2},
|
||||
{5.0, 2.3, 3.3, 1.0},
|
||||
{5.6, 2.7, 4.2, 1.3},
|
||||
{5.7, 3.0, 4.2, 1.2},
|
||||
{5.7, 2.9, 4.2, 1.3},
|
||||
{6.2, 2.9, 4.3, 1.3},
|
||||
{5.1, 2.5, 3.0, 1.1},
|
||||
{5.7, 2.8, 4.1, 1.3},
|
||||
{6.3, 3.3, 6.0, 2.5},
|
||||
{5.8, 2.7, 5.1, 1.9},
|
||||
{7.1, 3.0, 5.9, 2.1},
|
||||
{6.3, 2.9, 5.6, 1.8},
|
||||
{6.5, 3.0, 5.8, 2.2},
|
||||
{7.6, 3.0, 6.6, 2.1},
|
||||
{4.9, 2.5, 4.5, 1.7},
|
||||
{7.3, 2.9, 6.3, 1.8},
|
||||
{6.7, 2.5, 5.8, 1.8},
|
||||
{7.2, 3.6, 6.1, 2.5},
|
||||
{6.5, 3.2, 5.1, 2.0},
|
||||
{6.4, 2.7, 5.3, 1.9},
|
||||
{6.8, 3.0, 5.5, 2.1},
|
||||
{5.7, 2.5, 5.0, 2.0},
|
||||
{5.8, 2.8, 5.1, 2.4},
|
||||
{6.4, 3.2, 5.3, 2.3},
|
||||
{6.5, 3.0, 5.5, 1.8},
|
||||
{7.7, 3.8, 6.7, 2.2},
|
||||
{7.7, 2.6, 6.9, 2.3},
|
||||
{6.0, 2.2, 5.0, 1.5},
|
||||
{6.9, 3.2, 5.7, 2.3},
|
||||
{5.6, 2.8, 4.9, 2.0},
|
||||
{7.7, 2.8, 6.7, 2.0},
|
||||
{6.3, 2.7, 4.9, 1.8},
|
||||
{6.7, 3.3, 5.7, 2.1},
|
||||
{7.2, 3.2, 6.0, 1.8},
|
||||
{6.2, 2.8, 4.8, 1.8},
|
||||
{6.1, 3.0, 4.9, 1.8},
|
||||
{6.4, 2.8, 5.6, 2.1},
|
||||
{7.2, 3.0, 5.8, 1.6},
|
||||
{7.4, 2.8, 6.1, 1.9},
|
||||
{7.9, 3.8, 6.4, 2.0},
|
||||
{6.4, 2.8, 5.6, 2.2},
|
||||
{6.3, 2.8, 5.1, 1.5},
|
||||
{6.1, 2.6, 5.6, 1.4},
|
||||
{7.7, 3.0, 6.1, 2.3},
|
||||
{6.3, 3.4, 5.6, 2.4},
|
||||
{6.4, 3.1, 5.5, 1.8},
|
||||
{6.0, 3.0, 4.8, 1.8},
|
||||
{6.9, 3.1, 5.4, 2.1},
|
||||
{6.7, 3.1, 5.6, 2.4},
|
||||
{6.9, 3.1, 5.1, 2.3},
|
||||
{5.8, 2.7, 5.1, 1.9},
|
||||
{6.8, 3.2, 5.9, 2.3},
|
||||
{6.7, 3.3, 5.7, 2.5},
|
||||
{6.7, 3.0, 5.2, 2.3},
|
||||
{6.3, 2.5, 5.0, 1.9},
|
||||
{6.5, 3.0, 5.2, 2.0},
|
||||
{6.2, 3.4, 5.4, 2.3},
|
||||
{5.9, 3.0, 5.1, 1.8}
|
||||
};
|
@ -0,0 +1,97 @@
|
||||
#include <Arduino.h>
|
||||
#include <esp_now.h>
|
||||
#include "WiFi.h"
|
||||
#include "observations.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
const int timeBetweenBroadcasts = 5 * 60 * 1000;
|
||||
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
||||
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success 1" : "Delivery Fail 1");
|
||||
if (status ==0){
|
||||
Serial.println("Delivery Success 2");
|
||||
}
|
||||
else{
|
||||
Serial.println("Delivery fail 2");
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Seed random number generator
|
||||
randomSeed(analogRead(26));
|
||||
// 2700451
|
||||
// 5827809
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Starting...\n");
|
||||
|
||||
// Set device as a Wi-Fi Station
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
Serial.println(WiFi.macAddress());
|
||||
|
||||
// Init ESP-NOW
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error initializing ESP-NOW");
|
||||
for(;;) { delay(1); } // do not initialize wait forever
|
||||
}
|
||||
|
||||
Serial.println("initialized ESP-NOW");
|
||||
|
||||
|
||||
// Once ESPNow is successfully Init, we will register for Send CB to
|
||||
// get the status of Trasnmitted packet
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
|
||||
// Register peer
|
||||
esp_now_peer_info_t peerInfo;
|
||||
memset(&peerInfo, 0, sizeof(peerInfo));
|
||||
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
|
||||
// Add peer
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK){
|
||||
Serial.println("Failed to add peer");
|
||||
for(;;) { delay(1); } // do not initialize wait forever
|
||||
}
|
||||
}
|
||||
|
||||
float pickedObservation[observationFeatureCount];
|
||||
const int datasize=observationFeatureCount*sizeof(float);
|
||||
unsigned int pickedIndex;
|
||||
|
||||
void loop()
|
||||
{
|
||||
pickedIndex = random(observationCount);
|
||||
|
||||
Serial.print("Picked index ");
|
||||
Serial.print(pickedIndex);
|
||||
Serial.println();
|
||||
|
||||
// Fix. Less ugly way of copying?
|
||||
for (int i=0; i < observationFeatureCount; i++) {
|
||||
pickedObservation[i] = observations[pickedIndex][i];
|
||||
Serial.println(pickedObservation[i]);
|
||||
}
|
||||
//pickedObservation[4] = observations[pickedIndex];
|
||||
|
||||
// Send message via ESP-NOW (size of an int is 4 bytes on ESP32)
|
||||
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) pickedObservation, datasize);
|
||||
|
||||
if (result == ESP_OK) {
|
||||
Serial.println("Sent observation with success");
|
||||
}
|
||||
else {
|
||||
Serial.println("Error sending the data");
|
||||
}
|
||||
|
||||
Serial.println(WiFi.macAddress());
|
||||
delay(timeBetweenBroadcasts);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
# A Council of trees
|
||||
|
||||
Code is meant to run on ESP-32 boards.
|
||||
|
||||
The sender broadcasts an observation, over WiFi.
|
||||
|
||||
On receiving the data the receivers classify trees using a decision tree model.
|
|
@ -0,0 +1,159 @@
|
||||
indent_prefix = 2 * ' '
|
||||
|
||||
|
||||
def indent_line(line):
|
||||
return indent_prefix + line
|
||||
|
||||
|
||||
def encode_branch_with_unease(branch):
|
||||
if isinstance(branch, dict):
|
||||
if 'label' in branch:
|
||||
return encode_tree_with_unease(branch)
|
||||
else:
|
||||
return [ indent_line('return "?";') ]
|
||||
elif isinstance(branch, (int, float)):
|
||||
return [ indent_line('return {};'.format(branch)) ]
|
||||
else:
|
||||
return [ indent_line('return "{}";'.format(branch)) ]
|
||||
|
||||
# if value at index is smaller than threshold
|
||||
# enter left, else right
|
||||
|
||||
|
||||
def encode_tree_with_unease(tree):
|
||||
lines = [
|
||||
"if ((considerWithUnease)(\"{unease}\",(getObservationValue)({index}), {index}, {threshold})) {{".format(index=tree['index'], threshold=tree['value'], unease=tree['label']),
|
||||
*encode_branch_with_unease(tree['left']),
|
||||
"}",
|
||||
"else {",
|
||||
*encode_branch_with_unease(tree['right']),
|
||||
"}"
|
||||
]
|
||||
|
||||
return map(indent_line, lines)
|
||||
|
||||
|
||||
def encode_branch(branch):
|
||||
if isinstance(branch, dict):
|
||||
return encode_tree(branch)
|
||||
elif isinstance(branch, (int, float)):
|
||||
return [ indent_line('return {};'.format(branch)) ]
|
||||
else:
|
||||
return [ indent_line('return "{}";'.format(branch)) ]
|
||||
|
||||
# if value at index is smaller than threshold
|
||||
# enter left, else right
|
||||
|
||||
|
||||
def encode_tree(tree):
|
||||
lines = [
|
||||
"if ((consider)((getObservationValue)({index}), {index}, {threshold})) {{".format(index=tree['index'], threshold=tree['value']),
|
||||
*encode_branch(tree['left']),
|
||||
"}",
|
||||
"else {",
|
||||
*encode_branch(tree['right']),
|
||||
"}"
|
||||
]
|
||||
|
||||
return map(indent_line, lines)
|
||||
|
||||
|
||||
def make_classifier (tree):
|
||||
lines = [
|
||||
"#pragma once",
|
||||
"#include <cstdarg>",
|
||||
"namespace PublishingHouse",
|
||||
"{",
|
||||
*map(indent_line, [
|
||||
"namespace RandomForest",
|
||||
"{",
|
||||
*map(indent_line, [
|
||||
"class DecisionTree",
|
||||
"{",
|
||||
"public:",
|
||||
*map(indent_line, [
|
||||
"const char* predict(float (*getObservationValue)(int), bool (*consider)(float, int, float))",
|
||||
"{",
|
||||
*encode_tree(tree),
|
||||
"}",
|
||||
]),
|
||||
"private:",
|
||||
"};"
|
||||
]),
|
||||
"}"
|
||||
]),
|
||||
"}",
|
||||
]
|
||||
|
||||
return('\n'.join(lines))
|
||||
|
||||
|
||||
def make_classifier_unease (tree):
|
||||
lines = [
|
||||
"#pragma once",
|
||||
"#include <cstdarg>",
|
||||
"namespace PublishingHouse",
|
||||
"{",
|
||||
*map(indent_line, [
|
||||
"namespace RandomForest",
|
||||
"{",
|
||||
*map(indent_line, [
|
||||
"class TreeOfUnease",
|
||||
"{",
|
||||
"public:",
|
||||
*map(indent_line, [
|
||||
"const char* traverse(float (*getObservationValue)(int), bool (*considerWithUnease)(const char*, float, int, float))",
|
||||
"{",
|
||||
*encode_tree_with_unease(tree),
|
||||
"}",
|
||||
]),
|
||||
"private:",
|
||||
"};"
|
||||
]),
|
||||
"}"
|
||||
]),
|
||||
"}",
|
||||
]
|
||||
|
||||
return('\n'.join(lines))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import json
|
||||
import os.path
|
||||
import glob
|
||||
basepath = os.path.dirname(os.path.realpath(__file__))
|
||||
globpath = os.path.realpath(os.path.join(basepath, 'random_forest_model_*-trees.json'))
|
||||
globpath = os.path.realpath(os.path.join(basepath, '*.json'))
|
||||
|
||||
models = glob.glob(globpath)
|
||||
|
||||
# Search for exported models
|
||||
print("Found:")
|
||||
for k, modelpath in enumerate(models):
|
||||
print("[{}] {}".format(k, modelpath))
|
||||
|
||||
model_key = int(input("Which model?\n"))
|
||||
|
||||
modelpath = models[model_key]
|
||||
|
||||
# Open model
|
||||
with open(modelpath, 'r') as file_in:
|
||||
# Parse the forest
|
||||
forest = json.load(file_in)
|
||||
modelname, _ = os.path.splitext(os.path.basename(modelpath))
|
||||
classifiernamepattern = 'Tree_{{}}.h'.format(modelname)
|
||||
uneasenamepattern = 'Tree_of_unease_{{}}.h'.format(modelname)
|
||||
|
||||
# Walk through the forest and visualize the trees
|
||||
for idx, tree in enumerate(forest):
|
||||
print('Transforming tree {} of {}'.format(idx, len(forest)))
|
||||
|
||||
with open(os.path.join(basepath, classifiernamepattern.format(idx)), 'w') as h:
|
||||
h.write(make_classifier(tree))
|
||||
|
||||
with open(os.path.join(basepath, uneasenamepattern.format(idx)), 'w') as h:
|
||||
h.write(make_classifier_unease(tree))
|
||||
|
||||
|
||||
print('Classifiers placed in: {}'.format(basepath))
|
@ -0,0 +1,44 @@
|
||||
"""
|
||||
Parses the spreadsheet into a tree structure
|
||||
"""
|
||||
|
||||
import csv
|
||||
import os.path
|
||||
import json
|
||||
|
||||
columns = 4
|
||||
position = [ 0 for _ in range(columns) ]
|
||||
tree = []
|
||||
|
||||
def parse_column_chunk (column, start):
|
||||
label = data[start][column]
|
||||
end = start
|
||||
children = []
|
||||
|
||||
while end + 1 < len(data) and (data[end + 1][column] == label or data[end + 1][column] == ''):
|
||||
end += 1
|
||||
|
||||
if column + 1 < columns:
|
||||
child_end = start
|
||||
|
||||
while child_end <= end and child_end < len(data):
|
||||
_, child_end, child = parse_column_chunk(column + 1, child_end)
|
||||
child_end += 1
|
||||
children.append(child)
|
||||
|
||||
return (start, end, { 'label': label, 'children': children})
|
||||
|
||||
basepath = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
with open(os.path.join(basepath, 'data.csv'), 'r') as csvfile:
|
||||
reader = csv.reader(csvfile)
|
||||
data = [row for row in reader]
|
||||
|
||||
start = 0
|
||||
|
||||
while start < len(data):
|
||||
_, end, node = parse_column_chunk(0, start)
|
||||
start = end + 1
|
||||
tree.append(node)
|
||||
|
||||
json.dump(tree, open(os.path.join(basepath, 'unease.json'), 'w'), ensure_ascii=False)
|
Loading…
Reference in New Issue