Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef CLASP_SOLVE_ALGORITHMS_H_INCLUDED
00021 #define CLASP_SOLVE_ALGORITHMS_H_INCLUDED
00022
00023 #ifdef _MSC_VER
00024 #pragma warning (disable : 4200) // nonstandard extension used : zero-sized array
00025 #pragma once
00026 #endif
00027
00028 #include <clasp/solver_strategies.h>
00033 namespace Clasp {
00034
00036 struct SolveLimits {
00037 explicit SolveLimits(uint64 conf = UINT64_MAX, uint64 r = UINT64_MAX)
00038 : conflicts(conf)
00039 , restarts(r) {
00040 }
00041 bool reached() const { return conflicts == 0 || restarts == 0; }
00042 uint64 conflicts;
00043 uint64 restarts;
00044 };
00045
00047
00050 class BasicSolve {
00051 public:
00053
00057 BasicSolve(Solver& s, const SolveParams& p, SolveLimits* lim = 0);
00058 BasicSolve(Solver& s, SolveLimits* lim = 0);
00059 ~BasicSolve();
00060
00062
00069 bool assume(const LitVec& assumptions);
00070
00072
00082 ValueRep solve();
00084
00088 bool satisfiable(const LitVec& assumptions, bool init);
00089
00091 void reset(bool reinit = false);
00093 void reset(Solver& s, const SolveParams& p, SolveLimits* lim = 0);
00094 Solver& solver() { return *solver_; }
00095 private:
00096 BasicSolve(const BasicSolve&);
00097 BasicSolve& operator=(const BasicSolve&);
00098 typedef const SolveParams Params;
00099 typedef SolveLimits Limits;
00100 struct State;
00101 Solver* solver_;
00102 Params* params_;
00103 Limits* limits_;
00104 State* state_;
00105 };
00106
00107 struct BasicSolveEvent : SolveEvent<BasicSolveEvent> {
00108 enum EventOp { event_none = 0, event_deletion = 'D', event_exit = 'E', event_grow = 'G', event_restart = 'R' };
00109 BasicSolveEvent(const Solver& s, EventOp a_op, uint64 cLim, uint32 lLim) : SolveEvent<BasicSolveEvent>(s, verbosity_max), cLimit(cLim), lLimit(lLim) {
00110 op = a_op;
00111 }
00112 uint64 cLimit;
00113 uint32 lLimit;
00114 };
00116
00118 class Enumerator;
00120
00125 class SolveAlgorithm {
00126 public:
00130 explicit SolveAlgorithm(Enumerator* enumerator = 0, const SolveLimits& limit = SolveLimits());
00131 virtual ~SolveAlgorithm();
00132
00133 const Enumerator* enumerator() const { return enum_; }
00134 const SolveLimits& limits() const { return limits_; }
00135 virtual bool interrupted()const = 0;
00136
00137 void setEnumerator(Enumerator& e) { enum_ = &e; }
00138 void setEnumLimit(uint64 m) { enumLimit_= m; }
00139 void setLimits(const SolveLimits& x) { limits_ = x; }
00140
00142
00154 bool solve(SharedContext& ctx, const LitVec& assume = LitVec(), EventHandler* modelHandler = 0);
00155
00157
00160 virtual void resetSolve() = 0;
00161
00163 virtual void enableInterrupts() = 0;
00164
00166
00170 bool interrupt();
00171 protected:
00172 SolveAlgorithm(const SolveAlgorithm&);
00173 SolveAlgorithm& operator=(const SolveAlgorithm&);
00175 virtual bool doSolve(SharedContext& ctx, const LitVec& assume) = 0;
00177 virtual bool doInterrupt() = 0;
00178 bool reportModel(Solver& s) const;
00179 Enumerator& enumerator() { return *enum_; }
00180 private:
00181 SolveLimits limits_;
00182 Enumerator* enum_;
00183 EventHandler* onModel_;
00184 uint64 enumLimit_;
00185 };
00186
00187 class SequentialSolve : public SolveAlgorithm {
00188 public:
00189 explicit SequentialSolve(Enumerator* enumerator = 0, const SolveLimits& limit = SolveLimits());
00190 ~SequentialSolve();
00191 virtual bool interrupted() const;
00192 virtual void resetSolve();
00193 virtual void enableInterrupts();
00194 protected:
00195 virtual bool doSolve(SharedContext& ctx, const LitVec& assume);
00196 virtual bool doInterrupt();
00197 private:
00198 struct InterruptHandler;
00199 InterruptHandler* term_;
00200 };
00201
00202 struct BasicSolveOptions {
00203 SolveLimits limit;
00204 SolveAlgorithm* createSolveObject() const { return new SequentialSolve(0, limit); }
00205 static uint32 supportedSolvers() { return 1; }
00206 static uint32 recommendedSolvers() { return 1; }
00207 uint32 numSolver() const { return 1; }
00208 bool defaultPortfolio() const { return false; }
00209 };
00210
00211 }
00212 #endif