Package kodkod.instance

Examples of kodkod.instance.Instance


   * Creates an instance from the given Bounds.  The instance
   * is simply the mapping bounds.lowerBound.
   * @return the instance corresponding to bounds.lowerBound
   */
  private static Instance toInstance(Bounds bounds) {
    final Instance instance = new Instance(bounds.universe());
    for (Relation r : bounds.relations()) {
      instance.add(r, bounds.lowerBound(r));
    }
    for (IntIterator iter = bounds.ints().iterator(); iter.hasNext();) {
      int i = iter.next();
      instance.add(i, bounds.exactBound(i));
    }
    return instance;
  }
View Full Code Here


    private Solution trivialSolution(TrivialFormulaException tfe) {
      final Statistics stats = new Statistics(0, 0, 0, translTime, 0);
      if (tfe.value().booleanValue()) {
        trivial++;
        final Bounds translBounds = tfe.bounds();
        final Instance trivialInstance = padInstance(toInstance(translBounds), bounds);
        final Solution sol = Solution.triviallySatisfiable(stats, trivialInstance);
       
        final List<Formula> changes = new LinkedList<Formula>();
               
        for(Map.Entry<Relation, TupleSet> entry: trivialInstance.relationTuples().entrySet()) {
          final Relation r = entry.getKey();
         
          if (!translBounds.relations().contains(r)) {
            translBounds.bound(r, bounds.lowerBound(r), bounds.upperBound(r));
          }
View Full Code Here

   * @throws IllegalStateException - this.solver.solve() has not been called or the
   * outcome of the last call was not <code>true</code>.
   */
  public Instance interpret() {
    final TupleFactory f = bounds.universe().factory();
    final Instance instance = new Instance(bounds.universe());
//    System.out.println(varUsage);
    for(Relation r : bounds.relations()) {
      TupleSet lower = bounds.lowerBound(r);
      IntSet indeces = Ints.bestSet(lower.capacity());
      indeces.addAll(lower.indexView());
      IntSet vars = primaryVarUsage.get(r);
      if (vars!=null) {
        int lit = vars.min();
        for(IntIterator iter = bounds.upperBound(r).indexView().iterator(); iter.hasNext();) {
          final int index = iter.next();
          if (!indeces.contains(index) && solver.valueOf(lit++))
            indeces.add(index);
        }
      }
      instance.add(r, f.setOf(r.arity(), indeces));
    }
    return instance;
  }
View Full Code Here

    /** Construct a new A4Solution that is the continuation of the old one, but with the "next" instance. */
    private A4Solution(A4Solution old) throws Err {
        if (!old.solved) throw new ErrorAPI("This solution is not yet solved, so next() is not allowed.");
        if (old.kEnumerator==null) throw new ErrorAPI("This solution was not generated by an incremental SAT solver.\n" + "Solution enumeration is currently only implemented for MiniSat and SAT4J.");
        if (old.eval==null) throw new ErrorAPI("This solution is already unsatisfiable, so you cannot call next() to get the next solution.");
        Instance inst = old.kEnumerator.next().instance();
        unrolls = old.unrolls;
        originalOptions = old.originalOptions;
        originalCommand = old.originalCommand;
        bitwidth = old.bitwidth;
        maxseq = old.maxseq;
View Full Code Here

    A4Solution solve(final A4Reporter rep, Command cmd, Simplifier simp, boolean tryBookExamples) throws Err, IOException {
        // If already solved, then return this object as is
        if (solved) return this;
        // If cmd==null, then all four arguments are ignored, and we simply use the lower bound of each relation
        if (cmd==null) {
           Instance inst = new Instance(bounds.universe());
           for(int max=max(), i=min(); i<=max; i++) {
              Tuple it = factory.tuple(""+i);
              inst.add(i, factory.range(it, it));
           }
           for(Relation r: bounds.relations()) inst.add(r, bounds.lowerBound(r));
           eval = new Evaluator(inst, solver.options());
           rename(this, null, null, new UniqueNameGenerator());
           solved();
           return this;
        }
        // Otherwise, prepare to do the solve...
        final A4Options opt = originalOptions;
        long time = System.currentTimeMillis();
        rep.debug("Simplifying the bounds...\n");
        if (simp!=null && formulas.size()>0 && !simp.simplify(rep, this, formulas)) addFormula(Formula.FALSE, Pos.UNKNOWN);
        rep.translate(opt.solver.id(), bitwidth, maxseq, solver.options().skolemDepth(), solver.options().symmetryBreaking());
        Formula fgoal = Formula.and(formulas);
        rep.debug("Generating the solution...\n");
        kEnumerator = null;
        Solution sol = null;
        final Reporter oldReporter = solver.options().reporter();
        final boolean solved[] = new boolean[]{true};
        solver.options().setReporter(new AbstractReporter() { // Set up a reporter to catch the type+pos of skolems
            @Override public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
                try {
                    Type t=kv2typepos(decl.variable()).a;
                    if (t==Type.EMPTY) return;
                    for(int i=(predecl==null ? -1 : predecl.size()-1); i>=0; i--) {
                        Type pp=kv2typepos(predecl.get(i).variable()).a;
                        if (pp==Type.EMPTY) return;
                        t=pp.product(t);
                    }
                    kr2type(skolem, t);
                } catch(Throwable ex) { } // Exception here is not fatal
            }
            @Override public void solvingCNF(int primaryVars, int vars, int clauses) {
               if (solved[0]) return; else solved[0]=true; // initially solved[0] is true, so we won't report the # of vars/clauses
               if (rep!=null) rep.solve(primaryVars, vars, clauses);
           }
        });
        if (!opt.solver.equals(SatSolver.CNF) && !opt.solver.equals(SatSolver.KK) && tryBookExamples) { // try book examples
           A4Reporter r = "yes".equals(System.getProperty("debug")) ? rep : null;
           try { sol = BookExamples.trial(r, this, fgoal, solver, cmd.check); } catch(Throwable ex) { sol = null; }
        }
        solved[0] = false; // this allows the reporter to report the # of vars/clauses
        for(Relation r: bounds.relations()) { formulas.add(r.eq(r)); } // Without this, kodkod refuses to grow unmentioned relations
        fgoal = Formula.and(formulas);
        // Now pick the solver and solve it!
        if (opt.solver.equals(SatSolver.KK)) {
            File tmpCNF = File.createTempFile("tmp", ".java", new File(opt.tempDirectory));
            String out = tmpCNF.getAbsolutePath();
            Util.writeAll(out, debugExtractKInput());
            rep.resultCNF(out);
            return null;
         }
        if (opt.solver.equals(SatSolver.CNF)) {
            File tmpCNF = File.createTempFile("tmp", ".cnf", new File(opt.tempDirectory));
            String out = tmpCNF.getAbsolutePath();
            solver.options().setSolver(WriteCNF.factory(out));
            try { sol = solver.solve(fgoal, bounds); } catch(WriteCNF.WriteCNFCompleted ex) { rep.resultCNF(out); return null; }
            // The formula is trivial (otherwise, it would have thrown an exception)
            // Since the user wants it in CNF format, we manually generate a trivially satisfiable (or unsatisfiable) CNF file.
            Util.writeAll(out, sol.instance()!=null ? "p cnf 1 1\n1 0\n" : "p cnf 1 2\n1 0\n-1 0\n");
            rep.resultCNF(out);
            return null;
         }
        if (solver.options().solver()==SATFactory.ZChaff || !solver.options().solver().incremental()) {
           rep.debug("Begin solve()\n");
           if (sol==null) sol = solver.solve(fgoal, bounds);
           rep.debug("End solve()\n");
        } else {
           rep.debug("Begin solveAll()\n");
           kEnumerator = new Peeker<Solution>(solver.solveAll(fgoal, bounds));
           if (sol==null) sol = kEnumerator.next();
           rep.debug("End solveAll()\n");
        }
        if (!solved[0]) rep.solve(0, 0, 0);
        final Instance inst = sol.instance();
        // To ensure no more output during SolutionEnumeration
        solver.options().setReporter(oldReporter);
        // If unsatisfiable, then retreive the unsat core if desired
        if (inst==null && solver.options().solver()==SATFactory.MiniSatProver) {
           try {
View Full Code Here

    @Override public String toString() {
        if (!solved) return "---OUTCOME---\nUnknown.\n";
        if (eval == null) return "---OUTCOME---\nUnsatisfiable.\n";
        String answer = toStringCache;
        if (answer != null) return answer;
        Instance sol = eval.instance();
        StringBuilder sb = new StringBuilder();
        sb.append("---INSTANCE---\n" + "integers={");
        boolean firstTuple = true;
        for(IndexedEntry<TupleSet> e:sol.intTuples()) {
            if (firstTuple) firstTuple=false; else sb.append(", ");
            // No need to print e.index() since we've ensured the Int atom's String representation is always equal to ""+e.index()
            Object atom = e.value().iterator().next().atom(0);
            sb.append(atom2name(atom));
        }
View Full Code Here

TOP

Related Classes of kodkod.instance.Instance

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.