spot 2.16
Loading...
Searching...
No Matches
satsolver.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) by the Spot authors, see the AUTHORS file for details.
3//
4// This file is part of Spot, a model checking library.
5//
6// Spot is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3 of the License, or
9// (at your option) any later version.
10//
11// Spot is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14// License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#pragma once
20
21#include <spot/misc/common.hh>
22#include <spot/misc/tmpfile.hh>
23#include <vector>
24#include <stdexcept>
25#include <iosfwd>
26#include <initializer_list>
27
28struct PicoSAT; // forward
29
30namespace spot
31{
32 class printable;
33
44 {
45 private:
46 const char* satsolver;
47
48 public:
50
53
55 int run(printable* in, printable* out);
56
57 };
58
70 class SPOT_API satsolver
71 {
72 public:
77 ~satsolver();
78
80 void adjust_nvars(int nvars);
81
83 void set_nassumptions_vars(int nassumptions_vars);
84
86 void add(std::initializer_list<int> values);
87
89 void add(int v);
90
92 int get_nb_clauses() const;
93
95 int get_nb_vars() const;
96
98 std::pair<int, int> stats() const;
99
102 template<typename T>
103 void comment_rec(T single);
104
107 template<typename T, typename... Args>
108 void comment_rec(T first, Args... args);
109
112 template<typename T>
113 void comment(T single);
114
117 template<typename T, typename... Args>
118 void comment(T first, Args... args);
119
122 void assume(int lit);
123
124 typedef std::vector<bool> solution;
126 typedef std::pair<int, solution> solution_pair;
127
135
136 private: // methods
138 void start();
139
141 void end_clause();
142
146 picosat_get_sol(int res);
147
150 satsolver_get_sol(const char* filename);
151
153 bool
154 xcnf_mode();
155
156 private: // variables
159
160 // cnf streams and associated clause counter.
161 // The next 2 pointers will be != nullptr if SPOT_SATSOLVER is given.
162 temporary_file* cnf_tmp_;
163 std::ostream* cnf_stream_;
164 int nclauses_;
165 int nvars_;
166 int nassumptions_vars_; // Surplus of vars (for 'assume' algorithm).
167
170 int nsols_;
171
173 PicoSAT* psat_;
174
175 // The next 2 pointers will be initialized if SPOT_XCNF env var
176 // is set. This requires SPOT_SATSOLVER to be set as well.
177 std::ofstream* xcnf_tmp_;
178 std::ofstream* xcnf_stream_;
179 std::string path_;
180 };
181
182 template<typename T>
183 void
185 {
186 if (!psat_)
187 *cnf_stream_ << ' ' << single;
188 }
189
190 template<typename T, typename... Args>
191 void
192 satsolver::comment_rec(T first, Args... args)
193 {
194 if (!psat_)
195 {
196 *cnf_stream_ << ' ' << first;
197 comment_rec(args...);
198 }
199 }
200
201 template<typename T>
202 void
204 {
205 if (!psat_)
206 *cnf_stream_ << "c " << single;
207 }
208
209 template<typename T, typename... Args>
210 void
211 satsolver::comment(T first, Args... args)
212 {
213 if (!psat_)
214 {
215 *cnf_stream_ << "c " << first;
216 comment_rec(args...);
217 }
218 }
219
220}
A string formatter that dispatches %-escape sequences to printable objects.
Definition formater.hh:130
Abstract base class for objects that can be printed to a stream via a format string.
Definition formater.hh:35
Interface with a given sat solver.
Definition satsolver.hh:44
bool command_given()
Return true if a satsolver is given, false otherwise.
int run(printable *in, printable *out)
Run the given satsolver.
Interface with a SAT solver.
Definition satsolver.hh:71
std::vector< bool > solution
Definition satsolver.hh:124
std::pair< int, int > stats() const
Returns std::pair<nvars, nclauses>;.
void add(std::initializer_list< int > values)
Add a list of lit. to the current clause.
void add(int v)
Add a single lit. to the current clause.
solution_pair get_solution()
Return std::pair<solving_return_code, solution>.
satsolver()
Construct the sat solver and initialize variables. If no satsolver is provided through SPOT_SATSOLVER...
int get_nb_vars() const
Get the current number of variables.
std::pair< int, solution > solution_pair
Return code paired with a solution.
Definition satsolver.hh:126
void comment(T single)
Add a comment. It will start with "c ". It should be used only in debug mode after providing a satsol...
Definition satsolver.hh:203
void comment_rec(T single)
Add a comment. It should be used only in debug mode after providing a satsolver.
Definition satsolver.hh:184
void set_nassumptions_vars(int nassumptions_vars)
Declare the number of vars reserved for assumptions.
int get_nb_clauses() const
Get the current number of clauses.
void assume(int lit)
Assume a literal value. Must only be used with distributed picolib.
void adjust_nvars(int nvars)
Adjust the number of variables used in the cnf formula.
Temporary file name.
Definition tmpfile.hh:49
Definition automata.hh:26

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.8