Search in sources :

Example 51 with Relation

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

the class Instance method toPrettyString.

public String toPrettyString() {
    StringBuilder sb = new StringBuilder();
    for (Relation r : relations()) {
        TupleSet val = tuples(r);
        sb.append(r.name()).append(" = [").append(val.size()).append("] ").append(val).append("\n");
    }
    return sb.toString();
}
Also used : Relation(kodkod.ast.Relation)

Example 52 with Relation

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

the class Instance method skolems.

public Collection<Relation> skolems() {
    Set<Relation> rels = relations();
    ArrayList<Relation> ans = new ArrayList<Relation>(rels.size());
    for (Relation r : rels) if (r.isSkolem())
        ans.add(r);
    return ans;
}
Also used : Relation(kodkod.ast.Relation) ArrayList(java.util.ArrayList)

Example 53 with Relation

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

the class KK method main.

public static void main(String[] args) throws Exception {
    Relation x6 = Relation.unary("R");
    int[] ints = new int[] { 0, 1, 2 };
    List<Object> atomlist = new LinkedList<Object>();
    atomlist.add("R$0");
    atomlist.add("R$1");
    atomlist.add("R$2");
    for (int i : ints) atomlist.add(i);
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x6_upper = factory.noneOf(1);
    x6_upper.add(factory.tuple("R$0"));
    x6_upper.add(factory.tuple("R$1"));
    x6_upper.add(factory.tuple("R$2"));
    bounds.bound(x6, x6_upper);
    for (int i : ints) {
        bounds.boundExactly(i, factory.setOf(i));
    }
    Formula x11 = x6.some();
    IntExpression x5 = x6.count();
    Formula x9 = x11.implies(x5.gt(IntConstant.constant(0)));
    Formula x7 = x9.not();
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(2);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(0);
    System.out.println("Solving...");
    System.out.println(PrettyPrinter.print(x7, 0));
    System.out.println(bounds);
    Solution sol = solver.solve(x7, bounds);
    System.out.println(sol.toString());
    Instance inst = sol.instance();
    Evaluator ev = new Evaluator(inst);
    System.out.println(ev.evaluate(x6.some()));
    System.out.println(ev.evaluate(x5));
    System.out.println(ev.evaluate(x5.gt(IntConstant.constant(0))));
    System.out.println(ev.evaluate(x6.some().implies(x5.gt(IntConstant.constant(0))).not()));
    System.out.println(ev.evaluate(x7));
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) IntExpression(kodkod.ast.IntExpression) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) LinkedList(java.util.LinkedList) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Solution(kodkod.engine.Solution)

Example 54 with Relation

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

the class LeafInterpreter method extend.

/**
 * Extends this interpreter with interpretations for the given relations, based
 * on the specified lower and upper bound on each relation's value. Note that
 * this method may fail if the underlying boolean factory does not permit
 * introduction of new variables.
 *
 * @requires no rels & this.relations
 * @requires rels in lowers.keySet() && rels in uppers.keySet()
 * @requires all r: rels | let lr = lowers.get(r), ur = uppers.get(r) |
 *           lr.tuples in ur.tuples && lr.universe = ur.universe = this.universe
 * @ensures this.relations' = this.relations + rels && this.lbounds' =
 *          this.lbounds + rels<:(lowers.map) && this.ubounds' = this.ubounds +
 *          rels<:(uppers.map) && let newVars = this.factory.components' -
 *          this.factory.components | newVars in BooleanVariable && #newVars =
 *          (sum r: rels | uppers.get(r).size() - lowers.get(r).size()) &&
 *          (this.vars' - this.vars) in rels -> newVars
 * @return this
 */
public final void extend(Set<Relation> rels, Map<Relation, TupleSet> lowers, Map<Relation, TupleSet> uppers) {
    for (Relation r : rels) {
        this.lowers.put(r, lowers.get(r));
        this.uppers.put(r, uppers.get(r));
    }
    factory.addVariables(allocateVars(factory.maxFormula() + 1, vars, rels, lowers, uppers));
}
Also used : Relation(kodkod.ast.Relation)

Example 55 with Relation

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

the class SymmetryDetector method sort.

/**
 * Returns an array that contains unique non-empty tuplesets in the given
 * bounds, sorted in the order of increasing size.
 *
 * @return unique non-empty tuplesets in the given bounds, sorted in the order
 *         of increasing size.
 */
private TupleSet[] sort(Bounds bounds) {
    final List<TupleSet> sets = new ArrayList<TupleSet>(bounds.relations().size());
    for (Relation r : bounds.relations()) {
        if (r.isAtom() && ignoreAllAtomRelsExcept != null && !ignoreAllAtomRelsExcept.contains(r))
            continue;
        if (ignoreRels != null && ignoreRels.contains(r))
            continue;
        final TupleSet lower = bounds.lowerBound(r);
        final TupleSet upper = bounds.upperBound(r);
        if (!lower.isEmpty() && lower.size() < upper.size()) {
            sets.add(lower);
        }
        if (!upper.isEmpty()) {
            sets.add(upper);
        }
    }
    final TupleSet[] sorted = sets.toArray(new TupleSet[sets.size()]);
    Arrays.sort(sorted, new Comparator<TupleSet>() {

        @Override
        public int compare(TupleSet o1, TupleSet o2) {
            return o1.size() - o2.size();
        }
    });
    return sorted;
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) ArrayList(java.util.ArrayList)

Aggregations

Relation (kodkod.ast.Relation)87 Bounds (kodkod.instance.Bounds)49 Formula (kodkod.ast.Formula)45 TupleFactory (kodkod.instance.TupleFactory)45 Universe (kodkod.instance.Universe)45 TupleSet (kodkod.instance.TupleSet)43 Solution (kodkod.engine.Solution)30 Expression (kodkod.ast.Expression)26 IntExpression (kodkod.ast.IntExpression)24 Solver (kodkod.engine.Solver)24 ArrayList (java.util.ArrayList)23 Variable (kodkod.ast.Variable)23 Instance (kodkod.instance.Instance)15 Decls (kodkod.ast.Decls)13 Evaluator (kodkod.engine.Evaluator)9 Tuple (kodkod.instance.Tuple)8 IntSet (kodkod.util.ints.IntSet)7 BinaryExpression (kodkod.ast.BinaryExpression)6 Decl (kodkod.ast.Decl)6 LinkedHashSet (java.util.LinkedHashSet)4