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
93 public void AssertAndTrack(Expr<BoolSort> constraint, Expr<BoolSort> p)
94 {
95 getContext().checkContextMatch(constraint);
96 getContext().checkContextMatch(p);
97
98 Native.optimizeAssertAndTrack(getContext().nCtx(), getNativeObject(),
99 constraint.getNativeObject(), p.getNativeObject());
100 }
101
105 public static class Handle<R extends Sort> {
106
107 private final Optimize opt;
108 private final int handle;
109
110 Handle(Optimize opt, int h)
111 {
112 this.opt = opt;
113 this.handle = h;
114 }
115
119 public Expr<R> getLower()
120 {
121 return opt.GetLower(handle);
122 }
123
127 public Expr<R> getUpper()
128 {
129 return opt.GetUpper(handle);
130 }
131
140 public Expr<?>[] getUpperAsVector()
141 {
142 return opt.GetUpperAsVector(handle);
143 }
144
150 public Expr<?>[] getLowerAsVector()
151 {
152 return opt.GetLowerAsVector(handle);
153 }
154
158 public Expr<R> getValue()
159 {
160 return getLower();
161 }
162
166 @Override
167 public String toString()
168 {
169 return getValue().toString();
170 }
171 }
172
179 public Handle<?> AssertSoft(Expr<BoolSort> constraint, int weight, String group)
180 {
181 return AssertSoft(constraint, Integer.toString(weight), group);
182 }
183
190 public Handle<?> AssertSoft(Expr<BoolSort> constraint, String weight, String group)
191 {
192 getContext().checkContextMatch(constraint);
193 Symbol s = getContext().mkSymbol(group);
194 return new Handle<>(this, Native.optimizeAssertSoft(getContext().nCtx(), getNativeObject(), constraint.getNativeObject(), weight, s.getNativeObject()));
195 }
196
202 public Status Check(Expr<BoolSort>... assumptions)
203 {
204 Z3_lbool r;
205 if (assumptions == null) {
206 r = Z3_lbool.fromInt(
207 Native.optimizeCheck(
208 getContext().nCtx(),
209 getNativeObject(), 0, null));
210 }
211 else {
212 r = Z3_lbool.fromInt(
213 Native.optimizeCheck(
214 getContext().nCtx(),
215 getNativeObject(),
216 assumptions.length,
217 AST.arrayToNative(assumptions)));
218 }
219 switch (r) {
220 case Z3_L_TRUE:
221 return Status.SATISFIABLE;
222 case Z3_L_FALSE:
223 return Status.UNSATISFIABLE;
224 default:
225 return Status.UNKNOWN;
226 }
227 }
228
232 public void Push()
233 {
234 Native.optimizePush(getContext().nCtx(), getNativeObject());
235 }
236
242 public void Pop()
243 {
244 Native.optimizePop(getContext().nCtx(), getNativeObject());
245 }
246
247
255 {
256 long x = Native.optimizeGetModel(getContext().nCtx(), getNativeObject());
257 if (x == 0) {
258 return null;
259 } else {
260 return new Model(getContext(), x);
261 }
262 }
263
274 {
275 ASTVector core = new ASTVector(getContext(), Native.optimizeGetUnsatCore(getContext().nCtx(), getNativeObject()));
276 return core.ToBoolExprArray();
277 }
278
284 public <R extends Sort> Handle<R> MkMaximize(Expr<R> e)
285 {
286 return new Handle<>(this, Native.optimizeMaximize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
287 }
288
293 public <R extends Sort> Handle<R> MkMinimize(Expr<R> e)
294 {
295 return new Handle<>(this, Native.optimizeMinimize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
296 }
297
301 private <R extends Sort> Expr<R> GetLower(int index)
302 {
303 return (Expr<R>) Expr.create(getContext(), Native.optimizeGetLower(getContext().nCtx(), getNativeObject(), index));
304 }
305
309 private <R extends Sort> Expr<R> GetUpper(int index)
310 {
311 return (Expr<R>) Expr.create(getContext(), Native.optimizeGetUpper(getContext().nCtx(), getNativeObject(), index));
312 }
313
319 private Expr<?>[] GetUpperAsVector(int index) {
320 return unpackObjectiveValueVector(
321 Native.optimizeGetUpperAsVector(
322 getContext().nCtx(), getNativeObject(), index
323 )
324 );
325 }
326
332 private Expr<?>[] GetLowerAsVector(int index) {
333 return unpackObjectiveValueVector(
334 Native.optimizeGetLowerAsVector(
335 getContext().nCtx(), getNativeObject(), index
336 )
337 );
338 }
339
340 private Expr<?>[] unpackObjectiveValueVector(long nativeVec) {
341 ASTVector vec = new ASTVector(
342 getContext(), nativeVec
343 );
344 return new Expr[] {
345 (Expr<?>) vec.get(0), (Expr<?>) vec.get(1), (Expr<?>) vec.get(2)
346 };
347
348 }
349
353 public String getReasonUnknown()
354 {
355 return Native.optimizeGetReasonUnknown(getContext().nCtx(), getNativeObject());
356 }
357
361 @Override
362 public String toString()
363 {
364 return Native.optimizeToString(getContext().nCtx(), getNativeObject());
365 }
366
371 public void fromFile(String file)
372 {
373 Native.optimizeFromFile(getContext().nCtx(), getNativeObject(), file);
374 }
375
379 public void fromString(String s)
380 {
381 Native.optimizeFromString(getContext().nCtx(), getNativeObject(), s);
382 }
383
388 {
389 ASTVector assertions = new ASTVector(getContext(), Native.optimizeGetAssertions(getContext().nCtx(), getNativeObject()));
390 return assertions.ToBoolExprArray();
391 }
392
397 {
398 ASTVector objectives = new ASTVector(getContext(), Native.optimizeGetObjectives(getContext().nCtx(), getNativeObject()));
399 return objectives.ToExprArray();
400 }
401
410 public void setInitialValue(Expr<?> var, Expr<?> value)
411 {
412 getContext().checkContextMatch(var);
413 getContext().checkContextMatch(value);
414 Native.optimizeSetInitialValue(getContext().nCtx(), getNativeObject(),
415 var.getNativeObject(), value.getNativeObject());
416 }
417
422 {
423 return new Statistics(getContext(), Native.optimizeGetStatistics(getContext().nCtx(), getNativeObject()));
424 }
425
426
427 Optimize(Context ctx, long obj) throws Z3Exception
428 {
429 super(ctx, obj);
430 }
431
432 Optimize(Context ctx) throws Z3Exception
433 {
434 super(ctx, Native.mkOptimize(ctx.nCtx()));
435 }
436
437 @Override
438 void incRef() {
439 Native.optimizeIncRef(getContext().nCtx(), getNativeObject());
440 }
441
442 @Override
443 void addToReferenceQueue() {
444 getContext().getReferenceQueue().storeReference(this, OptimizeRef::new);
445 }
446
447 private static class OptimizeRef extends Z3ReferenceQueue.Reference<Optimize> {
448
449 private OptimizeRef(Optimize referent, ReferenceQueue<Z3Object> q) {
450 super(referent, q);
451 }
452
453 @Override
454 void decRef(Context ctx, long z3Obj) {
455 Native.optimizeDecRef(ctx.nCtx(), z3Obj);
456 }
457 }
458}
Status Check(Expr< BoolSort >... assumptions)
void setInitialValue(Expr<?> var, Expr<?> value)
void Assert(Expr< BoolSort >... constraints)
Definition Optimize.java:61
void AssertAndTrack(Expr< BoolSort > constraint, Expr< BoolSort > p)
Definition Optimize.java:93
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:11932