GuessAndCheckModelGenerator::GuessAndCheckModelGenerator(
Factory& factory,
InterpretationConstPtr input):
FLPModelGeneratorBase(factory, input),
factory(factory)
{
    DBGLOG(DBG, "GnC-ModelGenerator is instantiated for a " << (factory.ci.disjunctiveHeads ? "" : "non-") << "disjunctive component");

    RegistryPtr reg = factory.reg;

    // create new interpretation as copy
    InterpretationPtr postprocInput;
    if( input == 0 ) {
        // empty construction
        postprocInput.reset(new Interpretation(reg));
    }
    else {
        // copy construction
        postprocInput.reset(new Interpretation(*input));
    }

    // augment input with edb
    WARNING("perhaps we can pass multiple partially preprocessed input edb's to the external solver and save a lot of processing here")
        postprocInput->add(*factory.ctx.edb);

    // remember which facts we must remove
    mask.reset(new Interpretation(*postprocInput));

    // manage outer external atoms
    if( !factory.outerEatoms.empty() ) {
        // augment input with result of external atom evaluation
        // use newint as input and as output interpretation
        IntegrateExternalAnswerIntoInterpretationCB cb(postprocInput);
        evaluateExternalAtoms(factory.ctx,
            factory.outerEatoms, postprocInput, cb);
        DLVHEX_BENCHMARK_REGISTER(sidcountexternalatomcomps,
            "outer eatom computations");
        DLVHEX_BENCHMARK_COUNT(sidcountexternalatomcomps,1);

        assert(!factory.xidb.empty() &&
            "the guess and check model generator is not required for "
            "non-idb components! (use plain)");
    }

    // assign to const member -> stays the same from here no!
    postprocessedInput = postprocInput;

    // start evaluate edb+xidb+gidb
    {
        DBGLOG(DBG,"evaluating guessing program");
        // no mask
        OrdinaryASPProgram program(reg, factory.xidb, postprocessedInput, factory.ctx.maxint);
        // append gidb to xidb
        program.idb.insert(program.idb.end(), factory.gidb.begin(), factory.gidb.end());
        ASPSolverManager mgr;
        guessres = mgr.solve(*factory.externalEvalConfig, program);
    }
}
Ejemplo n.º 2
0
PlainModelGenerator::InterpretationPtr
PlainModelGenerator::generateNextModel()
{
  RegistryPtr reg = factory.ctx.registry();
  if( currentResults == 0 )
  {
    do // breakout possibility
    {
      // we need to create currentResults

      // create new interpretation as copy
      Interpretation::Ptr newint;
      if( input == 0 )
      {
        // empty construction
        newint.reset(new Interpretation(reg));
      }
      else
      {
        // copy construction
        newint.reset(new Interpretation(*input));
      }

      // augment input with edb
      newint->add(*factory.ctx.edb);

      // remember facts so far (we have to remove these from any output)
      InterpretationConstPtr mask(new Interpretation(*newint));

      // manage outer external atoms
      if( !factory.eatoms.empty() )
      {
        // augment input with result of external atom evaluation
        // use newint as input and as output interpretation
        IntegrateExternalAnswerIntoInterpretationCB cb(newint);
        evaluateExternalAtoms(factory.ctx, factory.eatoms, newint, cb);
        DLVHEX_BENCHMARK_REGISTER(sidcountexternalanswersets,
            "outer eatom computations");
        DLVHEX_BENCHMARK_COUNT(sidcountexternalanswersets,1);

        if( factory.xidb.empty() )
        {
          // we only have eatoms -> return singular result

          // remove EDB and direct input from newint
          // (keep local models as small as possible)
          newint->getStorage() -= mask->getStorage();

          PreparedResults* pr = new PreparedResults;
          currentResults.reset(pr);
          pr->add(AnswerSetPtr(new AnswerSet(newint)));
          break;
        }
      }

      // store in model generator and store as const
      postprocessedInput = newint;

      DLVHEX_BENCHMARK_REGISTER_AND_START(sidaspsolve,
          "initiating external solver");
      OrdinaryASPProgram program(reg,
          factory.xidb, postprocessedInput, factory.ctx.maxint, mask);
      ASPSolverManager mgr;
      currentResults = mgr.solve(*factory.externalEvalConfig, program);
      DLVHEX_BENCHMARK_STOP(sidaspsolve);
    }
    while(false); // end of breakout possibility
  }

  assert(currentResults != 0);
  AnswerSet::Ptr ret = currentResults->getNextAnswerSet();
  if( ret == 0 )
  {
    currentResults.reset();
    // the following is just for freeing memory early
    postprocessedInput.reset();
    return InterpretationPtr();
  }
  DLVHEX_BENCHMARK_REGISTER(sidcountplainanswersets, "PlainMG answer sets");
  DLVHEX_BENCHMARK_COUNT(sidcountplainanswersets,1);

  return ret->interpretation;
}