-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEfficiencyTests.cpp
More file actions
66 lines (54 loc) · 1.98 KB
/
EfficiencyTests.cpp
File metadata and controls
66 lines (54 loc) · 1.98 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
#include "TestConfig.h"
#include "EfficiencyTests.h"
#include "GeneticAlgorithmBuilder.h"
#include <fstream>
#include <iostream>
#include <chrono>
void GeneticAlgorithmTester::runTests() {
std::ofstream outFile(outputFileName);
if (!outFile.is_open()) {
std::cerr << "Failed to open " << outputFileName << std::endl;
return;
}
outFile << "GenerationSize,MaxIter,RestartAfter,SelectStrategy,Duration(us),Iterations,bestScore,isSolved\n";
for (const auto& config : configs) {
auto start = std::chrono::high_resolution_clock::now();
GeneticAlgorithmBuilder builder(sudoku);
GeneticAlgorithm geneticAlgorithm = builder
.generationSize(config.generationSize)
.maxIter(config.maxIter)
.restartAfter(config.restartAfter)
.selectStrategy(config.selectStrategy)
.build();
geneticAlgorithm.Solve();
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
outFile << config.generationSize << ","
<< config.maxIter << ","
<< config.restartAfter << ","
<< static_cast<int>(config.selectStrategy) << ","
<< duration << ","
<< geneticAlgorithm._iterationsnumber << ","
<< geneticAlgorithm._bestScore << ","
<< geneticAlgorithm._solved << "\n";
}
outFile.close();
}
void GeneticAlgorithmTester::Config() {
configs.clear();
int maxIter = 500;
int restartAfter = 100;
GeneticAlgorithm::ParentSelectStrategy selectStrategy = GeneticAlgorithm::ParentSelectStrategy::Roulette;
for (int generationSize = 6000; generationSize <= 10000; generationSize += 1000) {
TestConfig config;
config.generationSize = generationSize;
config.maxIter = maxIter;
config.restartAfter = restartAfter;
config.selectStrategy = selectStrategy;
configs.push_back(config);
config.selectStrategy = GeneticAlgorithm::ParentSelectStrategy::Tournament;
configs.push_back(config);
config.selectStrategy = GeneticAlgorithm::ParentSelectStrategy::RankAndRandom;
configs.push_back(config);
}
}