1 #ifndef SEARCH_COMMON_HPP
2 #define SEARCH_COMMON_HPP
3 
4 #include "problem.hpp"
5 #include "rbase/build_rbase.hpp"
6 #include "queue/trace_following_queue.hpp"
7 #include "solution_store.hpp"
8 #include "search/search_options.hpp"
9 #include "library/stats.hpp"
10 #include "library/perm.hpp"
11 
12 // Checks a solution satisfies all the constraints, and
13 // adds to the solutionStore if it is. Returns true if
14 // the solution is real
handlePossibleSolution(Problem * p,SolutionStore * ss,RBase * rbase)15 bool handlePossibleSolution(Problem* p, SolutionStore* ss, RBase* rbase)
16 {
17     D_ASSERT(p->p_stack.cellCount() == p->p_stack.domainSize());
18     Permutation perm = getRawPermutation(p->p_stack.domainSize());
19 
20     for(int i : range1(perm.size()) )
21     {
22         perm.raw(rbase->initial_permstack->val(i)) = p->p_stack.val(i);
23     }
24     D_ASSERT(perm.validate());
25     if(!p->con_store.verifySolution(perm))
26         return false;
27     info_out(1, "Solution: " << perm);
28     ss->addSolution(perm);
29     return true;
30 }
31 
32 // Orders a cell of a partition stack according to a given heuristic
33 
34 template<typename It>
orderCell(It begin,It end,SearchHeuristic sh,RBase * rbase)35 void orderCell(It begin, It end, SearchHeuristic sh, RBase* rbase)
36 {
37     switch(sh)
38     {
39         case SearchBranch_RBase:
40              std::sort(begin, end,
41                 IndirectSorter([&](auto i) -> auto& { return (rbase->inv_value_ordering)[i]; }));
42             return;
43         case SearchBranch_InvRBase:
44              std::sort(begin, end,
45                 ReverseSorter(IndirectSorter([&](auto i) -> auto& { return (rbase->inv_value_ordering)[i]; })));
46             return;
47         case SearchBranch_Random:
48             std::random_shuffle(begin, end);
49             return;
50         case SearchBranch_Sorted:
51             std::sort(begin, end);
52             return;
53         case SearchBranch_Nosort:
54             return;
55         default:
56             abort();
57     }
58 }
59 
60 #endif
61