Skip to content

Commit 39cfbce

Browse files
committed
Introduce randomized DFS
Randomized DFS shuffles the queue of successor states
1 parent ad95cd2 commit 39cfbce

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/symex/path_search.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ Author: Daniel Kroening, [email protected]
2020
#include <path-symex/path_symex.h>
2121
#include <path-symex/build_goto_trace.h>
2222

23+
#include <random>
24+
25+
void path_searcht::shuffle_queue(queuet &queue)
26+
{
27+
if(queue.size()<=1)
28+
return;
29+
30+
INVARIANT(queue.size()<std::numeric_limits<int>::max(),
31+
"Queue size must be less than maximum integer");
32+
// pick random state and move to front
33+
int rand_i = rand() % queue.size();
34+
35+
std::list<statet>::iterator it = queue.begin();
36+
for(int i=0; i<rand_i; i++)
37+
it++;
38+
39+
statet tmp = *it;
40+
queue.push_front(tmp);
41+
queue.erase(it);
42+
}
43+
2344
path_searcht::resultt path_searcht::operator()(
2445
const goto_functionst &goto_functions)
2546
{
@@ -142,6 +163,9 @@ path_searcht::resultt path_searcht::operator()(
142163
// execute
143164
path_symex(state, tmp_queue);
144165

166+
if(search_heuristic == search_heuristict::RAN_DFS)
167+
shuffle_queue(tmp_queue);
168+
145169
// put at head of main queue
146170
queue.splice(queue.begin(), tmp_queue);
147171
}
@@ -208,6 +232,7 @@ void path_searcht::pick_state()
208232
switch(search_heuristic)
209233
{
210234
case search_heuristict::DFS:
235+
case search_heuristict::RAN_DFS:
211236
// Picking the first one (most recently added) is a DFS.
212237
return;
213238

src/symex/path_search.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class path_searcht:public safety_checkert
107107
};
108108

109109
void set_dfs() { search_heuristic=search_heuristict::DFS; }
110+
void set_randomized_dfs() { search_heuristic=search_heuristict::RAN_DFS; }
110111
void set_bfs() { search_heuristic=search_heuristict::BFS; }
111112
void set_locs() { search_heuristic=search_heuristict::LOCS; }
112113

@@ -120,7 +121,9 @@ class path_searcht:public safety_checkert
120121
// The states most recently executed are at the head.
121122
typedef std::list<statet> queuet;
122123
queuet queue;
123-
124+
/// Pick random element of queue and move to front
125+
/// \param queue queue to be shuffled
126+
void shuffle_queue(queuet &queue);
124127
// search heuristic
125128
void pick_state();
126129

@@ -151,7 +154,8 @@ class path_searcht:public safety_checkert
151154
unsigned unwind_limit;
152155
unsigned time_limit;
153156

154-
enum class search_heuristict { DFS, BFS, LOCS } search_heuristic;
157+
enum class search_heuristict
158+
{ DFS, RAN_DFS, BFS, LOCS } search_heuristic;
155159

156160
source_locationt last_source_location;
157161
};

src/symex/symex_parse_options.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ int symex_parse_optionst::doit()
210210
safe_string2unsigned(cmdline.get_value("max-search-time")));
211211

212212
if(cmdline.isset("dfs"))
213-
path_search.set_dfs();
213+
{
214+
if(cmdline.isset("randomize"))
215+
path_search.set_randomized_dfs();
216+
else
217+
path_search.set_dfs();
218+
}
214219

215220
if(cmdline.isset("bfs"))
216221
path_search.set_bfs();
@@ -627,6 +632,7 @@ void symex_parse_optionst::help()
627632
" --max-search-time s limit search to approximately s seconds\n"
628633
" --dfs use depth first search\n"
629634
" --bfs use breadth first search\n"
635+
" --randomize used in conjunction with dfs, to search by randomized dfs\n" // NOLINT(*)
630636
" --eager-infeasibility query solver early to determine whether a path is infeasible before searching it\n" // NOLINT(*)
631637
"\n"
632638
"Other options:\n"

src/symex/symex_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class optionst;
3939
"(error-label):(verbosity):(no-library)" \
4040
"(version)" \
4141
"(bfs)(dfs)(locs)" \
42+
"(randomize)" \
4243
"(cover):" \
4344
"(i386-linux)(i386-macos)(i386-win32)(win32)(winx64)(gcc)" \
4445
"(ppc-macos)(unsigned-char)" \

0 commit comments

Comments
 (0)