solve_algorithms.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-2012, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 // Basic solving
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_; // active solver
00102         Params* params_; // active solving options
00103         Limits* limits_; // active solving limits
00104         State*  state_;  // internal solving 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; // next conflict limit
00113         uint32 lLimit; // next learnt limit
00114 };
00116 // General solve
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


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:39