-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
71 lines (59 loc) · 1.96 KB
/
Controller.cpp
File metadata and controls
71 lines (59 loc) · 1.96 KB
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
//
// Created by Kevin Gaffney on 4/12/18.
//
#include <iostream>
#include <cmath>
#include <random>
#include <bitset>
#include "Controller.h"
using namespace std;
Controller::Controller() {
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<unsigned int> dist(0, 255);
for (int i = 0; i < 33; i++) {
genome[i] = dist(mt);
}
}
Controller::Controller(const unsigned int *genome) {
for (int i = 0; i < 33; i++) {
this->genome[i] = genome[i];
}
}
void Controller::control(double leftCameraValueR, double leftCameraValueG, double leftCameraValueB,
double rightCameraValueR, double rightCameraValueG, double rightCameraValueB,
double* controls) {
double hidden[3];
for (int i = 0; i < 3; i++) {
hidden[i] = tanh(genome[i] * leftCameraValueR
+ genome[3 + i] * leftCameraValueG
+ genome[6 + i] * leftCameraValueB
+ genome[9 + i] * rightCameraValueR
+ genome[12 + i] * rightCameraValueG
+ genome[15 + i] * rightCameraValueB
- genome[27 + i]);
}
for (int i = 0; i < 3; i++) {
controls[i] = tanh(genome[18 + i] * hidden[0]
+ genome[21 + i] * hidden[1]
+ genome[24 + i] * hidden[2]
- genome[30 + i]);
}
}
unsigned int* Controller::getGenome() {
return genome;
}
vector<bitset<8>> Controller::getBinaryGenome() {
vector<bitset<8>> binaryGenome;
binaryGenome.reserve(33);
for (unsigned int gene : genome) {
binaryGenome.emplace_back(bitset<8>(gene));
}
return binaryGenome;
}
void Controller::setBinaryGenome(vector<bitset<8>> binaryGenome) {
if (binaryGenome.size() != 33) return;
for (unsigned long i = 0; i < 33; i++) {
genome[i] = (unsigned int) binaryGenome.at(i).to_ulong();
}
}