Search in sources :

Example 6 with Bounds

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

the class BenchmarkSymmStats method main.

/**
 * Usage: java tests.benchmarks.BenchmarkSymmStats <class name>[(<primitive |
 * string | enum>[,<primitive | string | enum>]*)] <method name>[(<primitive |
 * string | enum>[,<primitive | string | enum>]*)] [<method name>(<primitive |
 * string | enum>[,<primitive | string | enum>]*)]
 */
public static void main(String[] args) {
    if (args.length != 2 && args.length != 3)
        usage();
    try {
        final Object instance = construct(args[0].contains("(") ? args[0] : args[0] + "()");
        final Formula formula = create(instance, args[1].contains("(") ? args[1] : args[1] + "()");
        final Bounds bounds = create(instance, args.length == 3 ? args[2] : "bounds()");
        // <class name> <method name>
        System.out.print(args[0] + "\t");
        System.out.print(args[1].split("\\(")[0] + "\t");
        // <PURE|EXT> <gbp (ms)> <gbp (symms)> <gad (ms)> <gad (symms)>
        printSymmInfo(formula, bounds);
    } catch (NumberFormatException nfe) {
        usage();
    }
}
Also used : Formula(kodkod.ast.Formula) Bounds(kodkod.instance.Bounds)

Example 7 with Bounds

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

the class Translator method completeBounds.

/**
 * Completes {@code this.bounds} using the bindings from
 * {@code this.originalBounds} so that the result satisfies the
 * {@linkplain Translation} invariants. This involves updating
 * {@code this.bounds} with bindings from {@code this.originalBounds}, if any,
 * that had been discarded in the {@link #translate() first step} of the
 * translation. The first step of a non-incremental translation is to discard
 * bounds for relations that are not constrained by
 * {@code this.originalFormula}, and to discard all integer bounds if
 * {@code this.originalFormula} contains no integer expressions. This is sound
 * since any instance of {@code this.originalFormula} with respect to
 * {@code this.originalBounds} only needs to satisfy the lower bound constraint
 * on each discarded relation/integer. By updating {@code this.bounds} with the
 * bindings for discarded relations/integers for which no variables were
 * allocated, we ensure that any instance returned by
 * {@linkplain Translation#interpret()} will bind those relations/integers to
 * their lower bound, therefore satisfying the original problem
 * {@code (this.originalFormula, this.originalBounds, this.options)}.
 *
 * @requires no this.bounds.intBound or this.bounds.intBound =
 *           this.originalBounds.intBound
 * @ensures this.bounds.relations' = this.bounds.relations +
 *          this.originalBounds.relations && this.bounds.intBound' =
 *          this.originalBounds.intBound && this.bounds.lowerBound' =
 *          this.bounds.lowerBound + (this.originalBounds.relations -
 *          this.bounds.relations)<:(this.originalBounds.lowerBound) &&
 *          this.bounds.upperBound' = bounds.upperBound +
 *          (this.originalBounds.relations -
 *          this.bounds.relations)<:(this.originalBounds.upperBound)
 * @return this.bounds
 */
private Bounds completeBounds() {
    final Bounds optimized = this.bounds;
    final Bounds original = this.originalBounds;
    if (optimized.ints().isEmpty()) {
        for (IndexedEntry<TupleSet> entry : original.intBounds()) {
            optimized.boundExactly(entry.index(), entry.value());
        }
    } else {
        assert optimized.intBounds().equals(original.intBounds());
    }
    final Set<Relation> rels = optimized.relations();
    for (Relation r : original.relations()) {
        if (!rels.contains(r)) {
            optimized.bound(r, original.lowerBound(r), original.upperBound(r));
        }
    }
    return optimized;
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) Bounds(kodkod.instance.Bounds)

Example 8 with Bounds

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

the class Translator method translateIncrementalTrivial.

/**
 * @requires checkIncrementalBounds(bounds, transl)
 * @requires checkIncrementalOptions(transl.options)
 * @requires transl.trivial()
 * @requires transl.cnf.solve()
 * @return see {@link #translateIncremental(Formula, Bounds, Options)}
 */
private static Translation.Incremental translateIncrementalTrivial(Formula formula, Bounds bounds, Translation.Incremental transl) {
    if (!transl.cnf().solve())
        throw new IllegalArgumentException("Expected a satisfiable translation, given " + transl);
    // release the old empty solver since we are going
    transl.cnf().free();
    // to re-translate
    final Options tOptions = transl.options();
    final Bounds tBounds = transl.bounds();
    // transl.originalFormula.
    for (Relation r : bounds.relations()) {
        tBounds.bound(r, bounds.lowerBound(r), bounds.upperBound(r));
    }
    // re-translate the given formula with respect to tBounds. note that we
    // don't have to re-translate
    // the conjunction of transl.formula and formula since transl.formula is
    // guaranteed to evaluate to
    // TRUE with respect to tBounds (since no bindings that were originally
    // in tBounds were changed by the above loop).
    final Translation.Incremental updated = translateIncremental(formula, tBounds, tOptions);
    // due to symmetry breaking.
    return new Translation.Incremental(updated.bounds(), tOptions, transl.symmetries(), updated.interpreter(), updated.incrementer());
}
Also used : Options(kodkod.engine.config.Options) Relation(kodkod.ast.Relation) HOLTranslation(kodkod.engine.hol.HOLTranslation) Bounds(kodkod.instance.Bounds)

Example 9 with Bounds

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

the class Handshake method main.

/**
 * Usage: java examples.Handshake [# persons, must be >= 2]
 */
public static void main(String[] args) {
    if (args.length < 1)
        usage();
    final Handshake model = new Handshake();
    final Solver solver = new Solver();
    try {
        final int persons = Integer.parseInt(args[0]);
        if (persons < 2)
            usage();
        solver.options().setBitwidth(6);
        // solver.options().setSolver(SATFactory.ZChaff);
        solver.options().setSolver(SATFactory.MiniSat);
        solver.options().setSymmetryBreaking(0);
        solver.options().setBitwidth(32 - Integer.numberOfLeadingZeros(persons));
        solver.options().setReporter(new ConsoleReporter());
        final Bounds b = model.bounds(persons);
        // .and(model.Person.count().eq(IntConstant.constant(persons)));
        final Formula f = model.runPuzzle();
        Solution sol = solver.solve(f, b);
        System.out.println(sol);
    } catch (NumberFormatException nfe) {
        usage();
    }
}
Also used : Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) ConsoleReporter(kodkod.engine.config.ConsoleReporter) Bounds(kodkod.instance.Bounds) Solution(kodkod.engine.Solution)

Example 10 with Bounds

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

the class Handshake method bounds.

/**
 * Returns a bounds for the given number of persons.
 *
 * @return a bounds for the given number of persons.
 */
public Bounds bounds(int persons) {
    final List<String> atoms = new ArrayList<String>(persons);
    atoms.add("Hilary");
    atoms.add("Jocelyn");
    for (int i = 2; i < persons; i++) {
        atoms.add("Person" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    b.boundExactly(Person, f.allOf(1));
    b.boundExactly(Hilary, f.setOf("Hilary"));
    b.boundExactly(Jocelyn, f.setOf("Jocelyn"));
    b.bound(spouse, f.allOf(2));
    b.bound(shaken, f.allOf(2));
    return b;
}
Also used : Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe)

Aggregations

Bounds (kodkod.instance.Bounds)140 Formula (kodkod.ast.Formula)83 Universe (kodkod.instance.Universe)79 TupleFactory (kodkod.instance.TupleFactory)76 Solution (kodkod.engine.Solution)75 Solver (kodkod.engine.Solver)67 TupleSet (kodkod.instance.TupleSet)55 Relation (kodkod.ast.Relation)49 ArrayList (java.util.ArrayList)45 Expression (kodkod.ast.Expression)21 Variable (kodkod.ast.Variable)21 IntExpression (kodkod.ast.IntExpression)20 Instance (kodkod.instance.Instance)12 Decls (kodkod.ast.Decls)11 Evaluator (kodkod.engine.Evaluator)8 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)7 UnboundLeafException (kodkod.engine.fol2sat.UnboundLeafException)7 ConsoleReporter (kodkod.engine.config.ConsoleReporter)6 Tuple (kodkod.instance.Tuple)6 LinkedHashSet (java.util.LinkedHashSet)5