Search in sources :

Example 51 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class BookExamples method trial.

/**
 * This tries a particular solution against the formula.
 */
private static Solution trial(A4Reporter rep, TupleFactory fac, Solver solver, Iterable<Sig> sigs, Formula f, A4Solution frame, Object[] t) {
    try {
        frame.kr2typeCLEAR();
        Bounds b = null;
        TupleSet ts = null;
        for (int i = 1; i < t.length; i++) {
            Object x = t[i];
            if (x == null)
                return null;
            if (x instanceof String && ((String) x).length() > 0) {
                // This
                // means
                // it's
                // a
                // unary
                // Tuple
                // containing
                // the
                // given
                // atom
                Tuple xx = fac.tuple((String) x);
                if (ts == null)
                    ts = fac.noneOf(1);
                ts.add(xx);
                continue;
            }
            if (x instanceof Tuple) {
                // This means it's a Tuple
                Tuple xx = (Tuple) x;
                if (ts == null)
                    ts = fac.noneOf(xx.arity());
                ts.add(xx);
                continue;
            }
            if (x instanceof String) {
                // The empty string means the sig
                // name follows here
                i++;
                if (i >= t.length - 1 || !(t[i] instanceof String) || !(t[i + 1] instanceof String))
                    return null;
                String sigName = (String) (t[i]);
                i++;
                String fieldName = (String) (t[i]);
                Sig first = hasSig(sigs, sigName);
                if (first == null)
                    return null;
                Expression expr = null;
                if (fieldName.length() == 0) {
                    expr = frame.a2k(first);
                } else {
                    for (Field field : first.getFields()) if (field.label.equals(fieldName)) {
                        expr = frame.a2k(field);
                        while (expr instanceof BinaryExpression) expr = ((BinaryExpression) expr).right();
                        break;
                    }
                }
                if (!(expr instanceof Relation))
                    return null;
                if (b == null)
                    // We delay the expansive
                    b = frame.getBounds();
                // really find a possible match
                if (ts == null)
                    ts = fac.noneOf(expr.arity());
                if (!ts.containsAll(b.lowerBound((Relation) expr)))
                    // Sanity check
                    return null;
                if (!b.upperBound((Relation) expr).containsAll(ts))
                    // Sanity check
                    return null;
                b.boundExactly((Relation) expr, ts);
                ts = null;
                continue;
            }
        }
        SATFactory sat = solver.options().solver();
        Solution sol;
        try {
            solver.options().setSolver(SATFactory.DefaultSAT4J);
            sol = solver.solve(f, b);
        } finally {
            solver.options().setSolver(sat);
        }
        if (sol == null || (sol.outcome() != SATISFIABLE && sol.outcome() != TRIVIALLY_SATISFIABLE))
            return null;
        if (rep != null)
            rep.debug("Comment: " + t[0] + "\n");
        return sol;
    } catch (Throwable ex) {
        return null;
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) Relation(kodkod.ast.Relation) SATFactory(kodkod.engine.satlab.SATFactory) BinaryExpression(kodkod.ast.BinaryExpression) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) Tuple(kodkod.instance.Tuple) Solution(kodkod.engine.Solution)

Example 52 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class Simplifier method simplify_in.

/**
 * Simplify the bounds based on the fact that "a is subset of b"; return false
 * if we discover the formula is unsat.
 */
private final boolean simplify_in(Expression a, Expression b) {
    a = condense(a);
    b = condense(b);
    if (a instanceof Relation) {
        try {
            Relation r = (Relation) a;
            TupleSet ub = sol.query(true, r, false), lb = sol.query(false, r, false), t = sol.approximate(b);
            t.retainAll(ub);
            if (!t.containsAll(lb)) {
                rep.debug("Comment: Simplify " + a + " " + ub.size() + "->false\n");
                return false;
            }
            // This means the upperbound is shrunk BELOW the lowerbound.
            if (t.size() < ub.size()) {
                rep.debug("Comment: Simplify " + a + " " + ub.size() + "->" + t.size() + "\n");
                sol.shrink(r, lb, t);
            }
        } catch (Throwable ex) {
            // Not
            rep.debug("Comment: Simplify " + a + " exception: " + ex + "\n" + MailBug.dump(ex).trim() + "\n");
        // fatal;
        // let's
        // report
        // it
        // to
        // the
        // debug()
        // reporter
        }
    }
    return true;
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation)

Example 53 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class RingElection method bounds.

/**
 * Returns a bounds object that scopes Process, Time, and their fields according
 * to given values.
 *
 * @return bounds
 */
public Bounds bounds(int processes, int times) {
    final List<String> atoms = new ArrayList<String>(processes + times);
    for (int i = 0; i < processes; i++) {
        atoms.add("Process" + i);
    }
    for (int i = 0; i < times; i++) {
        atoms.add("Time" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    final TupleSet pb = f.range(f.tuple("Process0"), f.tuple("Process" + (processes - 1)));
    final TupleSet tb = f.range(f.tuple("Time0"), f.tuple("Time" + (times - 1)));
    b.bound(Process, pb);
    b.bound(succ, pb.product(pb));
    b.bound(toSend, pb.product(pb).product(tb));
    b.bound(elected, pb.product(tb));
    b.bound(pord, pb.product(pb));
    b.bound(pfirst, pb);
    b.bound(plast, pb);
    b.bound(Time, tb);
    b.bound(tord, tb.product(tb));
    b.bound(tfirst, tb);
    b.bound(tlast, tb);
    return b;
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe)

Example 54 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class Bigconfig method bounds.

/**
 * Returns a bounds with the given number of hqs and subs, constructed using the
 * given universe.
 *
 * @requires hqNum > 0 && subNum > 0
 * @requires u contains at least (hqNum+sub) Router atoms and as many Site atoms
 * @return bounds
 */
private Bounds bounds(int hqNum, int subNum, Universe u) {
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    final int siteMax = hqNum + subNum - 1;
    final String site0 = "Site0";
    final String siteN = "Site" + siteMax;
    final String siteHQ = "Site" + (hqNum - 1);
    final String siteSub = "Site" + hqNum;
    final String router0 = "Router0";
    final String routerN = "Router" + siteMax;
    final TupleSet sites = f.range(f.tuple(site0), f.tuple(siteN));
    b.boundExactly(Site, sites);
    b.boundExactly(HQ, f.range(f.tuple(site0), f.tuple(siteHQ)));
    b.boundExactly(Sub, f.range(f.tuple(siteSub), f.tuple(siteN)));
    final TupleSet routers = f.range(f.tuple(router0), f.tuple(routerN));
    b.boundExactly(Router, routers);
    b.bound(link, routers.product(routers));
    // b.bound(site, routers.product(sites));
    final TupleSet routerLocations = f.noneOf(2);
    for (int i = 0; i <= siteMax; i++) {
        routerLocations.add(f.tuple("Router" + i, "Site" + i));
    }
    b.boundExactly(site, routerLocations);
    return b;
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory)

Example 55 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class IntConstraints method bounds.

/**
 * Returns a bounds for the problem.
 */
public final Bounds bounds() {
    final List<Integer> atoms = new ArrayList<Integer>(14);
    for (int i = 0; i < 32; i++) {
        atoms.add(Integer.valueOf(1 << i));
    }
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    // bound the integers
    for (int i = 0; i < 32; i++) {
        b.boundExactly(1 << i, f.setOf(Integer.valueOf(1 << i)));
    }
    // instance based on the low/high range for each variable.
    for (int i = 0; i < 1000; i++) {
        TupleSet lower = f.noneOf(1), upper = f.noneOf(1);
        int min = low + i * 10, max = min + 10, bit = 31;
        // constraints
        while (bit >= 0) {
            int bitVal = 1 << bit;
            if ((min & bitVal) == (max & bitVal)) {
                if ((min & bitVal) != 0) {
                    Tuple bitTuple = f.tuple(Integer.valueOf(bitVal));
                    lower.add(bitTuple);
                    upper.add(bitTuple);
                }
            } else {
                // get out of the loop as soon as patterns diverge
                break;
            }
            bit--;
        }
        // them into upper bound but not lower
        while (bit >= 0) {
            upper.add(f.tuple(Integer.valueOf(1 << bit)));
            bit--;
        }
        b.bound(var[i], lower, upper);
    }
    return b;
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Tuple(kodkod.instance.Tuple)

Aggregations

TupleSet (kodkod.instance.TupleSet)89 TupleFactory (kodkod.instance.TupleFactory)55 Bounds (kodkod.instance.Bounds)53 Universe (kodkod.instance.Universe)50 Relation (kodkod.ast.Relation)43 ArrayList (java.util.ArrayList)30 Formula (kodkod.ast.Formula)29 Solution (kodkod.engine.Solution)23 Solver (kodkod.engine.Solver)21 Expression (kodkod.ast.Expression)20 IntExpression (kodkod.ast.IntExpression)18 Variable (kodkod.ast.Variable)15 Instance (kodkod.instance.Instance)13 Tuple (kodkod.instance.Tuple)12 Decls (kodkod.ast.Decls)10 Sig (edu.mit.csail.sdg.ast.Sig)7 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)7 Evaluator (kodkod.engine.Evaluator)7 Field (edu.mit.csail.sdg.ast.Sig.Field)5 Map (java.util.Map)5