Compare commits

..

No commits in common. '47e26c83d4fa173ee0048b20d260450f8e9a1089' and '849ed2761f6caeb20f4550ba8afc949514b3220b' have entirely different histories.

@ -1,84 +0,0 @@
#include "cnf.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
namespace fs = std::filesystem;
const fs::path CNF_DIR = "./CNF";
bool CNF::write_dimacs(const std::string& filename) const {
fs::create_directories(CNF_DIR);
fs::path path = CNF_DIR / filename;
path = path.lexically_normal();
std::ofstream out(path);
if (!out) {
std::cerr << "Error: Could not open file " << path << "\n";
return false;
}
out << "p cnf " << n << " " << clauses.size() << "\n";
for (const auto& cl : clauses) {
for (int lit : cl) {
out << lit << " ";
}
out << "0\n";
}
return true;
}
CNF CNF::read_dimacs(const std::string& filename) {
CNF f;
std::ifstream in(filename);
if (!in) {
throw std::runtime_error("Could not open DIMACS file.");
}
std::string line;
long expected_clauses = 0;
while (std::getline(in, line)) {
if (line.empty()) continue;
if (line[0] == 'c') continue;
if (line[0] == 'p') {
std::istringstream iss(line);
std::string tmp, format;
iss >> tmp >> format >> f.n >> expected_clauses;
if (format != "cnf") {
throw std::runtime_error("Unsupported DIMACS format.");
}
continue;
}
std::istringstream iss(line);
Clause cl;
int lit;
while (iss >> lit) {
if (lit == 0) break;
cl.push_back(lit);
}
if (!cl.empty()) {
f.clauses.push_back(cl);
}
}
if (expected_clauses != 0 &&
f.clauses.size() != (size_t)expected_clauses) {
std::cerr << "Warning: Clause count mismatch.\n";
}
return f;
}

@ -1,17 +0,0 @@
#pragma once
#include <string>
#include <vector>
using Clause = std::vector<int>;
class CNF {
public:
long n = 0;
std::vector<Clause> clauses;
virtual ~CNF() = default;
virtual bool write_dimacs(const std::string& filename) const;
static CNF read_dimacs(const std::string& filename);
};

@ -1,6 +1,8 @@
#include "kcnf.hpp" #include "kcnf.hpp"
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream> #include <iostream>
#include <random> #include <random>
#include <stdexcept> #include <stdexcept>
@ -11,6 +13,10 @@
#include <omp.h> #include <omp.h>
#endif #endif
namespace fs = std::filesystem;
const fs::path CNF_DIR = "./CNF";
long binom(long n, long k) { long binom(long n, long k) {
return (k > n) ? 0 : return (k > n) ? 0 :
(k == 0 || k == n) ? 1 : (k == 0 || k == n) ? 1 :
@ -120,25 +126,79 @@ KCNF KCNF::generate_random(long n, long c, long k) {
} }
bool KCNF::write_dimacs(const std::string& filename) const { bool KCNF::write_dimacs(const std::string& filename) const {
fs::create_directories(CNF_DIR);
fs::path path = CNF_DIR / filename;
path = path.lexically_normal();
std::ofstream out(path);
if (!out) {
std::cerr << "Error: Could not open file " << path << "\n";
return false;
}
out << "p cnf " << n << " " << clauses.size() << "\n";
for (const auto& cl : clauses) { for (const auto& cl : clauses) {
if ((long)cl.size() != k) { for (int lit : cl) {
throw std::runtime_error("Not a k-CNF formula."); out << lit << " ";
} }
out << "0\n";
} }
return CNF::write_dimacs(filename); return true;
} }
KCNF KCNF::read_dimacs(const std::string& filename) { KCNF KCNF::read_dimacs(const std::string& filename) {
CNF base = CNF::read_dimacs(filename);
KCNF f; KCNF f;
f.n = base.n;
f.clauses = std::move(base.clauses); std::ifstream in(filename);
if (!in) {
throw std::runtime_error("Could not open DIMACS file.");
}
std::string line;
long expected_clauses = 0;
while (std::getline(in, line)) {
if (line.empty()) continue;
if (line[0] == 'c') continue;
if (line[0] == 'p') {
std::istringstream iss(line);
std::string tmp, format;
iss >> tmp >> format >> f.n >> expected_clauses;
if (format != "cnf") {
throw std::runtime_error("Unsupported DIMACS format.");
}
continue;
}
std::istringstream iss(line);
Clause cl;
int lit;
while (iss >> lit) {
if (lit == 0) break;
cl.push_back(lit);
}
if (!cl.empty()) {
f.clauses.push_back(cl);
}
}
if (expected_clauses != 0 &&
f.clauses.size() != (size_t)expected_clauses) {
std::cerr << "Warning: Clause count mismatch.\n";
}
if (!f.clauses.empty()) { if (!f.clauses.empty()) {
f.k = f.clauses[0].size(); f.k = f.clauses[0].size();
// check whether actually k-cnf
for (const auto& cl : f.clauses) { for (const auto& cl : f.clauses) {
if ((long)cl.size() != f.k) { if ((long)cl.size() != f.k) {
throw std::runtime_error("Input is not a k-CNF formula."); throw std::runtime_error("Input is not a k-CNF formula.");

@ -1,13 +1,18 @@
#pragma once #pragma once
#include "cnf.hpp" #include <string>
#include <vector>
class KCNF : public CNF { using Clause = std::vector<int>; // makes it nicer to read
class KCNF {
public: public:
long n = 0;
long k = 0; long k = 0;
std::vector<Clause> clauses;
static KCNF generate_random(long n, long c, long k); static KCNF generate_random(long n, long c, long k);
bool write_dimacs(const std::string& filename) const override; bool write_dimacs(const std::string& filename) const;
static KCNF read_dimacs(const std::string& filename); static KCNF read_dimacs(const std::string& filename);
}; };

Loading…
Cancel
Save