Skip to content

Commit

Permalink
Switch from std::shuffle to std::random_shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Feb 21, 2024
1 parent f67f34d commit 428a22b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 5 additions & 2 deletions YAPB++/source/search/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ template<bool firstbranch>
bool doSearchBranch(const SearchOptions& so, Problem* p, SolutionStore* ss,
RBase* rbase, TraceFollowingQueue* tfq, int depth)
{
std::minstd_rand rd;
static std::default_random_engine rng(rd());

info_out(1, "search depth: " << depth);
info_out(2, "Current partition: " << p->p_stack.dumpCurrentPartition());
if(depth > rbase->depth())
Expand Down Expand Up @@ -41,7 +44,7 @@ bool doSearchBranch(const SearchOptions& so, Problem* p, SolutionStore* ss,
orderCell(firstbranch ? cell.begin() + 1 : cell.begin(),
cell.end(),
firstbranch ? so.heuristic.search_first_branch_value : so.heuristic.search_value,
rbase);
rbase, rng);

for(int i : range1(cell.size()))
{
Expand Down Expand Up @@ -127,7 +130,7 @@ SolutionStore doSearch(Problem* p, const std::vector<AbstractConstraint*>& cons,
try {
doSearchBranch<true>(so, p, &solutions, rb, &tfq, 1);
}
catch(const EndOfSearch&) {
catch(const EndOfSearch&) {
debug_out(1, "search", "Node limit reached!");
}
}
Expand Down
6 changes: 4 additions & 2 deletions YAPB++/source/search/search_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "library/stats.hpp"
#include "library/perm.hpp"

#include <random>

// Checks a solution satisfies all the constraints, and
// adds to the solutionStore if it is. Returns true if
// the solution is real
Expand All @@ -32,7 +34,7 @@ bool handlePossibleSolution(Problem* p, SolutionStore* ss, RBase* rbase)
// Orders a cell of a partition stack according to a given heuristic

template<typename It>
void orderCell(It begin, It end, SearchHeuristic sh, RBase* rbase)
void orderCell(It begin, It end, SearchHeuristic sh, RBase* rbase, std::default_random_engine& rng)
{
switch(sh)
{
Expand All @@ -45,7 +47,7 @@ void orderCell(It begin, It end, SearchHeuristic sh, RBase* rbase)
ReverseSorter(IndirectSorter([&](auto i) -> auto& { return (rbase->inv_value_ordering)[i]; })));
return;
case SearchBranch_Random:
std::random_shuffle(begin, end);
std::shuffle(begin, end, rng);
return;
case SearchBranch_Sorted:
std::sort(begin, end);
Expand Down

0 comments on commit 428a22b

Please sign in to comment.