Search in sources :

Example 1 with IntConstant

use of kodkod.ast.IntConstant in project org.alloytools.alloy by AlloyTools.

the class IntConstraints method formula.

/**
 * Returns the formula a1 <= v1 <= a2 <= v2 .... a1000<=v1000
 *
 * @return the formula
 */
public final Formula formula() {
    final List<Formula> constraints = new ArrayList<Formula>(2000);
    final List<IntConstant> constants = new ArrayList<IntConstant>(1001);
    for (int i = low; i <= high; i += 10) {
        constants.add(IntConstant.constant(i));
    }
    for (int i = 0; i < 1000; i++) {
        // convert to primitive int
        IntExpression varExpr = var[i].sum();
        // a_i <= v_i
        constraints.add(constants.get(i).lte(varExpr));
        // v_i <=
        constraints.add(varExpr.lte(constants.get(i + 1)));
    // a_(i+1)
    }
    return Formula.and(constraints);
}
Also used : Formula(kodkod.ast.Formula) IntExpression(kodkod.ast.IntExpression) ArrayList(java.util.ArrayList) IntConstant(kodkod.ast.IntConstant)

Example 2 with IntConstant

use of kodkod.ast.IntConstant in project org.alloytools.alloy by AlloyTools.

the class TranslateKodkodToJava method countHeight.

/**
 * Count the height of the given Kodkod AST tree.
 */
public static int countHeight(Node node) {
    ReturnVisitor<Integer, Integer, Integer, Integer> vis = new ReturnVisitor<Integer, Integer, Integer, Integer>() {

        private int max(int a, int b) {
            return (a >= b) ? a : b;
        }

        private int max(int a, int b, int c) {
            return (a >= b) ? (a >= c ? a : c) : (b >= c ? b : c);
        }

        @Override
        public Integer visit(Relation x) {
            return 1;
        }

        @Override
        public Integer visit(IntConstant x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantFormula x) {
            return 1;
        }

        @Override
        public Integer visit(Variable x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantExpression x) {
            return 1;
        }

        @Override
        public Integer visit(NotFormula x) {
            return 1 + x.formula().accept(this);
        }

        @Override
        public Integer visit(IntToExprCast x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(Decl x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(ExprToIntCast x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryExpression x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryIntExpression x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(MultiplicityFormula x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(BinaryExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(ComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryIntExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IntComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IfExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(IfIntExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(SumExpression x) {
            return 1 + max(x.decls().accept(this), x.intExpr().accept(this));
        }

        @Override
        public Integer visit(QuantifiedFormula x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(FixFormula x) {
            return 1 + max(x.condition().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Comprehension x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Decls x) {
            int max = 0, n = x.size();
            for (int i = 0; i < n; i++) max = max(max, x.get(i).accept(this));
            return max;
        }

        @Override
        public Integer visit(ProjectExpression x) {
            int max = x.expression().accept(this);
            for (Iterator<IntExpression> t = x.columns(); t.hasNext(); ) {
                max = max(max, t.next().accept(this));
            }
            return max;
        }

        @Override
        public Integer visit(RelationPredicate x) {
            if (x instanceof Function) {
                Function f = ((Function) x);
                return max(f.domain().accept(this), f.range().accept(this));
            }
            return 1;
        }

        @Override
        public Integer visit(NaryExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryIntExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryFormula x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }
    };
    Object ans = node.accept(vis);
    if (ans instanceof Integer)
        return ((Integer) ans).intValue();
    else
        return 0;
}
Also used : Variable(kodkod.ast.Variable) UnaryIntExpression(kodkod.ast.UnaryIntExpression) Decls(kodkod.ast.Decls) ConstantExpression(kodkod.ast.ConstantExpression) ComparisonFormula(kodkod.ast.ComparisonFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) UnaryExpression(kodkod.ast.UnaryExpression) Function(kodkod.ast.RelationPredicate.Function) Relation(kodkod.ast.Relation) BinaryExpression(kodkod.ast.BinaryExpression) IntConstant(kodkod.ast.IntConstant) NaryIntExpression(kodkod.ast.NaryIntExpression) NotFormula(kodkod.ast.NotFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) IfExpression(kodkod.ast.IfExpression) IntToExprCast(kodkod.ast.IntToExprCast) ProjectExpression(kodkod.ast.ProjectExpression) IfIntExpression(kodkod.ast.IfIntExpression) IfIntExpression(kodkod.ast.IfIntExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) IntExpression(kodkod.ast.IntExpression) RelationPredicate(kodkod.ast.RelationPredicate) NaryExpression(kodkod.ast.NaryExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) Decl(kodkod.ast.Decl) ConstantFormula(kodkod.ast.ConstantFormula) Comprehension(kodkod.ast.Comprehension) NaryFormula(kodkod.ast.NaryFormula) ExprToIntCast(kodkod.ast.ExprToIntCast) MultiplicityFormula(kodkod.ast.MultiplicityFormula) BinaryFormula(kodkod.ast.BinaryFormula) SumExpression(kodkod.ast.SumExpression) FixFormula(kodkod.ast.FixFormula) ReturnVisitor(kodkod.ast.visitor.ReturnVisitor) IntComparisonFormula(kodkod.ast.IntComparisonFormula)

Example 3 with IntConstant

use of kodkod.ast.IntConstant in project org.alloytools.alloy by AlloyTools.

the class Viktor method equations.

/**
 * Returns the equations to be satisfied.
 *
 * @return equations to be satisfied.
 */
public final Formula equations() {
    // each b <= cols-1
    Formula f0 = Formula.TRUE;
    final IntConstant colConst = IntConstant.constant(cols - 1);
    for (IntExpression bi : b) {
        f0 = f0.and(bi.lte(colConst));
    }
    final Variable[] y = new Variable[rows];
    for (int i = 0; i < rows; i++) {
        y[i] = Variable.unary("y" + i);
    }
    Decls decls = y[0].oneOf(INTS);
    for (int i = 1; i < rows; i++) decls = decls.and(y[i].oneOf(INTS));
    Formula f1 = Formula.TRUE;
    final Expression[] combo = new Expression[rows];
    for (int i = 0; i < cols; i++) {
        for (int j = i + 1; j < cols; j++) {
            for (int k = j + 1; k < cols; k++) {
                Formula f2 = Formula.TRUE;
                for (int m = 0; m < rows; m++) {
                    combo[0] = a[m][i];
                    combo[1] = a[m][j];
                    combo[2] = a[m][k];
                    f2 = f2.and(conditionalSum(combo, y, 0, rows - 1).eq(b[m]));
                }
                f1 = f1.and(f2.not().forAll(decls));
            }
        }
    }
    return f0.and(f1);
}
Also used : Formula(kodkod.ast.Formula) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) IntExpression(kodkod.ast.IntExpression) IntConstant(kodkod.ast.IntConstant)

Aggregations

IntConstant (kodkod.ast.IntConstant)3 IntExpression (kodkod.ast.IntExpression)3 Decls (kodkod.ast.Decls)2 Formula (kodkod.ast.Formula)2 Variable (kodkod.ast.Variable)2 ArrayList (java.util.ArrayList)1 BinaryExpression (kodkod.ast.BinaryExpression)1 BinaryFormula (kodkod.ast.BinaryFormula)1 BinaryIntExpression (kodkod.ast.BinaryIntExpression)1 ComparisonFormula (kodkod.ast.ComparisonFormula)1 Comprehension (kodkod.ast.Comprehension)1 ConstantExpression (kodkod.ast.ConstantExpression)1 ConstantFormula (kodkod.ast.ConstantFormula)1 Decl (kodkod.ast.Decl)1 ExprToIntCast (kodkod.ast.ExprToIntCast)1 Expression (kodkod.ast.Expression)1 FixFormula (kodkod.ast.FixFormula)1 IfExpression (kodkod.ast.IfExpression)1 IfIntExpression (kodkod.ast.IfIntExpression)1 IntComparisonFormula (kodkod.ast.IntComparisonFormula)1