solver_bind.h 7.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Project SUBool
 * Petr Kucera, 2017
 */
/**@file solver_bind.h
 * Binding class to solver library. */

#ifndef __SOLVER_BIND_H
#define __SOLVER_BIND_H

#include <chrono>
#include <future>
#include <list>
#include <memory>
#include <optional>
#include <thread>

18
#include "enum.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "normalform.h"

namespace SUBool
{
   class SolverInterface
   {
      static std::atomic<long unsigned> sSolveCountUNSAT;
      static std::atomic<long unsigned> sSolveCountSAT;
      static std::atomic<long unsigned> sSolveCountUNDEF;

    protected:
      static void
      IncreaseSolveCount(PartialValue result)
      {
         switch (result)
         {
         case PartialValue::TRUE:
            ++sSolveCountSAT;
            break;
         case PartialValue::FALSE:
            ++sSolveCountUNSAT;
            break;
         case PartialValue::UNDEF:
            ++sSolveCountUNDEF;
            break;
         }
      }

    public:
      enum class ADAPTATION
      {
         DEFAULT,
         LIKELY_SAT,
         LIKELY_UNSAT
      };

      virtual ~SolverInterface() = default;
      virtual bool Reset(const CNF &cnf) = 0;
      virtual bool AddCnf(const CNF &cnf) = 0;
      virtual bool AddClause(const Clause &clause) = 0;
      virtual bool AddClauses(const std::vector<Clause> &clause);
      static long unsigned
      SolveCountSAT()
      {
         return sSolveCountSAT;
      }
      static long unsigned
      SolveCountUNSAT()
      {
         return sSolveCountUNSAT;
      }
      static long unsigned
      SolveCountUNDEF()
      {
         return sSolveCountUNDEF;
      }
      static long unsigned
      SolveCount()
      {
         return sSolveCountSAT + sSolveCountUNSAT + sSolveCountUNDEF;
      }

      virtual bool Solve(const std::vector<Literal> &assump) const = 0;
      virtual bool Solve() const = 0;
      virtual bool Solve(const PartialAssignment &assump) const = 0;
      virtual bool Solve(const Assignment &assump) const = 0;

      template <class Rep, class Period>
      PartialValue SolveWithTimeout(
          const std::chrono::duration<Rep, Period> &timeout_duration) const;
      template <class Rep, class Period>
90
      PartialValue SolveWithTimeout(const std::vector<Literal> &assump,
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
          const std::chrono::duration<Rep, Period> &timeout_duration) const;

      virtual PartialValue ValueOfVariable(unsigned var) const = 0;
      virtual PartialValue ValueOfLiteral(const Literal &lit) const = 0;
      virtual PartialAssignment Model() const = 0;
      virtual Clause ConflictClause() const = 0;

      virtual void Interrupt() const = 0;

      virtual size_t LearntsSize() const = 0;
      virtual std::vector<Clause> Learnts() const = 0;

      virtual bool IsIncremental() const = 0;
   };

   struct SolverHandle
   {
      std::thread::id thread_id;
      std::unique_ptr<SolverInterface> solver;

      SolverHandle() : thread_id(), solver() {}
      SolverHandle(std::thread::id id, std::unique_ptr<SolverInterface> s)
          : thread_id(id), solver(std::move(s))
      {
      }
   };
   using SolverList = std::list<SolverHandle>;
   using SolverPtr = SolverList::iterator;
   using SolverConstPtr = SolverList::const_iterator;

   class SolverPool
   {
      SolverList mSolverList;

      static SolverPool sDefaultPool;

      std::mutex mMutex;

    public:
      SolverPool() : mSolverList(), mMutex() {}
      ~SolverPool() = default;

      static SolverPool &
      DefaultPool()
      {
         return sDefaultPool;
      }

      // template <class S> SolverPtr NewSolver();
140
      template <class S, class... Args> SolverPtr NewSolver(Args &&...arg);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

      void
      Release(SolverPtr &ptr)
      {
         std::lock_guard<std::mutex> lock(mMutex);
         if (ptr != NullPtr())
         {
            mSolverList.erase(ptr);
            ptr = NullPtr();
         }
      }

      void InterruptAll();
      void InterruptInThread(std::thread::id thread_id);
      SolverPtr
      NullPtr()
      {
         return mSolverList.end();
      }

      SolverConstPtr
      NullPtr() const
      {
         return mSolverList.end();
      }
   };

   inline bool
   valid_solver_ptr(SolverConstPtr solver_ptr)
   {
      return solver_ptr != SolverPool::DefaultPool().NullPtr();
   }

174
   class AbsSolverWrapper
175
   {
Kučera Petr RNDr. Ph.D.'s avatar
Kučera Petr RNDr. Ph.D. committed
176
177
    protected:
      SolverPtr mSolverPtr;
178
179

    public:
Kučera Petr RNDr. Ph.D.'s avatar
Kučera Petr RNDr. Ph.D. committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
      AbsSolverWrapper(SolverPtr ptr) : mSolverPtr(ptr) {}
      AbsSolverWrapper(const AbsSolverWrapper &s) = delete;
      AbsSolverWrapper(AbsSolverWrapper &&s) : mSolverPtr(s.mSolverPtr)
      {
         s.mSolverPtr = SolverPool::DefaultPool().NullPtr();
      }
      AbsSolverWrapper &operator=(const AbsSolverWrapper &s) = delete;
      AbsSolverWrapper &
      operator=(AbsSolverWrapper &&s)
      {
         SolverPool::DefaultPool().Release(mSolverPtr);
         mSolverPtr = s.mSolverPtr;
         s.mSolverPtr = SolverPool::DefaultPool().NullPtr();
         return *this;
      }

      virtual ~AbsSolverWrapper()
      {
         SolverPool::DefaultPool().Release(mSolverPtr);
      }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

      SolverInterface &
      Solver()
      {
         return *(mSolverPtr->solver);
      }

      const SolverInterface &
      Solver() const
      {
         return *(mSolverPtr->solver);
      }

      SolverPtr
      GetSolverPtr()
      {
         return mSolverPtr;
      }
      SolverConstPtr
      GetSolverPtr() const
      {
         return mSolverPtr;
      }
Kučera Petr RNDr. Ph.D.'s avatar
Kučera Petr RNDr. Ph.D. committed
223
   }; // namespace SUBool
224

225
226
227
   enum class SOLVER_TYPE
   {
      GLUCOSE,
228
      KISSAT
229
230
   };

231
   extern const EnumAnnotation<SOLVER_TYPE> kSolverTypeAnnotation;
232

233
234
   AbsSolverWrapper solver_wrapper(
       SOLVER_TYPE solver_type, SolverInterface::ADAPTATION adapt);
235
236
237
238
239

   template <class S> class SolverWrapperTemplate : public AbsSolverWrapper
   {
    public:
      template <class... Args>
240
      SolverWrapperTemplate(Args &&...arg)
Kučera Petr RNDr. Ph.D.'s avatar
Kučera Petr RNDr. Ph.D. committed
241
          : AbsSolverWrapper(SolverPool::DefaultPool().NewSolver<S>(
242
243
244
245
              std::forward<Args>(arg)...))
      {
      }

Kučera Petr RNDr. Ph.D.'s avatar
Kučera Petr RNDr. Ph.D. committed
246
247
248
249
      SolverWrapperTemplate(const SolverWrapperTemplate &s) = delete;
      SolverWrapperTemplate(SolverWrapperTemplate &&s) = default;
      SolverWrapperTemplate &operator=(const SolverWrapperTemplate &s) = delete;
      SolverWrapperTemplate &operator=(SolverWrapperTemplate &&s) = default;
250
251
   };

252
253
254
255
256
257
258
259
260
261
262
263
264
} // namespace SUBool

// Template functions implementation
template <class Rep, class Period>
SUBool::PartialValue
SUBool::SolverInterface::SolveWithTimeout(
    const std::chrono::duration<Rep, Period> &timeout_duration) const
{
   return SolveWithTimeout({}, timeout_duration);
}

template <class Rep, class Period>
SUBool::PartialValue
265
SUBool::SolverInterface::SolveWithTimeout(const std::vector<Literal> &assump,
266
267
    const std::chrono::duration<Rep, Period> &timeout_duration) const
{
268
269
   auto fut = std::async(
       std::launch::async, [this, &assump]() { return Solve(assump); });
270
271
272
273
274
275
276
277
278
279
280
   if (fut.wait_for(timeout_duration) == std::future_status::timeout)
   {
      Interrupt();
      fut.wait();
      return PartialValue::UNDEF;
   }
   return partial_value_of_bool(fut.get());
}

template <class S, class... Args>
SUBool::SolverPtr
281
SUBool::SolverPool::NewSolver(Args &&...arg)
282
283
284
{
   std::lock_guard<std::mutex> lock(mMutex);
   mSolverList.emplace_back(std::this_thread::get_id(),
285
       std::make_unique<S>(std::forward<Args>(arg)...));
286
287
288
   return std::prev(mSolverList.end());
}
#endif