/*++ Copyright (c) 2012 Microsoft Corporation Module Name: Optimize.cs Abstract: Z3 Managed API: Optimizes Author: Nikolaj Bjorner (nbjorner) 2013-12-03 Notes: --*/ using System; using System.Collections.Generic; using System.Diagnostics.Contracts; namespace Microsoft.Z3 { /// /// Object for managing optimizization context /// [ContractVerification(true)] public class Optimize : Z3Object { /// /// A string that describes all available optimize solver parameters. /// public string Help { get { Contract.Ensures(Contract.Result() != null); return Native.Z3_optimize_get_help(Context.nCtx, NativeObject); } } /// /// Sets the optimize solver parameters. /// public Params Parameters { set { Contract.Requires(value != null); Context.CheckContextMatch(value); Native.Z3_optimize_set_params(Context.nCtx, NativeObject, value.NativeObject); } } /// /// Retrieves parameter descriptions for Optimize solver. /// public ParamDescrs ParameterDescriptions { get { return new ParamDescrs(Context, Native.Z3_optimize_get_param_descrs(Context.nCtx, NativeObject)); } } /// /// Assert a constraint (or multiple) into the optimize solver. /// public void Assert(params BoolExpr[] constraints) { AddConstraints(constraints); } /// /// Assert a constraint (or multiple) into the optimize solver. /// public void Assert(IEnumerable constraints) { AddConstraints(constraints); } /// /// Alias for Assert. /// public void Add(params BoolExpr[] constraints) { AddConstraints(constraints); } /// /// Alias for Assert. /// public void Add(IEnumerable constraints) { AddConstraints(constraints); } /// /// Assert a constraint (or multiple) into the optimize solver. /// private void AddConstraints(IEnumerable constraints) { Contract.Requires(constraints != null); Contract.Requires(Contract.ForAll(constraints, c => c != null)); Context.CheckContextMatch(constraints); foreach (BoolExpr a in constraints) { Native.Z3_optimize_assert(Context.nCtx, NativeObject, a.NativeObject); } } /// /// Handle to objectives returned by objective functions. /// public class Handle { Optimize opt; uint handle; internal Handle(Optimize opt, uint h) { this.opt = opt; this.handle = h; } /// /// Retrieve a lower bound for the objective handle. /// public ArithExpr Lower { get { return opt.GetLower(handle); } } /// /// Retrieve an upper bound for the objective handle. /// public ArithExpr Upper { get { return opt.GetUpper(handle); } } /// /// Retrieve the value of an objective. /// public ArithExpr Value { get { return Lower; } } } /// /// Assert soft constraint /// /// /// Return an objective which associates with the group of constraints. /// public Handle AssertSoft(BoolExpr constraint, uint weight, string group) { Context.CheckContextMatch(constraint); Symbol s = Context.MkSymbol(group); return new Handle(this, Native.Z3_optimize_assert_soft(Context.nCtx, NativeObject, constraint.NativeObject, weight.ToString(), s.NativeObject)); } /// /// Check satisfiability of asserted constraints. /// Produce a model that (when the objectives are bounded and /// don't use strict inequalities) meets the objectives. /// /// public Status Check() { Z3_lbool r = (Z3_lbool)Native.Z3_optimize_check(Context.nCtx, NativeObject); switch (r) { case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE; case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE; default: return Status.UNKNOWN; } } /// /// Creates a backtracking point. /// /// public void Push() { Native.Z3_optimize_push(Context.nCtx, NativeObject); } /// /// Backtrack one backtracking point. /// /// Note that an exception is thrown if Pop is called without a corresponding Push /// public void Pop() { Native.Z3_optimize_pop(Context.nCtx, NativeObject); } /// /// The model of the last Check. /// /// /// The result is null if Check was not invoked before, /// if its results was not SATISFIABLE, or if model production is not enabled. /// public Model Model { get { IntPtr x = Native.Z3_optimize_get_model(Context.nCtx, NativeObject); if (x == IntPtr.Zero) return null; else return new Model(Context, x); } } /// /// Declare an arithmetical maximization objective. /// Return a handle to the objective. The handle is used as /// to retrieve the values of objectives after calling Check. /// public Handle MkMaximize(ArithExpr e) { return new Handle(this, Native.Z3_optimize_maximize(Context.nCtx, NativeObject, e.NativeObject)); } /// /// Declare an arithmetical minimization objective. /// Similar to MkMaximize. /// public Handle MkMinimize(ArithExpr e) { return new Handle(this, Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject)); } /// /// Retrieve a lower bound for the objective handle. /// private ArithExpr GetLower(uint index) { return (ArithExpr)Expr.Create(Context, Native.Z3_optimize_get_lower(Context.nCtx, NativeObject, index)); } /// /// Retrieve an upper bound for the objective handle. /// private ArithExpr GetUpper(uint index) { return (ArithExpr)Expr.Create(Context, Native.Z3_optimize_get_upper(Context.nCtx, NativeObject, index)); } /// /// Return a string the describes why the last to check returned unknown /// public String getReasonUnknown() { Contract.Ensures(Contract.Result() != null); return Native.Z3_optimize_get_reason_unknown(Context.nCtx, NativeObject); } /// /// Print the context to a string (SMT-LIB parseable benchmark). /// public override string ToString() { return Native.Z3_optimize_to_string(Context.nCtx, NativeObject); } /// /// Parse an SMT-LIB2 file with optimization objectives and constraints. /// The parsed constraints and objectives are added to the optimization context. /// public void FromFile(string file) { Native.Z3_optimize_from_file(Context.nCtx, NativeObject, file); } /// /// Similar to FromFile. Instead it takes as argument a string. /// public void FromString(string s) { Native.Z3_optimize_from_string(Context.nCtx, NativeObject, s); } /// /// The set of asserted formulas. /// public BoolExpr[] Assertions { get { Contract.Ensures(Contract.Result() != null); ASTVector assertions = new ASTVector(Context, Native.Z3_optimize_get_assertions(Context.nCtx, NativeObject)); return assertions.ToBoolExprArray(); } } /// /// The set of asserted formulas. /// public Expr[] Objectives { get { Contract.Ensures(Contract.Result() != null); ASTVector objectives = new ASTVector(Context, Native.Z3_optimize_get_objectives(Context.nCtx, NativeObject)); return objectives.ToExprArray(); } } /// /// Optimize statistics. /// public Statistics Statistics { get { Contract.Ensures(Contract.Result() != null); return new Statistics(Context, Native.Z3_optimize_get_statistics(Context.nCtx, NativeObject)); } } #region Internal internal Optimize(Context ctx, IntPtr obj) : base(ctx, obj) { Contract.Requires(ctx != null); } internal Optimize(Context ctx) : base(ctx, Native.Z3_mk_optimize(ctx.nCtx)) { Contract.Requires(ctx != null); } internal class DecRefQueue : IDecRefQueue { public DecRefQueue() : base() { } public DecRefQueue(uint move_limit) : base(move_limit) { } internal override void IncRef(Context ctx, IntPtr obj) { Native.Z3_optimize_inc_ref(ctx.nCtx, obj); } internal override void DecRef(Context ctx, IntPtr obj) { Native.Z3_optimize_dec_ref(ctx.nCtx, obj); } }; internal override void IncRef(IntPtr o) { Context.Optimize_DRQ.IncAndClear(Context, o); base.IncRef(o); } internal override void DecRef(IntPtr o) { Context.Optimize_DRQ.Add(o); base.DecRef(o); } #endregion } }