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 00038 #ifdef HAVE_CONFIG_H 00039 # include "config.h" 00040 #endif 00041 00042 #include "dlvhex2/GenuineSolver.h" 00043 #include "dlvhex2/PlatformDefinitions.h" 00044 #include "dlvhex2/Benchmarking.h" 00045 #include "dlvhex2/Printer.h" 00046 #include "dlvhex2/Registry.h" 00047 #include "dlvhex2/ProgramCtx.h" 00048 #include "dlvhex2/AnswerSet.h" 00049 00050 #include "dlvhex2/InternalGrounder.h" 00051 #include "dlvhex2/InternalGroundDASPSolver.h" 00052 #include "dlvhex2/GringoGrounder.h" 00053 #include "dlvhex2/ClaspSolver.h" 00054 00055 #include <boost/tokenizer.hpp> 00056 #include <boost/thread.hpp> 00057 #include <boost/shared_ptr.hpp> 00058 #include <boost/foreach.hpp> 00059 #include <list> 00060 00061 #ifdef HAVE_LIBCLINGO 00062 #include <gringo/gringo_app.h> 00063 #endif 00064 00065 #include "dlvhex2/GringoGrounder.h" 00066 00067 DLVHEX_NAMESPACE_BEGIN 00068 00069 GenuineGrounderPtr GenuineGrounder::getInstance(ProgramCtx& ctx, const OrdinaryASPProgram& p, InterpretationConstPtr frozen) 00070 { 00071 00072 switch(ctx.config.getOption("GenuineSolver")) { 00073 case 1: case 3: // internal grounder + internal solver or clasp 00074 { 00075 if (!!frozen) { 00076 throw GeneralError("Internal grounder does not support frozen atoms"); 00077 } 00078 DBGLOG(DBG, "Instantiating genuine grounder with internal grounder"); 00079 GenuineGrounderPtr ptr(new InternalGrounder(ctx, p)); 00080 return ptr; 00081 } 00082 break; 00083 case 2: case 4: // Gringo + internal solver or clasp 00084 #ifdef HAVE_LIBGRINGO 00085 { 00086 DBGLOG(DBG, "Instantiating genuine grounder with gringo"); 00087 #ifndef GRINGO3 // GRINGO4 00088 GenuineGrounderPtr ptr(new GringoGrounder(ctx, p, frozen)); 00089 #else // GRINGO3 00090 // if (!!frozen){ 00091 // throw GeneralError("Gringo 3 does not support frozen atoms"); 00092 // } 00093 GenuineGrounderPtr ptr(new GringoGrounder(ctx, p, frozen)); 00094 #endif 00095 return ptr; 00096 } 00097 #else 00098 throw GeneralError("No support for gringo compiled into this binary"); 00099 #endif // HAVE_LIBGRINGO 00100 break; 00101 default: 00102 assert(false); 00103 return GenuineGrounderPtr(); 00104 } 00105 } 00106 00107 00108 GenuineGroundSolverPtr GenuineGroundSolver::getInstance(ProgramCtx& ctx, const AnnotatedGroundProgram& p, InterpretationConstPtr frozen, bool minCheck) 00109 { 00110 00111 switch (ctx.config.getOption("GenuineSolver")) { 00112 case 1: case 2: // internal grounder or Gringo + internal solver 00113 { 00114 DBGLOG(DBG, "Instantiating genuine solver with internal solver (min-check: " << minCheck << ")"); 00115 GenuineGroundSolverPtr ptr(minCheck ? new InternalGroundDASPSolver(ctx, p, frozen) : new InternalGroundASPSolver(ctx, p, frozen)); 00116 return ptr; 00117 } 00118 break; 00119 case 3: case 4: // internal grounder or Gringo + clasp 00120 #ifdef HAVE_LIBCLASP 00121 { 00122 DBGLOG(DBG, "Instantiating genuine solver with clasp (min-check: " << minCheck << ")"); 00123 // clasp 3 is always disjunctive 00124 GenuineGroundSolverPtr ptr(new ClaspSolver(ctx, p, frozen)); 00125 return ptr; 00126 } 00127 #else 00128 throw GeneralError("No support for clasp compiled into this binary"); 00129 #endif // HAVE_LIBCLASP 00130 break; 00131 default: 00132 assert(false); 00133 return GenuineGroundSolverPtr(); 00134 } 00135 } 00136 00137 00138 GenuineGroundSolverPtr GenuineGroundSolver::getInstance(ProgramCtx& ctx, const OrdinaryASPProgram& p, InterpretationConstPtr frozen, bool minCheck) 00139 { 00140 //DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sid, "grounding (GenuineGroundS.::getInst)"); 00141 00142 switch (ctx.config.getOption("GenuineSolver")) { 00143 case 1: case 2: // internal grounder or Gringo + internal solver 00144 { 00145 DBGLOG(DBG, "Instantiating genuine solver with internal solver (min-check: " << minCheck << ")"); 00146 GenuineGroundSolverPtr ptr(minCheck ? new InternalGroundDASPSolver(ctx, AnnotatedGroundProgram(ctx, p), frozen) : new InternalGroundASPSolver(ctx, AnnotatedGroundProgram(ctx, p), frozen)); 00147 return ptr; 00148 } 00149 break; 00150 case 3: case 4: // internal grounder or Gringo + clasp 00151 #ifdef HAVE_LIBCLASP 00152 { 00153 DBGLOG(DBG, "Instantiating genuine solver with clasp (min-check: " << minCheck << ")"); 00154 // clasp 3 is always disjunctive 00155 GenuineGroundSolverPtr ptr(new ClaspSolver(ctx, AnnotatedGroundProgram(ctx, p), frozen)); 00156 return ptr; 00157 } 00158 #else 00159 throw GeneralError("No support for clasp compiled into this binary"); 00160 #endif // HAVE_LIBCLASP 00161 break; 00162 default: 00163 assert(false); 00164 return GenuineGroundSolverPtr(); 00165 } 00166 } 00167 00168 00169 GenuineSolverPtr GenuineSolver::getInstance(ProgramCtx& ctx, const OrdinaryASPProgram& p, InterpretationConstPtr frozen, bool minCheck) 00170 { 00171 const OrdinaryASPProgram* gprog; 00172 GenuineGrounderPtr grounder; 00173 { 00174 DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexground, "HEX grounder time (GenuineSolver ctor)"); 00175 DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexground2, "HEX grounder time"); 00176 grounder = GenuineGrounder::getInstance(ctx, p, frozen); 00177 gprog = &grounder->getGroundProgram(); 00178 } 00179 00180 DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexsolve, "HEX solver time (GenuineSolver ctor)"); 00181 DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexsolve2, "HEX solver time"); 00182 GenuineGroundSolverPtr gsolver = GenuineGroundSolver::getInstance(ctx, *gprog, frozen, minCheck); 00183 return GenuineSolverPtr(new GenuineSolver(grounder, gsolver, grounder->getGroundProgram())); 00184 } 00185 00186 00187 std::string GenuineSolver::getStatistics() 00188 { 00189 return solver->getStatistics(); 00190 } 00191 00192 00193 const OrdinaryASPProgram& GenuineSolver::getGroundProgram() 00194 { 00195 return gprog; 00196 } 00197 00198 00199 void GenuineSolver::setOptimum(std::vector<int>& optimum) 00200 { 00201 solver->setOptimum(optimum); 00202 } 00203 00204 00205 InterpretationPtr GenuineSolver::getNextModel() 00206 { 00207 DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sidhexsolve, "HEX solver (GenuineSolver gNM)"); 00208 return solver->getNextModel(); 00209 } 00210 00211 00212 int GenuineSolver::getModelCount() 00213 { 00214 return solver->getModelCount(); 00215 } 00216 00217 00218 void GenuineSolver::addNogood(Nogood ng) 00219 { 00220 solver->addNogood(ng); 00221 } 00222 00223 00224 void GenuineSolver::restartWithAssumptions(const std::vector<ID>& assumptions) 00225 { 00226 solver->restartWithAssumptions(assumptions); 00227 } 00228 00229 00230 void GenuineSolver::addPropagator(PropagatorCallback* pb) 00231 { 00232 solver->addPropagator(pb); 00233 } 00234 00235 00236 void GenuineSolver::removePropagator(PropagatorCallback* pb) 00237 { 00238 solver->removePropagator(pb); 00239 } 00240 00241 Nogood GenuineSolver::getInconsistencyCause(InterpretationConstPtr explanationAtoms) 00242 { 00243 return solver->getInconsistencyCause(explanationAtoms); 00244 } 00245 00246 void GenuineSolver::addProgram(const AnnotatedGroundProgram& program, InterpretationConstPtr frozen) 00247 { 00248 solver->addProgram(program, frozen); 00249 } 00250 00251 void GenuineSolver::addNogoodSet(const NogoodSet& ns, InterpretationConstPtr frozen) 00252 { 00253 solver->addNogoodSet(ns, frozen); 00254 } 00255 00256 DLVHEX_NAMESPACE_END 00257 00258 00259 // vim:expandtab:ts=4:sw=4: 00260 // mode: C++ 00261 // End: