All Downloads are FREE. Search and download functionalities are using the official Maven repository.

z3-z3-4.13.0.src.api.dotnet.Optimize.cs Maven / Gradle / Ivy

The newest version!
/*++
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;
using System.Linq;

namespace Microsoft.Z3
{
    /// 
    /// Object for managing optimization context
    /// 
    public class Optimize : Z3Object
    {
        /// 
        /// A string that describes all available optimize solver parameters.
        /// 
        public string Help
        {
            get
            {
                return Native.Z3_optimize_get_help(Context.nCtx, NativeObject);
            }
        }

        /// 
        /// Sets the optimize solver parameters.
        /// 
        public Params Parameters
        {
            set
            {
                Debug.Assert(value != null);
                Context.CheckContextMatch(value);
                Native.Z3_optimize_set_params(Context.nCtx, NativeObject, value.NativeObject);
            }
        }

	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(string name, bool value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(string name, uint value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(string name, double value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(string name, string value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(string name, Symbol value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(Symbol name, bool value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(Symbol name, uint value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(Symbol name, double value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(Symbol name, string value) { Parameters = Context.MkParams().Add(name, value); }
	/// 
	/// Sets parameter on the optimize solver
	/// 
	public void Set(Symbol name, Symbol value) { Parameters = Context.MkParams().Add(name, value); }

        /// 
        /// 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)
        {
            Debug.Assert(constraints != null);
            Debug.Assert(constraints.All(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 Expr Lower
            {
                get { return opt.GetLower(handle); }
            }

            /// 
            /// Retrieve an upper bound for the objective handle.
            ///                    
            public Expr Upper
            {
                get { return opt.GetUpper(handle); }
            }

            /// 
            /// Retrieve the value of an objective.
            ///                    
            public Expr Value
            {
                get { return Lower; }
            }

            /// 
            /// Retrieve a lower bound for the objective handle.
            ///                    
            public Expr[] LowerAsVector
            {
                get { return opt.GetLowerAsVector(handle); }
            }

            /// 
            /// Retrieve an upper bound for the objective handle.
            ///                    
            public Expr[] UpperAsVector
            {
                get { return opt.GetUpperAsVector(handle); }
            }

        }

        /// 
        /// 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);
            using 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) is optimal.
        /// 
        ///
        public Status Check(params Expr[] assumptions)
        {
            Z3_lbool r = (Z3_lbool)Native.Z3_optimize_check(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
            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);
            }
        }

        /// 
        /// The unsat core of the last Check.
        /// 
        /// 
        /// The unsat core is a subset of assumptions
        /// The result is empty if Check was not invoked before,
        /// if its results was not UNSATISFIABLE, or if core production is disabled.
        /// 
        public BoolExpr[] UnsatCore
        {
            get
            {

                using ASTVector core = new ASTVector(Context, Native.Z3_optimize_get_unsat_core(Context.nCtx, NativeObject));                
                return core.ToBoolExprArray();
            }
        }

        /// 
        /// 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.
        /// The expression can be either an arithmetical expression or bit-vector.
        ///             
        public Handle MkMaximize(Expr 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(Expr e)
        {
            return new Handle(this, Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject));
        }


        /// 
        /// Retrieve a lower bound for the objective handle.
        ///             
        private Expr GetLower(uint index)
        {
            return Expr.Create(Context, Native.Z3_optimize_get_lower(Context.nCtx, NativeObject, index));
        }


        /// 
        /// Retrieve an upper bound for the objective handle.
        ///             
        private Expr GetUpper(uint index)
        {
            return Expr.Create(Context, Native.Z3_optimize_get_upper(Context.nCtx, NativeObject, index));
        }

        /// 
        /// Retrieve a lower bound for the objective handle.
        ///             
        private Expr[] GetLowerAsVector(uint index)
        {
            using ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_lower_as_vector(Context.nCtx, NativeObject, index));
            return v.ToExprArray();
        }


        /// 
        /// Retrieve an upper bound for the objective handle.
        ///             
        private Expr[] GetUpperAsVector(uint index)
        {
            using ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_upper_as_vector(Context.nCtx, NativeObject, index));
            return v.ToExprArray();
        }

    /// 
    /// Return a string the describes why the last to check returned unknown
    ///     
        public String ReasonUnknown
        {
            get 
            {
                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
            {

                using 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
            {

                using ASTVector objectives = new ASTVector(Context, Native.Z3_optimize_get_objectives(Context.nCtx, NativeObject));
                return objectives.ToExprArray();
            }
        }


        /// 
        /// Optimize statistics.
        /// 
        public Statistics Statistics
        {
            get
            {

                return new Statistics(Context, Native.Z3_optimize_get_statistics(Context.nCtx, NativeObject));
            }
        }


        #region Internal
        internal Optimize(Context ctx, IntPtr obj)
            : base(ctx, obj)
        {
            Debug.Assert(ctx != null);
        }
        internal Optimize(Context ctx)
            : base(ctx, Native.Z3_mk_optimize(ctx.nCtx))
        {
            Debug.Assert(ctx != null);
        }

        internal override void IncRef(IntPtr o)
        {
            Native.Z3_optimize_inc_ref(Context.nCtx, o);
        }

        internal override void DecRef(IntPtr o)
        {
            lock (Context)
            {
                if (Context.nCtx != IntPtr.Zero)
                    Native.Z3_optimize_dec_ref(Context.nCtx, o);
            }
        }
        #endregion
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy