-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.h
148 lines (140 loc) · 5.55 KB
/
tests.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef TESTS_H
#define TESTS_H
#include "common.h"
#include "vector.h"
void test1()
{
QList<Vector> sample;
for (int i = 0; i < 11; ++i)
{
for (int j = 0; j < 11; ++j)
{
Vector v;
v.append(i/10.0f);
v.append(j/10.0f);
v.name = QString::number(v[0], 'f', 2) + ", " + QString::number(v[1], 'f', 2);
v.has_name = true;
sample.append(v);
}
}
Map *map = new Map(2);
map->print();
map->train(sample);
map->print();
cout << endl << endl;
Vector v;
v.append(0.1);
v.append(0.9);
auto cluster = map->findRecursive(v);
for(int i = 0; i < cluster.size(); ++i)
{
cout << "[" << cluster[i].first << ", " << cluster[i].second << "], ";
}
new GUI(map);
}
void test2()
{
QFile file("plants.data");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << file.errorString();
return;
}
QString content = file.readAll();
QStringList lines = content.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
QMultiHash<QString, QString> data;
QList<QString> states;
QList<QString> names;
foreach(QString line, lines)
{
QStringList fields = line.split(",");
QString name = fields[0].replace(" ", "_");
if(!names.contains(name)) names.append(name);
for(int i = 1; i < fields.size(); ++i)
{
QString state = fields[i];
data.insertMulti(name, state);
if(!states.contains(state)) states.append(state);
}
}
QList<Vector> data_set;
foreach(QString name, names)
{
Vector v;
auto values = data.values(name);
foreach(QString state, states)
{
if(values.contains(state))
v.append(1);
else
v.append(0);
}
data_set.append(v);
}
cout << data_set.size() << endl;
Map *map = new Map(states.size());
map->train(data_set);
map->print();
new GUI(map);
}
Vector fillVector(QString flags, QString name)
{
Vector v;
v.name = name;
v.has_name = true;
for(int i = 0; i < flags.size(); ++i) if(flags[i].toLatin1() == '1') v.append(1); else if (flags[i].toLatin1() == '0') v.append(0);
return v;
}
void test3()
{
QList<Vector> data_set;
/*
data_set.append(fillVector("1 0 0 1 0 0 0 0 1 0 0 1 0", "Dove"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 1 0 0 0 0", "Chicken"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 1 0 0 0 1", "Duck"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 1 0 0 1 1", "Goose"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 1 1 0 1 0", "Owl"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 1 1 0 1 0", "Hawk"));
data_set.append(fillVector("0 1 0 1 0 0 0 0 1 1 0 1 0", "Eagle"));
data_set.append(fillVector("0 1 0 0 1 1 0 0 0 1 0 0 0", "Fox"));
data_set.append(fillVector("0 1 0 0 1 1 0 0 0 0 1 0 0", "Dog"));
data_set.append(fillVector("0 1 0 0 1 1 0 1 0 1 1 0 0", "Wolf"));
data_set.append(fillVector("1 0 0 0 1 1 0 0 0 1 0 0 0", "Cat"));
data_set.append(fillVector("0 0 1 0 1 1 0 0 0 1 1 0 0", "Tiger"));
data_set.append(fillVector("0 0 1 0 1 1 0 1 0 1 1 0 0", "Lion"));
data_set.append(fillVector("0 0 1 0 1 1 1 1 0 0 1 0 0", "Horse"));
data_set.append(fillVector("0 0 1 0 1 1 1 1 0 0 1 0 0", "Zebra"));
data_set.append(fillVector("0 0 1 0 1 1 1 0 0 0 0 0 0", "Cow"));
*/
// Small Medium Big 2-Legs 4-Legs Hair Scales Hooves Gills Mane Feathers Fins Hunt Run Fly Swim
data_set.append(fillVector("1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0", " Dove"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0", " Chicken"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1", " Duck"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 1", " Goose"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0", " Owl"));
data_set.append(fillVector("1 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0", " Hawk"));
data_set.append(fillVector("0 1 0 1 0 0 0 0 0 0 1 0 1 0 1 0", " Eagle"));
data_set.append(fillVector("0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 0", " Fox"));
data_set.append(fillVector("0 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0", " Dog"));
data_set.append(fillVector("0 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0", " Wolf"));
data_set.append(fillVector("1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0", " Cat"));
data_set.append(fillVector("0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 0", " Tiger"));
data_set.append(fillVector("0 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0", " Lion"));
data_set.append(fillVector("0 0 1 0 1 1 0 1 0 1 0 0 0 1 0 0", " Horse"));
data_set.append(fillVector("0 0 1 0 1 1 0 1 0 1 0 0 0 1 0 0", " Zebra"));
data_set.append(fillVector("0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0", " Cow"));
data_set.append(fillVector("1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0", " Lizard"));
data_set.append(fillVector("0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1", " Snake"));
data_set.append(fillVector("0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1", " Crocodile"));
data_set.append(fillVector("1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1", " Turtle"));
data_set.append(fillVector("1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0", " Tortoise"));
data_set.append(fillVector("1 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1", " Bass"));
data_set.append(fillVector("0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 1", " Trout"));
data_set.append(fillVector("0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 1", " Shark"));
data_set.append(fillVector("0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 1", " Dolphin"));
Map *map = new Map(16);
map->train(data_set);
map->print();
new GUI(map);
}
#endif // TESTS_H