dlvhex
2.5.0
|
00001 /* dlvhex -- Answer-Set Programming with external interfaces. 00002 * Copyright (C) 2005-2007 Roman Schindlauer 00003 * Copyright (C) 2006-2015 Thomas Krennwallner 00004 * Copyright (C) 2009-2016 Peter Schüller 00005 * Copyright (C) 2011-2016 Christoph Redl 00006 * Copyright (C) 2015-2016 Tobias Kaminski 00007 * Copyright (C) 2015-2016 Antonius Weinzierl 00008 * 00009 * This file is part of dlvhex. 00010 * 00011 * dlvhex is free software; you can redistribute it and/or modify it 00012 * under the terms of the GNU Lesser General Public License as 00013 * published by the Free Software Foundation; either version 2.1 of 00014 * the License, or (at your option) any later version. 00015 * 00016 * dlvhex is distributed in the hope that it will be useful, but 00017 * WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with dlvhex; if not, write to the Free Software 00023 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 00024 * 02110-1301 USA. 00025 */ 00026 00036 #ifndef GENUINEGUESSANDCHECK_MODEL_GENERATOR_HPP_INCLUDED__09122011 00037 #define GENUINEGUESSANDCHECK_MODEL_GENERATOR_HPP_INCLUDED__09122011 00038 00039 #include "dlvhex2/PlatformDefinitions.h" 00040 #include "dlvhex2/fwd.h" 00041 #include "dlvhex2/ID.h" 00042 #include "dlvhex2/FLPModelGeneratorBase.h" 00043 #include "dlvhex2/ComponentGraph.h" 00044 #include "dlvhex2/PredicateMask.h" 00045 #include "dlvhex2/GenuineSolver.h" 00046 #include "dlvhex2/InternalGroundDASPSolver.h" 00047 #include "dlvhex2/UnfoundedSetChecker.h" 00048 #include "dlvhex2/NogoodGrounder.h" 00049 #include "dlvhex2/ExternalAtomVerificationTree.h" 00050 00051 #include <boost/unordered_map.hpp> 00052 #include <boost/shared_ptr.hpp> 00053 00054 DLVHEX_NAMESPACE_BEGIN 00055 00056 class GenuineGuessAndCheckModelGeneratorFactory; 00057 00059 class GenuineGuessAndCheckModelGenerator: 00060 public FLPModelGeneratorBase, 00061 public ostream_printable<GenuineGuessAndCheckModelGenerator>, 00062 public PropagatorCallback 00063 { 00064 // types 00065 public: 00066 typedef GenuineGuessAndCheckModelGeneratorFactory Factory; 00067 00068 // storage 00069 protected: 00070 // we store the factory again, because the base class stores it with the base type only! 00072 Factory& factory; 00074 RegistryPtr reg; 00075 00076 // information about verification/falsification of current external atom guesses 00078 std::vector<ID> activeInnerEatoms; 00080 boost::unordered_map<IDAddress, std::vector<int> > verifyWatchList; 00082 boost::unordered_map<IDAddress, std::vector<int> > unverifyWatchList; 00084 std::vector<bool> eaEvaluated; 00086 std::vector<bool> eaVerified; 00088 InterpretationPtr verifiedAuxes; 00090 std::vector<InterpretationPtr> changedAtomsPerExternalAtom; 00091 00092 // heuristics 00094 ExternalAtomEvaluationHeuristicsPtr defaultExternalAtomEvalHeuristics; 00096 std::vector<ExternalAtomEvaluationHeuristicsPtr> eaEvalHeuristics; 00097 /* \brief Heuristics to be used for unfounded set checking over partial assignments. */ 00098 UnfoundedSetCheckHeuristicsPtr ufsCheckHeuristics; 00099 00101 InterpretationConstPtr postprocessedInput; 00103 InterpretationPtr mask; 00104 00105 // internal solver 00107 NogoodGrounderPtr nogoodGrounder; 00109 SimpleNogoodContainerPtr learnedEANogoods; 00111 ExternalAtomVerificationTree eavTree; 00113 int learnedEANogoodsTransferredIndex; 00115 GenuineGrounderPtr grounder; 00117 GenuineGroundSolverPtr solver; 00119 int cmModelCount; 00121 InterpretationPtr explAtoms; 00123 InternalGroundDASPSolverPtr analysissolver; 00125 bool haveInconsistencyCause; 00127 Nogood inconsistencyCause; 00129 UnfoundedSetCheckerManagerPtr ufscm; 00131 InterpretationPtr programMask; 00132 00133 // members 00134 00142 void inlineExternalAtoms(OrdinaryASPProgram& program, GenuineGrounderPtr& grounder, AnnotatedGroundProgram& annotatedGroundProgram, std::vector<ID>& activeInnerEatoms); 00143 00151 ID replacePredForInlinedEAs(ID atomID, InterpretationConstPtr eliminatedExtAuxes); 00152 00158 void initializeExplanationAtoms(OrdinaryASPProgram& program); 00159 00163 void initializeInconsistencyAnalysis(); 00164 00168 void initializeHeuristics(); 00169 00173 void initializeVerificationWatchLists(); 00174 00179 void generalizeNogood(Nogood ng); 00180 00184 void learnSupportSets(); 00185 00195 void updateEANogoods(InterpretationConstPtr compatibleSet, InterpretationConstPtr assigned = InterpretationConstPtr(), InterpretationConstPtr changed = InterpretationConstPtr()); 00196 00206 bool finalCompatibilityCheck(InterpretationConstPtr modelCandidate); 00207 00218 bool isModel(InterpretationConstPtr compatibleSet); 00219 00228 bool unfoundedSetCheck(InterpretationConstPtr partialInterpretation, InterpretationConstPtr assigned = InterpretationConstPtr(), InterpretationConstPtr changed = InterpretationConstPtr(), bool partial = false); 00229 00238 IDAddress getWatchedLiteral(int eaIndex, InterpretationConstPtr search, bool truthValue); 00239 00244 void unverifyExternalAtoms(InterpretationConstPtr changed); 00245 00253 bool verifyExternalAtoms(InterpretationConstPtr partialInterpretation, InterpretationConstPtr assigned, InterpretationConstPtr changed); 00254 00266 bool verifyExternalAtom(int eaIndex, InterpretationConstPtr partialInterpretation, 00267 InterpretationConstPtr assigned = InterpretationConstPtr(), 00268 InterpretationConstPtr changed = InterpretationConstPtr(), 00269 bool* answeredFromCacheOrSupportSets = 0); 00281 bool verifyExternalAtomByEvaluation(int eaIndex, InterpretationConstPtr partialInterpretation, 00282 InterpretationConstPtr assigned = InterpretationConstPtr(), 00283 InterpretationConstPtr changed = InterpretationConstPtr(), 00284 bool* answeredFromCache = 0); 00285 00296 bool verifyExternalAtomBySupportSets(int eaIndex, InterpretationConstPtr partialInterpretation, 00297 InterpretationConstPtr assigned = InterpretationConstPtr(), 00298 InterpretationConstPtr changed = InterpretationConstPtr()); 00299 00304 const OrdinaryASPProgram& getGroundProgram(); 00305 00315 void propagate(InterpretationConstPtr partialInterpretation, InterpretationConstPtr assigned, InterpretationConstPtr changed); 00316 00317 public: 00323 GenuineGuessAndCheckModelGenerator(Factory& factory, InterpretationConstPtr input); 00324 00328 virtual ~GenuineGuessAndCheckModelGenerator(); 00329 00330 // generate and return next model, return null after last model 00331 virtual InterpretationPtr generateNextModel(); 00332 00336 void identifyInconsistencyCause(); 00337 00338 // handling inconsistencies propagated over multiple units 00339 virtual const Nogood* getInconsistencyCause(); 00340 virtual void addNogood(const Nogood* cause); 00341 }; 00342 00344 class GenuineGuessAndCheckModelGeneratorFactory: 00345 public FLPModelGeneratorFactoryBase, 00346 public ostream_printable<GenuineGuessAndCheckModelGeneratorFactory> 00347 { 00348 // types 00349 public: 00350 friend class GenuineGuessAndCheckModelGenerator; 00351 typedef ComponentGraph::ComponentInfo ComponentInfo; 00352 00353 // storage 00354 protected: 00356 ASPSolverManager::SoftwareConfigurationPtr externalEvalConfig; 00358 ProgramCtx& ctx; 00360 ComponentInfo ci; // should be a reference, but there is currently a bug in the copy constructor of ComponentGraph: it seems that the component info is shared between different copies of a component graph, hence it is deallocated when one of the copies dies. 00361 WARNING("TODO see comment above about ComponentInfo copy construction bug") 00362 00363 00364 std::vector<ID> outerEatoms; 00365 00367 std::vector<std::pair<Nogood, int> > succNogoods; 00368 public: 00375 GenuineGuessAndCheckModelGeneratorFactory( 00376 ProgramCtx& ctx, const ComponentInfo& ci, 00377 ASPSolverManager::SoftwareConfigurationPtr externalEvalConfig); 00378 00380 virtual ~GenuineGuessAndCheckModelGeneratorFactory() { } 00381 00382 // add inconsistency explanation nogoods from successor components 00383 virtual void addInconsistencyCauseFromSuccessor(const Nogood* cause); 00384 00388 virtual ModelGeneratorPtr createModelGenerator(InterpretationConstPtr input); 00389 00392 virtual std::ostream& print(std::ostream& o) const; 00396 virtual std::ostream& print(std::ostream& o, bool verbose) const; 00397 }; 00398 00399 DLVHEX_NAMESPACE_END 00400 #endif // GUESSANDCHECK_MODEL_GENERATOR_HPP_INCLUDED__09112010 00401 00402 // vim:expandtab:ts=4:sw=4: 00403 // mode: C++ 00404 // End: