mirror of
				https://github.com/Z3Prover/z3
				synced 2025-11-04 05:19:11 +00:00 
			
		
		
		
	add setting to dump intermediary models #2087
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
		
							parent
							
								
									4caadc6519
								
							
						
					
					
						commit
						c45ab6efed
					
				
					 5 changed files with 27 additions and 5 deletions
				
			
		| 
						 | 
					@ -125,7 +125,7 @@ namespace opt {
 | 
				
			||||||
        m_solver(nullptr),
 | 
					        m_solver(nullptr),
 | 
				
			||||||
        m_pareto1(false),
 | 
					        m_pareto1(false),
 | 
				
			||||||
        m_box_index(UINT_MAX),
 | 
					        m_box_index(UINT_MAX),
 | 
				
			||||||
        m_optsmt(m),
 | 
					        m_optsmt(m, *this),
 | 
				
			||||||
        m_scoped_state(m),
 | 
					        m_scoped_state(m),
 | 
				
			||||||
        m_fm(alloc(generic_model_converter, m, "opt")),
 | 
					        m_fm(alloc(generic_model_converter, m, "opt")),
 | 
				
			||||||
        m_model_fixed(),
 | 
					        m_model_fixed(),
 | 
				
			||||||
| 
						 | 
					@ -357,6 +357,17 @@ namespace opt {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void context::set_model(model_ref& m) { 
 | 
				
			||||||
 | 
					        m_model = m; 
 | 
				
			||||||
 | 
					        opt_params optp(m_params);
 | 
				
			||||||
 | 
					        if (optp.dump_models()) {
 | 
				
			||||||
 | 
					            model_ref md = m->copy();
 | 
				
			||||||
 | 
					            fix_model(md);
 | 
				
			||||||
 | 
					            std::cout << *md << "\n";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void context::get_model_core(model_ref& mdl) {
 | 
					    void context::get_model_core(model_ref& mdl) {
 | 
				
			||||||
        mdl = m_model;
 | 
					        mdl = m_model;
 | 
				
			||||||
        fix_model(mdl);
 | 
					        fix_model(mdl);
 | 
				
			||||||
| 
						 | 
					@ -1062,6 +1073,9 @@ namespace opt {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void context::model_updated(model* md) {
 | 
					    void context::model_updated(model* md) {
 | 
				
			||||||
 | 
					        model_ref mdl = md;
 | 
				
			||||||
 | 
					        set_model(mdl);
 | 
				
			||||||
 | 
					#if 0
 | 
				
			||||||
        opt_params optp(m_params);
 | 
					        opt_params optp(m_params);
 | 
				
			||||||
        symbol prefix = optp.solution_prefix();
 | 
					        symbol prefix = optp.solution_prefix();
 | 
				
			||||||
        if (prefix == symbol::null || prefix == symbol("")) return;        
 | 
					        if (prefix == symbol::null || prefix == symbol("")) return;        
 | 
				
			||||||
| 
						 | 
					@ -1074,6 +1088,7 @@ namespace opt {
 | 
				
			||||||
            out << *mdl;
 | 
					            out << *mdl;
 | 
				
			||||||
            out.close();
 | 
					            out.close();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -189,7 +189,7 @@ namespace opt {
 | 
				
			||||||
        bool empty() override { return m_scoped_state.m_objectives.empty(); }
 | 
					        bool empty() override { return m_scoped_state.m_objectives.empty(); }
 | 
				
			||||||
        void set_hard_constraints(expr_ref_vector const& hard) override;
 | 
					        void set_hard_constraints(expr_ref_vector const& hard) override;
 | 
				
			||||||
        lbool optimize(expr_ref_vector const& asms) override;
 | 
					        lbool optimize(expr_ref_vector const& asms) override;
 | 
				
			||||||
        void set_model(model_ref& _m) override { m_model = _m; }
 | 
					        void set_model(model_ref& _m) override;
 | 
				
			||||||
        void get_model_core(model_ref& _m) override;
 | 
					        void get_model_core(model_ref& _m) override;
 | 
				
			||||||
        void get_box_model(model_ref& _m, unsigned index) override;
 | 
					        void get_box_model(model_ref& _m, unsigned index) override;
 | 
				
			||||||
        void fix_model(model_ref& _m) override;
 | 
					        void fix_model(model_ref& _m) override;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,7 @@ def_module_params('opt',
 | 
				
			||||||
                          ('maxsat_engine', SYMBOL, 'maxres', "select engine for maxsat: 'core_maxsat', 'wmax', 'maxres', 'pd-maxres'"),
 | 
					                          ('maxsat_engine', SYMBOL, 'maxres', "select engine for maxsat: 'core_maxsat', 'wmax', 'maxres', 'pd-maxres'"),
 | 
				
			||||||
                          ('priority', SYMBOL, 'lex', "select how to priortize objectives: 'lex' (lexicographic), 'pareto', 'box'"),
 | 
					                          ('priority', SYMBOL, 'lex', "select how to priortize objectives: 'lex' (lexicographic), 'pareto', 'box'"),
 | 
				
			||||||
                          ('dump_benchmarks', BOOL, False, 'dump benchmarks for profiling'),
 | 
					                          ('dump_benchmarks', BOOL, False, 'dump benchmarks for profiling'),
 | 
				
			||||||
 | 
					                          ('dump_models', BOOL, False, 'display intermediary models to stdout'),
 | 
				
			||||||
                          ('solution_prefix', SYMBOL, '', "path prefix to dump intermediary, but non-optimal, solutions"),
 | 
					                          ('solution_prefix', SYMBOL, '', "path prefix to dump intermediary, but non-optimal, solutions"),
 | 
				
			||||||
                          ('timeout', UINT, UINT_MAX, 'timeout (in milliseconds) (UINT_MAX and 0 mean no timeout)'),
 | 
					                          ('timeout', UINT, UINT_MAX, 'timeout (in milliseconds) (UINT_MAX and 0 mean no timeout)'),
 | 
				
			||||||
                          ('rlimit', UINT, 0, 'resource limit (0 means no limit)'),
 | 
					                          ('rlimit', UINT, 0, 'resource limit (0 means no limit)'),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,6 +31,7 @@ Notes:
 | 
				
			||||||
#include <typeinfo>
 | 
					#include <typeinfo>
 | 
				
			||||||
#include "opt/optsmt.h"
 | 
					#include "opt/optsmt.h"
 | 
				
			||||||
#include "opt/opt_solver.h"
 | 
					#include "opt/opt_solver.h"
 | 
				
			||||||
 | 
					#include "opt/opt_context.h"
 | 
				
			||||||
#include "ast/arith_decl_plugin.h"
 | 
					#include "ast/arith_decl_plugin.h"
 | 
				
			||||||
#include "smt/theory_arith.h"
 | 
					#include "smt/theory_arith.h"
 | 
				
			||||||
#include "ast/ast_pp.h"
 | 
					#include "ast/ast_pp.h"
 | 
				
			||||||
| 
						 | 
					@ -362,12 +363,14 @@ namespace opt {
 | 
				
			||||||
                           verbose_stream() << "(optsmt lower bound: " << v << ")\n";
 | 
					                           verbose_stream() << "(optsmt lower bound: " << v << ")\n";
 | 
				
			||||||
                       else
 | 
					                       else
 | 
				
			||||||
                           verbose_stream() << "(optsmt upper bound: " << (-v) << ")\n";
 | 
					                           verbose_stream() << "(optsmt upper bound: " << (-v) << ")\n";
 | 
				
			||||||
                       );
 | 
					                       );            
 | 
				
			||||||
            expr_ref tmp(m);
 | 
					            expr_ref tmp(m);
 | 
				
			||||||
            for (unsigned i = idx+1; i < m_vars.size(); ++i) {
 | 
					            for (unsigned i = idx+1; i < m_vars.size(); ++i) {
 | 
				
			||||||
                m_s->maximize_objective(i, tmp);
 | 
					                m_s->maximize_objective(i, tmp);
 | 
				
			||||||
                m_lower[i] = m_s->saved_objective_value(i);
 | 
					                m_lower[i] = m_s->saved_objective_value(i);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            m_context.set_model(m_model);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,8 +27,11 @@ namespace opt {
 | 
				
			||||||
       Returns an optimal assignment to objective functions.
 | 
					       Returns an optimal assignment to objective functions.
 | 
				
			||||||
    */
 | 
					    */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class context;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class optsmt {
 | 
					    class optsmt {
 | 
				
			||||||
        ast_manager&     m;
 | 
					        ast_manager&     m;
 | 
				
			||||||
 | 
					        context&         m_context;
 | 
				
			||||||
        opt_solver*      m_s;
 | 
					        opt_solver*      m_s;
 | 
				
			||||||
        vector<inf_eps>  m_lower;
 | 
					        vector<inf_eps>  m_lower;
 | 
				
			||||||
        vector<inf_eps>  m_upper;
 | 
					        vector<inf_eps>  m_upper;
 | 
				
			||||||
| 
						 | 
					@ -40,8 +43,8 @@ namespace opt {
 | 
				
			||||||
        svector<symbol>  m_labels;
 | 
					        svector<symbol>  m_labels;
 | 
				
			||||||
        sref_vector<model> m_models;
 | 
					        sref_vector<model> m_models;
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
        optsmt(ast_manager& m): 
 | 
					        optsmt(ast_manager& m, context& ctx): 
 | 
				
			||||||
            m(m), m_s(nullptr), m_objs(m), m_lower_fmls(m) {}
 | 
					            m(m), m_context(ctx), m_s(nullptr), m_objs(m), m_lower_fmls(m) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        void setup(opt_solver& solver);
 | 
					        void setup(opt_solver& solver);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue