3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-24 01:25:31 +00:00

add to_string and get_help methods to optimize API

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2013-12-06 11:34:41 -08:00
parent 7884b2ab31
commit 4d6aa1a0f3
6 changed files with 170 additions and 7 deletions

View file

@ -62,6 +62,10 @@ namespace opt {
m_weights.push_back(w);
}
unsigned size() const { return m_soft_constraints.size(); }
expr* operator[](unsigned idx) const { return m_soft_constraints[idx]; }
rational weight(unsigned idx) const { return m_weights[idx]; }
void commit_assignment();
inf_eps get_value() const;
inf_eps get_lower() const;

View file

@ -22,6 +22,7 @@ Notes:
#include "opt_solver.h"
#include "opt_params.hpp"
#include "arith_decl_plugin.h"
#include "for_each_expr.h"
namespace opt {
@ -289,4 +290,114 @@ namespace opt {
}
}
typedef obj_hashtable<func_decl> func_decl_set;
struct context::free_func_visitor {
ast_manager& m;
func_decl_set m_funcs;
obj_hashtable<sort> m_sorts;
expr_mark m_visited;
public:
free_func_visitor(ast_manager& m): m(m) {}
void operator()(var * n) { }
void operator()(app * n) {
m_funcs.insert(n->get_decl());
sort* s = m.get_sort(n);
if (s->get_family_id() == null_family_id) {
m_sorts.insert(s);
}
}
void operator()(quantifier * n) { }
func_decl_set& funcs() { return m_funcs; }
obj_hashtable<sort>& sorts() { return m_sorts; }
void collect(expr* e) {
for_each_expr(*this, m_visited, e);
}
};
std::string context::to_string() const {
smt2_pp_environment_dbg env(m);
free_func_visitor visitor(m);
std::ostringstream out;
#define PP(_e_) ast_smt2_pp(out, _e_, env);
for (unsigned i = 0; i < m_hard_constraints.size(); ++i) {
visitor.collect(m_hard_constraints[i]);
}
for (unsigned i = 0; i < m_objectives.size(); ++i) {
objective const& obj = m_objectives[i];
switch(obj.m_type) {
case O_MAXIMIZE:
case O_MINIMIZE:
visitor.collect(obj.m_term);
break;
case O_MAXSMT: {
maxsmt& ms = *m_maxsmts.find(obj.m_id);
for (unsigned j = 0; j < ms.size(); ++j) {
visitor.collect(ms[j]);
}
break;
}
default:
UNREACHABLE();
break;
}
}
obj_hashtable<sort>::iterator sit = visitor.sorts().begin();
obj_hashtable<sort>::iterator send = visitor.sorts().end();
for (; sit != send; ++sit) {
PP(*sit);
}
func_decl_set::iterator it = visitor.funcs().begin();
func_decl_set::iterator end = visitor.funcs().end();
for (; it != end; ++it) {
PP(*it);
out << "\n";
}
for (unsigned i = 0; i < m_hard_constraints.size(); ++i) {
out << "(assert ";
PP(m_hard_constraints[i]);
out << ")\n";
}
for (unsigned i = 0; i < m_objectives.size(); ++i) {
objective const& obj = m_objectives[i];
switch(obj.m_type) {
case O_MAXIMIZE:
out << "(maximize ";
PP(obj.m_term);
out << ")\n";
break;
case O_MINIMIZE:
out << "(minimize ";
PP(obj.m_term);
out << ")\n";
break;
case O_MAXSMT: {
maxsmt& ms = *m_maxsmts.find(obj.m_id);
for (unsigned j = 0; j < ms.size(); ++j) {
out << "(assert-soft ";
PP(ms[j]);
rational w = ms.weight(j);
if (w.is_int()) {
out << " :weight " << ms.weight(j);
}
else {
out << " :dweight " << ms.weight(j);
}
if (obj.m_id != symbol::null) {
out << " :id " << obj.m_id;
}
out << ")\n";
}
break;
}
default:
UNREACHABLE();
break;
}
}
return out.str();
}
}

View file

@ -35,6 +35,7 @@ namespace opt {
class opt_solver;
class context {
struct free_func_visitor;
typedef map<symbol, maxsmt*, symbol_hash_proc, symbol_eq_proc> map_t;
typedef map<symbol, unsigned, symbol_hash_proc, symbol_eq_proc> map_id;
enum objective_t {
@ -92,6 +93,8 @@ namespace opt {
expr_ref get_lower(unsigned idx);
expr_ref get_upper(unsigned idx);
std::string to_string() const;
private:
void validate_feasibility(maxsmt& ms);