-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEGALab.cpp
More file actions
292 lines (263 loc) · 7.13 KB
/
EGALab.cpp
File metadata and controls
292 lines (263 loc) · 7.13 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// EGALab.cpp: îïðåäåëÿåò òî÷êó âõîäà äëÿ êîíñîëüíîãî ïðèëîæåíèÿ.
//
#include "stdafx.h"
#include "ByteVector.h"
#include "ByteVectorMath.h"
#include <iostream>
#include <exception>
#include "vcruntime_exception.h"
#include "IllegalVectorSizeException.h"
#include "MonteCarlosMethodSearching.h"
#include "DepthSearchMethod.h"
#include "SearchInWidthMethod.h"
#include "AnsambleSearchMethod.h"
#include <vector>
#include "DispacementVector.h"
#include "NeighborsSearchMethod.h"
#include "NearestCitySearchMethod.h"
#include <iostream>
#include <fstream>
#include <string>
#include "GreedyBackpackSearchMethod.h"
#include "GreedyDancigBackpackSearchMethod.h"
using namespace exceptions;
using namespace std;
using namespace MethodSearching;
using namespace Salesman;
using namespace BackpackSearchMethod;
void testMonte();
void testInDepth();
void testInWidth();
void testvalue();
void testmera();
void testfuncs();
void testGrayCode();
void testEnsemble();
void testDisplacementVector();
void testNeighbors();
void testNearestNeighbors();
void testBackpack();
void testDancigBackpack();
matrix ParseMatrixFromFile(string filename, int size);
constexpr int size = 5;
int main()
{
//throw IllegalVectorSizeException(1,2);
/*testMonte();
testInDepth();
testNeighbors();
testInWidth();
testEnsemble();
testDisplacementVector();*/
testNearestNeighbors();
/* testBackpack();
testDancigBackpack();
testvalue();
testmera();
testGrayCode();
testfuncs();*/
getchar();
return 0;
}
void testfuncs() {
int vect1 = 0b00101;
cout << ByteVectorMath::GrayCodeEncoder(ByteVector(0b000, 3))<<endl;
cout << ByteVectorMath::GrayCodeEncoder(ByteVector(0b010, 3))<<endl;
cout << ByteVectorMath::GrayCodeEncoder(ByteVector(0b110, 3))<<endl;
cout << ByteVector(0b110100,6)<<" " <<ByteVectorMath::GrayCodeDecoder( ByteVectorMath::GrayCodeEncoder(ByteVector(0b110100, 6)))<<endl;
}
void testBackpack() {
srand(12);
vector<weightvalue> weights;
vector<costvalue> costs;
for (size_t i = 0; i < 20; i++)
{
weights.push_back(rand() % 41+1);
costs.push_back(rand() % 13+1);
}
clog << "Answer is:" << GreedyBackpackSearchMethod(weights, costs, 41).find();
}
void testDancigBackpack() {
srand(11);
vector<weightvalue> weights;
vector<costvalue> costs;
for (size_t i = 0; i < 20; i++)
{
weights.push_back(rand() % 41 + 1);
costs.push_back(rand() % 13 + 1);
}
clog << "Answer is:" << GreedyDancigBackpackSearchMethod(weights, costs, 41).find();
}
void testmera() {
int vect1 = 0b00101;
int vect2 = 0b00110;
try
{
cout << ByteVectorMath::HemmingLength(ByteVector(vect1, 5), ByteVector(vect2, 6));
}
catch (const std::exception& expr)
{
cout << expr.what();
}
}
void testMonte() {
srand(0);
//ByteVector* bytes = new ByteVector[32];
vector<ByteVector> vectors = vector<ByteVector>();
vector<funcvalue> values = vector<funcvalue>();
for (size_t i = 0; i < 1<<5; i++)
{
vectors.push_back(ByteVector(i, 5));
int t = abs(rand() % 60);
values.push_back(t);
clog << ByteVector(i, 5) << " " << t << endl;;
}
cout << "Monte Carlo"<<endl;
cout << MonteCarlosMethodSearching(vectors, values, 8, 39).find()<<endl << endl<<endl;
}
void testInDepth() {
srand(0);
//ByteVector* bytes = new ByteVector[32];
vector<ByteVector> vectors = vector<ByteVector>();
vector<funcvalue> values = vector<funcvalue>();
for (size_t i = 0; i < 1 << 5; i++)
{
vectors.push_back(ByteVector(i, 5));
int t = i;
values.push_back(t);
clog << ByteVector(i, 5) << " " << t << endl;;
}
cout << "In Depth" << endl;
cout << DepthSearchMethod(vectors, values, 10, 66).find() << endl << endl << endl;
}
void testGrayCode() {
srand(0);
//ByteVector* bytes = new ByteVector[32];
vector<ByteVector> vectors = vector<ByteVector>();
vector<funcvalue> values = vector<funcvalue>();
cout << "True values" << endl;
for (size_t i = 0; i < 1 << 5; i++)
{
int t = i - (2 << 4);
t *= t;
clog << ByteVector(i, 5) << " " << t << endl;
}
cout << "Gray code values" << endl;
for (size_t i = 0; i < 1 << 5; i++)
{
vectors.push_back(ByteVectorMath::GrayCodeEncoder(ByteVector(i, 5)));
int t = i - (2 << 4);
t *= t;
values.push_back(t);
clog << ByteVectorMath::GrayCodeEncoder( ByteVector(i, 5)) << " " << t << endl;
}
cout << "In Width with Gray code" << endl;
cout <<"Decoded value:"<<ByteVectorMath::GrayCodeDecoder( SearchInWidthMethod(vectors, values, 8, 32).find()) << endl << endl;
}
void testEnsemble() {
srand(0);
//ByteVector* bytes = new ByteVector[32];
vector<ByteVector> vectors = vector<ByteVector>();
vector<funcvalue> values = vector<funcvalue>();
for (int i = 1; i < 1 << 7; i++)
{
vectors.push_back(ByteVector(i, 7));
funcvalue t = 5 * sin(i) + log(i);
values.push_back(t);
clog << ByteVector(i, 7) << " " << t << endl;;
}
cout << "Ensemble" << endl;
cout << AnsambleSearchMethod(vectors, values, 4, 4,23).find() << endl << endl;
}
void testDisplacementVector()
{
DispacementVector test(5);
cout << test <<endl;
cout << test.CheckValue()<<endl;
test.setNum(1, 5);
cout << test.CheckValue() << endl;
test = DispacementVector(5);
test.InsertWithRightOffset(1, 8);
cout << test << " ";
test = DispacementVector(5);
test.InsertWithRightOffset(3, 8);
cout << test << " ";
test = DispacementVector(5);
test.InsertWithRightOffset(5, 8);
cout << test << " "<<endl;
}
void testNeighbors()
{
const float inf = 1 << 16;
matrix matr = { {inf,10,5,9,16,8},
{6,inf,11,8,18,19},
{7,13,inf,3,4,14},
{5,9,6,inf,12,17},
{5,4,11,6,inf,14},
{17,7,12,13,16,inf} };
NeighborsSearchMethod neigh = NeighborsSearchMethod(ParseMatrixFromFile("SalesmanData.txt", 15),1);
cout << neigh.find();
}
matrix ParseMatrixFromFile(string filename,int size) {
matrix returnedmatrix;
ifstream fin;
fin.open(filename);
vector<value> row;
int column = 0;
while (!fin.eof())
{
value val;
fin >> val;
// cout << val<<" ";
row.push_back(val);
column++;
if (column >= size) {
// cout << endl;
returnedmatrix.push_back(row);
row.clear();
column = 0;
}
}
return returnedmatrix;
}
void testNearestNeighbors()
{
const float inf = 1 << 16;
srand(82);
matrix matr = { { inf,10,5,9,16,8 },
{ 6,inf,11,8,18,19 },
{ 7,13,inf,3,4,14 },
{ 5,9,6,inf,12,17 },
{ 5,4,11,6,inf,14 },
{ 17,7,12,13,16,inf } };
//NearestCitySearchMethod neigh = NearestCitySearchMethod(matr);
NearestCitySearchMethod neigh = NearestCitySearchMethod(ParseMatrixFromFile("SalesmanData.txt", 15),8);
cout << neigh.find();
}
void testInWidth() {
srand(0);
//ByteVector* bytes = new ByteVector[32];
vector<ByteVector> vectors = vector<ByteVector>();
vector<funcvalue> values = vector<funcvalue>();
for (size_t i = 0; i < 1 << 5; i++)
{
vectors.push_back(ByteVector(i, 5));
int t = i - (2<<4);
t *= t;
values.push_back(t);
clog << ByteVector(i, 5) << " " << t << endl;;
}
cout << "In Width" << endl;
cout << SearchInWidthMethod(vectors, values, 8, 39).find() << endl << endl;
}
void testvalue() {
int testval = 0b01001;
//cout << testval << endl;
ByteVector& byte = ByteVector(testval,5);
cout <<byte.offestRight(2);
cout << endl<<endl;
ByteVector vect1 = ByteVector(7,5);
ByteVector vect2 = ByteVector(6,5);
cout << vect1 << endl << vect2 << endl;
cout << vect1.SymmetrycSumm(vect2);
}