-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (110 loc) · 4.07 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (110 loc) · 4.07 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
#include <iostream>
#include <stdlib.h>
#include <numeric>
#include "ThreadPool.h"
#include "Result.h"
#include "Knowledge.h"
#include "DictManager.h"
int iterationCount(Knowledge knowledge, std::string_view initialGuess, std::string_view secret, std::shared_ptr<std::ofstream> outTxt) {
result res = defaultResult;
std::string_view guess = initialGuess;
int i = 1;
if (outTxt) (*outTxt) << secret;
for (; true; i++) {
if (outTxt) (* outTxt) << " " << guess;
res = checkWithSecret(guess, secret);
if (isComplete(res)) {
break;
}
else {
knowledge.ween(guess, res);
knowledge.updatePopularity();
guess = knowledge.generateGuess(res);
}
if (i == 6) {
if (outTxt) (*outTxt) << '\n';
return 6;
}
}
if (outTxt) (*outTxt) << '\n';
return i;
}
float average(std::vector<float> const& v) {
if (v.empty()) {
return 0;
}
auto const count = static_cast<float>(v.size());
return std::reduce(v.begin(), v.end()) / count;
}
void baseLineAlgorithm(DictManager& dict, Knowledge& knowledge) {
auto start = std::chrono::high_resolution_clock::now();
const size_t wordCount = dict.words().size();
knowledge.updatePopularity();
auto initialGuess = knowledge.generateGuess(defaultResult);
size_t threadCount = std::thread::hardware_concurrency();
thread_pool::ThreadPool tp{threadCount};
const size_t wordsPerThread = wordCount / threadCount;
std::vector<std::future<std::vector<int>>> res;
res.reserve(threadCount);
std::shared_ptr<std::ofstream> outTxt = (threadCount == 1) ? std::make_shared<std::ofstream>() : nullptr;
if (outTxt) outTxt->open("out_results.txt");
for (size_t i = 0; i <threadCount; i++) {
const size_t startIndex = i * wordsPerThread;
const size_t remainingWords = (i == threadCount - 1) ? wordCount % wordsPerThread : 0;
const size_t endIndex = (i+1) * wordsPerThread + remainingWords;
const auto& job = [knowledge, dict, outTxt, initialGuess, startIndex, endIndex]() mutable {
std::vector<int> iterationCounts;
iterationCounts.reserve(endIndex - startIndex);
for (size_t i = startIndex; i < endIndex; i++) {
auto iterations = iterationCount(knowledge, initialGuess, dict.word(i), outTxt);
iterationCounts.push_back(iterations);
}
return iterationCounts;
};
res.push_back(tp.submit(job));
}
std::vector<float> averageVec;
averageVec.reserve(wordCount);
for (auto& r : res) {
for (const auto& count : r.get()) {
averageVec.push_back(count);
}
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<std::chrono::seconds>(stop - start);
if (outTxt) outTxt->close();
std::cout << "The average is: " << average(averageVec) << ", duration: " << duration.count() << "(s)" << '\n';
}
void runAgainstWebsite(DictManager& dict, Knowledge& knowledge) {
std::optional<result> res = defaultResult;
std::string resStr;
std::string guess;
int i = 0;
for (; i < 20 && knowledge.wordCount() != 0; i++) {
knowledge.updatePopularity();
guess = knowledge.generateGuess(*res);
std::cout << "guess:" << guess << '\n';
do {
std::cout << "Enter Result:";
std::cin >> resStr;
res = stringToResult(resStr);
} while (res == std::nullopt);
if (isComplete(*res)) {
std::cout << i + 1 << '\n';
break;
}
else {
knowledge.ween(guess, *res);
std::cout << "word count: " << knowledge.wordCount() << '\n';
}
}
}
int main() {
std::unique_ptr<DictManager> dict = std::make_unique<DictManager>();
std::unique_ptr<Knowledge> knowledge = std::make_unique<Knowledge>();
dict->load();
knowledge->load(dict->words());
baseLineAlgorithm(*dict, *knowledge);
//runAgainstWebsite(*dict, *knowledge);
return 0;
}