/
main2.cpp
129 lines (105 loc) · 2.3 KB
/
main2.cpp
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
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include "evolutionaryAlgorithm.h"
#include "remoteArduino.h"
#include "camera.h"
#include "graphics.h"
static bool cameraThreadRunning = true;
static std::mutex posMutex;
static Vec2 currentPosition = Vec2(0, 0);
float getVirtualScore2(const std::vector<std::string>& data)
{
sf::Transform transform;
// Draw
for (const std::string& s : data)
{
if (s == "01")
{
transform.rotate(6.25f);
transform.translate(0.0f, 5.0f);
}
if (s == "02")
{
transform.rotate(-6.25f);
transform.translate(0.0f, -5.0f);
}
if (s == "10")
{
transform.rotate(-6.25f);
transform.translate(0.0f, 5.0f);
}
if (s == "11")
{
transform.translate(0.0f, 20.0f);
}
if (s == "12")
{
transform.rotate(-12.5f);
}
if (s == "20")
{
transform.rotate(6.25f);
transform.translate(0.0f, -5.0f);
}
if (s == "21")
{
transform.rotate(12.5f);
}
if (s == "22")
{
transform.translate(0.0f, -20.0f);
}
}
sf::Vector2f after = transform.transformPoint(0.0f, 0.0f);
return sqrtf(powf(after.x, 2.0f) + powf(after.y, 2.0f));
}
int main2()
{
// Start graphics thread
Graphics graphics;
graphics.startThread();
Sleep(1000);
// Arduino setup
EvolutionaryAlgorithm ea;
int counter = 0;
bool loop = true;
while (loop)
{
// Print population
std::vector<Individual> pop = ea.children;
std::cout << pop.size() << std::endl;
//Vecotr of scores
std::vector<double> score;
// For each population
for (unsigned i = 0; i < pop.size(); i++)
{
Individual ind = pop[i];
std::cout << "----" << "\n";
//Get stop position from camera
posMutex.lock();
Vec2 startPos = currentPosition;
posMutex.unlock();
graphics.setData(ind.genotype);
//Calculate score
double individualScore = (double)getVirtualScore2(ind.genotype);
std::cout << "Score: " << individualScore;
if (individualScore > 90.0)
{
loop = false;
graphics.saveAsImage("finished");
}
counter++;
graphics.saveAsImage(std::to_string(counter));
score.push_back(individualScore);
}
//Send score to algoritm
ea.evolutionaryLoop(score);
}
// Shutdown the software, wait for threads
std::cout << "Shutting down..." << std::endl;
cameraThreadRunning = false;
graphics.join();
return 0;
}