Skip to content

Randomized dfs #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/symex/path_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ Author: Daniel Kroening, [email protected]
#include <path-symex/path_symex.h>
#include <path-symex/build_goto_trace.h>

#include <random>

void path_searcht::shuffle_queue(queuet &queue)
{
if(queue.size()<=1)
return;

INVARIANT(queue.size()<std::numeric_limits<int>::max(),
"Queue size must be less than maximum integer");
// pick random state and move to front
int rand_i = rand() % queue.size();

std::list<statet>::iterator it = queue.begin();
for(int i=0; i<rand_i; i++)
it++;

statet tmp = *it;
queue.push_front(tmp);
queue.erase(it);
}

path_searcht::resultt path_searcht::operator()(
const goto_functionst &goto_functions)
{
Expand Down Expand Up @@ -142,6 +163,9 @@ path_searcht::resultt path_searcht::operator()(
// execute
path_symex(state, tmp_queue);

if(search_heuristic == search_heuristict::RAN_DFS)
shuffle_queue(tmp_queue);

// put at head of main queue
queue.splice(queue.begin(), tmp_queue);
}
Expand Down Expand Up @@ -208,6 +232,7 @@ void path_searcht::pick_state()
switch(search_heuristic)
{
case search_heuristict::DFS:
case search_heuristict::RAN_DFS:
// Picking the first one (most recently added) is a DFS.
return;

Expand Down
8 changes: 6 additions & 2 deletions src/symex/path_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class path_searcht:public safety_checkert
};

void set_dfs() { search_heuristic=search_heuristict::DFS; }
void set_randomized_dfs() { search_heuristic=search_heuristict::RAN_DFS; }
void set_bfs() { search_heuristic=search_heuristict::BFS; }
void set_locs() { search_heuristic=search_heuristict::LOCS; }

Expand All @@ -120,7 +121,9 @@ class path_searcht:public safety_checkert
// The states most recently executed are at the head.
typedef std::list<statet> queuet;
queuet queue;

/// Pick random element of queue and move to front
/// \param queue queue to be shuffled
void shuffle_queue(queuet &queue);
// search heuristic
void pick_state();

Expand Down Expand Up @@ -151,7 +154,8 @@ class path_searcht:public safety_checkert
unsigned unwind_limit;
unsigned time_limit;

enum class search_heuristict { DFS, BFS, LOCS } search_heuristic;
enum class search_heuristict
{ DFS, RAN_DFS, BFS, LOCS } search_heuristic;

source_locationt last_source_location;
};
Expand Down
11 changes: 10 additions & 1 deletion src/symex/symex_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ int symex_parse_optionst::doit()
safe_string2unsigned(cmdline.get_value("max-search-time")));

if(cmdline.isset("dfs"))
path_search.set_dfs();
{
if(cmdline.isset("randomize"))
path_search.set_randomized_dfs();
else
path_search.set_dfs();
}

if(cmdline.isset("bfs"))
path_search.set_bfs();
Expand Down Expand Up @@ -636,6 +641,10 @@ void symex_parse_optionst::help()
" --context-bound nr limit number of context switches\n"
" --branch-bound nr limit number of branches taken\n"
" --max-search-time s limit search to approximately s seconds\n"
" --dfs use depth first search\n"
" --bfs use breadth first search\n"
" --randomize used in conjunction with dfs, to search by randomized dfs\n" // NOLINT(*)
" --eager-infeasibility query solver early to determine whether a path is infeasible before searching it\n" // NOLINT(*)
"\n"
"Other options:\n"
" --version show version and exit\n"
Expand Down
1 change: 1 addition & 0 deletions src/symex/symex_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class optionst;
"(error-label):(verbosity):(no-library)" \
"(version)" \
"(bfs)(dfs)(locs)" \
"(randomize)" \
"(cover):" \
"(i386-linux)(i386-macos)(i386-win32)(win32)(winx64)(gcc)" \
"(ppc-macos)(unsigned-char)" \
Expand Down