Skip to content

Commit 80a5288

Browse files
Refactor local search solver and flaws: enhance flaw handling, introduce new resolver classes, and update solver methods for improved functionality
1 parent 8f469e5 commit 80a5288

11 files changed

Lines changed: 460 additions & 35 deletions

File tree

extern/linspire

extern/riddle

include/local_search/local_search_flaws.hpp

Lines changed: 154 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ namespace ratio
88
class ls_flaw : public riddle::flaw
99
{
1010
public:
11-
ls_flaw(solver &cr, std::vector<std::shared_ptr<riddle::resolver>> &&causes, const utils::lit &phi);
11+
ls_flaw(solver &cr, std::vector<std::shared_ptr<riddle::resolver>> &&causes);
1212

1313
const utils::lit &get_phi() const noexcept { return phi; }
1414

15+
utils::rational get_estimated_cost() const noexcept override;
16+
17+
private:
18+
utils::lit get_phi(const std::vector<std::shared_ptr<riddle::resolver>> &causes) const noexcept;
19+
1520
private:
1621
const utils::lit phi;
22+
utils::rational est_cost = utils::rational::positive_infinite; // the estimated cost of this flaw..
1723
};
1824

1925
class ls_resolver : public resolver
2026
{
2127
public:
28+
ls_resolver(ls_flaw &flw, utils::rational &&intrinsic_cost);
2229
ls_resolver(ls_flaw &flw, utils::rational &&intrinsic_cost, const utils::lit &rho);
2330

2431
const utils::lit &get_rho() const noexcept { return rho; }
@@ -30,9 +37,7 @@ namespace ratio
3037
class enum_flaw final : public ls_flaw
3138
{
3239
public:
33-
enum_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, const utils::lit &phi, riddle::component_type &tp, std::vector<riddle::expr> &&values, utils::var ev) noexcept;
34-
35-
utils::rational get_estimated_cost() const noexcept override;
40+
enum_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, riddle::component_type &tp, std::vector<riddle::expr> &&values, utils::var ev) noexcept;
3641

3742
[[nodiscard]] const riddle::enum_expr &get_var() const noexcept { return var; }
3843

@@ -57,9 +62,7 @@ namespace ratio
5762
class clause_flaw final : public ls_flaw
5863
{
5964
public:
60-
clause_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, const utils::lit &phi, std::vector<riddle::bool_expr> &&clause) noexcept;
61-
62-
utils::rational get_estimated_cost() const noexcept override;
65+
clause_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, std::vector<riddle::bool_expr> &&clause) noexcept;
6366

6467
[[nodiscard]] const std::vector<riddle::bool_expr> &get_clause() const noexcept { return clause; }
6568

@@ -72,12 +75,24 @@ namespace ratio
7275
std::vector<riddle::bool_expr> clause;
7376
};
7477

75-
class disjunction_flaw final : public ls_flaw
78+
class choose_lit final : public ls_resolver
7679
{
7780
public:
78-
disjunction_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, const utils::lit &phi, std::vector<std::unique_ptr<riddle::conjunction>> &&disjuncts) noexcept;
81+
choose_lit(clause_flaw &f, riddle::bool_expr lit) noexcept;
7982

80-
utils::rational get_estimated_cost() const noexcept override;
83+
private:
84+
bool apply() noexcept override;
85+
86+
[[nodiscard]] json::json to_json() const override;
87+
88+
private:
89+
riddle::bool_expr lit; // the literal to choose..
90+
};
91+
92+
class disjunction_flaw final : public ls_flaw
93+
{
94+
public:
95+
disjunction_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, std::vector<std::unique_ptr<riddle::conjunction>> &&disjuncts) noexcept;
8196

8297
[[nodiscard]] const std::vector<std::unique_ptr<riddle::conjunction>> &get_disjuncts() const noexcept { return disjuncts; }
8398

@@ -89,4 +104,133 @@ namespace ratio
89104
private:
90105
std::vector<std::unique_ptr<riddle::conjunction>> disjuncts;
91106
};
107+
108+
class choose_conjunction final : public ls_resolver
109+
{
110+
public:
111+
choose_conjunction(disjunction_flaw &f, riddle::conjunction &conj) noexcept;
112+
113+
private:
114+
bool apply() noexcept override;
115+
116+
[[nodiscard]] json::json to_json() const override;
117+
118+
private:
119+
riddle::conjunction &conj;
120+
};
121+
122+
class atom_flaw final : public ls_flaw
123+
{
124+
public:
125+
atom_flaw(solver &slv, std::vector<std::shared_ptr<riddle::resolver>> &&causes, bool is_fact, riddle::predicate &pred, std::map<std::string, riddle::expr, std::less<>> &&args, riddle::bool_expr &&sigma) noexcept;
126+
127+
[[nodiscard]] const riddle::atom_expr &get_atom() const noexcept { return atm; }
128+
129+
private:
130+
void compute_resolvers() override;
131+
132+
[[nodiscard]] json::json to_json() const override;
133+
134+
private:
135+
riddle::atom_expr atm;
136+
};
137+
138+
class activate_fact final : public ls_resolver
139+
{
140+
public:
141+
activate_fact(atom_flaw &f) noexcept;
142+
143+
private:
144+
bool apply() noexcept override;
145+
146+
json::json to_json() const override;
147+
};
148+
149+
class activate_goal final : public ls_resolver
150+
{
151+
public:
152+
activate_goal(atom_flaw &f) noexcept;
153+
154+
private:
155+
bool apply() noexcept override;
156+
157+
json::json to_json() const override;
158+
};
159+
160+
class unify_atom final : public ls_resolver
161+
{
162+
public:
163+
unify_atom(atom_flaw &f, riddle::atom_expr atm) noexcept;
164+
165+
private:
166+
bool apply() noexcept override;
167+
168+
json::json to_json() const override;
169+
170+
private:
171+
riddle::atom_expr atm; // the atom to unify with..
172+
};
173+
174+
class sv_peak final : public ls_flaw
175+
{
176+
public:
177+
sv_peak(solver &slv, std::vector<riddle::atom_expr> &&atms) noexcept;
178+
179+
private:
180+
void compute_resolvers() noexcept override;
181+
182+
private:
183+
std::vector<riddle::atom_expr> atms;
184+
};
185+
186+
class rr_peak final : public ls_flaw
187+
{
188+
public:
189+
rr_peak(solver &slv, std::vector<riddle::atom_expr> &&atms) noexcept;
190+
191+
private:
192+
void compute_resolvers() noexcept override;
193+
194+
private:
195+
std::vector<riddle::atom_expr> atms;
196+
};
197+
198+
class cr_overproduction final : public ls_flaw
199+
{
200+
public:
201+
cr_overproduction(solver &slv, std::vector<riddle::atom_expr> &&prod_atms, std::vector<riddle::atom_expr> &&cons_atms) noexcept;
202+
203+
private:
204+
void compute_resolvers() noexcept override;
205+
206+
private:
207+
std::vector<riddle::atom_expr> prod_atms;
208+
std::vector<riddle::atom_expr> cons_atms;
209+
};
210+
211+
class cr_overconsumption final : public ls_flaw
212+
{
213+
public:
214+
cr_overconsumption(solver &slv, std::vector<riddle::atom_expr> &&cons_atms, std::vector<riddle::atom_expr> &&prod_atms) noexcept;
215+
216+
private:
217+
void compute_resolvers() noexcept override;
218+
219+
private:
220+
std::vector<riddle::atom_expr> cons_atms;
221+
std::vector<riddle::atom_expr> prod_atms;
222+
};
223+
224+
class ordering final : public ls_resolver
225+
{
226+
public:
227+
ordering(ls_flaw &flw, riddle::atom_expr before, riddle::atom_expr after) noexcept;
228+
229+
private:
230+
bool apply() noexcept override;
231+
232+
private:
233+
riddle::atom_expr before;
234+
riddle::atom_expr after;
235+
};
92236
} // namespace ratio

include/local_search/local_search_solver.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
namespace ratio
77
{
88
class ls_flaw;
9+
class ls_resolver;
910

1011
class local_search_solver : public solver
1112
{
1213
friend class ls_flaw;
14+
friend class ls_resolver;
1315

1416
public:
1517
local_search_solver() noexcept;
1618

1719
[[nodiscard]] riddle::expr new_enum(riddle::component_type &tp, std::vector<riddle::expr> &&values) override;
1820

19-
void new_disjunction(std::vector<std::unique_ptr<riddle::conjunction>> &&) override;
20-
void new_clause(std::vector<riddle::bool_expr> &&) override;
21+
void new_disjunction(std::vector<std::unique_ptr<riddle::conjunction>> &&disjuncts) override;
22+
void new_clause(std::vector<riddle::bool_expr> &&exprs) override;
2123

2224
void solve() override;
2325

@@ -26,6 +28,9 @@ namespace ratio
2628
private:
2729
riddle::atom_expr create_atom(bool is_fact, riddle::predicate &pred, std::map<std::string, std::shared_ptr<riddle::term>, std::less<>> &&args) override;
2830

31+
void new_clause(std::vector<utils::lit> &&lits);
32+
utils::lbool sat_val(const utils::lit &l) const noexcept;
33+
2934
#ifdef ORATIO_ENABLE_LISTENERS
3035
private:
3136
/**

src/basic/basic_flaws.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ namespace ratio
154154
return j;
155155
}
156156

157-
unify_atom::unify_atom(atom_flaw &flw, riddle::atom_expr atm) noexcept : riddle::resolver(flw, utils::rational(1)), resolver(flw, utils::rational(2)), atm(std::move(atm)) {}
157+
unify_atom::unify_atom(atom_flaw &flw, riddle::atom_expr atm) noexcept : riddle::resolver(flw, utils::rational(0)), resolver(flw, utils::rational(0)), atm(std::move(atm)) {}
158158
bool unify_atom::apply() noexcept
159159
{
160160
if (!ratio::resolver::get_flaw().get_core().assert_expr(static_cast<riddle::atom &>(*atm).get_sigma()))

src/basic/basic_solver.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ namespace ratio
173173
}
174174
}
175175

176+
void basic_solver::new_disjunction(std::vector<std::unique_ptr<riddle::conjunction>> &&disjuncts)
177+
{
178+
assert(disjuncts.size() > 1);
179+
std::vector<std::shared_ptr<riddle::resolver>> causes;
180+
if (!static_cast<node &>(get_current_node()).resolvers.empty())
181+
causes.push_back(static_cast<node &>(get_current_node()).resolvers.back());
182+
auto df = new_flaw<disjunction_flaw>(*this, std::move(causes), std::move(disjuncts));
183+
FLAW_CREATED(*df);
184+
static_cast<node &>(get_current_node()).open_flaws.insert(df);
185+
}
176186
void basic_solver::new_clause(std::vector<riddle::bool_expr> &&exprs)
177187
{
178188
assert(!exprs.empty());
@@ -191,16 +201,6 @@ namespace ratio
191201
static_cast<node &>(get_current_node()).open_flaws.insert(cf);
192202
}
193203
}
194-
void basic_solver::new_disjunction(std::vector<std::unique_ptr<riddle::conjunction>> &&disjuncts)
195-
{
196-
assert(disjuncts.size() > 1);
197-
std::vector<std::shared_ptr<riddle::resolver>> causes;
198-
if (!static_cast<node &>(get_current_node()).resolvers.empty())
199-
causes.push_back(static_cast<node &>(get_current_node()).resolvers.back());
200-
auto df = new_flaw<disjunction_flaw>(*this, std::move(causes), std::move(disjuncts));
201-
FLAW_CREATED(*df);
202-
static_cast<node &>(get_current_node()).open_flaws.insert(df);
203-
}
204204

205205
void basic_solver::solve()
206206
{

0 commit comments

Comments
 (0)