-
Notifications
You must be signed in to change notification settings - Fork 0
/
chuck.cpp
298 lines (218 loc) · 6.74 KB
/
chuck.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
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
293
294
295
296
#include "chuck.h"
//=============================================
// @filename chuck.cpp
// @date April 1st 2013
// @author Jonathan H. (Sheljohn on Github)
// @contact ariel .dot hadida [at] gmail
// @license Creative Commons by-nc-sa 3.0
// http://creativecommons.org/licenses/by-nc-sa/3.0/
//=============================================
/******************** ********** ********************/
/******************** ********** ********************/
/**
* [Jonathan::clear Clear all member data.]
*/
void Jonathan::clear()
{
// Disable pointers
pi_new = pi_old = nullptr;
// Clear arrays
degrees.~valarray();
neighbors.~valarray();
array_a.~valarray();
array_b.~valarray();
// Scalars
next_shot = n_nodes = n_edges = 0;
}
/**
* [Jonathan::set_forest Introduce Chuck to the forest.]
* @param forest [Freshly generated forest.]
* @return [Initialization success.]
*/
bool Jonathan::set_forest( const Forest& forest )
{
// Safety check
if ( !forest ) return false;
// Set scalar properties first
next_shot = 0;
n_nodes = forest.size();
n_edges = forest.get_neighbors().size();
// Copy degrees and neighbors
degrees = forest.get_degrees(); // Note: std requires to resize first, but gcc is a big boy.
neighbors = std::valarray<unsigned>( forest.get_neighbors().data(), n_edges );
// Load gun
restart();
// Set distributions pointers
pi_new = &array_a[0];
pi_old = &array_b[0];
// Report success
return true;
}
/**
* [Jonathan::swap_pointers This is a trick to avoid copying each time pi_new to pi_old.
* Instead, pointers are swapped just before the update. That way, we virtually replace
* the old-old distribution with the old-new (which becomes the new-old), and clear the
* old-old to store the new-new. Got it? If yes, frankly cheers. ;)]
*/
void Jonathan::swap_pointers()
{
double *ptr = pi_old;
pi_old = pi_new;
pi_new = ptr;
}
/**
* [Jonathan::reload Reset tables to uniform probability distributions.]
*/
void Jonathan::restart()
{
array_a.resize(n_nodes, (1.0/n_nodes) );
array_b.resize(n_nodes, (1.0/n_nodes) );
}
/**
* [Jonathan::shoot Shoot the darn monkey.]
* @return [The chosen tree (that sounds like Avatar..). If Chuck is stuck, dial -1.]
*/
int Jonathan::shoot()
{
// Remember current shot
const unsigned tree = next_shot;
// Trick to avoid testing for the tree being shot
swap_pointers(); pi_old[ tree ] = 0.0;
// Find new max probability
register double pi_max = 0.0;
// Iterator on neighbors
const unsigned *neighbor = &neighbors[0];
// Compute new probability distribution
for ( unsigned t = 0; t < n_nodes; ++t )
{
// Reset probability
pi_new[t] = 0.0;
// Update probability
for ( unsigned d = 0; d++ < degrees[t]; ++neighbor )
pi_new[t] += pi_old[ *neighbor ] / degrees[ *neighbor ];
// Select tree with max probability for the next shot
if ( pi_new[t] > pi_max )
{
next_shot = t;
pi_max = pi_new[t];
}
}
// If max has become too small, scale tables
if ( pi_max <= CHUCK_EPSILON )
{
array_a /= CHUCK_EPSILON;
array_b /= CHUCK_EPSILON;
}
// Return current shot
return (int) tree;
}
/******************** ********** ********************/
/******************** ********** ********************/
/**
* [Angelo::clear Clear all member data.]
*/
void Angelo::clear()
{
// Clear shot sequence
shot_sequence.clear();
// Reset iterator
current_shot = shot_sequence.rbegin();
}
/**
* [Angelo::set_forest Set member data from current forest.]
* @param forest [A freshly generated forest.]
* @return [Whether setting was successful or not.]
*/
bool Angelo::set_forest( const Forest& forest )
{
// Get forest information
Forest::agl_pair_type cfg;
Forest::agl_vector_type cx;
forest.acm_export( cfg, cx );
// Remember the number of trees
n_nodes = cfg.first;
// Reset adjacency
memset( adjacency, 0, 22*sizeof(int) );
for ( auto it = cx.cbegin(); it != cx.cend(); ++it )
{
adjacency[ it->first ] |= 1 << it->second;
adjacency[ it->second ] |= 1 << it->first;
}
// Compute results
impossible = !bfs();
// Restart iterator
restart();
// // Print shot sequence
// printf("Shot sequence(%u) = [", shot_sequence.size());
// for ( auto it = shot_sequence.begin(); it != shot_sequence.end(); ) printf(" %d ", *it++);
// if (impossible) printf("] [IMP] \n"); else printf("]\n");
// Report success
return true;
}
/**
* [Angelo::restart Reset shot sequence.]
*/
void Angelo::restart()
{
current_shot = shot_sequence.rbegin();
}
/**
* [Angelo::bfs Setup shooting strategy from the forest adjacency.]
* @return [Whether the planning was successful or the algorithm failed.]
*/
bool Angelo::bfs()
{
// Local variables
int exploration_set, complement_adjacency;
// Allocate storage
std::bitset<MAXSIZE> explored;
std::vector<int> target_tree( MAXSIZE ), parent_set( MAXSIZE );
// Reset shot sequence
shot_sequence.clear();
// Begin from the first node (explore all others)
const int all_nodes_but_first = (1 << n_nodes) - 1;
// Initialize unexplored queue
std::list<int> unexplored;
unexplored.push_back(all_nodes_but_first);
// Explore nodes
while( !unexplored.empty() )
{
// Pull new exploration set from queue
if( (exploration_set = unexplored.front()) == 0 ) break;
unexplored.pop_front();
// Explore each node of the set
for ( int i = 0, current_node = 1; i < n_nodes; ++i, current_node = current_node << 1 )
if( exploration_set & current_node )
{
// All nodes of the set, except the current one
const int node_complement = exploration_set ^ current_node;
// Create a new set with the adjacency of the complement
complement_adjacency = 0;
// Iterate on each node of the complement, adding its adjacency
for ( int j = 0, other_node = 1; j < n_nodes; ++j, other_node = other_node << 1 )
if( node_complement & other_node)
complement_adjacency |= adjacency[j];
// Remember the current node, the current complement adjacency, and
// the link between this new set and the current exploration set.
if( !explored[complement_adjacency] )
{
explored[complement_adjacency] = true;
parent_set[complement_adjacency] = exploration_set;
target_tree[complement_adjacency] = i;
unexplored.push_back(complement_adjacency);
}
}
}
// Impossible
if( exploration_set != 0 ) return false;
// Rollback through the parent sets, and stack the corresponding target trees
while( exploration_set != all_nodes_but_first )
{
shot_sequence.push_back(target_tree[exploration_set]);
exploration_set = parent_set[exploration_set];
}
// Report success
return true;
}
/******************** ********** ********************/
/******************** ********** ********************/