Search in sources :

Example 81 with Bounds

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

the class Viktor method main.

/**
 * Usage: java tests.Viktor
 */
public static void main(String[] args) {
    final Viktor model = new Viktor();
    final Solver solver = new Solver();
    solver.options().setSolver(SATFactory.MiniSat);
    solver.options().setReporter(new ConsoleReporter());
    solver.options().setBitwidth(7);
    final Formula f = model.checkEquations();
    final Bounds b = model.bounds();
    System.out.println(f);
    System.out.println(b);
    final Solution sol = solver.solve(f, b);
    System.out.println(sol);
    if (sol.instance() != null)
        model.display(sol.instance(), solver.options());
}
Also used : Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) ConsoleReporter(kodkod.engine.config.ConsoleReporter) Bounds(kodkod.instance.Bounds) Solution(kodkod.engine.Solution)

Example 82 with Bounds

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

the class Viktor method bounds.

/**
 * Returns the bounds for the problem.
 *
 * @return bounds
 */
public final Bounds bounds() {
    List<String> atoms = new ArrayList<String>(cols + 1);
    for (int i = 0; i < cols; i++) {
        atoms.add(String.valueOf(i));
    }
    atoms.add("a");
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    final TupleSet abound = f.setOf("a");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            b.bound(a[i][j], abound);
        }
    }
    final TupleSet xbound = f.range(f.tuple(String.valueOf(0)), f.tuple(String.valueOf(cols - 1)));
    for (int j = 0; j < cols; j++) {
        b.bound(x[j], xbound);
        b.boundExactly(j, f.setOf(String.valueOf(j)));
    }
    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 83 with Bounds

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

the class ConfigAssure method bounds.

/**
 * Returns the bounds corresponding to the given ip address and subnet files.
 *
 * @return bounds corresponding to the given ip address and subnet files.
 * @throws IOException if either of the files cannot be found or accessed
 * @throws IllegalArgumentException if either of the files cannot be parsed
 */
public Bounds bounds(String ipAddrsFile, String subnetsFile) throws IOException {
    final Map<String, NetNode> nodes = parseAddresses(ipAddrsFile);
    final Map<NetNode, Subnet> subnets = parseSubnets(subnetsFile, nodes);
    final Universe universe = universe(nodes.keySet());
    final Bounds bounds = new Bounds(universe);
    final TupleFactory factory = universe.factory();
    // bind the integers
    for (int i = 0; i < 32; i++) {
        bounds.boundExactly(1 << i, factory.setOf(Integer.valueOf(1 << i)));
    }
    // bind the port relation exactly to the port names
    bounds.boundExactly(port, factory.range(factory.tuple(universe.atom(0)), factory.tuple(universe.atom(nodes.keySet().size() - 1))));
    // bind the unknown relation exactly to the port names of ports with
    // unknown addresses or masks
    final TupleSet unknownBound = factory.noneOf(1);
    for (NetNode n : nodes.values()) {
        if (!n.known()) {
            unknownBound.add(factory.tuple(n.port));
        }
    }
    bounds.boundExactly(unknown, unknownBound);
    // bind the subnet relation exactly, choosing the first element of each
    // subnet as the representative
    final TupleSet subnetBound = factory.noneOf(2);
    for (Map.Entry<NetNode, Subnet> entry : subnets.entrySet()) {
        final NetNode rep = entry.getKey();
        for (NetNode member : entry.getValue().members) {
            subnetBound.add(factory.tuple(rep.port, member.port));
        }
    }
    bounds.boundExactly(subnet, subnetBound);
    // bind the addr relation so that each address is guaranteed to be
    // between minAddr (121.96.0.0) and maxAddr (121.96.255.255), inclusive
    final TupleSet lAddr = factory.noneOf(2), uAddr = factory.noneOf(2);
    for (NetNode node : nodes.values()) {
        if (node.varAddress) {
            // unknown address
            lAddr.addAll(portToBits(factory, node.port, minAddr));
            uAddr.addAll(portToBits(factory, node.port, maxAddr));
        } else {
            // known address
            final TupleSet portToAddrBits = portToBits(factory, node.port, node.address);
            lAddr.addAll(portToAddrBits);
            uAddr.addAll(portToAddrBits);
        }
    }
    bounds.bound(addr, lAddr, uAddr);
    // bind the group and groupMask relations so that all ports with the
    // same interface on the same subnet are guaranteed to have the same
    // mask
    final TupleSet lMask = factory.noneOf(2), uMask = factory.noneOf(2), groupBound = factory.noneOf(2);
    for (Subnet sub : subnets.values()) {
        for (Map.Entry<NetNode, Set<NetNode>> entry : sub.groups.entrySet()) {
            final NetNode rep = entry.getKey();
            for (NetNode member : entry.getValue()) {
                groupBound.add(factory.tuple(member.port, rep.port));
                // remove a grouped member out of
                nodes.remove(member.port);
            // the addresses set
            }
            if (rep.varMask) {
                // unknown mask for the representative
                uMask.addAll(portToBits(factory, rep.port, 31));
            } else {
                // known mask for the representative
                final TupleSet portToMaskBits = portToBits(factory, rep.port, rep.mask);
                lMask.addAll(portToMaskBits);
                uMask.addAll(portToMaskBits);
            }
        }
    }
    // of any subnet
    for (NetNode ungrouped : nodes.values()) {
        groupBound.add(factory.tuple(ungrouped.port, ungrouped.port));
        if (ungrouped.varMask) {
            // unknown mask for the representative
            uMask.addAll(portToBits(factory, ungrouped.port, 31));
        } else {
            // known mask for the representative
            final TupleSet portToMaskBits = portToBits(factory, ungrouped.port, ungrouped.mask);
            lMask.addAll(portToMaskBits);
            uMask.addAll(portToMaskBits);
        }
    }
    bounds.bound(groupMask, lMask, uMask);
    bounds.boundExactly(group, groupBound);
    // ", unknown.size: " + unknownBound.size());
    return bounds;
}
Also used : TupleSet(kodkod.instance.TupleSet) LinkedHashSet(java.util.LinkedHashSet) TupleSet(kodkod.instance.TupleSet) Set(java.util.Set) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 84 with Bounds

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

the class ConfigAssure method main.

/**
 * Usage: java examples.ConfigAssure <ipAddresses file> <subnets file>
 */
public static void main(String[] args) {
    if (args.length < 2)
        usage();
    try {
        final ConfigAssure ca = new ConfigAssure();
        final Solver solver = new Solver();
        solver.options().setBitwidth(32);
        solver.options().setSolver(SATFactory.MiniSat);
        final Formula formula = ca.requirements();
        final Bounds bounds = ca.bounds(args[0], args[1]);
        System.out.println("---explicit requirements (others are implicit in the bounds)---");
        System.out.println(PrettyPrinter.print(formula, 2));
        System.out.println("\n---solving with config files " + args[0] + " and " + args[1] + "---");
        final Solution sol = solver.solve(formula, bounds);
        System.out.println("\n---OUTCOME---");
        System.out.println(sol.outcome());
        System.out.println("\n---STATS---");
        System.out.println(sol.stats());
        if (sol.instance() != null) {
            System.out.println("\n---INSTANCE--");
            ca.display(sol.instance(), solver.options());
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        usage();
    }
}
Also used : Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) Bounds(kodkod.instance.Bounds) IOException(java.io.IOException) Solution(kodkod.engine.Solution)

Example 85 with Bounds

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

the class ALG212 method main.

/**
 * Usage: java examples.tptp.ALG212 [univ size]
 */
public static void main(String[] args) {
    if (args.length < 1)
        usage();
    try {
        final int n = Integer.parseInt(args[0]);
        if (n < 1)
            usage();
        final ALG212 model = new ALG212();
        final Solver solver = new Solver();
        solver.options().setSolver(SATFactory.MiniSat);
        // solver.options().setSymmetryBreaking(n*n);
        // solver.options().setFlatten(false);
        final Formula f = model.checkDistLong();
        final Bounds b = model.bounds(n);
        System.out.println(f);
        final Solution sol = solver.solve(f, b);
        System.out.println(sol);
    } catch (NumberFormatException nfe) {
        usage();
    }
}
Also used : Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) Bounds(kodkod.instance.Bounds) Solution(kodkod.engine.Solution)

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