-
Notifications
You must be signed in to change notification settings - Fork 2
/
algorithms.h
308 lines (255 loc) · 8.04 KB
/
algorithms.h
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
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef ALGORITHMS_H
#define ALGORITHMS_H
#include "global.h"
#include "hash.h"
#include <assert.h>
#include <math.h>
#include <deque>
#include <set>
class action_t;
class hash_t;
class state_t;
class problem_t;
class heuristic_t;
class Action;
/*******************************************************************************
*
* algorithm (abstract class)
*
******************************************************************************/
class algorithm_t
{
protected:
const problem_t *problem_;
public:
algorithm_t() : problem_(0) { }
algorithm_t( const problem_t &problem ) : problem_(&problem) { }
virtual ~algorithm_t() { }
virtual const action_t *next( const state_t &state ) = 0;
virtual double value( const state_t &state ) = 0;
virtual void statistics( std::ostream &os ) const = 0;
};
/*******************************************************************************
*
* random
*
******************************************************************************/
class random_t : public algorithm_t
{
public:
random_t( const problem_t &problem ) : algorithm_t(problem) { }
virtual ~random_t() { }
virtual const action_t *next( const state_t &state )
{
std::set<const action_t*> actions;
actionList_t::const_iterator ai;
std::set<const action_t*>::const_iterator it;
for( ai = problem_->actionsT().begin(); ai != problem_->actionsT().end(); ++ai )
if( (*ai)->enabled( state ) )
actions.insert( *ai );
assert( actions.size() > 0 );
unsigned action = lrand48() % actions.size();
for( it = actions.begin(); it != actions.end(); ++it )
if( action-- == 0 ) return( *it );
return( 0 );
}
virtual double value( const state_t &state ) { return( 0.0 ); }
virtual void statistics( std::ostream &os ) const { }
};
/*******************************************************************************
*
* value iteration
*
******************************************************************************/
class valueIteration_t : public algorithm_t
{
hash_t *hash_table_;
double epsilon_;
enum { OPEN = 0x1, CLOSED = 0x2 };
void solve( void );
public:
valueIteration_t( const problem_t &problem, hash_t &hash_table, double epsilon );
virtual ~valueIteration_t();
virtual const action_t *next( const state_t &state )
{
double value;
return( problem_->actionsT()[hash_table_->bestAction( state, *problem_, value )] );
}
virtual double value( const state_t &state )
{
hashEntry_t *entry = hash_table_->find( state );
assert( !entry );
return( entry->value() );
}
virtual void statistics( std::ostream &os ) const;
};
/*******************************************************************************
*
* lrtdp
*
******************************************************************************/
class lrtdp_t : public algorithm_t
{
hash_t *hash_table_;
double epsilon_;
std::pair<state_t*,Rational> *display_;
enum { SOLVED = 0x1 };
void solve( hashEntry_t *node ) { while( !(node->bits() & SOLVED) ) trial( node ); }
void trial( hashEntry_t *node );
bool checkSolved( hashEntry_t *node, std::deque<hashEntry_t*> &closed );
public:
lrtdp_t( const problem_t &problem, hash_t &hash_table, double epsilon );
virtual ~lrtdp_t();
virtual const action_t *next( const state_t &state )
{
double value;
hashEntry_t *node = hash_table_->get( state );
if( !(node->bits() & SOLVED) ) solve( node );
int action = hash_table_->bestAction( state, *problem_, value );
if( (action < 0) || (action >= (int)problem_->actionsT().size()) )
return( NULL );
else
return( problem_->actionsT()[action] );
}
virtual double value( const state_t &state )
{
hashEntry_t *node = hash_table_->get( state );
if( !(node->bits() & SOLVED) ) solve( node );
return( node->value() );
}
virtual void statistics( std::ostream &os ) const;
};
/*******************************************************************************
*
* asp
*
******************************************************************************/
class asp_t : public algorithm_t
{
hash_t *hash_table_;
unsigned simulations_;
void trial( hashEntry_t *node );
public:
asp_t( const problem_t &problem, hash_t &hash_table, unsigned simulations );
virtual ~asp_t();
virtual const action_t *next( const state_t &state )
{
double value;
hashEntry_t *node = hash_table_->get( state );
for( unsigned i = 0; i < simulations_; ++i )
trial( node );
unsigned action = hash_table_->bestAction( state, *problem_, value );
node->update( value );
return( problem_->actionsT()[action] );
}
virtual double value( const state_t &state )
{
hashEntry_t *node = hash_table_->get( state );
for( unsigned i = 0; i < simulations_; ++i )
trial( node );
return( node->value() );
}
virtual void statistics( std::ostream &os ) const;
};
/*******************************************************************************
*
* hdp(0,j)
*
******************************************************************************/
class hdpzj_t : public algorithm_t
{
hash_t *hash_table_;
double epsilon_;
unsigned J_;
double logbase_;
bool dfs( hashEntryX_t *node, int cost, unsigned &index, hashEntryX_t* &top,
std::deque<hashEntryX_t*> &visited );
void solve( hashEntryX_t* entry );
protected:
enum { MARK = 0x1, VISITED = 0x2, ACTIVE = 0x4 };
unsigned kappa( double f ) const { return( (unsigned)ceil( -log(f) / logbase_ ) ); }
unsigned transition_kappa( const state_t &state, const action_t &action,
const state_t &nstate ) const;
hashEntryX_t* get( const state_t &state )
{
return( (hashEntryX_t*)hash_table_->get( state ) );
}
public:
hdpzj_t( const problem_t &problem, hash_t &hash_table, double epsilon, unsigned J );
virtual ~hdpzj_t();
virtual const action_t *next( const state_t &state )
{
double value;
return( problem_->actionsT()[hash_table_->bestAction( state, *problem_, value )] );
}
virtual double value( const state_t &state )
{
return( 0 );
}
virtual void statistics( std::ostream &os ) const;
friend class plannerHDPZJ_t;
};
/*******************************************************************************
*
* IDA
*
******************************************************************************/
class IDA_t : public algorithm_t
{
hash_t *hash_table_;
bool useTT_;
enum { SOLVED = 0x1 };
double ida( const state_t &state, double cost, double threshold, bool &found );
double ida( hashEntry_t *node, double cost, double threshold, bool &found );
double solve( const state_t &state );
void solve( hashEntry_t *node );
public:
IDA_t( const problem_t &problem, hash_t &hash_table, bool useTT );
virtual ~IDA_t();
virtual const action_t *next( const state_t &state )
{
return( 0 );
}
virtual double value( const state_t &state )
{
if( useTT_ )
{
hashEntry_t *node = hash_table_->get( state );
if( !(node->bits() & SOLVED) )
solve( node );
return( node->value() );
}
else
return( solve( state ) );
}
virtual void statistics( std::ostream &os ) const;
};
/*******************************************************************************
*
* bounded Dijkstra
*
******************************************************************************/
class boundedDijkstra_t : public algorithm_t
{
hash_t *hash_table_;
unsigned size_;
enum { OPEN = 0x100, CLOSED = 0x200 };
double ida( hashEntry_t *node, double cost, double threshold, bool &found );
void solve( hashEntry_t *node );
public:
boundedDijkstra_t( const problem_t &problem, hash_t &hash_table, unsigned size );
virtual ~boundedDijkstra_t();
virtual const action_t *next( const state_t &state )
{
return( 0 );
}
virtual double value( const state_t &state )
{
hashEntry_t *node = hash_table_->get( state );
if( !(node->bits() & CLOSED) )
solve( node );
return( node->value() );
}
virtual void statistics( std::ostream &os ) const;
};
#endif // ALGORITHMS_H