SAT Solver Template
Loading...
Searching...
No Matches
heuristics.hpp
Go to the documentation of this file.
1/**
2* @author Tim Luchterhand
3* @date 29.11.24
4* @file heuristics.hpp
5* @brief Contains different branching heuristics
6*/
7
8#ifndef HEURISTICS_HPP
9#define HEURISTICS_HPP
10
11#include <vector>
12#include <memory>
13
15#include "util/concepts.hpp"
16
17namespace sat {
18 /**
19 * Concept modelling the heuristic interface. A heuristic is a type that can be called with a vector of truth
20 * values (the model) and an unsigned integer representing the number of variables that are undecided and returns
21 * a sat::Variable
22 */
23 template<typename H>
24 concept heuristic = concepts::callable_r<H, Variable, const std::vector<TruthValue>, std::size_t>;
25
26 /**
27 * @brief Variable selection strategy that selects the first unassigned variable
28 */
30 Variable operator()(const std::vector<TruthValue> &model, std::size_t) const;
31 };
32
33 namespace detail {
34 /**
35 * @brief This is a helper class for the implementation of a type erasure heuristic wrapper
36 */
38 HeuristicCallableBase() = default;
39
40 virtual ~HeuristicCallableBase() = default;
41
42 HeuristicCallableBase(HeuristicCallableBase &&) = default;
43
44 HeuristicCallableBase &operator=(HeuristicCallableBase &&) = default;
45
46 HeuristicCallableBase(const HeuristicCallableBase &) = default;
47
48 HeuristicCallableBase &operator=(const HeuristicCallableBase &) = default;
49
50 virtual Variable invoke(const std::vector<TruthValue> &, std::size_t) = 0;
51 };
52
53 /**
54 * @brief This is a helper class for the implementation of a type erasure heuristic wrapper
55 */
56 template<heuristic H>
58 H impl;
59
60 template<typename... Args>
61 explicit HeuristicCallable(Args &&... args): impl(std::forward<Args>(args)...) {
62 }
63
64 Variable invoke(const std::vector<TruthValue> &values, std::size_t numOpenVariables) override {
65 return impl(values, numOpenVariables);
66 }
67 };
68 }
69
70 /**
71 * @brief Type erasure heuristic wrapper that can hold any type of heuristic
72 */
73 class Heuristic {
74 std::unique_ptr<detail::HeuristicCallableBase> impl;
75 public:
76 /**
77 * Default Ctor. Constructs an empty heuristic that must not be called
78 */
79 Heuristic() = default;
80
81 /**
82 * Ctor.
83 * @tparam H Heuristic type
84 * @param heuristic The heuristic to store in the wrapper
85 */
86 template<heuristic H>
87 Heuristic(H &&heuristic): impl(
89 }
90
91 Variable operator()(const std::vector<TruthValue> &values, std::size_t numOpenVariables) const;
92
93 /**
94 * Whether the wrapper holds a valid heuristic
95 * @return true if heuristic wrapper is valid, false otherwise
96 */
97 bool isValid() const;
98 };
99
100 /**
101 * @brief Wrapper for heuristics that do not support move construction or assignment
102 * @tparam H heuristic type
103 */
104 template<heuristic H>
106 std::unique_ptr<H> h;
107 public:
108 /**
109 * Ctor
110 * @tparam Args argument types
111 * @param args ctor arguments to heuristic
112 */
113 template<typename... Args>
114 explicit MovableHeuristic(Args &&...args): h(std::make_unique<H>(std::forward<Args>(args)...)) {}
115
116 Variable operator()(const std::vector<TruthValue> &values, std::size_t numOpenVariables) const {
117 return h->operator()(values, numOpenVariables);
118 }
119 };
120}
121
122#endif //HEURISTICS_HPP
Type erasure heuristic wrapper that can hold any type of heuristic.
Definition heuristics.hpp:73
Heuristic()=default
bool isValid() const
Definition heuristics.cpp:32
Heuristic(H &&heuristic)
Definition heuristics.hpp:87
Wrapper for heuristics that do not support move construction or assignment.
Definition heuristics.hpp:105
MovableHeuristic(Args &&...args)
Definition heuristics.hpp:114
Structure representing a binary variable in a CNF-SAT problem.
Definition basic_structures.hpp:29
Definition heuristics.hpp:33
Definition basic_structures.cpp:10
Variable selection strategy that selects the first unassigned variable.
Definition heuristics.hpp:29
This is a helper class for the implementation of a type erasure heuristic wrapper.
Definition heuristics.hpp:37
This is a helper class for the implementation of a type erasure heuristic wrapper.
Definition heuristics.hpp:57