Package org.sat4j.core

Examples of org.sat4j.core.VecInt


      int indicatorVars = numberOfVertices * colors;
      solver.newVar(indicatorVars + colors);

      /* PB constraints to enforce assignment of exactly one color. */
      for (int i = 0; i < numberOfVertices; i++) {
        IVecInt literals = new VecInt(colors);
        IVec<BigInteger> coefficients = new Vec<BigInteger>(colors);
        for (int j = 1; j <= colors; j++) {
          literals.push(i * colors + j);
          coefficients.push(BigInteger.ONE);
        }
        solver.addPseudoBoolean(literals, coefficients, false,
            BigInteger.ONE);
        solver.addPseudoBoolean(literals, coefficients, true,
            BigInteger.ONE);
      }

      /*
       * CNF constraints enforcing that no adjacent vertices have the same
       * color.
       */
      for (int i = 0; i < vertices.size(); i++) {
        V u = vertices.get(i);
        for (int j = 0; j < vertices.size(); j++) {
          V v = vertices.get(j);
          if (graph.containsEdge(u, v)) {
            for (int k = 1; k <= colors; k++) {
              IVecInt literals = new VecInt(2);
              literals.push(-(i * colors + k));
              literals.push(-(j * colors + k));
              solver.addClause(literals);
            }
          }
        }
      }

      /*
       * CNF constraints (derived from propositional logic expression) to
       * set track variable, if a color has been assigned to one of the
       * indicator variables for a given bundle.
       */
      for (int k = 1; k <= colors; k++) {
        /* First term. */
        IVecInt literals = new VecInt(numberOfVertices + 1);
        literals.push(-(numberOfVertices * colors + k));
        for (int i = 0; i < numberOfVertices; i++) {
          literals.push(i * colors + k);
        }
        solver.addClause(literals);

        /* Second term. */
        for (int i = 0; i < numberOfVertices; i++) {
          literals.clear();
          literals.push(-(i * colors + k));
          literals.push(numberOfVertices * colors + k);
          solver.addClause(literals);
        }
      }

      /* Symmetry breaking. */
      selectiveColoring(colors, solver, vertices);

      IVecInt literals = new VecInt(2);
      for (int k = 1; k <= colors; k++) {
        literals.push(-(numberOfVertices * colors + k));
        for (int i = 1; i < k; i++) {
          literals.push(numberOfVertices * colors + i);
          solver.addClause(literals);
        }
        literals.clear();
      }

      /* Objective function. */
      literals = new VecInt(colors);
      IVec<BigInteger> coefficients = new Vec<BigInteger>(colors);
      for (int k = 1; k <= colors; k++) {
        literals.push(numberOfVertices * colors + k);
        coefficients.push(BigInteger.ONE);
      }
      ObjectiveFunction objFunc = new ObjectiveFunction(literals,
          coefficients);
      solver.setObjectiveFunction(objFunc);
View Full Code Here


      List<V> vertices) throws ContradictionException {
    VertexDegreeComparator<V, E> c = new VertexDegreeComparator<V, E>(graph);

    /* Precolor vertex v with maximum degree with first color. */
    V v = Collections.max(vertices, c);
    IVecInt literals = new VecInt(1);
    literals.push(vertices.indexOf(v) * colors + 1);
    solver.addClause(literals);
    literals.clear();

    /* Precolor neighbor of v with maximum degree with second color. */
    Collection<V> neighbors = graph.getNeighbors(v);
    if (!neighbors.isEmpty()) {
      v = Collections.max(neighbors, c);
      literals.push(vertices.indexOf(v) * colors + 2);
      solver.addClause(literals);
    }
  }
View Full Code Here

            ParseFormatException, ContradictionException {
        String line;

        int realNbOfConstr = 0;

        IVecInt literals = new VecInt();

        while (true) {
            line = in.readLine();

            if (line == null) {
                // end of file
                if (literals.size() > 0) {
                    // no 0 end the last clause
                    solver.addClause(literals);
                    realNbOfConstr++;
                }
View Full Code Here

        }
        return -var;
    }

    private int readOutput(int nboutputs, Scanner scanner) {
        IVecInt outputs = new VecInt(nboutputs);
        for (int i = 0; i < nboutputs; i++) {
            outputs.push(scanner.nextInt());
        }
        return outputs.get(0);
    }
View Full Code Here

        return outputs.get(0);
    }


    private IVecInt readInput(int numberOfInputs, Scanner scanner) {
        IVecInt inputs = new VecInt(numberOfInputs);
        for (int i = 0; i < numberOfInputs; i++) {
            inputs.push(scanner.nextInt());
        }
        return inputs;
    }
View Full Code Here

        String line;
        StringTokenizer stk;

        int realNbOfClauses = 0;

        IVecInt literals = new VecInt();

        while (true) {
            line = in.readLine();

            if (line == null) {
                // end of file
                if (literals.size() > 0) {
                    // no 0 end the last clause
                    solver.addClause(literals);
                    realNbOfClauses++;
                }

                break;
            }

            if (line.startsWith("c ")) {
                // skip commented line
                continue;
            }
            if (line.startsWith("%") && expectedNbOfConstr == realNbOfClauses) {
                System.out
                        .println("Ignoring the rest of the file (SATLIB format");
                break;
            }
            stk = new StringTokenizer(line);
            String token;

            while (stk.hasMoreTokens()) {
                // on lit le prochain token
                token = stk.nextToken();

                if ("<=".equals(token) || ">=".equals(token)) {
                    // on est sur une contrainte de cardinalit?
                    readCardinalityConstr(token, stk, literals);
                    literals.clear();
                    realNbOfClauses++;
                } else {
                    lit = Integer.parseInt(token);
                    if (lit == 0) {
                        if (literals.size() > 0) {
                            solver.addClause(literals);
                            literals.clear();
                            realNbOfClauses++;
                        }
                    } else {
                        literals.push(lit);
                    }
                }
            }
        }
        if (expectedNbOfConstr != realNbOfClauses) {
View Full Code Here

        case TRUE:
            assert inputs.length == 0;
            gater.gateTrue(output);
            break;
        case OR:
            literals = new VecInt(inputs);
            gater.or(output, literals);
            break;
        case NOT:
            assert inputs.length == 1;
            gater.not(output, inputs[0]);
            break;
        case AND:
            literals = new VecInt(inputs);
            gater.and(output, literals);
            break;
        case XOR:
            literals = new VecInt(inputs);
            gater.xor(output, literals);
            break;
        case IFF:
            literals = new VecInt(inputs);
            gater.iff(output, literals);
            break;
        case IFTHENELSE:
            assert inputs.length == 3;
            gater.ite(output, inputs[0], inputs[1], inputs[2]);
View Full Code Here

     *         the solver.
     * @throws TimeoutException
     * @see {@link ISolver#removeConstr(IConstr)}
     */
    public boolean hasASingleSolution() throws TimeoutException {
        return hasASingleSolution(new VecInt());
    }
View Full Code Here

     */
    public boolean hasASingleSolution(IVecInt assumptions)
            throws TimeoutException {
        int[] firstmodel = model();
        assert firstmodel != null;
        IVecInt clause = new VecInt(firstmodel.length);
        for (int q : firstmodel) {
            clause.push(-q);
        }
        boolean result = false;
        try {
            IConstr added = addClause(clause);
            result = !isSatisfiable(assumptions);
View Full Code Here

     * @return the set of literals common to all models of the formula contained
     *         in the solver, in dimacs format.
     * @throws TimeoutException
     */
    public static IVecInt backbone(ISolver s) throws TimeoutException {
        IVecInt backbone = new VecInt();
        int nvars = s.nVars();
        for (int i = 1; i <= nvars; i++) {
            backbone.push(i);
            if (s.isSatisfiable(backbone)) {
                backbone.pop().push(-i);
                if (s.isSatisfiable(backbone)) {
                    backbone.pop();
                } else {
                    // -i is in the backbone
                    backbone.pop().push(i);
                }
            } else {
                // -i is in the backbone
                backbone.pop().push(-i);
            }
        }
        return backbone;
    }
View Full Code Here

TOP

Related Classes of org.sat4j.core.VecInt

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.