public void Run() { using (Context ctx = new Context()) { BoolExpr p = ctx.MkBoolConst("p"); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkNot(p)); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1))); Console.WriteLine(ctx.MkAdd(ctx.MkInt(1), x)); Console.WriteLine(ctx.MkAdd(x, y)); Console.WriteLine(ctx.MkMul(ctx.MkInt(2), x)); Console.WriteLine(ctx.MkMul(x, ctx.MkInt(2))); Console.WriteLine(ctx.MkMul(x, y)); Console.WriteLine(ctx.MkDiv(x, y)); Console.WriteLine(ctx.MkMod(x, y)); Console.WriteLine(ctx.MkEq(x, y)); Console.WriteLine(ctx.MkDistinct(x, y, x)); Console.WriteLine(ctx.MkNot(ctx.MkEq(x, y))); Console.WriteLine(ctx.MkEq(x, y)); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1))); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1))); BoolExpr q = ctx.MkBoolConst("q"); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkAnd(p, q)); Console.WriteLine(ctx.MkAnd(p, q)); Console.WriteLine(ctx.MkEq(x, y)); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { IntExpr x = ctx.MkIntConst("x"); FuncDecl x_d = x.FuncDecl; Console.WriteLine("is_expr(x_d): " + x_d.IsExpr); Console.WriteLine("is_func_decl(x_d): " + x_d.IsFuncDecl); Console.WriteLine("x_d.Name: " + x_d.Name); Console.WriteLine("x_d.Range: " + x_d.Range); Console.WriteLine("x_d.Arity: " + x_d.Arity); Console.WriteLine("x_d() == x: " + (x_d.Apply() == x)); FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.RealSort }, ctx.BoolSort); Console.WriteLine("f.Name: " + f.Name); Console.WriteLine("f.Range: " + f.Range); Console.WriteLine("f.Arity: " + f.Arity); for (uint i = 0; i < f.Arity; i++) { Console.WriteLine("domain(" + i + "): " + f.Domain[i]); } Console.WriteLine(f[x, ctx.MkInt2Real(x)]); Console.WriteLine(f[x, ctx.MkInt2Real(x)].FuncDecl == f); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { }; using (Context ctx = new Context(cfg)) { FuncDecl f = ctx.MkFuncDecl("f", ctx.IntSort, ctx.IntSort); FuncDecl g = ctx.MkFuncDecl("g", ctx.IntSort, ctx.IntSort); IntExpr a = ctx.MkIntConst("a"); IntExpr b = ctx.MkIntConst("b"); IntExpr c = ctx.MkIntConst("c"); IntExpr x = ctx.MkIntConst("x"); Solver s = ctx.MkSolver(); Params p = ctx.MkParams(); p.Add("AUTO_CONFIG", false); p.Add("MBQI", false); s.Parameters = p; s.Assert(ctx.MkForall(new Expr[] { x }, ctx.MkEq(f[g[x]], x), 1, new Pattern[] { ctx.MkPattern(f[g[x]]) })); s.Assert(ctx.MkEq(g[a], c)); s.Assert(ctx.MkEq(g[b], c)); s.Assert(ctx.MkDistinct(a, b)); Console.WriteLine(s); Console.WriteLine(s.Check()); } }
public void NewSearchTest() { Solver solver = new Google.OrTools.ConstraintSolver.Solver("p"); // creating dummy variables List <IntVar> vars = new List <IntVar>(); for (int i = 0; i < 100000; i++) { vars.Add(solver.MakeIntVar(0, 1)); } IntExpr globalSum = solver.MakeSum(vars.ToArray()); DecisionBuilder db = solver.MakePhase(vars.ToArray(), Google.OrTools.ConstraintSolver.Solver.INT_VAR_SIMPLE, Google.OrTools.ConstraintSolver.Solver.INT_VALUE_SIMPLE); solver.NewSearch(db, new OptimizeVar(solver, true, globalSum.Var(), 100)); // force Garbage Collector GC.Collect(); GC.WaitForPendingFinalizers(); // Try to read all solutions int count = 0; while (solver.NextSolution()) { count++; // Console.WriteLine("solution " + globalSum.Var().Value()); } Console.WriteLine("Solutions: " + count); }
public static void Example1(Context ctx) { Console.WriteLine("Example 1:"); Sort[] types = new Sort[3]; IntExpr[] xs = new IntExpr[3]; Symbol[] names = new Symbol[3]; IntExpr[] vars = new IntExpr[3]; for (uint j = 0; j < 3; j++) { types[j] = ctx.IntSort; names[j] = ctx.MkSymbol(String.Format("x_{0}", j)); xs[j] = (IntExpr)ctx.MkConst(names[j], types[j]); vars[j] = (IntExpr)ctx.MkBound(2 - j, types[j]); // reversed } Expr body_vars = ctx.MkAnd(ctx.MkGe(ctx.MkAdd(vars[0], ctx.MkInt(1)), ctx.MkInt(2)), ctx.MkLe(ctx.MkAdd(-vars[1], ctx.MkInt(2)), ctx.MkAdd(vars[2], ctx.MkInt(3)))); Expr x = ctx.MkForall(types, names, body_vars, 1, null, null); Console.WriteLine("Formula: " + x.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); PrintResult(q, s); }
public static void Example4(Context ctx) { Console.WriteLine("Example 4:"); //types Sort[] types = new Sort[1]; types[0] = ctx.IntSort; //names Symbol[] names = new Symbol[2]; names[0] = ctx.MkSymbol("x"); names[1] = ctx.MkSymbol("y"); IntExpr[] xs = new IntExpr[2]; xs[0] = (IntExpr)ctx.MkConst(names[0], types[0]); xs[1] = (IntExpr)ctx.MkConst(names[1], types[0]); var expr = ctx.MkEq(xs[0], xs[1]); var exists = ctx.MkExists(types, new[] { names[1] }, expr); var any = ctx.MkForall(types, new[] { names[0] }, exists); Console.WriteLine("Formula: " + any.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); PrintResult(q, s); }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BoolExpr p1 = ctx.MkBoolConst("p1"); BoolExpr p2 = ctx.MkBoolConst("p2"); BoolExpr p3 = ctx.MkBoolConst("p3"); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkImplies(p1, ctx.MkGt(x, ctx.MkInt(10))), ctx.MkImplies(p1, ctx.MkGt(y, x)), ctx.MkImplies(p2, ctx.MkLt(y, ctx.MkInt(5))), ctx.MkImplies(p3, ctx.MkGt(y, ctx.MkInt(0)))); Console.WriteLine(s); Console.WriteLine(s.Check(p1, p2, p3)); Console.WriteLine("Core: "); foreach (Expr e in s.UnsatCore) { Console.WriteLine(e); } Console.WriteLine(s.Check(p1, p3)); Console.WriteLine(s.Model); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { IntExpr dog = ctx.MkIntConst("dog"); IntExpr cat = ctx.MkIntConst("cat"); IntExpr mouse = ctx.MkIntConst("mouse"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkGe(dog, ctx.MkInt(1))); s.Assert(ctx.MkGe(cat, ctx.MkInt(1))); s.Assert(ctx.MkGe(mouse, ctx.MkInt(1))); s.Assert(ctx.MkEq(ctx.MkAdd(dog, cat, mouse), ctx.MkInt(100))); s.Assert(ctx.MkEq(ctx.MkAdd(ctx.MkMul(ctx.MkInt(1500), dog), ctx.MkMul(ctx.MkInt(100), cat), ctx.MkMul(ctx.MkInt(25), mouse)), ctx.MkInt(10000))); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); } }
private Constraint CreateConstraintBy(OperatorType operatorType, IntExpr lhsExpr, int rhs) { switch (operatorType) { case OperatorType.Equals: return(this.solver.MakeEquality(lhsExpr, rhs)); case OperatorType.NotEqual: return(this.solver.MakeNonEquality(lhsExpr, rhs)); case OperatorType.GreaterThanOrEqual: return(this.solver.MakeGreaterOrEqual(lhsExpr, rhs)); case OperatorType.LessThanOrEqual: return(this.solver.MakeLessOrEqual(lhsExpr, rhs)); case OperatorType.Greater: return(this.solver.MakeGreater(lhsExpr, rhs)); case OperatorType.Less: return(this.solver.MakeLess(lhsExpr, rhs)); default: throw new NotImplementedException("Not sure how to represent this operator type."); } }
public void ConstraintAndIntVar() { Solver solver = new Solver("Solver"); IntVar x = solver.MakeIntVar(3, 13, "x"); Assert.Equal(3, x.Min()); Assert.Equal(13, x.Max()); Constraint c1 = x == 7; Assert.Equal("(x(3..13) == 7)", c1.ToString()); IntVar y = solver.MakeIntVar(5, 17, "y"); Assert.Equal(5, y.Min()); Assert.Equal(17, y.Max()); // Arithmetic operator with IntVar IntExpr e3a = c1 + y; Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3a.ToString()); IntExpr e3b = y + c1; Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3b.ToString()); IntExpr e3c = c1 - y; Assert.Equal("(Watch<x == 7>(0 .. 1) - y(5..17))", e3c.ToString()); IntExpr e3d = y - c1; Assert.Equal("(y(5..17) - Watch<x == 7>(0 .. 1))", e3d.ToString()); IntExpr e3e = c1 * y; Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3e.ToString()); IntExpr e3f = y * c1; Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3f.ToString()); // Relational operator with an IntVar Constraint c9a = c1 == y; Assert.Equal("Watch<x == 7>(0 .. 1) == y(5..17)", c9a.ToString()); Constraint c9b = y == c1; Assert.Equal("y(5..17) == Watch<x == 7>(0 .. 1)", c9b.ToString()); Constraint c9c = c1 != y; Assert.Equal("Watch<x == 7>(0 .. 1) != y(5..17)", c9c.ToString()); Constraint c9d = y != c1; Assert.Equal("y(5..17) != Watch<x == 7>(0 .. 1)", c9d.ToString()); Constraint c9e = c1 >= y; Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9e.ToString()); Constraint c9f = y >= c1; Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9f.ToString()); Constraint c9g = c1 > y; Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9g.ToString()); Constraint c9h = y > c1; Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9h.ToString()); Constraint c9i = c1 <= y; Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9i.ToString()); Constraint c9j = y <= c1; Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9j.ToString()); Constraint c9k = c1 < y; Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9k.ToString()); Constraint c9l = y < c1; Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9l.ToString()); }
public void Downcast() { Solver solver = new Solver("Solver"); IntVar x = solver.MakeIntVar(2, 17, "x"); IntExpr e = x + 5; IntVar y = e.Var(); Assert.Equal("(x(2..17) + 5)", y.ToString()); }
public void Run() { using (Context ctx = new Context()) { RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); Console.WriteLine(ctx.MkEq(x, y)); Console.WriteLine(ctx.MkNot(ctx.MkEq(x, y))); BoolExpr f1 = ctx.MkEq(x, y); BoolExpr f2 = ctx.MkEq(x, y); BoolExpr f3 = ctx.MkNot(ctx.MkEq(x, y)); Console.WriteLine(f1 == f2); Console.WriteLine(f2 == f1); Console.WriteLine(f1 == f3); FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.RealSort }, ctx.IntSort); IntExpr a = ctx.MkIntConst("a"); IntExpr b = ctx.MkIntConst("b"); Console.WriteLine(ctx.MkGt((IntExpr)f[a, ctx.MkInt2Real(a)], b)); Console.WriteLine(f.Range); Console.WriteLine(f.Arity); Console.WriteLine(f.Domain[0]); Console.WriteLine(f.Domain[1]); Console.WriteLine(f.DeclKind == Z3_decl_kind.Z3_OP_UNINTERPRETED); Console.WriteLine(ctx.MkAdd(a, b).FuncDecl.DeclKind == Z3_decl_kind.Z3_OP_ADD); Console.WriteLine(ctx.MkAdd(a, b).FuncDecl.DeclKind == Z3_decl_kind.Z3_OP_UNINTERPRETED); } }
private BoolExpr handleRectangularObstacle(RectangularObstacle obstacle, Context ctx, IntExpr[] sourcesX, IntExpr[] sourcesY, IntExpr[] destinationsX, IntExpr[] destinationsY) { IntExpr obstacleLeftX = ctx.MkInt(obstacle.location.x); IntExpr obstacleRightX = ctx.MkInt(obstacle.location.x + obstacle.width); IntExpr obstacleLeftY = ctx.MkInt(obstacle.location.y); IntExpr obstacleRightY = ctx.MkInt(obstacle.location.y + obstacle.length); IntExpr passDistance = ctx.MkInt(obstaclePassDistance); BoolExpr[] avoidingObstaclesX = new BoolExpr[pathSegments]; BoolExpr[] avoidingObstaclesY = new BoolExpr[pathSegments]; BoolExpr[] avoidingObstaclesCombined = new BoolExpr[pathSegments]; for (int i = 0; i < pathSegments; i++) { ArithExpr xl_minus_xs = ctx.MkSub(obstacleLeftX, sourcesX[i]); ArithExpr xl_minus_xd = ctx.MkSub(obstacleLeftX, destinationsX[i]); ArithExpr xs_minus_xh = ctx.MkSub(sourcesX[i], obstacleRightX); ArithExpr xd_minus_xh = ctx.MkSub(destinationsX[i], obstacleRightX); avoidingObstaclesX[i] = ctx.MkOr(ctx.MkAnd(ctx.MkGe(xl_minus_xs, passDistance), ctx.MkGe(xl_minus_xd, passDistance)), ctx.MkAnd(ctx.MkGe(xs_minus_xh, passDistance), ctx.MkGe(xd_minus_xh, passDistance))); ArithExpr yl_minus_ys = ctx.MkSub(obstacleLeftY, sourcesY[i]); ArithExpr yl_minus_yd = ctx.MkSub(obstacleLeftY, destinationsY[i]); ArithExpr ys_minus_yh = ctx.MkSub(sourcesY[i], obstacleRightY); ArithExpr yd_minus_yh = ctx.MkSub(destinationsY[i], obstacleRightY); avoidingObstaclesY[i] = ctx.MkOr(ctx.MkAnd(ctx.MkGe(yl_minus_ys, passDistance), ctx.MkGe(yl_minus_yd, passDistance)), ctx.MkAnd(ctx.MkGe(ys_minus_yh, passDistance), ctx.MkGe(yd_minus_yh, passDistance))); avoidingObstaclesCombined[i] = ctx.MkOr(avoidingObstaclesX[i], avoidingObstaclesY[i]); } return(ctx.MkAnd(avoidingObstaclesCombined)); }
public void Example1() { Sort[] types = new Sort[3]; IntExpr[] xs = new IntExpr[3]; Symbol[] names = new Symbol[3]; IntExpr[] vars = new IntExpr[3]; for (uint j = 0; j < 3; j++) { types[j] = ctx.IntSort; names[j] = ctx.MkSymbol(String.Format("x_{0}", j)); xs[j] = (IntExpr)ctx.MkConst(names[j], types[j]); vars[j] = (IntExpr)ctx.MkBound(2 - j, types[j]); // reversed } Expr body_vars = ctx.MkAnd(ctx.MkGe(ctx.MkAdd(vars[0], ctx.MkInt(1)), ctx.MkInt(2)), ctx.MkLe(ctx.MkAdd(-vars[1], ctx.MkInt(2)), ctx.MkAdd(vars[2], ctx.MkInt(3)))); Expr x = ctx.MkForall(types, names, body_vars, 1, null, null); Console.WriteLine("Formula: " + x.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); Assert.IsNotNull(q); Assert.AreEqual(q.ToString(), Status.SATISFIABLE.ToString()); }
public void Example3() { //types Sort[] types = new Sort[1]; types[0] = ctx.IntSort; //names Symbol[] names = new Symbol[1]; names[0] = ctx.MkSymbol("x"); IntExpr[] xs = new IntExpr[1]; xs[0] = (IntExpr)ctx.MkConst(names[0], types[0]); IntExpr[] constants = new IntExpr[2]; constants[0] = ctx.MkInt(5); constants[1] = ctx.MkInt(3); var expr = ctx.MkImplies(ctx.MkLe(xs[0], constants[0]), ctx.MkGe(xs[0], constants[1])); var x = ctx.MkExists(types, names, expr); Console.WriteLine("Formula: " + x.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); Assert.IsNotNull(q); Assert.AreEqual(q.ToString(), Status.SATISFIABLE.ToString()); }
public void Example5() { //types Sort[] types = new Sort[1]; types[0] = ctx.IntSort; //names Symbol[] names = new Symbol[1]; names[0] = ctx.MkSymbol("x"); IntExpr[] xs = new IntExpr[1]; xs[0] = (IntExpr)ctx.MkConst(names[0], types[0]); IntExpr[] constants = new IntExpr[1]; constants[0] = ctx.MkInt(2); var expr = ctx.MkLe(ctx.MkMod(xs[0], constants[0]), ctx.MkMul(xs[0], constants[0])); Console.WriteLine("Formula: " + expr.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); Assert.IsNotNull(q); Assert.AreEqual(q.ToString(), Status.SATISFIABLE.ToString()); }
public void Example4() { //types Sort[] types = new Sort[1]; types[0] = ctx.IntSort; //names Symbol[] names = new Symbol[2]; names[0] = ctx.MkSymbol("x"); names[1] = ctx.MkSymbol("y"); IntExpr[] xs = new IntExpr[2]; xs[0] = (IntExpr)ctx.MkConst(names[0], types[0]); xs[1] = (IntExpr)ctx.MkConst(names[1], types[0]); var expr = ctx.MkEq(xs[0], xs[1]); var exists = ctx.MkExists(types, new[] { names[1] }, expr); var any = ctx.MkForall(types, new[] { names[0] }, exists); Console.WriteLine("Formula: " + any.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); Assert.IsNotNull(q); Assert.AreEqual(q.ToString(), Status.SATISFIABLE.ToString()); }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Solver s = ctx.MkSolver(); Console.WriteLine(s); s.Assert(ctx.MkGt(x, ctx.MkInt(10)), ctx.MkEq(y, ctx.MkAdd(x, ctx.MkInt(2)))); Console.WriteLine(s); Console.WriteLine("solving s"); Console.WriteLine(s.Check()); Console.WriteLine("creating new scope"); s.Push(); s.Assert(ctx.MkLt(y, ctx.MkInt(11))); Console.WriteLine(s); Console.WriteLine("solving updated constraints"); Console.WriteLine(s.Check()); Console.WriteLine("restoring"); s.Pop(); Console.WriteLine(s); Console.WriteLine("solving restored constraints"); Console.WriteLine(s.Check()); } }
public static void Example3(Context ctx) { Console.WriteLine("Example 3:"); //types Sort[] types = new Sort[1]; types[0] = ctx.IntSort; //names Symbol[] names = new Symbol[1]; names[0] = ctx.MkSymbol("x"); IntExpr[] xs = new IntExpr[1]; xs[0] = (IntExpr)ctx.MkConst(names[0], types[0]); IntExpr[] constants = new IntExpr[2]; constants[0] = ctx.MkInt(5); constants[1] = ctx.MkInt(3); var expr = ctx.MkImplies(ctx.MkLe(xs[0], constants[0]), ctx.MkGe(xs[0], constants[1])); var x = ctx.MkExists(types, names, expr); Console.WriteLine("Formula: " + x.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); PrintResult(q, s); }
public Coordinate[] findPath() { Coordinate[] path = new Coordinate[pathSegments + 1]; Context ctx = new Context(); IntExpr[] sourcesX = new IntExpr[pathSegments]; IntExpr[] sourcesY = new IntExpr[pathSegments]; for (int i = 0; i < pathSegments; i++) { sourcesX[i] = ctx.MkIntConst("xs" + i); sourcesY[i] = ctx.MkIntConst("ys" + i); } IntExpr[] destinationsX = new IntExpr[pathSegments]; IntExpr[] destinationsY = new IntExpr[pathSegments]; for (int i = 0; i < pathSegments; i++) { destinationsX[i] = ctx.MkIntConst("xd" + i); destinationsY[i] = ctx.MkIntConst("yd" + i); } BoolExpr worldSizeConstraints = generateWorldSizeConstraints(ctx, sourcesX, sourcesY, destinationsX, destinationsY); BoolExpr movementConstraints = generateMovementConstraints(ctx, sourcesX, sourcesY, destinationsX, destinationsY); BoolExpr orthogonalConstraints = generateOrthogonalConstraints(ctx, sourcesX, sourcesY, destinationsX, destinationsY); BoolExpr prerequisitesConstraints = generateStartAndGoal(ctx, sourcesX, sourcesY, destinationsX, destinationsY); BoolExpr joiningPathSegments = joinPathSegments(ctx, sourcesX, sourcesY, destinationsX, destinationsY); BoolExpr avoidingObstacles = avoidObstacles(world, ctx, sourcesX, sourcesY, destinationsX, destinationsY); BoolExpr pathLengthConstraint = generatePathLengthConstraint(ctx, sourcesX, sourcesY, destinationsX, destinationsY); Solver s = ctx.MkSolver(); s.Assert(worldSizeConstraints); if (!(curvedPath)) { s.Assert(movementConstraints); s.Assert(orthogonalConstraints); } s.Assert(prerequisitesConstraints); s.Assert(joiningPathSegments); s.Assert(avoidingObstacles); s.Assert(pathLengthConstraint); Status status = s.Check(); if (status != Status.SATISFIABLE) { throw new TestFailedException(); } for (int i = 0; i < pathSegments; i++) { path[i] = new Coordinate(convertExprToInt(s.Model.ConstInterp(sourcesX[i])), convertExprToInt(s.Model.ConstInterp(sourcesY[i]))); } path[pathSegments] = new Coordinate(convertExprToInt(s.Model.ConstInterp(destinationsX[pathSegments - 1])), convertExprToInt(s.Model.ConstInterp(destinationsY[pathSegments - 1]))); return(path); }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" }, { "PROOF_MODE", "2" } }; using (Context ctx = new Context(cfg)) { ArrayExpr AllOne = ctx.MkConstArray(ctx.IntSort, ctx.MkInt(1)); IntExpr a = ctx.MkIntConst("a"); IntExpr i = ctx.MkIntConst("i"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(a, ctx.MkSelect(AllOne, i))); Console.WriteLine(s.Check()); s = ctx.MkSolver(); s.Assert(ctx.MkEq(a, ctx.MkSelect(AllOne, i))); s.Assert(ctx.MkNot(ctx.MkEq(a, ctx.MkInt(1)))); Console.WriteLine(s.Check()); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.IntSort }, ctx.IntSort); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Console.WriteLine(ctx.MkForall(new Expr[] { x, y }, ctx.MkEq(f[x, y], ctx.MkInt(0)))); Console.WriteLine(ctx.MkExists(new Expr[] { x }, ctx.MkGe((ArithExpr)f[x, x], ctx.MkInt(0)))); IntExpr a = ctx.MkIntConst("a"); IntExpr b = ctx.MkIntConst("b"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkForall(new Expr[] { x }, ctx.MkEq(f[x, x], ctx.MkInt(0)))); s.Assert(ctx.MkEq(f[a, b], ctx.MkInt(1))); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); } }
public void ConstraintConstructor() { Solver solver = new Solver("Solver"); IntVar x = solver.MakeIntVar(3, 7, "x"); Assert.Equal("x(3..7)", x.ToString()); // Unary operator Constraint c0 = (x == 5); Assert.Equal("(x(3..7) == 5)", c0.ToString()); IntExpr e1 = -c0; Assert.Equal("-(Watch<x == 5>(0 .. 1))", e1.ToString()); IntExpr e2 = c0.Abs(); Assert.Equal("Watch<x == 5>(0 .. 1)", e2.ToString()); IntExpr e3 = c0.Square(); Assert.Equal("IntSquare(Watch<x == 5>(0 .. 1))", e3.ToString()); // Relational operator with a scalar Constraint c1 = x == 5; Assert.Equal("(x(3..7) == 5)", c1.ToString()); Constraint c2 = x >= 5; Assert.Equal("(x(3..7) >= 5)", c2.ToString()); Constraint c3 = x > 5; Assert.Equal("(x(3..7) >= 6)", c3.ToString()); Constraint c4 = x <= 5; Assert.Equal("(x(3..7) <= 5)", c4.ToString()); Constraint c5 = x < 5; Assert.Equal("(x(3..7) <= 4)", c5.ToString()); Constraint c6 = x != 5; Assert.Equal("(x(3..7) != 5)", c6.ToString()); Constraint c7 = x == 2; Assert.Equal("FalseConstraint()", c7.ToString()); }
public void Run() { Dictionary <string, string> settings = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(settings)) { IntExpr a = ctx.MkIntConst("a"); IntExpr b = ctx.MkIntConst("b"); IntExpr c = ctx.MkIntConst("c"); RealExpr d = ctx.MkRealConst("d"); RealExpr e = ctx.MkRealConst("e"); BoolExpr q = ctx.MkAnd( ctx.MkGt(a, ctx.MkAdd(b, ctx.MkInt(2))), ctx.MkEq(a, ctx.MkAdd(ctx.MkMul(ctx.MkInt(2), c), ctx.MkInt(10))), ctx.MkLe(ctx.MkAdd(c, b), ctx.MkInt(1000)), ctx.MkGe(d, e)); Solver s = ctx.MkSolver(); s.Assert(q); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { IntExpr x = ctx.MkIntConst("x"); Console.WriteLine("is expression: " + (x is Expr)); Expr n = ctx.MkAdd(x, ctx.MkInt(1)); Console.WriteLine("is application: " + (x.IsNumeral || x.IsExpr)); Console.WriteLine("decl: " + n.FuncDecl); Console.WriteLine("num args: " + n.NumArgs); for (uint i = 0; i < n.NumArgs; i++) { Console.WriteLine("arg(" + i + ") -> " + n.Args[i]); } } }
public static void Example5(Context ctx) { Console.WriteLine("Example 5:"); //types Sort[] types = new Sort[1]; types[0] = ctx.IntSort; //names Symbol[] names = new Symbol[1]; names[0] = ctx.MkSymbol("x"); IntExpr[] xs = new IntExpr[1]; xs[0] = (IntExpr)ctx.MkConst(names[0], types[0]); IntExpr[] constants = new IntExpr[1]; constants[0] = ctx.MkInt(2); var expr = ctx.MkLe(ctx.MkMod(xs[0], constants[0]), ctx.MkMul(xs[0], constants[0])); Console.WriteLine("Formula: " + expr.ToString()); Solver s = ctx.MkSolver(); Status q = s.Check(); PrintResult(q, s); }
public void Run() { using (Context ctx = new Context()) { Sort U = ctx.MkUninterpretedSort("U"); Console.WriteLine(U); Expr a = ctx.MkConst("a", U); a = ctx.MkConst("a", U); Expr b = ctx.MkConst("b", U); Expr c = ctx.MkConst("c", U); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Console.WriteLine(ctx.MkAnd(ctx.MkEq(a, b), ctx.MkEq(a, c))); Console.WriteLine(U == ctx.IntSort); Sort U2 = ctx.MkUninterpretedSort("U"); Console.WriteLine(U == U2); U2 = ctx.MkUninterpretedSort("U2"); Console.WriteLine(U == U2); Console.WriteLine(ctx.MkDistinct(a, b, c)); FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { U, U }, U); Console.WriteLine(ctx.MkEq(f[a, b], b)); } }
static void Main() { Solver solver = new Google.OrTools.ConstraintSolver.Solver("p"); // creating dummy variables List <IntVar> vars = new List <IntVar>(); for (int i = 0; i < 200000; i++) { vars.Add(solver.MakeIntVar(0, 1)); } IntExpr globalSum = solver.MakeSum(vars.ToArray()); DecisionBuilder db = solver.MakePhase( vars.ToArray(), Google.OrTools.ConstraintSolver.Solver.INT_VAR_SIMPLE, Google.OrTools.ConstraintSolver.Solver.INT_VALUE_SIMPLE); solver.NewSearch(db, new OptimizeVar(solver, true, globalSum.Var(), 100)); GC.Collect(); GC.WaitForPendingFinalizers(); while (solver.NextSolution()) { Console.WriteLine("solution " + globalSum.Var().Value()); } Console.WriteLine("fini"); Console.ReadLine(); }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.IntSort }, ctx.IntSort); FuncDecl g = ctx.MkFuncDecl("g", new Sort[] { ctx.IntSort }, ctx.IntSort); Expr n = f[f[g[x], g[g[x]]], g[g[y]]]; Console.WriteLine(n); Expr nn = n.Substitute(new Expr[] { g[g[x]], g[y] }, new Expr[] { y, ctx.MkAdd(x, ctx.MkInt(1)) }); Console.WriteLine(nn); Console.WriteLine(n.Substitute(g[g[x]], y)); } }
public void Run() { using (Context ctx = new Context()) { BoolExpr p = ctx.MkBoolConst("p"); BoolExpr q = ctx.MkBoolConst("q"); Console.WriteLine(ctx.MkAnd(p, q)); Console.WriteLine(ctx.MkOr(p, q)); Console.WriteLine(ctx.MkAnd(p, ctx.MkTrue())); Console.WriteLine(ctx.MkOr(p, ctx.MkFalse())); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkImplies(p, q)); Console.WriteLine(ctx.MkEq(p, q).Simplify()); Console.WriteLine(ctx.MkEq(p, q)); BoolExpr r = ctx.MkBoolConst("r"); Console.WriteLine(ctx.MkNot(ctx.MkEq(p, ctx.MkNot(ctx.MkEq(q, r))))); Console.WriteLine(ctx.MkNot(ctx.MkEq(ctx.MkNot(ctx.MkEq(p, q)), r))); Console.WriteLine(ctx.MkEq(p, ctx.MkTrue())); Console.WriteLine(ctx.MkEq(p, ctx.MkFalse())); Console.WriteLine(ctx.MkEq(p, ctx.MkTrue()).Simplify()); Console.WriteLine(ctx.MkEq(p, ctx.MkFalse()).Simplify()); Console.WriteLine(ctx.MkEq(p, p).Simplify()); Console.WriteLine(ctx.MkEq(p, q).Simplify()); Console.WriteLine(ctx.MkAnd(p, q, r)); Console.WriteLine(ctx.MkOr(p, q, r)); IntExpr x = ctx.MkIntConst("x"); Console.WriteLine(x is BoolExpr); Console.WriteLine(p is BoolExpr); Console.WriteLine(ctx.MkAnd(p, q) is BoolExpr); Console.WriteLine(p is BoolExpr); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1)) is BoolExpr); Console.WriteLine(p.IsAnd); Console.WriteLine(ctx.MkOr(p, q).IsOr); Console.WriteLine(ctx.MkAnd(p, q).IsAnd); Console.WriteLine(x.IsNot); Console.WriteLine(p.IsNot); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkNot(p).IsDistinct); Console.WriteLine(ctx.MkEq(p, q).IsDistinct); Console.WriteLine(ctx.MkDistinct(p, q).IsDistinct); Console.WriteLine(ctx.MkDistinct(x, ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkAdd(x, ctx.MkInt(2))).IsDistinct); Console.WriteLine(); Console.WriteLine(ctx.MkBool(true)); Console.WriteLine(ctx.MkBool(false)); Console.WriteLine(ctx.BoolSort); Context ctx1 = new Context(); Console.WriteLine(ctx1.MkBool(true)); Console.WriteLine(ctx1.BoolSort); Console.WriteLine(ctx1.MkBool(true).Sort == ctx1.BoolSort); Console.WriteLine(ctx1.MkBool(true).Sort == ctx.BoolSort); Console.WriteLine(ctx1.MkBool(true).Sort != ctx.BoolSort); } }
// <-- Name // | FN Name TupleType // | BOOL_LITERAL // | INT_LITERAL // | STRING_LITERAL // | LPAREN (Expression)? RPAREN private IUnboundExpr PrimaryExpr() { IUnboundExpr expression; if (CurrentIs(TokenType.Name)) { var name = Consume(); expression = new NameExpr(name.Position, name.StringValue, TypeArgs()); } else if (CurrentIs(TokenType.Fn)) expression = FuncExpr(); else if (CurrentIs(TokenType.Bool)) expression = new BoolExpr(Consume(TokenType.Bool)); else if (CurrentIs(TokenType.Int)) expression = new IntExpr(Consume(TokenType.Int)); else if (CurrentIs(TokenType.String)) expression = new StringExpr(Consume(TokenType.String)); else if (CurrentIs(TokenType.LeftParen)) { Token leftParen = Consume(TokenType.LeftParen); if (CurrentIs(TokenType.RightParen)) { // () -> unit expression = new UnitExpr(leftParen.Position); } else if (CurrentIs(TokenType.Operator)) { // ( OPERATOR ) -> an operator in prefix form Token op = Consume(TokenType.Operator); expression = new NameExpr(op.Position, op.StringValue); } else { // anything else is a regular parenthesized expression expression = Expression(); } Consume(TokenType.RightParen); } else expression = null; return expression; }
public override Null Visit(IntExpr node) { context = null; node.computedType = new PrimType { kind = PrimKind.Int }; return null; }