Skip to content

Commit

Permalink
re-enable pre-process
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolajBjorner committed Apr 13, 2022
1 parent c9fa00a commit 3f5eb7f
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/opt/maxlex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ namespace opt {
public:

maxlex(maxsat_context& c, unsigned id, vector<soft>& s):
maxsmt_solver_base(c, s),
maxsmt_solver_base(c, s, id),
m(c.get_manager()),
m_c(c) {
// ensure that soft constraints are sorted with largest soft constraints first.
Expand Down
4 changes: 1 addition & 3 deletions src/opt/maxres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class maxres : public maxsmt_solver_base {
expr_ref_vector const& soft() override { return i.m_asms; }
};

unsigned m_index;
stats m_stats;
expr_ref_vector m_B;
expr_ref_vector m_asms;
Expand Down Expand Up @@ -132,8 +131,7 @@ class maxres : public maxsmt_solver_base {
maxres(maxsat_context& c, unsigned index,
vector<soft>& soft,
strategy_t st):
maxsmt_solver_base(c, soft),
m_index(index),
maxsmt_solver_base(c, soft, index),
m_B(m), m_asms(m), m_defs(m),
m_new_core(m),
m_mus(c.get_solver()),
Expand Down
42 changes: 21 additions & 21 deletions src/opt/maxsmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Module Name:

namespace opt {

maxsmt_solver_base::maxsmt_solver_base(
maxsat_context& c, vector<soft>& s):
maxsmt_solver_base::maxsmt_solver_base(maxsat_context& c, vector<soft>& s, unsigned index):
m(c.get_manager()),
m_c(c),
m_index(index),
m_soft(s),
m_assertions(m),
m_trail(m) {
Expand Down Expand Up @@ -91,18 +91,17 @@ namespace opt {
m_upper += s.weight;
}

return true;
// return true;

preprocess pp(s());
rational lower(0);
bool r = pp(m_soft, lower);


if (lower != 0)
m_adjust_value->set_offset(lower + m_adjust_value->get_offset());
m_c.add_offset(m_index, lower);
m_upper -= lower;

TRACE("opt",
tout << "upper: " << m_upper << " assignments: ";
tout << "lower " << lower << " upper: " << m_upper << " assignments: ";
for (soft& s : m_soft) tout << (s.is_true()?"T":"F");
tout << "\n";);
return r;
Expand Down Expand Up @@ -169,8 +168,8 @@ namespace opt {

void maxsmt_solver_base::trace_bounds(char const * solver) {
IF_VERBOSE(1,
rational l = (*m_adjust_value)(m_lower);
rational u = (*m_adjust_value)(m_upper);
rational l = m_c.adjust(m_index, m_lower);
rational u = m_c.adjust(m_index, m_upper);
if (l > u) std::swap(l, u);
verbose_stream() << "(opt." << solver << " [" << l << ":" << u << "])\n";);
}
Expand All @@ -196,10 +195,10 @@ namespace opt {
m_msolver = mk_primal_dual_maxres(m_c, m_index, m_soft);
}
else if (maxsat_engine == symbol("wmax")) {
m_msolver = mk_wmax(m_c, m_soft);
m_msolver = mk_wmax(m_c, m_soft, m_index);
}
else if (maxsat_engine == symbol("sortmax")) {
m_msolver = mk_sortmax(m_c, m_soft);
m_msolver = mk_sortmax(m_c, m_soft, m_index);
}
else {
auto str = maxsat_engine.str();
Expand All @@ -209,7 +208,6 @@ namespace opt {

if (m_msolver) {
m_msolver->updt_params(m_params);
m_msolver->set_adjust_value(*m_adjust_value);
is_sat = l_undef;
try {
is_sat = (*m_msolver)();
Expand All @@ -233,13 +231,6 @@ namespace opt {
return is_sat;
}

void maxsmt::set_adjust_value(adjust_value& adj) {
m_adjust_value = &adj;
if (m_msolver) {
m_msolver->set_adjust_value(adj);
}
}

void maxsmt::reset_upper() {
if (m_msolver) {
m_msolver->reset_upper();
Expand Down Expand Up @@ -268,7 +259,7 @@ namespace opt {
rational q = m_msolver->get_lower();
if (q > r) r = q;
}
return (*m_adjust_value)(r);
return m_c.adjust(m_index, r);
}

rational maxsmt::get_upper() const {
Expand All @@ -277,7 +268,7 @@ namespace opt {
rational q = m_msolver->get_upper();
if (q < r) r = q;
}
return (*m_adjust_value)(r);
return m_c.adjust(m_index, r);
}

void maxsmt::update_lower(rational const& r) {
Expand Down Expand Up @@ -370,6 +361,7 @@ namespace opt {
model_ref m_model;
ref<generic_model_converter> m_fm;
symbol m_maxsat_engine;
vector<rational> m_offsets;
public:
solver_maxsat_context(params_ref& p, solver* s, model * m):
m_params(p),
Expand All @@ -394,6 +386,14 @@ namespace opt {
bool verify_model(unsigned id, model* mdl, rational const& v) override { return true; };
void set_model(model_ref& _m) override { m_model = _m; }
void model_updated(model* mdl) override { } // no-op
rational adjust(unsigned id, rational const& r) override {
m_offsets.reserve(id+1);
return r + m_offsets[id];
}
void add_offset(unsigned id, rational const& r) override {
m_offsets.reserve(id+1);
m_offsets[id] += r;
}
};

lbool maxsmt_wrapper::operator()(vector<std::pair<expr*,rational>>& soft) {
Expand Down
10 changes: 3 additions & 7 deletions src/opt/maxsmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ namespace opt {
class maxsat_context;

class maxsmt_solver {
protected:
adjust_value* m_adjust_value = nullptr;
public:
virtual ~maxsmt_solver() {}
virtual lbool operator()() = 0;
Expand All @@ -45,7 +43,6 @@ namespace opt {
virtual void collect_statistics(statistics& st) const = 0;
virtual void get_model(model_ref& mdl, svector<symbol>& labels) = 0;
virtual void updt_params(params_ref& p) = 0;
void set_adjust_value(adjust_value& adj) { m_adjust_value = &adj; }

};

Expand All @@ -67,7 +64,8 @@ namespace opt {
class maxsmt_solver_base : public maxsmt_solver {
protected:
ast_manager& m;
maxsat_context& m_c;
maxsat_context& m_c;
unsigned m_index;
vector<soft>& m_soft;
expr_ref_vector m_assertions;
expr_ref_vector m_trail;
Expand All @@ -78,7 +76,7 @@ namespace opt {
params_ref m_params; // config

public:
maxsmt_solver_base(maxsat_context& c, vector<soft>& soft);
maxsmt_solver_base(maxsat_context& c, vector<soft>& soft, unsigned index);

~maxsmt_solver_base() override {}
rational get_lower() const override { return m_lower; }
Expand Down Expand Up @@ -128,7 +126,6 @@ namespace opt {
expr_ref_vector m_answer;
rational m_lower;
rational m_upper;
adjust_value* m_adjust_value = nullptr;
model_ref m_model;
svector<symbol> m_labels;
params_ref m_params;
Expand All @@ -137,7 +134,6 @@ namespace opt {
lbool operator()();
void updt_params(params_ref& p);
void add(expr* f, rational const& w);
void set_adjust_value(adjust_value& adj);
unsigned size() const { return m_soft.size(); }
expr* operator[](unsigned idx) const { return m_soft[idx].s; }
rational weight(unsigned idx) const { return m_soft[idx].weight; }
Expand Down
40 changes: 22 additions & 18 deletions src/opt/opt_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ namespace opt {
}

void context::set_model(model_ref& m) {
m_model = m;
m_model = m;
opt_params optp(m_params);
if (optp.dump_models() && m) {
model_ref md = m->copy();
Expand Down Expand Up @@ -930,7 +930,8 @@ namespace opt {
bool context::is_maxsat(expr* fml, expr_ref_vector& terms,
vector<rational>& weights, rational& offset,
bool& neg, symbol& id, expr_ref& orig_term, unsigned& index) {
if (!is_app(fml)) return false;
if (!is_app(fml))
return false;
neg = false;
orig_term = nullptr;
index = 0;
Expand Down Expand Up @@ -1105,8 +1106,7 @@ namespace opt {
obj.m_weights.append(weights);
obj.m_adjust_value.set_offset(offset);
obj.m_adjust_value.set_negate(neg);
m_maxsmts.find(id)->set_adjust_value(obj.m_adjust_value);
TRACE("opt", tout << "maxsat: " << id << " offset:" << offset << "\n";
TRACE("opt", tout << "maxsat: " << neg << " " << id << " offset: " << offset << "\n";
tout << terms << "\n";);
}
else if (is_maximize(fml, tr, orig_term, index)) {
Expand Down Expand Up @@ -1158,7 +1158,14 @@ namespace opt {
#endif
}

rational context::adjust(unsigned id, rational const& v) {
return m_objectives[id].m_adjust_value(v);
}

void context::add_offset(unsigned id, rational const& o) {
m_objectives[id].m_adjust_value.add_offset(o);
}

bool context::verify_model(unsigned index, model* md, rational const& _v) {
rational r;
app_ref term = m_objectives[index].m_term;
Expand Down Expand Up @@ -1341,24 +1348,21 @@ namespace opt {
break;
}
case O_MAXSMT: {
bool ok = true;
for (unsigned j = 0; ok && j < obj.m_terms.size(); ++j) {
for (unsigned j = 0; j < obj.m_terms.size(); ++j) {
val = (*m_model)(obj.m_terms[j]);
TRACE("opt", tout << mk_pp(obj.m_terms[j], m) << " " << val << "\n";);
if (!m.is_true(val)) {
if (!m.is_true(val))
r += obj.m_weights[j];
}
}
if (ok) {
maxsmt& ms = *m_maxsmts.find(obj.m_id);
if (is_lower) {
ms.update_upper(r);
TRACE("opt", tout << "update upper from " << r << " to " << ms.get_upper() << "\n";);
}
else {
ms.update_lower(r);
TRACE("opt", tout << "update lower from " << r << " to " << ms.get_lower() << "\n";);
}

maxsmt& ms = *m_maxsmts.find(obj.m_id);
if (is_lower) {
ms.update_upper(r);
TRACE("opt", tout << "update upper from " << r << " to " << ms.get_upper() << "\n";);
}
else {
ms.update_lower(r);
TRACE("opt", tout << "update lower from " << r << " to " << ms.get_lower() << "\n";);
}
break;
}
Expand Down
9 changes: 7 additions & 2 deletions src/opt/opt_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace opt {
virtual smt::context& smt_context() = 0; // access SMT context for SMT based MaxSMT solver (wmax requires SMT core)
virtual unsigned num_objectives() = 0;
virtual bool verify_model(unsigned id, model* mdl, rational const& v) = 0;
virtual rational adjust(unsigned id, rational const& v) = 0;
virtual void add_offset(unsigned id, rational const& o) = 0;
virtual void set_model(model_ref& _m) = 0;
virtual void model_updated(model* mdl) = 0;
};
Expand Down Expand Up @@ -93,7 +95,7 @@ namespace opt {
app_ref m_term; // for maximize, minimize term
expr_ref_vector m_terms; // for maxsmt
vector<rational> m_weights; // for maxsmt
adjust_value m_adjust_value;
adjust_value m_adjust_value;
symbol m_id; // for maxsmt
unsigned m_index; // for maximize/minimize index

Expand Down Expand Up @@ -269,11 +271,14 @@ namespace opt {

void model_updated(model* mdl) override;

rational adjust(unsigned id, rational const& v) override;

void add_offset(unsigned id, rational const& o) override;

void register_on_model(on_model_t& ctx, std::function<void(on_model_t&, model_ref&)>& on_model) {
m_on_model_ctx = ctx;
m_on_model_eh = on_model;
}


void collect_timer_stats(statistics& st) const {
if (m_time != 0)
Expand Down
1 change: 1 addition & 0 deletions src/opt/opt_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace opt {
void set_offset(rational const& o) { m_offset = o; }
void set_negate(bool neg) { m_negate = neg; }
rational const& get_offset() const { return m_offset; }
void add_offset(rational const& o) { if (m_negate) m_offset -= o; else m_offset += o; }
bool get_negate() { return m_negate; }
inf_eps operator()(inf_eps const& r) const {
inf_eps result = r;
Expand Down
8 changes: 4 additions & 4 deletions src/opt/sortmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ namespace opt {
expr_ref_vector m_trail;
func_decl_ref_vector m_fresh;
ref<generic_model_converter> m_filter;
sortmax(maxsat_context& c, vector<soft>& s):
maxsmt_solver_base(c, s), m_sort(*this), m_trail(m), m_fresh(m) {}
sortmax(maxsat_context& c, vector<soft>& s, unsigned index):
maxsmt_solver_base(c, s, index), m_sort(*this), m_trail(m), m_fresh(m) {}

~sortmax() override {}

Expand Down Expand Up @@ -138,8 +138,8 @@ namespace opt {
};


maxsmt_solver_base* mk_sortmax(maxsat_context& c, vector<soft>& s) {
return alloc(sortmax, c, s);
maxsmt_solver_base* mk_sortmax(maxsat_context& c, vector<soft>& s, unsigned index) {
return alloc(sortmax, c, s, index);
}

}
8 changes: 4 additions & 4 deletions src/opt/wmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace opt {
}

public:
wmax(maxsat_context& c, vector<soft>& s):
maxsmt_solver_base(c, s),
wmax(maxsat_context& c, vector<soft>& s, unsigned index):
maxsmt_solver_base(c, s, index),
m_trail(m),
m_defs(m) {}

Expand Down Expand Up @@ -304,8 +304,8 @@ namespace opt {

};

maxsmt_solver_base* mk_wmax(maxsat_context& c, vector<soft> & s) {
return alloc(wmax, c, s);
maxsmt_solver_base* mk_wmax(maxsat_context& c, vector<soft> & s, unsigned index) {
return alloc(wmax, c, s, index);
}

}
4 changes: 2 additions & 2 deletions src/opt/wmax.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Module Name:
#include "opt/maxsmt.h"

namespace opt {
maxsmt_solver_base* mk_wmax(maxsat_context& c, vector<soft>& s);
maxsmt_solver_base* mk_wmax(maxsat_context& c, vector<soft>& s, unsigned index);

maxsmt_solver_base* mk_sortmax(maxsat_context& c, vector<soft>& s);
maxsmt_solver_base* mk_sortmax(maxsat_context& c, vector<soft>& s, unsigned index);

}

0 comments on commit 3f5eb7f

Please sign in to comment.