Z3
 
Loading...
Searching...
No Matches
Data Structures | Public Member Functions | Properties
Optimize Class Reference

Object for managing optimization context. More...

+ Inheritance diagram for Optimize:

Data Structures

class  Handle
 Handle to objectives returned by objective functions. More...
 

Public Member Functions

void Set (string name, bool value)
 Sets parameter on the optimize solver.
 
void Set (string name, uint value)
 Sets parameter on the optimize solver.
 
void Set (string name, double value)
 Sets parameter on the optimize solver.
 
void Set (string name, string value)
 Sets parameter on the optimize solver.
 
void Set (string name, Symbol value)
 Sets parameter on the optimize solver.
 
void Set (Symbol name, bool value)
 Sets parameter on the optimize solver.
 
void Set (Symbol name, uint value)
 Sets parameter on the optimize solver.
 
void Set (Symbol name, double value)
 Sets parameter on the optimize solver.
 
void Set (Symbol name, string value)
 Sets parameter on the optimize solver.
 
void Set (Symbol name, Symbol value)
 Sets parameter on the optimize solver.
 
void Assert (params BoolExpr[] constraints)
 Assert a constraint (or multiple) into the optimize solver.
 
void Assert (IEnumerable< BoolExpr > constraints)
 Assert a constraint (or multiple) into the optimize solver.
 
void Add (params BoolExpr[] constraints)
 Alias for Assert.
 
void Add (IEnumerable< BoolExpr > constraints)
 Alias for Assert.
 
void AssertAndTrack (BoolExpr constraint, BoolExpr p)
 Assert a constraint into the optimize solver, and track it (in the unsat) core using the Boolean constant p.
 
Handle AssertSoft (BoolExpr constraint, uint weight, string group)
 Assert soft constraint.
 
Status Check (params Expr[] assumptions)
 Check satisfiability of asserted constraints. Produce a model that (when the objectives are bounded and don't use strict inequalities) is optimal.
 
void Push ()
 Creates a backtracking point.
 
void Pop ()
 Backtrack one backtracking point.
 
Handle MkMaximize (Expr e)
 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.
 
Handle MkMinimize (Expr e)
 Declare an arithmetical minimization objective. Similar to MkMaximize.
 
override string ToString ()
 Print the context to a string (SMT-LIB parseable benchmark).
 
void FromFile (string file)
 Parse an SMT-LIB2 file with optimization objectives and constraints. The parsed constraints and objectives are added to the optimization context.
 
void FromString (string s)
 Similar to FromFile. Instead it takes as argument a string.
 
- Public Member Functions inherited from Z3Object
void Dispose ()
 Disposes of the underlying native Z3 object.
 

Properties

string Help [get]
 A string that describes all available optimize solver parameters.
 
Params Parameters [set]
 Sets the optimize solver parameters.
 
ParamDescrs ParameterDescriptions [get]
 Retrieves parameter descriptions for Optimize solver.
 
Model Model [get]
 The model of the last Check.
 
BoolExpr[] UnsatCore [get]
 The unsat core of the last Check.
 
String ReasonUnknown [get]
 Return a string the describes why the last to check returned unknown.
 
BoolExpr[] Assertions [get]
 The set of asserted formulas.
 
Expr[] Objectives [get]
 The set of asserted formulas.
 
Statistics Statistics [get]
 Optimize statistics.
 
- Properties inherited from Z3Object
Context Context [get]
 Access Context object.
 

Detailed Description

Object for managing optimization context.

Definition at line 30 of file Optimize.cs.

Member Function Documentation

◆ Add() [1/2]

void Add ( IEnumerable< BoolExpr constraints)
inline

Alias for Assert.


Definition at line 132 of file Optimize.cs.

133 {
134 AddConstraints(constraints);
135 }

◆ Add() [2/2]

void Add ( params BoolExpr[]  constraints)
inline

Alias for Assert.


Definition at line 124 of file Optimize.cs.

125 {
126 AddConstraints(constraints);
127 }

◆ Assert() [1/2]

void Assert ( IEnumerable< BoolExpr constraints)
inline

Assert a constraint (or multiple) into the optimize solver.


Definition at line 116 of file Optimize.cs.

117 {
118 AddConstraints(constraints);
119 }

◆ Assert() [2/2]

void Assert ( params BoolExpr[]  constraints)
inline

Assert a constraint (or multiple) into the optimize solver.


Definition at line 108 of file Optimize.cs.

109 {
110 AddConstraints(constraints);
111 }

◆ AssertAndTrack()

void AssertAndTrack ( BoolExpr  constraint,
BoolExpr  p 
)
inline

Assert a constraint into the optimize solver, and track it (in the unsat) core using the Boolean constant p.

This API is an alternative to Check(Expr[]) with assumptions for extracting unsat cores. Both APIs can be used in the same solver. The unsat core will contain a combination of the Boolean variables provided using AssertAndTrack(BoolExpr,BoolExpr) and the Boolean literals provided using Check(Expr[]) with assumptions.


Definition at line 163 of file Optimize.cs.

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 }
Context Context
Access Context object.
Definition Z3Object.cs:111

◆ AssertSoft()

Handle AssertSoft ( BoolExpr  constraint,
uint  weight,
string  group 
)
inline

Assert soft constraint.


Return an objective which associates with the group of constraints.

Definition at line 234 of file Optimize.cs.

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 }
IntSymbol MkSymbol(int i)
Creates a new symbol using an integer.
Definition Context.cs:111

◆ Check()

Status Check ( params Expr[]  assumptions)
inline

Check satisfiability of asserted constraints. Produce a model that (when the objectives are bounded and don't use strict inequalities) is optimal.

Definition at line 248 of file Optimize.cs.

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 }
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58
Status
Status values.
Definition Status.cs:29

◆ FromFile()

void FromFile ( string  file)
inline

Parse an SMT-LIB2 file with optimization objectives and constraints. The parsed constraints and objectives are added to the optimization context.


Definition at line 400 of file Optimize.cs.

401 {
402 Native.Z3_optimize_from_file(Context.nCtx, NativeObject, file);
403 }

◆ FromString()

void FromString ( string  s)
inline

Similar to FromFile. Instead it takes as argument a string.

Definition at line 408 of file Optimize.cs.

409 {
410 Native.Z3_optimize_from_string(Context.nCtx, NativeObject, s);
411 }

◆ MkMaximize()

Handle MkMaximize ( Expr  e)
inline

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.


Definition at line 325 of file Optimize.cs.

326 {
327 return new Handle(this, Native.Z3_optimize_maximize(Context.nCtx, NativeObject, e.NativeObject));
328 }

◆ MkMinimize()

Handle MkMinimize ( Expr  e)
inline

Declare an arithmetical minimization objective. Similar to MkMaximize.


Definition at line 334 of file Optimize.cs.

335 {
336 return new Handle(this, Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject));
337 }

◆ Pop()

void Pop ( )
inline

Backtrack one backtracking point.

Note that an exception is thrown if Pop is called without a corresponding Push

See also
Push

Definition at line 276 of file Optimize.cs.

277 {
278 Native.Z3_optimize_pop(Context.nCtx, NativeObject);
279 }

◆ Push()

void Push ( )
inline

Creates a backtracking point.

See also
Pop

Definition at line 266 of file Optimize.cs.

267 {
268 Native.Z3_optimize_push(Context.nCtx, NativeObject);
269 }

◆ Set() [1/10]

void Set ( string  name,
bool  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 59 of file Optimize.cs.

59{ Parameters = Context.MkParams().Add(name, value); }
Params MkParams()
Creates a new ParameterSet.
Definition Context.cs:3465
Params Parameters
Sets the optimize solver parameters.
Definition Optimize.cs:47
Params Add(Symbol name, bool value)
Adds a parameter setting.
Definition Params.cs:33

◆ Set() [2/10]

void Set ( string  name,
double  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 67 of file Optimize.cs.

67{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [3/10]

void Set ( string  name,
string  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 71 of file Optimize.cs.

71{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [4/10]

void Set ( string  name,
Symbol  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 75 of file Optimize.cs.

75{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [5/10]

void Set ( string  name,
uint  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 63 of file Optimize.cs.

63{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [6/10]

void Set ( Symbol  name,
bool  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 79 of file Optimize.cs.

79{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [7/10]

void Set ( Symbol  name,
double  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 87 of file Optimize.cs.

87{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [8/10]

void Set ( Symbol  name,
string  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 91 of file Optimize.cs.

91{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [9/10]

void Set ( Symbol  name,
Symbol  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 95 of file Optimize.cs.

95{ Parameters = Context.MkParams().Add(name, value); }

◆ Set() [10/10]

void Set ( Symbol  name,
uint  value 
)
inline

Sets parameter on the optimize solver.

Definition at line 83 of file Optimize.cs.

83{ Parameters = Context.MkParams().Add(name, value); }

◆ ToString()

override string ToString ( )
inline

Print the context to a string (SMT-LIB parseable benchmark).


Definition at line 391 of file Optimize.cs.

392 {
393 return Native.Z3_optimize_to_string(Context.nCtx, NativeObject);
394 }

Property Documentation

◆ Assertions

BoolExpr [] Assertions
get

The set of asserted formulas.

Definition at line 416 of file Optimize.cs.

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 }

◆ Help

string Help
get

A string that describes all available optimize solver parameters.

Definition at line 35 of file Optimize.cs.

36 {
37 get
38 {
39 return Native.Z3_optimize_get_help(Context.nCtx, NativeObject);
40 }
41 }

◆ Model

Model Model
get

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.

Definition at line 289 of file Optimize.cs.

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 }
Model Model
The model of the last Check.
Definition Optimize.cs:290

◆ Objectives

Expr [] Objectives
get

The set of asserted formulas.

Definition at line 429 of file Optimize.cs.

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 }

◆ ParameterDescriptions

ParamDescrs ParameterDescriptions
get

Retrieves parameter descriptions for Optimize solver.

Definition at line 100 of file Optimize.cs.

101 {
102 get { return new ParamDescrs(Context, Native.Z3_optimize_get_param_descrs(Context.nCtx, NativeObject)); }
103 }

◆ Parameters

Params Parameters
set

Sets the optimize solver parameters.

Definition at line 46 of file Optimize.cs.

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 }

Referenced by Optimize.Set(), Optimize.Set(), Optimize.Set(), Optimize.Set(), Optimize.Set(), Optimize.Set(), Optimize.Set(), Optimize.Set(), Optimize.Set(), and Optimize.Set().

◆ ReasonUnknown

String ReasonUnknown
get

Return a string the describes why the last to check returned unknown.


Definition at line 379 of file Optimize.cs.

380 {
381 get
382 {
383 return Native.Z3_optimize_get_reason_unknown(Context.nCtx, NativeObject);
384 }
385 }

◆ Statistics

Optimize statistics.

Definition at line 443 of file Optimize.cs.

444 {
445 get
446 {
447
448 return new Statistics(Context, Native.Z3_optimize_get_statistics(Context.nCtx, NativeObject));
449 }
450 }
Statistics Statistics
Optimize statistics.
Definition Optimize.cs:444

◆ UnsatCore

BoolExpr [] UnsatCore
get

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.

Definition at line 309 of file Optimize.cs.

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 }