Skip to content

Commit d5c5a0a

Browse files
Refactor flaw and resolver classes: Move constructors to respective implementations and streamline flaw creation in basic_solver
1 parent 270e7d0 commit d5c5a0a

4 files changed

Lines changed: 289 additions & 317 deletions

File tree

include/basic_solver.hpp

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,8 @@
22

33
#include "solver_core.hpp"
44

5-
#ifdef ORATIO_ENABLE_LISTENERS
6-
#define NEW_FLAW(f) flaw_created(f)
7-
#define NEW_RESOLVER(r) resolver_created(r)
8-
#else
9-
#define NEW_FLAW(f)
10-
#define NEW_RESOLVER(r)
11-
#endif
12-
135
namespace ratio
146
{
15-
class basic_solver;
16-
class resolver;
17-
18-
class flaw
19-
{
20-
public:
21-
flaw(basic_solver &slv, std::vector<std::reference_wrapper<resolver>> &&causes) noexcept;
22-
flaw(const flaw &) = delete;
23-
virtual ~flaw() = default;
24-
25-
private:
26-
virtual void compute_resolvers() = 0;
27-
28-
protected:
29-
basic_solver &slv; // The solver managing this flaw..
30-
std::vector<std::reference_wrapper<resolver>> causes; // The causes of this flaw..
31-
};
32-
33-
class resolver
34-
{
35-
friend class basic_solver;
36-
37-
public:
38-
resolver(flaw &flw, utils::rational &&intrinsic_cost) noexcept;
39-
resolver(const resolver &) = delete;
40-
virtual ~resolver() = default;
41-
42-
private:
43-
virtual void apply() = 0;
44-
45-
protected:
46-
flaw &flw; // The flaw this resolver addresses..
47-
utils::rational intrinsic_cost; // The intrinsic cost of applying this resolver..
48-
linspire::constraint cnst; // The constraint associated with this resolver..
49-
std::vector<std::reference_wrapper<arc_consistency::constraint>> ac_cnsts; // The arc consistency constraints associated with this resolver..
50-
};
51-
527
class basic_solver : public solver_core
538
{
549
public:
@@ -59,77 +14,10 @@ namespace ratio
5914
void new_clause(std::vector<riddle::bool_expr> &&exprs) override;
6015
void new_disjunction(std::vector<std::unique_ptr<riddle::conjunction>> &&disjuncts) override;
6116

62-
/**
63-
* @brief Creates a new flaw of the given type.
64-
*
65-
* @tparam Tp The type of the flaw to create.
66-
* @tparam Args The types of the arguments to pass to the flaw
67-
* @param args The arguments to pass to the flaw
68-
* @return Tp& The created flaw
69-
*/
70-
template <typename Tp, typename... Args>
71-
Tp &new_flaw(Args &&...args) noexcept
72-
{
73-
static_assert(std::is_base_of_v<flaw, Tp>, "Tp must be a subclass of flaw");
74-
auto f = std::make_unique<Tp>(std::forward<Args>(args)...);
75-
auto &f_ref = *f;
76-
NEW_FLAW(f_ref);
77-
flaws.emplace_back(std::move(f));
78-
return f_ref;
79-
}
80-
81-
/**
82-
* @brief Creates a new resolver of the given type.
83-
*
84-
* @tparam Tp The type of the resolver to create.
85-
* @tparam Args The types of the arguments to pass to the resolver
86-
* @param args The arguments to pass to the resolver
87-
* @return Tp& The created resolver
88-
*/
89-
template <typename Tp, typename... Args>
90-
Tp &new_resolver(Args &&...args) noexcept
91-
{
92-
static_assert(std::is_base_of_v<resolver, Tp>, "Tp must be a subclass of resolver");
93-
auto r = std::make_unique<Tp>(std::forward<Args>(args)...);
94-
auto &r_ref = *r;
95-
NEW_RESOLVER(r_ref);
96-
resolvers.emplace_back(std::move(r));
97-
return r_ref;
98-
}
99-
10017
void solve() override;
10118

10219
private:
10320
[[nodiscard]] riddle::atom_expr create_atom(bool is_fact, riddle::predicate &pred, std::map<std::string, riddle::expr, std::less<>> &&args) override;
104-
105-
[[nodiscard]] bool execute(const riddle::bool_expr &expr) noexcept;
106-
107-
#ifdef ORATIO_ENABLE_LISTENERS
108-
private:
109-
/**
110-
* @brief Notifies that a new flaw has been created.
111-
*
112-
* This function is called whenever a new flaw is created in the solver.
113-
*
114-
* @param f The newly created flaw.
115-
*/
116-
virtual void flaw_created([[maybe_unused]] flaw &f) noexcept {}
117-
118-
/**
119-
* @brief Notifies that a new resolver has been created.
120-
*
121-
* This function is called whenever a new resolver is created in the solver.
122-
*
123-
* @param r The newly created resolver.
124-
*/
125-
virtual void resolver_created([[maybe_unused]] resolver &r) noexcept {}
126-
#endif
127-
128-
private:
129-
std::vector<std::unique_ptr<flaw>> flaws; // The set of flaws
130-
std::vector<std::unique_ptr<resolver>> resolvers; // The set of resolvers
131-
std::optional<std::reference_wrapper<flaw>> c_flaw; // The current flaw..
132-
std::optional<std::reference_wrapper<resolver>> c_res; // The current resolver..
13321
};
13422

13523
class enum_flaw final : public flaw

include/solver_core.hpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,53 @@
44
#include "linspire.hpp"
55
#include "arc_consistency.hpp"
66

7+
#ifdef ORATIO_ENABLE_LISTENERS
8+
#define NEW_FLAW(f) flaw_created(f)
9+
#define NEW_RESOLVER(r) resolver_created(r)
10+
#else
11+
#define NEW_FLAW(f)
12+
#define NEW_RESOLVER(r)
13+
#endif
14+
715
namespace ratio
816
{
17+
class solver_core;
18+
class resolver;
19+
20+
class flaw
21+
{
22+
public:
23+
flaw(solver_core &slv, std::vector<std::reference_wrapper<resolver>> &&causes) noexcept;
24+
flaw(const flaw &) = delete;
25+
virtual ~flaw() = default;
26+
27+
private:
28+
virtual void compute_resolvers() = 0;
29+
30+
protected:
31+
solver_core &slv; // The solver managing this flaw..
32+
std::vector<std::reference_wrapper<resolver>> causes; // The causes of this flaw..
33+
};
34+
35+
class resolver
36+
{
37+
friend class solver_core;
38+
39+
public:
40+
resolver(flaw &flw, utils::rational &&intrinsic_cost) noexcept;
41+
resolver(const resolver &) = delete;
42+
virtual ~resolver() = default;
43+
44+
private:
45+
virtual void apply() = 0;
46+
47+
protected:
48+
flaw &flw; // The flaw this resolver addresses..
49+
utils::rational intrinsic_cost; // The intrinsic cost of applying this resolver..
50+
linspire::constraint cnst; // The constraint associated with this resolver..
51+
std::vector<std::reference_wrapper<arc_consistency::constraint>> ac_cnsts; // The arc consistency constraints associated with this resolver..
52+
};
53+
954
class solver_core : public riddle::core
1055
{
1156
public:
@@ -43,15 +88,86 @@ namespace ratio
4388
[[nodiscard]] riddle::arith_expr new_product(std::vector<riddle::arith_expr> &&xprs) override;
4489
[[nodiscard]] riddle::arith_expr new_division(std::vector<riddle::arith_expr> &&xprs) override;
4590

91+
/**
92+
* @brief Creates a new flaw of the given type.
93+
*
94+
* @tparam Tp The type of the flaw to create.
95+
* @tparam Args The types of the arguments to pass to the flaw
96+
* @param args The arguments to pass to the flaw
97+
* @return Tp& The created flaw
98+
*/
99+
template <typename Tp, typename... Args>
100+
Tp &new_flaw(Args &&...args) noexcept
101+
{
102+
static_assert(std::is_base_of_v<flaw, Tp>, "Tp must be a subclass of flaw");
103+
auto f = std::make_unique<Tp>(std::forward<Args>(args)...);
104+
auto &f_ref = *f;
105+
NEW_FLAW(f_ref);
106+
flaws.emplace_back(std::move(f));
107+
return f_ref;
108+
}
109+
110+
/**
111+
* @brief Creates a new resolver of the given type.
112+
*
113+
* @tparam Tp The type of the resolver to create.
114+
* @tparam Args The types of the arguments to pass to the resolver
115+
* @param args The arguments to pass to the resolver
116+
* @return Tp& The created resolver
117+
*/
118+
template <typename Tp, typename... Args>
119+
Tp &new_resolver(Args &&...args) noexcept
120+
{
121+
static_assert(std::is_base_of_v<resolver, Tp>, "Tp must be a subclass of resolver");
122+
auto r = std::make_unique<Tp>(std::forward<Args>(args)...);
123+
auto &r_ref = *r;
124+
NEW_RESOLVER(r_ref);
125+
resolvers.emplace_back(std::move(r));
126+
return r_ref;
127+
}
128+
46129
virtual void solve() = 0;
47130

48131
[[nodiscard]] bool match(riddle::term &lhs, riddle::term &rhs) const;
49132

133+
protected:
134+
[[nodiscard]] bool execute(const riddle::bool_expr &expr) noexcept;
135+
136+
void add_constraint(arc_consistency::constraint &c) noexcept;
137+
138+
std::vector<std::reference_wrapper<resolver>> get_causes() const noexcept;
139+
50140
private:
51141
[[nodiscard]] riddle::atom_state get_atom_state(const riddle::atom_term &atom) const noexcept override;
52142

143+
#ifdef ORATIO_ENABLE_LISTENERS
144+
private:
145+
/**
146+
* @brief Notifies that a new flaw has been created.
147+
*
148+
* This function is called whenever a new flaw is created in the solver.
149+
*
150+
* @param f The newly created flaw.
151+
*/
152+
virtual void flaw_created([[maybe_unused]] flaw &f) noexcept {}
153+
154+
/**
155+
* @brief Notifies that a new resolver has been created.
156+
*
157+
* This function is called whenever a new resolver is created in the solver.
158+
*
159+
* @param r The newly created resolver.
160+
*/
161+
virtual void resolver_created([[maybe_unused]] resolver &r) noexcept {}
162+
#endif
163+
53164
protected:
54165
arc_consistency::solver ac_slv; // The arc consistency solver..
55166
linspire::solver lin_slv; // The linear programming solver..
167+
private:
168+
std::vector<std::unique_ptr<flaw>> flaws; // The set of flaws
169+
std::vector<std::unique_ptr<resolver>> resolvers; // The set of resolvers
170+
std::optional<std::reference_wrapper<flaw>> c_flaw; // The current flaw..
171+
std::optional<std::reference_wrapper<resolver>> c_res; // The current resolver..
56172
};
57173
} // namespace ratio

0 commit comments

Comments
 (0)