Z3
Optimize.java
Go to the documentation of this file.
1
19package com.microsoft.z3;
20
21import com.microsoft.z3.enumerations.Z3_lbool;
22
23
27@SuppressWarnings("unchecked")
28public class Optimize extends Z3Object {
29
33 public String getHelp()
34 {
35 return Native.optimizeGetHelp(getContext().nCtx(), getNativeObject());
36 }
37
43 public void setParameters(Params value)
44 {
45 Native.optimizeSetParams(getContext().nCtx(), getNativeObject(), value.getNativeObject());
46 }
47
52 {
53 return new ParamDescrs(getContext(), Native.optimizeGetParamDescrs(getContext().nCtx(), getNativeObject()));
54 }
55
59 public void Assert(Expr<BoolSort> ... constraints)
60 {
61 getContext().checkContextMatch(constraints);
62 for (Expr<BoolSort> a : constraints)
63 {
64 Native.optimizeAssert(getContext().nCtx(), getNativeObject(), a.getNativeObject());
65 }
66 }
67
71 public void Add(Expr<BoolSort> ... constraints)
72 {
73 Assert(constraints);
74 }
75
89 public void AssertAndTrack(Expr<BoolSort> constraint, Expr<BoolSort> p)
90 {
91 getContext().checkContextMatch(constraint);
92 getContext().checkContextMatch(p);
93
94 Native.optimizeAssertAndTrack(getContext().nCtx(), getNativeObject(),
95 constraint.getNativeObject(), p.getNativeObject());
96 }
97
101 public static class Handle<R extends Sort> {
102
103 private final Optimize opt;
104 private final int handle;
105
106 Handle(Optimize opt, int h)
107 {
108 this.opt = opt;
109 this.handle = h;
110 }
111
115 public Expr<R> getLower()
116 {
117 return opt.GetLower(handle);
118 }
119
123 public Expr<R> getUpper()
124 {
125 return opt.GetUpper(handle);
126 }
127
136 public Expr<?>[] getUpperAsVector()
137 {
138 return opt.GetUpperAsVector(handle);
139 }
140
146 public Expr<?>[] getLowerAsVector()
147 {
148 return opt.GetLowerAsVector(handle);
149 }
150
154 public Expr<R> getValue()
155 {
156 return getLower();
157 }
158
162 @Override
163 public String toString()
164 {
165 return getValue().toString();
166 }
167 }
168
175 public Handle<?> AssertSoft(Expr<BoolSort> constraint, int weight, String group)
176 {
177 return AssertSoft(constraint, Integer.toString(weight), group);
178 }
179
186 public Handle<?> AssertSoft(Expr<BoolSort> constraint, String weight, String group)
187 {
188 getContext().checkContextMatch(constraint);
189 Symbol s = getContext().mkSymbol(group);
190 return new Handle<>(this, Native.optimizeAssertSoft(getContext().nCtx(), getNativeObject(), constraint.getNativeObject(), weight, s.getNativeObject()));
191 }
192
198 public Status Check(Expr<BoolSort>... assumptions)
199 {
200 Z3_lbool r;
201 if (assumptions == null) {
202 r = Z3_lbool.fromInt(
203 Native.optimizeCheck(
204 getContext().nCtx(),
205 getNativeObject(), 0, null));
206 }
207 else {
208 r = Z3_lbool.fromInt(
209 Native.optimizeCheck(
210 getContext().nCtx(),
211 getNativeObject(),
212 assumptions.length,
213 AST.arrayToNative(assumptions)));
214 }
215 switch (r) {
216 case Z3_L_TRUE:
217 return Status.SATISFIABLE;
218 case Z3_L_FALSE:
219 return Status.UNSATISFIABLE;
220 default:
221 return Status.UNKNOWN;
222 }
223 }
224
228 public void Push()
229 {
230 Native.optimizePush(getContext().nCtx(), getNativeObject());
231 }
232
238 public void Pop()
239 {
240 Native.optimizePop(getContext().nCtx(), getNativeObject());
241 }
242
243
251 {
252 long x = Native.optimizeGetModel(getContext().nCtx(), getNativeObject());
253 if (x == 0) {
254 return null;
255 } else {
256 return new Model(getContext(), x);
257 }
258 }
259
270 {
271 ASTVector core = new ASTVector(getContext(), Native.optimizeGetUnsatCore(getContext().nCtx(), getNativeObject()));
272 return core.ToBoolExprArray();
273 }
274
280 public <R extends Sort> Handle<R> MkMaximize(Expr<R> e)
281 {
282 return new Handle<>(this, Native.optimizeMaximize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
283 }
284
289 public <R extends Sort> Handle<R> MkMinimize(Expr<R> e)
290 {
291 return new Handle<>(this, Native.optimizeMinimize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
292 }
293
297 private <R extends Sort> Expr<R> GetLower(int index)
298 {
299 return (Expr<R>) Expr.create(getContext(), Native.optimizeGetLower(getContext().nCtx(), getNativeObject(), index));
300 }
301
305 private <R extends Sort> Expr<R> GetUpper(int index)
306 {
307 return (Expr<R>) Expr.create(getContext(), Native.optimizeGetUpper(getContext().nCtx(), getNativeObject(), index));
308 }
309
315 private Expr<?>[] GetUpperAsVector(int index) {
316 return unpackObjectiveValueVector(
317 Native.optimizeGetUpperAsVector(
318 getContext().nCtx(), getNativeObject(), index
319 )
320 );
321 }
322
328 private Expr<?>[] GetLowerAsVector(int index) {
329 return unpackObjectiveValueVector(
330 Native.optimizeGetLowerAsVector(
331 getContext().nCtx(), getNativeObject(), index
332 )
333 );
334 }
335
336 private Expr<?>[] unpackObjectiveValueVector(long nativeVec) {
337 ASTVector vec = new ASTVector(
338 getContext(), nativeVec
339 );
340 return new Expr[] {
341 (Expr<?>) vec.get(0), (Expr<?>) vec.get(1), (Expr<?>) vec.get(2)
342 };
343
344 }
345
350 {
351 return Native.optimizeGetReasonUnknown(getContext().nCtx(), getNativeObject());
352 }
353
357 @Override
359 {
360 return Native.optimizeToString(getContext().nCtx(), getNativeObject());
361 }
362
367 public void fromFile(String file)
368 {
369 Native.optimizeFromFile(getContext().nCtx(), getNativeObject(), file);
370 }
371
375 public void fromString(String s)
376 {
377 Native.optimizeFromString(getContext().nCtx(), getNativeObject(), s);
378 }
379
384 {
385 ASTVector assertions = new ASTVector(getContext(), Native.optimizeGetAssertions(getContext().nCtx(), getNativeObject()));
386 return assertions.ToBoolExprArray();
387 }
388
393 {
394 ASTVector objectives = new ASTVector(getContext(), Native.optimizeGetObjectives(getContext().nCtx(), getNativeObject()));
395 return objectives.ToExprArray();
396 }
397
402 {
403 return new Statistics(getContext(), Native.optimizeGetStatistics(getContext().nCtx(), getNativeObject()));
404 }
405
406
407 Optimize(Context ctx, long obj) throws Z3Exception
408 {
409 super(ctx, obj);
410 }
411
412 Optimize(Context ctx) throws Z3Exception
413 {
414 super(ctx, Native.mkOptimize(ctx.nCtx()));
415 }
416
417 @Override
418 void incRef() {
419 Native.optimizeIncRef(getContext().nCtx(), getNativeObject());
420 }
421
422 @Override
423 void addToReferenceQueue() {
424 getContext().getOptimizeDRQ().storeReference(getContext(), this);
425 }
426}
BoolExpr[] ToBoolExprArray()
Definition: ASTVector.java:140
String toString()
Definition: Expr.java:208
Status Check(Expr< BoolSort >... assumptions)
Definition: Optimize.java:198
void AssertAndTrack(Expr< BoolSort > constraint, Expr< BoolSort > p)
Definition: Optimize.java:89
ParamDescrs getParameterDescriptions()
Definition: Optimize.java:51
void fromString(String s)
Definition: Optimize.java:375
void setParameters(Params value)
Definition: Optimize.java:43
BoolExpr[] getUnsatCore()
Definition: Optimize.java:269
Handle<?> AssertSoft(Expr< BoolSort > constraint, int weight, String group)
Definition: Optimize.java:175
void Assert(Expr< BoolSort > ... constraints)
Definition: Optimize.java:59
BoolExpr[] getAssertions()
Definition: Optimize.java:383
void Add(Expr< BoolSort > ... constraints)
Definition: Optimize.java:71
void fromFile(String file)
Definition: Optimize.java:367
Handle<?> AssertSoft(Expr< BoolSort > constraint, String weight, String group)
Definition: Optimize.java:186
Statistics getStatistics()
Definition: Optimize.java:401
static long[] arrayToNative(Z3Object[] a)
Definition: Z3Object.java:73
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition: z3_api.h:60
@ Z3_L_TRUE
Definition: z3_api.h:63
@ Z3_L_FALSE
Definition: z3_api.h:61
def String(name, ctx=None)
Definition: z3py.py:10903
def Model(ctx=None)
Definition: z3py.py:6680