Z3
 
Loading...
Searching...
No Matches
Optimize.cs
Go to the documentation of this file.
1/*++
2Copyright (c) 2012 Microsoft Corporation
3
4Module Name:
5
6 Optimize.cs
7
8Abstract:
9
10 Z3 Managed API: Optimizes
11
12Author:
13
14 Nikolaj Bjorner (nbjorner) 2013-12-03
15
16Notes:
17
18--*/
19
20using System;
21using System.Collections.Generic;
22using System.Diagnostics;
23using System.Linq;
24
25namespace Microsoft.Z3
26{
30 public class Optimize : Z3Object
31 {
35 public string Help
36 {
37 get
38 {
39 return Native.Z3_optimize_get_help(Context.nCtx, NativeObject);
40 }
41 }
42
47 {
48 set
49 {
50 Debug.Assert(value != null);
51 Context.CheckContextMatch(value);
52 Native.Z3_optimize_set_params(Context.nCtx, NativeObject, value.NativeObject);
53 }
54 }
55
59 public void Set(string name, bool value) { Parameters = Context.MkParams().Add(name, value); }
63 public void Set(string name, uint value) { Parameters = Context.MkParams().Add(name, value); }
67 public void Set(string name, double value) { Parameters = Context.MkParams().Add(name, value); }
71 public void Set(string name, string value) { Parameters = Context.MkParams().Add(name, value); }
75 public void Set(string name, Symbol value) { Parameters = Context.MkParams().Add(name, value); }
79 public void Set(Symbol name, bool value) { Parameters = Context.MkParams().Add(name, value); }
83 public void Set(Symbol name, uint value) { Parameters = Context.MkParams().Add(name, value); }
87 public void Set(Symbol name, double value) { Parameters = Context.MkParams().Add(name, value); }
91 public void Set(Symbol name, string value) { Parameters = Context.MkParams().Add(name, value); }
95 public void Set(Symbol name, Symbol value) { Parameters = Context.MkParams().Add(name, value); }
96
101 {
102 get { return new ParamDescrs(Context, Native.Z3_optimize_get_param_descrs(Context.nCtx, NativeObject)); }
103 }
104
108 public void Assert(params BoolExpr[] constraints)
109 {
110 AddConstraints(constraints);
111 }
112
116 public void Assert(IEnumerable<BoolExpr> constraints)
117 {
118 AddConstraints(constraints);
119 }
120
124 public void Add(params BoolExpr[] constraints)
125 {
126 AddConstraints(constraints);
127 }
128
132 public void Add(IEnumerable<BoolExpr> constraints)
133 {
134 AddConstraints(constraints);
135 }
136
140 private void AddConstraints(IEnumerable<BoolExpr> constraints)
141 {
142 Debug.Assert(constraints != null);
143 Debug.Assert(constraints.All(c => c != null));
144
145 Context.CheckContextMatch(constraints);
146 foreach (BoolExpr a in constraints)
147 {
148 Native.Z3_optimize_assert(Context.nCtx, NativeObject, a.NativeObject);
149 }
150 }
151
163 public void AssertAndTrack(BoolExpr constraint, BoolExpr p)
164 {
165 Debug.Assert(constraint != null);
166 Debug.Assert(p != null);
167 Context.CheckContextMatch(constraint);
168 Context.CheckContextMatch(p);
169
170 Native.Z3_optimize_assert_and_track(Context.nCtx, NativeObject, constraint.NativeObject, p.NativeObject);
171 }
172
176 public class Handle
177 {
178 Optimize opt;
179 uint handle;
180 internal Handle(Optimize opt, uint h)
181 {
182 this.opt = opt;
183 this.handle = h;
184 }
185
189 public Expr Lower
190 {
191 get { return opt.GetLower(handle); }
192 }
193
197 public Expr Upper
198 {
199 get { return opt.GetUpper(handle); }
200 }
201
205 public Expr Value
206 {
207 get { return Lower; }
208 }
209
214 {
215 get { return opt.GetLowerAsVector(handle); }
216 }
217
222 {
223 get { return opt.GetUpperAsVector(handle); }
224 }
225
226 }
227
234 public Handle AssertSoft(BoolExpr constraint, uint weight, string group)
235 {
236 Context.CheckContextMatch(constraint);
237 using Symbol s = Context.MkSymbol(group);
238 return new Handle(this, Native.Z3_optimize_assert_soft(Context.nCtx, NativeObject, constraint.NativeObject, weight.ToString(), s.NativeObject));
239 }
240
241
248 public Status Check(params Expr[] assumptions)
249 {
250 Z3_lbool r = (Z3_lbool)Native.Z3_optimize_check(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
251 switch (r)
252 {
253 case Z3_lbool.Z3_L_TRUE:
254 return Status.SATISFIABLE;
255 case Z3_lbool.Z3_L_FALSE:
256 return Status.UNSATISFIABLE;
257 default:
258 return Status.UNKNOWN;
259 }
260 }
261
266 public void Push()
267 {
268 Native.Z3_optimize_push(Context.nCtx, NativeObject);
269 }
270
276 public void Pop()
277 {
278 Native.Z3_optimize_pop(Context.nCtx, NativeObject);
279 }
280
281
290 {
291 get
292 {
293 IntPtr x = Native.Z3_optimize_get_model(Context.nCtx, NativeObject);
294 if (x == IntPtr.Zero)
295 return null;
296 else
297 return new Model(Context, x);
298 }
299 }
300
310 {
311 get
312 {
313
314 using ASTVector core = new ASTVector(Context, Native.Z3_optimize_get_unsat_core(Context.nCtx, NativeObject));
315 return core.ToBoolExprArray();
316 }
317 }
318
326 {
327 return new Handle(this, Native.Z3_optimize_maximize(Context.nCtx, NativeObject, e.NativeObject));
328 }
329
335 {
336 return new Handle(this, Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject));
337 }
338
339
343 private Expr GetLower(uint index)
344 {
345 return Expr.Create(Context, Native.Z3_optimize_get_lower(Context.nCtx, NativeObject, index));
346 }
347
348
352 private Expr GetUpper(uint index)
353 {
354 return Expr.Create(Context, Native.Z3_optimize_get_upper(Context.nCtx, NativeObject, index));
355 }
356
360 private Expr[] GetLowerAsVector(uint index)
361 {
362 using ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_lower_as_vector(Context.nCtx, NativeObject, index));
363 return v.ToExprArray();
364 }
365
366
370 private Expr[] GetUpperAsVector(uint index)
371 {
372 using ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_upper_as_vector(Context.nCtx, NativeObject, index));
373 return v.ToExprArray();
374 }
375
379 public String ReasonUnknown
380 {
381 get
382 {
383 return Native.Z3_optimize_get_reason_unknown(Context.nCtx, NativeObject);
384 }
385 }
386
387
391 public override string ToString()
392 {
393 return Native.Z3_optimize_to_string(Context.nCtx, NativeObject);
394 }
395
400 public void FromFile(string file)
401 {
402 Native.Z3_optimize_from_file(Context.nCtx, NativeObject, file);
403 }
404
408 public void FromString(string s)
409 {
410 Native.Z3_optimize_from_string(Context.nCtx, NativeObject, s);
411 }
412
417 {
418 get
419 {
420
421 using ASTVector assertions = new ASTVector(Context, Native.Z3_optimize_get_assertions(Context.nCtx, NativeObject));
422 return assertions.ToBoolExprArray();
423 }
424 }
425
430 {
431 get
432 {
433
434 using ASTVector objectives = new ASTVector(Context, Native.Z3_optimize_get_objectives(Context.nCtx, NativeObject));
435 return objectives.ToExprArray();
436 }
437 }
438
439
444 {
445 get
446 {
447
448 return new Statistics(Context, Native.Z3_optimize_get_statistics(Context.nCtx, NativeObject));
449 }
450 }
451
452
453 #region Internal
454 internal Optimize(Context ctx, IntPtr obj)
455 : base(ctx, obj)
456 {
457 Debug.Assert(ctx != null);
458 }
459 internal Optimize(Context ctx)
460 : base(ctx, Native.Z3_mk_optimize(ctx.nCtx))
461 {
462 Debug.Assert(ctx != null);
463 }
464
465 internal override void IncRef(IntPtr o)
466 {
467 Native.Z3_optimize_inc_ref(Context.nCtx, o);
468 }
469
470 internal override void DecRef(IntPtr o)
471 {
472 lock (Context)
473 {
474 if (Context.nCtx != IntPtr.Zero)
475 Native.Z3_optimize_dec_ref(Context.nCtx, o);
476 }
477 }
478 #endregion
479 }
480}
The abstract syntax tree (AST) class.
Definition AST.cs:31
Vectors of ASTs.
Definition ASTVector.cs:29
Boolean expressions.
Definition BoolExpr.cs:32
The main interaction with Z3 happens via the Context.
Definition Context.cs:34
Params MkParams()
Creates a new ParameterSet.
Definition Context.cs:3465
IntSymbol MkSymbol(int i)
Creates a new symbol using an integer.
Definition Context.cs:111
Expressions are terms.
Definition Expr.cs:31
A Model contains interpretations (assignments) of constants and functions.
Definition Model.cs:30
Handle to objectives returned by objective functions.
Definition Optimize.cs:177
Expr[] UpperAsVector
Retrieve an upper bound for the objective handle.
Definition Optimize.cs:222
Expr[] LowerAsVector
Retrieve a lower bound for the objective handle.
Definition Optimize.cs:214
Expr Upper
Retrieve an upper bound for the objective handle.
Definition Optimize.cs:198
Expr Value
Retrieve the value of an objective.
Definition Optimize.cs:206
Expr Lower
Retrieve a lower bound for the objective handle.
Definition Optimize.cs:190
Object for managing optimization context.
Definition Optimize.cs:31
void Set(string name, uint value)
Sets parameter on the optimize solver.
Definition Optimize.cs:63
BoolExpr[] Assertions
The set of asserted formulas.
Definition Optimize.cs:417
void Set(string name, double value)
Sets parameter on the optimize solver.
Definition Optimize.cs:67
Model Model
The model of the last Check.
Definition Optimize.cs:290
string Help
A string that describes all available optimize solver parameters.
Definition Optimize.cs:36
void Set(string name, string value)
Sets parameter on the optimize solver.
Definition Optimize.cs:71
void Add(IEnumerable< BoolExpr > constraints)
Alias for Assert.
Definition Optimize.cs:132
void AssertAndTrack(BoolExpr constraint, BoolExpr p)
Assert a constraint into the optimize solver, and track it (in the unsat) core using the Boolean cons...
Definition Optimize.cs:163
void Set(Symbol name, Symbol value)
Sets parameter on the optimize solver.
Definition Optimize.cs:95
void Push()
Creates a backtracking point.
Definition Optimize.cs:266
Handle MkMaximize(Expr e)
Declare an arithmetical maximization objective. Return a handle to the objective. The handle is used ...
Definition Optimize.cs:325
void Assert(params BoolExpr[] constraints)
Assert a constraint (or multiple) into the optimize solver.
Definition Optimize.cs:108
void FromString(string s)
Similar to FromFile. Instead it takes as argument a string.
Definition Optimize.cs:408
Params Parameters
Sets the optimize solver parameters.
Definition Optimize.cs:47
Statistics Statistics
Optimize statistics.
Definition Optimize.cs:444
void Pop()
Backtrack one backtracking point.
Definition Optimize.cs:276
Handle MkMinimize(Expr e)
Declare an arithmetical minimization objective. Similar to MkMaximize.
Definition Optimize.cs:334
void Set(Symbol name, string value)
Sets parameter on the optimize solver.
Definition Optimize.cs:91
ParamDescrs ParameterDescriptions
Retrieves parameter descriptions for Optimize solver.
Definition Optimize.cs:101
Expr[] Objectives
The set of asserted formulas.
Definition Optimize.cs:430
Status Check(params Expr[] assumptions)
Check satisfiability of asserted constraints. Produce a model that (when the objectives are bounded a...
Definition Optimize.cs:248
BoolExpr[] UnsatCore
The unsat core of the last Check.
Definition Optimize.cs:310
void Assert(IEnumerable< BoolExpr > constraints)
Assert a constraint (or multiple) into the optimize solver.
Definition Optimize.cs:116
override string ToString()
Print the context to a string (SMT-LIB parseable benchmark).
Definition Optimize.cs:391
void FromFile(string file)
Parse an SMT-LIB2 file with optimization objectives and constraints. The parsed constraints and objec...
Definition Optimize.cs:400
void Set(string name, bool value)
Sets parameter on the optimize solver.
Definition Optimize.cs:59
void Set(Symbol name, double value)
Sets parameter on the optimize solver.
Definition Optimize.cs:87
void Set(Symbol name, bool value)
Sets parameter on the optimize solver.
Definition Optimize.cs:79
void Add(params BoolExpr[] constraints)
Alias for Assert.
Definition Optimize.cs:124
Handle AssertSoft(BoolExpr constraint, uint weight, string group)
Assert soft constraint.
Definition Optimize.cs:234
void Set(string name, Symbol value)
Sets parameter on the optimize solver.
Definition Optimize.cs:75
void Set(Symbol name, uint value)
Sets parameter on the optimize solver.
Definition Optimize.cs:83
String ReasonUnknown
Return a string the describes why the last to check returned unknown.
Definition Optimize.cs:380
A ParamDescrs describes a set of parameters.
A Params objects represents a configuration in the form of Symbol/value pairs.
Definition Params.cs:29
Params Add(Symbol name, bool value)
Adds a parameter setting.
Definition Params.cs:33
Objects of this class track statistical information about solvers.
Definition Statistics.cs:31
Symbols are used to name several term and type constructors.
Definition Symbol.cs:30
Internal base class for interfacing with native Z3 objects. Should not be used externally.
Definition Z3Object.cs:33
Context Context
Access Context object.
Definition Z3Object.cs:111
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58
Z3_optimize Z3_API Z3_mk_optimize(Z3_context c)
Create a new optimize context.
Status
Status values.
Definition Status.cs:29