Skip to content

Commit 47ff63f

Browse files
committed
Field now
1 parent 94177cc commit 47ff63f

File tree

8 files changed

+55
-41
lines changed

8 files changed

+55
-41
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ The system can be used as a library:
179179
```c++
180180
#include <approxmc/approxmc.h>
181181
#include <vector>
182-
#include <complex>
183182
#include <cassert>
184183

185184
using std::vector;

src/appmcconfig.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828

2929
#pragma once
3030

31+
#include <memory>
3132
#include <vector>
3233
#include <cstdint>
3334
#include <string>
3435
#include <gmpxx.h>
35-
#include <complex>
36-
37-
using std::complex;
36+
#include <cryptominisat5/solvertypesmini.h>
3837

3938
namespace AppMCInt {
4039

41-
struct Config {
40+
class Config {
41+
public:
42+
Config(const std::unique_ptr<CMSat::FieldGen>& _fg) : multiplier_weight(_fg->one()) {}
4243
uint32_t start_iter = 0;
4344
double epsilon = 0.80; //Tolerance. CAV-2020 paper default
4445
double delta = 0.2; //Confidence. CAV-2020 paper default
@@ -56,7 +57,7 @@ struct Config {
5657

5758
std::vector<uint32_t> sampl_vars;
5859
bool sampl_vars_set = false;
59-
complex<mpq_class> multiplier_weight = complex<mpq_class>(1,0);
60+
std::unique_ptr<CMSat::Field> multiplier_weight = nullptr;
6061
};
6162

6263
}

src/approxmc.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
#include "counter.h"
3131
#include "appmc_constants.h"
3232
#include "appmcconfig.h"
33+
#include "cryptominisat5/solvertypesmini.h"
3334
#include <iostream>
35+
#include <memory>
3436

3537
using std::cout;
3638
using std::endl;
37-
using std::complex;
3839
using namespace AppMCInt;
3940

4041
#if defined _WIN32
@@ -46,7 +47,9 @@ using namespace AppMCInt;
4647

4748
namespace ApproxMC {
4849
struct AppMCPrivateData {
49-
AppMCPrivateData(): counter(conf) {}
50+
AppMCPrivateData(const std::unique_ptr<CMSat::FieldGen>& fg):
51+
conf(fg),
52+
counter(conf, fg) {}
5053
Config conf;
5154
Counter counter;
5255
bool sampl_vars_declared = false;
@@ -55,9 +58,9 @@ namespace ApproxMC {
5558

5659
using namespace ApproxMC;
5760

58-
DLL_PUBLIC AppMC::AppMC()
61+
DLL_PUBLIC AppMC::AppMC(const std::unique_ptr<CMSat::FieldGen>& _fg)
5962
{
60-
data = new AppMCPrivateData;
63+
data = new AppMCPrivateData(_fg);
6164
data->counter.solver = new SATSolver();
6265
data->counter.solver->set_up_for_scalmc();
6366
data->counter.solver->set_allow_otf_gauss();
@@ -309,11 +312,11 @@ DLL_PUBLIC bool AppMC::get_sampl_vars_set() const {
309312
return data->conf.sampl_vars_set;
310313
}
311314

312-
DLL_PUBLIC void AppMC::set_multiplier_weight(const complex<mpq_class>& weight) {
313-
data->conf.multiplier_weight = weight;
315+
DLL_PUBLIC void AppMC::set_multiplier_weight(const std::unique_ptr<CMSat::Field>& weight) {
316+
data->conf.multiplier_weight = weight->dup();
314317
}
315318

316-
DLL_PUBLIC const complex<mpq_class>& AppMC::get_multiplier_weight() const {
319+
DLL_PUBLIC const std::unique_ptr<CMSat::Field>& AppMC::get_multiplier_weight() const {
317320
return data->conf.multiplier_weight;
318321
}
319322

@@ -327,7 +330,7 @@ DLL_PUBLIC void AppMC::set_weighted(const bool weighted) {
327330
DLL_PUBLIC void AppMC::set_projected(const bool) {
328331
}
329332

330-
DLL_PUBLIC void AppMC::set_lit_weight(const Lit&, const complex<mpq_class>&) {
333+
DLL_PUBLIC void AppMC::set_lit_weight(const Lit&, const std::unique_ptr<Field>&) {
331334
cout << "ERROR: Weighted ApproxMC is not supported" << endl;
332335
exit(-1);
333336
}

src/approxmc.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
#ifndef APPROXMC_H__
3131
#define APPROXMC_H__
3232

33+
#include <algorithm>
3334
#include <cstdint>
35+
#include <memory>
3436
#include <string>
3537
#include <vector>
3638
#ifdef CMS_LOCAL_BUILD
@@ -65,7 +67,7 @@ class AppMC
6567
#endif
6668
{
6769
public:
68-
AppMC();
70+
AppMC(const std::unique_ptr<CMSat::FieldGen>& _fg);
6971
~AppMC();
7072
ApproxMC::SolCount count();
7173
bool find_one_solution();
@@ -76,11 +78,11 @@ class AppMC
7678
bool get_sampl_vars_set() const;
7779
bool get_opt_sampl_vars_set() const { return false; }
7880
const std::vector<uint32_t>& get_sampl_vars() const;
79-
void set_multiplier_weight(const std::complex<mpq_class>& weight);
80-
const std::complex<mpq_class>& get_multiplier_weight() const;
81+
void set_multiplier_weight(const std::unique_ptr<CMSat::Field>& weight);
82+
const std::unique_ptr<CMSat::Field>& get_multiplier_weight() const;
8183
void set_weighted(const bool weighted);
8284
void set_projected(const bool projected);
83-
void set_lit_weight(const CMSat::Lit& lit, const std::complex<mpq_class>& weight);
85+
void set_lit_weight(const CMSat::Lit& lit, const std::unique_ptr<CMSat::Field>& weight);
8486

8587
// Adding constraints
8688
void new_var();

src/counter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include <list>
4242
#include <array>
4343
#include <cmath>
44-
#include <complex>
4544

4645
#include "counter.h"
4746
#include "appmc_constants.h"

src/counter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "appmcconfig.h"
3030
#include <fstream>
31+
#include <memory>
3132
#include <random>
3233
#include <map>
3334
#include <utility>
@@ -92,7 +93,7 @@ struct SparseData {
9293

9394
class Counter {
9495
public:
95-
Counter(Config& _conf) : conf(_conf) {}
96+
Counter(Config& _conf, const std::unique_ptr<FieldGen>& _fg) : fg(_fg->dup()), conf(_conf) {}
9697
ApproxMC::SolCount solve();
9798
string gen_rnd_bits(const uint32_t size,
9899
const uint32_t numhashes, SparseData& sparse_data);
@@ -109,6 +110,7 @@ class Counter {
109110
bool solver_add_xor_clause(const vector<Lit>& lits, const bool rhs);
110111

111112
private:
113+
std::unique_ptr<FieldGen> fg;
112114
Config& conf;
113115
ApproxMC::SolCount count();
114116
void add_appmc_options();

src/main.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
THE SOFTWARE.
2727
*/
2828

29+
#include "cryptominisat5/solvertypesmini.h"
30+
#include <memory>
2931
#include <string>
3032
#include <vector>
3133
#if defined(__GNUC__) && defined(__linux__)
3234
#include <cfenv>
3335
#endif
3436
#include <cstdint>
35-
#include <complex>
3637
#include <set>
3738
#include <gmp.h>
3839

@@ -50,7 +51,6 @@ using std::endl;
5051
using std::set;
5152
using std::string;
5253
using std::vector;
53-
using std::complex;
5454
ApproxMC::AppMC* appmc = nullptr;
5555
argparse::ArgumentParser program = argparse::ArgumentParser("approxmc");
5656

@@ -69,6 +69,7 @@ uint32_t sparse = 0;
6969
int dump_intermediary_cnf = 0;
7070
int arjun_gates = 0;
7171
bool debug = false;
72+
std::unique_ptr<CMSat::FieldGen> fg;
7273

7374
//Arjun
7475
ArjunNS::SimpConf simp_conf;
@@ -99,7 +100,7 @@ void print_version() {
99100

100101
void add_appmc_options()
101102
{
102-
ApproxMC::AppMC tmp;
103+
ApproxMC::AppMC tmp(fg);
103104
epsilon = tmp.get_epsilon();
104105
delta = tmp.get_delta();
105106
simplify = tmp.get_simplify();
@@ -217,18 +218,18 @@ template<class T> void read_stdin(T* myreader) {
217218
#endif
218219
}
219220

220-
void print_num_solutions(uint32_t cell_sol_cnt, uint32_t hash_count, const complex<mpq_class>& mult)
221-
{
222-
assert(mult.imag() == 0);
221+
void print_num_solutions(uint32_t cell_sol_cnt, uint32_t hash_count, const std::unique_ptr<Field>& mult) {
222+
const CMSat::Field* ptr = mult.get();
223+
const CMSat::FMpz* od = dynamic_cast<const CMSat::FMpz*>(ptr);
223224
cout << "c [appmc] Number of solutions is: "
224-
<< cell_sol_cnt << "*2**" << hash_count << "*" << mult << endl;
225+
<< cell_sol_cnt << "*2**" << hash_count << "*" << od->val << endl;
225226
if (cell_sol_cnt == 0) cout << "s UNSATISFIABLE" << endl;
226227
else cout << "s SATISFIABLE" << endl;
227228

228229
mpz_class num_sols(2);
229230
mpz_pow_ui(num_sols.get_mpz_t(), num_sols.get_mpz_t(), hash_count);
230231
num_sols *= cell_sol_cnt;
231-
mpq_class final = mult.real() * num_sols;
232+
mpq_class final = od->val * num_sols;
232233

233234
cout << "s mc " << final << endl;
234235
}
@@ -267,10 +268,10 @@ void set_approxmc_options()
267268
template<class T> void parse_file(const std::string& filename, T* reader) {
268269
#ifndef USE_ZLIB
269270
FILE * in = fopen(filename.c_str(), "rb");
270-
DimacsParser<StreamBuffer<FILE*, CMSat::FN>, T> parser(reader, nullptr, 0);
271+
DimacsParser<StreamBuffer<FILE*, CMSat::FN>, T> parser(reader, nullptr, 0, fg);
271272
#else
272273
gzFile in = gzopen(filename.c_str(), "rb");
273-
DimacsParser<StreamBuffer<gzFile, CMSat::GZ>, T> parser(reader, nullptr, 0);
274+
DimacsParser<StreamBuffer<gzFile, CMSat::GZ>, T> parser(reader, nullptr, 0, fg);
274275
#endif
275276
if (in == nullptr) {
276277
std::cout << "ERROR! Could not open file '" << filename
@@ -287,7 +288,7 @@ template<class T> void parse_file(const std::string& filename, T* reader) {
287288
if (!reader->get_sampl_vars_set()) {
288289
vector<uint32_t> tmp;
289290
for(uint32_t i = 0; i < reader->nVars(); i++) tmp.push_back(i);
290-
reader->set_sampl_vars(tmp); // will automatically set the opt_sampl_vars
291+
reader->set_sampl_vars(tmp);
291292
} else {
292293
// Check if CNF has all vars as indep. Then its's all_indep
293294
set<uint32_t> tmp;
@@ -320,7 +321,8 @@ int main(int argc, char** argv)
320321
if (i+1 < argc) command_line += " ";
321322
}
322323

323-
appmc = new ApproxMC::AppMC;
324+
fg = std::make_unique<CMSat::FGenMpz>();
325+
appmc = new ApproxMC::AppMC(fg);
324326
simp_conf.appmc = true;
325327
simp_conf.oracle_sparsify = false;
326328
simp_conf.iter1 = 2;
@@ -334,9 +336,9 @@ int main(int argc, char** argv)
334336
}
335337
set_approxmc_options();
336338

337-
ArjunNS::SimplifiedCNF cnf;
338-
auto files = program.get<std::vector<std::string>>("inputfile");
339-
string fname = files[0];
339+
ArjunNS::SimplifiedCNF cnf(fg);
340+
const auto& files = program.get<std::vector<std::string>>("inputfile");
341+
const string fname(files[0]);
340342
if (do_arjun) {
341343
parse_file(fname, &cnf);
342344
const auto orig_sampl_vars = cnf.sampl_vars;

tests/simpletest.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ THE SOFTWARE.
2323
#include "gtest/gtest.h"
2424

2525
#include "approxmc.h"
26+
#include "cryptominisat5/solvertypesmini.h"
2627
#include "test_helper.h"
28+
#include <memory>
2729
#include <string>
2830
#include <vector>
29-
#include <complex>
3031
using std::string;
3132
using std::vector;
3233

@@ -37,7 +38,8 @@ using std::vector;
3738

3839
TEST(normal_interface, start)
3940
{
40-
AppMC s;
41+
std::unique_ptr<FieldGen> fg = std::make_unique<CMSat::FGenMpz>();
42+
AppMC s(fg);
4143
s.set_sampl_vars({});
4244
SolCount c = s.count();
4345
EXPECT_EQ(1U, c.cellSolCount);
@@ -46,7 +48,8 @@ TEST(normal_interface, start)
4648

4749
TEST(normal_interface, example1)
4850
{
49-
AppMC s;
51+
std::unique_ptr<FieldGen> fg = std::make_unique<CMSat::FGenMpz>();
52+
AppMC s(fg);
5053
s.new_vars(2);
5154
s.add_clause(str_to_cl("-1, 2"));
5255
s.set_sampl_vars({0, 1});
@@ -57,7 +60,8 @@ TEST(normal_interface, example1)
5760

5861
TEST(normal_interface, example2)
5962
{
60-
AppMC s;
63+
std::unique_ptr<FieldGen> fg = std::make_unique<CMSat::FGenMpz>();
64+
AppMC s(fg);
6165
s.new_vars(10);
6266
vector<uint32_t> sampl;
6367
for(uint32_t i = 0; i < 10; i++) sampl.push_back(i);
@@ -70,7 +74,8 @@ TEST(normal_interface, example2)
7074

7175
TEST(normal_interface, example3)
7276
{
73-
AppMC s;
77+
std::unique_ptr<FieldGen> fg = std::make_unique<CMSat::FGenMpz>();
78+
AppMC s(fg);
7479
s.new_vars(10);
7580
vector<uint32_t> sampl;
7681
for(uint32_t i = 0; i < 10; i++) sampl.push_back(i);
@@ -84,7 +89,8 @@ TEST(normal_interface, example3)
8489

8590
TEST(normal_interface, example4)
8691
{
87-
AppMC s;
92+
std::unique_ptr<FieldGen> fg = std::make_unique<CMSat::FGenMpz>();
93+
AppMC s(fg);
8894
s.new_vars(10);
8995
vector<uint32_t> sampl;
9096
for(uint32_t i = 0; i < 10; i++) sampl.push_back(i);

0 commit comments

Comments
 (0)