GenuineSolverPtr GenuineSolver::getInstance(ProgramCtx& ctx, const OrdinaryASPProgram& p, InterpretationConstPtr frozen, bool minCheck) { const OrdinaryASPProgram* gprog; GenuineGrounderPtr grounder; { DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexground, "HEX grounder time (GenuineSolver ctor)"); DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexground2, "HEX grounder time"); grounder = GenuineGrounder::getInstance(ctx, p, frozen); gprog = &grounder->getGroundProgram(); } DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexsolve, "HEX solver time (GenuineSolver ctor)"); DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexsolve2, "HEX solver time"); GenuineGroundSolverPtr gsolver = GenuineGroundSolver::getInstance(ctx, *gprog, frozen, minCheck); return GenuineSolverPtr(new GenuineSolver(grounder, gsolver, grounder->getGroundProgram())); }
bool CSVAnswerSetPrinterCallback::operator()( AnswerSetPtr as) { DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sid,"AnswerSetPrinterCallback"); // uses the Registry to print the interpretation, including // possible influence from AuxiliaryPrinter objects (if any are registered) Interpretation::Storage::enumerator it, it_end; RegistryPtr reg = as->interpretation->getRegistry(); // must be in this scope! Interpretation::Storage filteredbits; assert( !!filterpm && "filter must always be defined in CSV format" ); filterpm->updateMask(); filteredbits = as->interpretation->getStorage() & filterpm->mask()->getStorage(); it = filteredbits.first(); it_end = filteredbits.end(); std::ostream& o = std::cout; if (!firstas) o << std::endl; firstas = false; typedef std::pair<IDAddress, IDAddress> OT; std::vector<OT> output; while( it != it_end ) { const OrdinaryAtom& oatom = reg->ogatoms.getByAddress(*it); if (oatom.tuple.size() < 3) throw GeneralError("Atoms which define CSV output must have an arity of 2 or greater."); output.push_back(OT(oatom.tuple[1].address, *it)); ++it; } std::sort(output.begin(), output.end()); bool first = true; BOOST_FOREACH (OT outputElement, output){ const OrdinaryAtom& oatom = reg->ogatoms.getByAddress(outputElement.second); if (!first) o << std::endl; first = false; for (int i = 2; i < oatom.tuple.size(); ++i) { o << (i > 2 ? ";" : ""); if (oatom.tuple[i].isIntegerTerm()) o << oatom.tuple[i].address; else o << reg->terms.getByID(oatom.tuple[i]).getUnquotedString(); } } o << std::endl; // never abort return true; }
// gets next answer set or throws exception on error condition // returns AnswerSetPtr() on end of queue AnswerSetPtr ConcurrentQueueResults::getNextAnswerSet() { DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sid,"ConcurrentQueueRes:getNextAS"); assert(!!queue); AnswerSetQueueElementPtr qe; unsigned u = 0; queue->receive(qe,u); assert(!!qe); if( qe->error.empty() ) return qe->answerset; else throw FatalError("ConcurrentQueueResults error:" + qe->error); }
InterpretationPtr GuessAndCheckModelGenerator::generateNextModel() { RegistryPtr reg = factory.reg; // now we have postprocessed input in postprocessedInput DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidgcsolve, "guess and check loop"); InterpretationPtr modelCandidate; do { AnswerSetPtr guessas = guessres->getNextAnswerSet(); if( !guessas ) return InterpretationPtr(); modelCandidate = guessas->interpretation; DBGLOG_SCOPE(DBG,"gM", false); DBGLOG(DBG,"= got guess model " << *modelCandidate); DBGLOG(DBG, "doing compatibility check for model candidate " << *modelCandidate); assert(!factory.ctx.config.getOption("ExternalLearning") && "cannot use external learning in (non-genuine) GuessAndCheckModelGenerator"); bool compatible = isCompatibleSet( modelCandidate, postprocessedInput, factory.ctx, SimpleNogoodContainerPtr()); DBGLOG(DBG, "Compatible: " << compatible); if (!compatible) continue; // FLP check if (factory.ctx.config.getOption("FLPCheck")) { DBGLOG(DBG, "FLP Check"); if( !isSubsetMinimalFLPModel<ASMOrdinaryASPSolver>( modelCandidate, postprocessedInput, factory.ctx) ) continue; } else { DBGLOG(DBG, "Skipping FLP Check"); } // remove edb and the guess (from here we don't need the guess anymore) modelCandidate->getStorage() -= factory.gpMask.mask()->getStorage(); modelCandidate->getStorage() -= factory.gnMask.mask()->getStorage(); modelCandidate->getStorage() -= mask->getStorage(); DBGLOG(DBG,"= final model candidate " << *modelCandidate); return modelCandidate; } while(true); }
InterpretationPtr GenuineSolver::getNextModel() { DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexsolve, "HEX solver (GenuineSolver gNM)"); return solver->getNextModel(); }
bool AnswerSetPrinterCallback::operator()( AnswerSetPtr as) { DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sid,"AnswerSetPrinterCallback"); // uses the Registry to print the interpretation, including // possible influence from AuxiliaryPrinter objects (if any are registered) Interpretation::Storage::enumerator it, it_end; RegistryPtr reg = as->interpretation->getRegistry(); // must be in this scope! Interpretation::Storage filteredbits; if( !filterpm ) { const Interpretation::Storage& bits = as->interpretation->getStorage(); it = bits.first(); it_end = bits.end(); } else { filterpm->updateMask(); filteredbits = as->interpretation->getStorage() & filterpm->mask()->getStorage(); it = filteredbits.first(); it_end = filteredbits.end(); } std::ostream& o = std::cout; WARNING("TODO think about more efficient printing") o << '{'; if( it != it_end ) { bool gotOutput = reg->printAtomForUser(o, *it); //DBGLOG(DBG,"printed with prefix '' and output " << gotOutput << " " << // printToString<RawPrinter>(ID(ID::MAINKIND_ATOM | ID::SUBKIND_ATOM_ORDINARYG, *it), reg)); it++; for(; it != it_end; ++it) { if( gotOutput ) { gotOutput |= reg->printAtomForUser(o, *it, ","); //DBGLOG(DBG,"printed with prefix ',' and output " << gotOutput << " " << // printToString<RawPrinter>(ID(ID::MAINKIND_ATOM | ID::SUBKIND_ATOM_ORDINARYG, *it), reg)); } else { gotOutput |= reg->printAtomForUser(o, *it); //DBGLOG(DBG,"printed with prefix '' and output " << gotOutput << " " << // printToString<RawPrinter>(ID(ID::MAINKIND_ATOM | ID::SUBKIND_ATOM_ORDINARYG, *it), reg)); } } } o << '}'; as->printWeightVector(o); o << std::endl; if (ctx.config.getOption("WaitOnModel")) { std::cerr << "<waiting>" << std::endl; std::string line; std::getline(std::cin, line); } // never abort return true; }