-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencoder.h
137 lines (115 loc) · 4.02 KB
/
encoder.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
#ifndef ENCODER_H
#define ENCODER_H
#include "evolution.h"
#include "search.h"
#include <fstream>
#include <map>
using namespace Evolution;
namespace Napoleon
{
class Encoder
{
public:
Chromosome Encode(int arr[])
{
Chromosome encoded(Bits());
int start = 0;
for (auto const& m : sizes)
{
Chromosome temp(m.second, arr[m.first]);
for(auto i=0u; i<temp.size(); i++)
encoded.set(start + i, temp.test(i));
start += m.second;
}
assert(encoded.size() == Bits());
return encoded;
}
Encoder ()
:sizes()
{
}
size_t Bits()
{
return bits;
}
Encoder Decode(const Chromosome& code)
{
int start = 0;
for(auto m : sizes)
{
fields[m.first] = Subset(code, start, m.second).to_ulong();
start += m.second;
}
for(auto& t : vectors)
{
std::vector<int>& v = t.second;
for(auto i=0u; i<v.size(); i++)
{
v[i] = char(Subset(code, start, vfield_bits).to_ulong());
start += vfield_bits;
}
}
return *this;
}
void AddField(Search::Parameters id, size_t bits)
{
if (sizes.find(id) != sizes.end())
throw std::exception();
sizes.emplace(std::make_pair(id, bits));
//sizes[id] = bits;
this->bits += bits;
}
void AddVector(std::vector<int> vect, std::string name)
{
if (vectors.find(name) != vectors.end())
throw std::exception();
vectors.emplace(std::make_pair(name, vect));
this->bits += vfield_bits*vect.size();
}
int GetGene(Search::Parameters index)
{
return fields[index];
}
void ApplyGenes(ofstream& file)
{
/*
for (auto const& m : sizes) // search parameters
file << "setoption name " << Search::param_name[m.first]
<< " value " << fields[m.first] << std::endl;
*/
for (auto &v : vectors) // evaluation parameters
{
file << "setoption name " << v.first << " value";
for (auto &e : v.second)
file << " " << e;
file << std::endl;
}
}
friend ostream& operator<<(ostream& out, const Encoder& encoder)
{
out << "{ ";
for (auto const& m : encoder.sizes)
out << encoder.fields[m.first] << " , ";
out << " };" << std::endl;
out << std::endl;
for (auto const& m : encoder.sizes)
out << Search::param_name[m.first] << "=" << encoder.fields[m.first] << " ";
for (auto const& v : encoder.vectors) // evaluation parameters
{
out << v.first << " = { " << std::endl;
for (auto e : v.second)
out << e << ", ";
}
//for (auto i=0; i<Search::Parameters::MAX; i++)
//out << encoder.fields[i] << " , ";
return out;
}
private:
std::map<std::string, std::vector<int>> vectors;
std::map<Search::Parameters, std::size_t> sizes;
int fields[Search::Parameters::MAX];
size_t bits = 0;
const size_t vfield_bits = 8;
};
}
#endif