SAT Solver Template
Loading...
Searching...
No Matches
inout.hpp
Go to the documentation of this file.
1/**
2* @author Tim Luchterhand
3* @date 27.11.24
4* @file inout.hpp
5* @brief Contains functions for reading and writing dimacs format.
6*/
7
8#ifndef INOUT_HPP
9#define INOUT_HPP
10
11#include <ostream>
12#include <istream>
13#include <vector>
14#include <iterator>
15#include <sstream>
16
18#include "Clause.hpp"
19#include "util/concepts.hpp"
20
21
22/**
23 * @brief Namespace containing dimacs io utilities
24 */
25namespace sat::inout {
26 /**
27 * Converts an integer to a sat::Literal
28 * @param val literal value. The value +5 for example corresponds to the positive literal of the variable with
29 * ID 5, whereas -5 corresponds to the negative literal
30 * @return converted Literal
31 */
32 Literal from_dimacs(int val) noexcept;
33
34 /**
35 * Converts a sat::Literal to a dimacs literal
36 * @param l Literal to convert
37 * @return 1 based integer representing the literal in dimacs format
38 */
39 int to_dimacs(Literal l) noexcept;
40
41 /**
42 * Reads a SAT problem from a stream
43 * @param in input stream to read from
44 * @return std::pair containing (all clauses of the problem, the number of variables in the problem)
45 */
46 auto read_from_dimacs(std::istream &in) -> std::pair<std::vector<std::vector<Literal>>, std::size_t>;
47
48 /**
49 * Converts a range of clauses to dimacs format
50 * @tparam R clause range type
51 * @param clauses A range of clauses
52 * @return dimacs string
53 */
54 template<std::ranges::range R>
55 std::string to_dimacs(const R &clauses) {
56 static_assert(clause_like<std::ranges::range_value_t<R>>,
57 "The range you passed to this function does not hold elements that are clause-like");
58 Literal maxLit = 0;
59 std::size_t nClauses = 0;
60 for (const auto &c: clauses) {
61 ++nClauses;
62 for (Literal l: c) {
63 maxLit = std::max(maxLit.get(), l.get());
64 }
65 }
66
67 const std::size_t numVars = var(maxLit).get() + 1;
68 std::vector<std::string> lines;
69 lines.reserve(nClauses);
70 for (const auto &clause: clauses) {
71 std::stringstream ss;
72 for (auto l: clause) {
73 ss << to_dimacs(l) << " ";
74 }
75
76 ss << "0";
77 lines.emplace_back(ss.str());
78 }
79
80 std::stringstream ss;
81 ss << "p cnf " << numVars << " " << nClauses << std::endl;
82 for (const auto &l: lines) {
83 ss << l << std::endl;
84 }
85
86 return ss.str();
87 }
88
89 /**
90 * Converts a range of literals to dimacs format
91 * @tparam L Literal range type
92 * @param literals the literals to convert
93 * @return dimacs string
94 */
95 template<concepts::typed_range<Literal> L>
96 std::string to_dimacs(const L &literals) {
97 return to_dimacs(literals | std::views::transform([](auto literal) { return std::vector{literal}; }));
98 }
99}
100
101#endif //INOUT_HPP
Structure representing a literal in a CNF-SAT problem.
Definition basic_structures.hpp:56
short sign() const
Definition basic_structures.cpp:38
Structure representing a binary variable in a CNF-SAT problem.
Definition basic_structures.hpp:29
unsigned get() const
Definition basic_structures.cpp:18
Definition heuristics.hpp:33
Namespace containing dimacs io utilities.
Definition inout.cpp:34
auto read_from_dimacs(std::istream &in) -> std::pair< std::vector< std::vector< Literal > >, std::size_t >
Definition inout.cpp:45
int to_dimacs(Literal l) noexcept
Definition inout.cpp:40
Literal from_dimacs(int val) noexcept
Definition inout.cpp:35
std::string to_dimacs(const R &clauses)
Definition inout.hpp:55
std::string to_dimacs(const L &literals)
Definition inout.hpp:96
Definition basic_structures.cpp:10
Literal neg(Variable x)
Definition basic_structures.cpp:50
Literal pos(Variable x)
Definition basic_structures.cpp:46
Variable var(Literal l)
Definition basic_structures.cpp:54
Definition inout.cpp:14