Z3
 
Loading...
Searching...
No Matches
Optimize.java
Go to the documentation of this file.
1
19package com.microsoft.z3;
20
21import com.microsoft.z3.enumerations.Z3_lbool;
22
23import java.lang.ref.ReferenceQueue;
24
25
29@SuppressWarnings("unchecked")
30public class Optimize extends Z3Object {
31
35 public String getHelp()
36 {
37 return Native.optimizeGetHelp(getContext().nCtx(), getNativeObject());
38 }
39
45 public void setParameters(Params value)
46 {
47 Native.optimizeSetParams(getContext().nCtx(), getNativeObject(), value.getNativeObject());
48 }
49
54 {
55 return new ParamDescrs(getContext(), Native.optimizeGetParamDescrs(getContext().nCtx(), getNativeObject()));
56 }
57
61 public void Assert(Expr<BoolSort>... constraints)
62 {
63 getContext().checkContextMatch(constraints);
64 for (Expr<BoolSort> a : constraints)
65 {
66 Native.optimizeAssert(getContext().nCtx(), getNativeObject(), a.getNativeObject());
67 }
68 }
69
73 public void Add(Expr<BoolSort>... constraints)
74 {
75 Assert(constraints);
76 }
77
91 public void AssertAndTrack(Expr<BoolSort> constraint, Expr<BoolSort> p)
92 {
93 getContext().checkContextMatch(constraint);
94 getContext().checkContextMatch(p);
95
96 Native.optimizeAssertAndTrack(getContext().nCtx(), getNativeObject(),
97 constraint.getNativeObject(), p.getNativeObject());
98 }
99
103 public static class Handle<R extends Sort> {
104
105 private final Optimize opt;
106 private final int handle;
107
108 Handle(Optimize opt, int h)
109 {
110 this.opt = opt;
111 this.handle = h;
112 }
113
117 public Expr<R> getLower()
118 {
119 return opt.GetLower(handle);
120 }
121
125 public Expr<R> getUpper()
126 {
127 return opt.GetUpper(handle);
128 }
129
138 public Expr<?>[] getUpperAsVector()
139 {
140 return opt.GetUpperAsVector(handle);
141 }
142
148 public Expr<?>[] getLowerAsVector()
149 {
150 return opt.GetLowerAsVector(handle);
151 }
152
156 public Expr<R> getValue()
157 {
158 return getLower();
159 }
160
164 @Override
165 public String toString()
166 {
167 return getValue().toString();
168 }
169 }
170
177 public Handle<?> AssertSoft(Expr<BoolSort> constraint, int weight, String group)
178 {
179 return AssertSoft(constraint, Integer.toString(weight), group);
180 }
181
188 public Handle<?> AssertSoft(Expr<BoolSort> constraint, String weight, String group)
189 {
190 getContext().checkContextMatch(constraint);
191 Symbol s = getContext().mkSymbol(group);
192 return new Handle<>(this, Native.optimizeAssertSoft(getContext().nCtx(), getNativeObject(), constraint.getNativeObject(), weight, s.getNativeObject()));
193 }
194
200 public Status Check(Expr<BoolSort>... assumptions)
201 {
202 Z3_lbool r;
203 if (assumptions == null) {
204 r = Z3_lbool.fromInt(
205 Native.optimizeCheck(
206 getContext().nCtx(),
207 getNativeObject(), 0, null));
208 }
209 else {
210 r = Z3_lbool.fromInt(
211 Native.optimizeCheck(
212 getContext().nCtx(),
213 getNativeObject(),
214 assumptions.length,
215 AST.arrayToNative(assumptions)));
216 }
217 switch (r) {
218 case Z3_L_TRUE:
219 return Status.SATISFIABLE;
220 case Z3_L_FALSE:
221 return Status.UNSATISFIABLE;
222 default:
223 return Status.UNKNOWN;
224 }
225 }
226
230 public void Push()
231 {
232 Native.optimizePush(getContext().nCtx(), getNativeObject());
233 }
234
240 public void Pop()
241 {
242 Native.optimizePop(getContext().nCtx(), getNativeObject());
243 }
244
245
253 {
254 long x = Native.optimizeGetModel(getContext().nCtx(), getNativeObject());
255 if (x == 0) {
256 return null;
257 } else {
258 return new Model(getContext(), x);
259 }
260 }
261
272 {
273 ASTVector core = new ASTVector(getContext(), Native.optimizeGetUnsatCore(getContext().nCtx(), getNativeObject()));
274 return core.ToBoolExprArray();
275 }
276
282 public <R extends Sort> Handle<R> MkMaximize(Expr<R> e)
283 {
284 return new Handle<>(this, Native.optimizeMaximize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
285 }
286
291 public <R extends Sort> Handle<R> MkMinimize(Expr<R> e)
292 {
293 return new Handle<>(this, Native.optimizeMinimize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
294 }
295
299 private <R extends Sort> Expr<R> GetLower(int index)
300 {
301 return (Expr<R>) Expr.create(getContext(), Native.optimizeGetLower(getContext().nCtx(), getNativeObject(), index));
302 }
303
307 private <R extends Sort> Expr<R> GetUpper(int index)
308 {
309 return (Expr<R>) Expr.create(getContext(), Native.optimizeGetUpper(getContext().nCtx(), getNativeObject(), index));
310 }
311
317 private Expr<?>[] GetUpperAsVector(int index) {
318 return unpackObjectiveValueVector(
319 Native.optimizeGetUpperAsVector(
320 getContext().nCtx(), getNativeObject(), index
321 )
322 );
323 }
324
330 private Expr<?>[] GetLowerAsVector(int index) {
331 return unpackObjectiveValueVector(
332 Native.optimizeGetLowerAsVector(
333 getContext().nCtx(), getNativeObject(), index
334 )
335 );
336 }
337
338 private Expr<?>[] unpackObjectiveValueVector(long nativeVec) {
339 ASTVector vec = new ASTVector(
340 getContext(), nativeVec
341 );
342 return new Expr[] {
343 (Expr<?>) vec.get(0), (Expr<?>) vec.get(1), (Expr<?>) vec.get(2)
344 };
345
346 }
347
351 public String getReasonUnknown()
352 {
353 return Native.optimizeGetReasonUnknown(getContext().nCtx(), getNativeObject());
354 }
355
359 @Override
360 public String toString()
361 {
362 return Native.optimizeToString(getContext().nCtx(), getNativeObject());
363 }
364
369 public void fromFile(String file)
370 {
371 Native.optimizeFromFile(getContext().nCtx(), getNativeObject(), file);
372 }
373
377 public void fromString(String s)
378 {
379 Native.optimizeFromString(getContext().nCtx(), getNativeObject(), s);
380 }
381
386 {
387 ASTVector assertions = new ASTVector(getContext(), Native.optimizeGetAssertions(getContext().nCtx(), getNativeObject()));
388 return assertions.ToBoolExprArray();
389 }
390
395 {
396 ASTVector objectives = new ASTVector(getContext(), Native.optimizeGetObjectives(getContext().nCtx(), getNativeObject()));
397 return objectives.ToExprArray();
398 }
399
404 {
405 return new Statistics(getContext(), Native.optimizeGetStatistics(getContext().nCtx(), getNativeObject()));
406 }
407
408
409 Optimize(Context ctx, long obj) throws Z3Exception
410 {
411 super(ctx, obj);
412 }
413
414 Optimize(Context ctx) throws Z3Exception
415 {
416 super(ctx, Native.mkOptimize(ctx.nCtx()));
417 }
418
419 @Override
420 void incRef() {
421 Native.optimizeIncRef(getContext().nCtx(), getNativeObject());
422 }
423
424 @Override
425 void addToReferenceQueue() {
426 getContext().getReferenceQueue().storeReference(this, OptimizeRef::new);
427 }
428
429 private static class OptimizeRef extends Z3ReferenceQueue.Reference<Optimize> {
430
431 private OptimizeRef(Optimize referent, ReferenceQueue<Z3Object> q) {
432 super(referent, q);
433 }
434
435 @Override
436 void decRef(Context ctx, long z3Obj) {
437 Native.optimizeDecRef(ctx.nCtx(), z3Obj);
438 }
439 }
440}
Status Check(Expr< BoolSort >... assumptions)
void Assert(Expr< BoolSort >... constraints)
Definition Optimize.java:61
void AssertAndTrack(Expr< BoolSort > constraint, Expr< BoolSort > p)
Definition Optimize.java:91
ParamDescrs getParameterDescriptions()
Definition Optimize.java:53
void fromString(String s)
void setParameters(Params value)
Definition Optimize.java:45
Handle<?> AssertSoft(Expr< BoolSort > constraint, int weight, String group)
void Add(Expr< BoolSort >... constraints)
Definition Optimize.java:73
void fromFile(String file)
Handle<?> AssertSoft(Expr< BoolSort > constraint, String weight, String group)
static long[] arrayToNative(Z3Object[] a)
Definition Z3Object.java:73
static Status fromInt(int v)
Definition Status.java:41
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58
@ Z3_L_TRUE
Definition z3_api.h:61
@ Z3_L_FALSE
Definition z3_api.h:59
String(name, ctx=None)
Definition z3py.py:11228