dlvhex  2.5.0
include/dlvhex2/ClaspSolver.h
Go to the documentation of this file.
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 
00035 #ifndef CLASPSPSOLVER_HPP_INCLUDED__09122011
00036 #define CLASPSPSOLVER_HPP_INCLUDED__09122011
00037 
00038 #include "dlvhex2/PlatformDefinitions.h"
00039 
00040 #ifdef HAVE_LIBCLASP
00041 
00042 #define DISABLE_MULTI_THREADING  // we don't need multithreading capabilities
00043 
00044 #include "dlvhex2/ID.h"
00045 #include "dlvhex2/Interpretation.h"
00046 #include "dlvhex2/ProgramCtx.h"
00047 #include "dlvhex2/Nogood.h"
00048 #include "dlvhex2/Printhelpers.h"
00049 #include "dlvhex2/OrdinaryASPProgram.h"
00050 #include "dlvhex2/GenuineSolver.h"
00051 #include "dlvhex2/Set.h"
00052 #include "dlvhex2/SATSolver.h"
00053 #include "dlvhex2/UnfoundedSetChecker.h"
00054 #include "dlvhex2/AnnotatedGroundProgram.h"
00055 
00056 #include <vector>
00057 #include <set>
00058 #include <map>
00059 #include <queue>
00060 
00061 #include <boost/foreach.hpp>
00062 #include <boost/graph/graph_traits.hpp>
00063 #include <boost/graph/adjacency_list.hpp>
00064 #include <boost/shared_ptr.hpp>
00065 #include <boost/scoped_ptr.hpp>
00066 #include <boost/thread/thread.hpp>
00067 #include <boost/thread/mutex.hpp>
00068 #include <boost/thread/condition.hpp>
00069 #include <boost/date_time.hpp>
00070 #include <boost/date_time/posix_time/posix_time.hpp>
00071 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
00072 
00073 #define WITH_THREADS 0           // this is only relevant for option parsing, so we don't care at the moment
00074 
00075 #include "clasp/clasp_facade.h"
00076 #include "clasp/model_enumerators.h"
00077 #include "clasp/solve_algorithms.h"
00078 #include "clasp/cli/clasp_options.h"
00079 #include "program_opts/program_options.h"
00080 
00081 DLVHEX_NAMESPACE_BEGIN
00082 
00083 // forward declaration
00084 class PropagatorCallback;
00085 
00122 class ClaspSolver : public GenuineGroundSolver, public SATSolver
00123 {
00124     private:
00128         class ExternalPropagator : public Clasp::PostPropagator
00129         {
00130             private:
00132                 ClaspSolver& cs;
00133 
00134                 // for deferred propagation to HEX
00136                 boost::posix_time::ptime lastPropagation;
00138                 boost::posix_time::time_duration skipMaxDuration;
00140                 int skipAmount;
00142                 int skipCounter;
00143 
00144                 // current clasp assignment in terms of HEX
00146                 void startAssignmentExtraction();
00148                 void stopAssignmentExtraction();
00150                 InterpretationPtr currentIntr;
00152                 InterpretationPtr currentAssigned;
00154                 InterpretationPtr currentChanged;
00156                 std::vector<std::vector<IDAddress> > assignmentsOnDecisionLevel;
00157             public:
00162                 ExternalPropagator(ClaspSolver& cs);
00164                 virtual ~ExternalPropagator();
00165 
00170                 void callHexPropagators(Clasp::Solver& s);
00176                 bool addNewNogoodsToClasp(Clasp::Solver& s);
00177 
00178                 // inherited from clasp
00179                 virtual bool propagateFixpoint(Clasp::Solver& s, Clasp::PostPropagator* ctx);
00180                 virtual bool isModel(Clasp::Solver& s);
00181                 virtual Clasp::Constraint::PropResult propagate(Clasp::Solver& s, Clasp::Literal p, uint32& data);
00182                 virtual void undoLevel(Clasp::Solver& s);
00183                 virtual unsigned int priority() const;
00184         };
00185 
00186         // interface to clasp internals
00188         struct TransformNogoodToClaspResult
00189         {
00191             Clasp::LitVec clause;
00193             bool tautological;
00195             bool outOfDomain;
00202             TransformNogoodToClaspResult(Clasp::LitVec clause, bool tautological, bool outOfDomain) : clause(clause), tautological(tautological), outOfDomain(outOfDomain){}
00203         };
00204 
00205         // itoa/atio wrapper
00213         static std::string idAddressToString(IDAddress adr);
00221         static IDAddress stringToIDAddress(std::string str);
00222 
00231         void extractClaspInterpretation(Clasp::Solver& solver,
00232             InterpretationPtr currentIntr = InterpretationPtr(),
00233             InterpretationPtr currentAssigned = InterpretationPtr(),
00234             InterpretationPtr currentChanged = InterpretationPtr());
00235 
00236         // initialization/shutdown
00238         uint32_t false_;
00240         uint32_t nextVar;
00246         void freezeVariables(InterpretationConstPtr frozen, bool freezeByDefault);
00252         void sendWeightRuleToClasp(Clasp::Asp::LogicProgram& asp, ID ruleId);
00258         void sendOrdinaryRuleToClasp(Clasp::Asp::LogicProgram& asp, ID ruleId);
00264         void sendRuleToClasp(Clasp::Asp::LogicProgram& asp, ID ruleId);
00270         void sendProgramToClasp(const AnnotatedGroundProgram& p, InterpretationConstPtr frozen);
00275         void createMinimizeConstraints(const AnnotatedGroundProgram& p);
00281         void sendNogoodSetToClasp(const NogoodSet& ns, InterpretationConstPtr frozen);
00286         void interpretClaspCommandline(Clasp::Problem_t::Type type);
00290         void shutdownClasp();
00291 
00292         // learning
00299         TransformNogoodToClaspResult nogoodToClaspClause(const Nogood& ng, bool extendDomainIfNecessary = false);
00300 
00301         // management of the symbol table
00307         void prepareProblem(Clasp::Asp::LogicProgram& asp, const OrdinaryASPProgram& p);
00315         void prepareProblem(Clasp::SatBuilder& sat, const NogoodSet& ns);
00321         void updateSymbolTable();
00323         Clasp::Literal noLiteral;
00324 
00326         std::vector<Clasp::Literal> hexToClaspSolver;
00331         std::vector<Clasp::Literal> hexToClaspProgram;
00332         typedef std::vector<IDAddress> AddressVector;
00334         std::vector<AddressVector*> claspToHex;
00341         void storeHexToClasp(IDAddress addr, Clasp::Literal lit, bool alsoStoreNonoptimized = false);
00346         void resetAndResizeClaspToHex(unsigned size);
00347 
00353         inline bool isMappedToClaspLiteral(IDAddress addr) const { return addr < hexToClaspSolver.size() && hexToClaspSolver[addr] != noLiteral; }
00361         inline Clasp::Literal convertHexToClaspSolverLit(IDAddress addr, bool registerVar = false, bool inverseLits = false);
00369         inline Clasp::Literal convertHexToClaspProgramLit(IDAddress addr, bool registerVar = false, bool inverseLits = false);
00377         inline const AddressVector* convertClaspSolverLitToHex(int index);
00378 
00383         void outputProject(InterpretationPtr intr);
00384 
00385     protected:
00386         // structural program information
00388         ProgramCtx& ctx;
00390         InterpretationConstPtr projectionMask;
00392         RegistryPtr reg;
00393 
00394         // external learning
00396         Set<PropagatorCallback*> propagators;
00398         std::list<Nogood> nogoods;
00399 
00400         // instance information
00402         enum ProblemType
00403         {
00405             ASP,
00407             SAT
00408         };
00410         ProblemType problemType;
00411 
00412         // interface to clasp internals
00414         Clasp::Asp::LogicProgram asp;
00416         Clasp::SatBuilder sat;
00418         Clasp::MinimizeBuilder minb;
00420         Clasp::MinimizeConstraint* minc;
00422         Clasp::SharedMinimizeData* sharedMinimizeData;
00424         ProgramOptions::ParsedOptions parsedOptions;
00426         Clasp::Cli::ClaspCliConfig config;
00428         Clasp::SharedContext claspctx;
00430         std::auto_ptr<Clasp::BasicSolve> solve;
00432         std::auto_ptr<ProgramOptions::OptionContext> allOpts;
00434         std::auto_ptr<Clasp::Enumerator> modelEnumerator;
00436         std::auto_ptr<ProgramOptions::ParsedValues> parsedValues;
00438         std::auto_ptr<ExternalPropagator> ep;
00439 
00440         // control flow
00442         Clasp::LitVec assumptions;
00444         enum NextSolveStep
00445         {
00447             Restart,
00449             Solve,
00451             CommitModel,
00453             ExtractModel,
00455             ReturnModel,
00457             CommitSymmetricModel,
00459             Update
00460         };
00462         NextSolveStep nextSolveStep;
00464         InterpretationPtr model;
00466         bool enumerationStarted;
00468         bool inconsistent;
00469 
00470         // statistics
00472         int modelCount;
00473 
00474     public:
00475         // all of the following methods are inherited from GenuineGroundSolver.
00476 
00477         // constructors/destructors and initialization
00478         ClaspSolver(ProgramCtx& ctx, const AnnotatedGroundProgram& p, InterpretationConstPtr frozen = InterpretationConstPtr());
00479         ClaspSolver(ProgramCtx& ctx, const NogoodSet& ns, InterpretationConstPtr frozen = InterpretationConstPtr());
00480         virtual ~ClaspSolver();
00481         virtual void addProgram(const AnnotatedGroundProgram& p, InterpretationConstPtr frozen = InterpretationConstPtr());
00482         virtual Nogood getInconsistencyCause(InterpretationConstPtr explanationAtoms);
00483         virtual void addNogoodSet(const NogoodSet& ns, InterpretationConstPtr frozen);
00484 
00485         // search control
00486         void restartWithAssumptions(const std::vector<ID>& assumptions);
00487         virtual void setOptimum(std::vector<int>& optimum);
00488 
00489         // learning
00490         virtual void addPropagator(PropagatorCallback* pb);
00491         virtual void removePropagator(PropagatorCallback* pb);
00492         virtual void addNogood(Nogood ng);
00493 
00494         // querying
00495         virtual InterpretationPtr getNextModel();
00496         virtual int getModelCount();
00497         virtual std::string getStatistics();
00498 
00499         typedef boost::shared_ptr<ClaspSolver> Ptr;
00500         typedef boost::shared_ptr<const ClaspSolver> ConstPtr;
00501 };
00502 
00503 typedef ClaspSolver::Ptr ClaspSolverPtr;
00504 typedef ClaspSolver::ConstPtr ClaspSolverConstPtr;
00505 
00506 DLVHEX_NAMESPACE_END
00507 #endif
00508 #endif
00509 
00510 // vim:expandtab:ts=4:sw=4:
00511 // mode: C++
00512 // End: