|
4 | 4 | #include "linspire.hpp" |
5 | 5 | #include "arc_consistency.hpp" |
6 | 6 |
|
| 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 | + |
7 | 15 | namespace ratio |
8 | 16 | { |
| 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 | + |
9 | 54 | class solver_core : public riddle::core |
10 | 55 | { |
11 | 56 | public: |
@@ -43,15 +88,86 @@ namespace ratio |
43 | 88 | [[nodiscard]] riddle::arith_expr new_product(std::vector<riddle::arith_expr> &&xprs) override; |
44 | 89 | [[nodiscard]] riddle::arith_expr new_division(std::vector<riddle::arith_expr> &&xprs) override; |
45 | 90 |
|
| 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 | + |
46 | 129 | virtual void solve() = 0; |
47 | 130 |
|
48 | 131 | [[nodiscard]] bool match(riddle::term &lhs, riddle::term &rhs) const; |
49 | 132 |
|
| 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 | + |
50 | 140 | private: |
51 | 141 | [[nodiscard]] riddle::atom_state get_atom_state(const riddle::atom_term &atom) const noexcept override; |
52 | 142 |
|
| 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 | + |
53 | 164 | protected: |
54 | 165 | arc_consistency::solver ac_slv; // The arc consistency solver.. |
55 | 166 | 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.. |
56 | 172 | }; |
57 | 173 | } // namespace ratio |
0 commit comments