-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_abstraction.cpp
151 lines (118 loc) · 3.62 KB
/
model_abstraction.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
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
#include <iostream>
#include <numeric>
#include <vector>
#include <cassert>
#include <functional>
#include <chrono>
#include "model.hpp"
using namespace std;
using namespace chrono;
using namespace afg::model;
struct CoinGame {
int target;
int currentSum;
int turnCount;
vector<int> coins;
vector<int> coinsChosen;
using move_t = int;
CoinGame(int t, const vector<int>& coins)
: target(t),
currentSum(0),
turnCount(0),
coins(coins)
{}
int getSum() const {
return accumulate(coinsChosen.begin(), coinsChosen.end(), 0);
}
bool isTerminal() {
return getSum() >= target;
}
int getTurnCount() const {
return turnCount;
}
vector<move_t> getAvailableMoves() {
return coins;
}
void makeMove(int coin) {
coinsChosen.push_back(coin);
turnCount++;
}
bool operator==(const CoinGame& other) const
{
return getSum() == other.getSum() && coinsChosen == other.coinsChosen;
}
};
template<>
struct std::hash<CoinGame> {
/* https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x */
size_t operator() (const CoinGame & cg) const
{
size_t seed = 0;
for (const auto& coin : cg.coinsChosen) {
seed ^= hash<int>()(coin) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
seed ^= hash<int>()(cg.target) + 0x9e3779b9 + (seed<<6) + (seed>>2);
for (const auto& coin : cg.coins) {
seed ^= hash<int>()(coin) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
return seed;
}
};
struct CGSearchResult {
bool success;
int explored;
vector<CoinGame> matches;
};
CGSearchResult cgBfsFind(CoinGame initState,
std::function<bool(const CoinGame&)> isGoal,
int depthLimit) {
queue<CoinGame> frontier;
unordered_set<CoinGame> visited;
frontier.push(initState);
visited.insert(initState);
CGSearchResult result;
result.explored = 0;
while(!frontier.empty()) {
CoinGame st = frontier.front();
frontier.pop();
result.explored++;
if (isGoal(st)) {
result.success = true;
result.matches.push_back(st);
continue;
}
if (st.isTerminal() || st.getTurnCount() == depthLimit) {
continue;
}
for (int mv : st.getAvailableMoves()) {
CoinGame neighbor(st);
neighbor.makeMove(mv);
if (visited.find(neighbor) != visited.end()) {
continue;
}
visited.insert(neighbor);
frontier.push(neighbor);
}
}
result.success = !result.matches.empty();
return result;
}
int main(int argc, char **argv) {
if (argc != 2) {
cout << "usage: " << argv[0] << " <depth>" << endl;
exit(1);
}
int depth = atoi(argv[1]);
CoinGame cg1(100, {1, 5});
auto sum50 = [](const CoinGame& cg){return cg.getSum() >= 50;};
auto t0 = high_resolution_clock::now();
auto result1 = bfsFind(cg1, sum50, depth);
auto t1 = high_resolution_clock::now();
cout << "Templated: " << duration_cast<milliseconds>(t1 - t0).count() << endl;
t0 = high_resolution_clock::now();
auto result2 = cgBfsFind(cg1, sum50, depth);
t1 = high_resolution_clock::now();
cout << "Explicit: " << duration_cast<milliseconds>(t1 - t0).count() << endl;
cout << "Nodes explored: " << result1.explored << endl;
return 0;
}