-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxinfoGuesser.cpp
79 lines (61 loc) · 1.66 KB
/
MaxinfoGuesser.cpp
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
#include <iostream>
#include <string>
#include <list>
#include <utility>
#include <cstdlib>
#include <cmath>
#include "Jotto_utils.h"
#include "RandomGuesser.h"
#include "MaxinfoGuesser.h"
using namespace std;
double my_log2(const double&);
MaxinfoGuesser::MaxinfoGuesser (const string& name, const string& path_to_wordlist) :
RandomGuesser(name, path_to_wordlist) {}
string MaxinfoGuesser::submit_codeword() const
{
return RandomGuesser::guess();
}
string MaxinfoGuesser::guess() const
{
string best_word = RandomGuesser::guess();
int numwords = get_wordlist_size();
double max_info = 0.0;
int i = 0;
for(list<string>::const_iterator it = wordlist.begin();
it != wordlist.end(); ++it)
{
if ((rand() % numwords) < (numwords * SAMPLING_PROB))
{
double w_info = calc_information(*it);
if(w_info > max_info)
{
max_info = w_info;
best_word = *it;
}
++i;
}
}
return best_word;
}
double MaxinfoGuesser::calc_information (const string& s) const
{
double count_of_counts [JOTTO_LENGTH + 1];
//for w in wordlist
for (list<string>::const_iterator it = wordlist.begin();
it != wordlist.end(); ++it)
{
++count_of_counts[score(s, *it)];
}
double information = 0.0;
// Calculate entropy (bits of information)
for (int i = 0; i < JOTTO_LENGTH + 1; ++i)
{
double frac = count_of_counts[i] / wordlist.size();
information += -1 * frac * my_log2(frac);
}
return information;
}
double my_log2(const double& d)
{
return log(d)/log(2);
}