Skip to content

Commit ec7cb67

Browse files
committed
separate out approxmc when counting
1 parent 7892cc6 commit ec7cb67

File tree

5 files changed

+334
-3
lines changed

5 files changed

+334
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ if (NOT NOUNIGEN)
529529
message(STATUS "ApproxMC static lib: ${APPROXMC_STATIC_LIBRARIES}")
530530
message(STATUS "ApproxMC static lib deps: ${APPROXMC_STATIC_LIBRARIES_DEPS}")
531531
message(STATUS "ApproxMC include dirs: ${APPROXMC_INCLUDE_DIRS}")
532+
include_directories(SYSTEM ${APPROXMC_INCLUDE_DIRS})
532533

533534
find_package(sbva CONFIG REQUIRED)
534535
message(STATUS "Found sbva")

include/stp/Sat/ApxMC.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/********************************************************************
2+
* AUTHORS: Arijit Shaw
3+
*
4+
* BEGIN DATE: November, 2023
5+
*
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
********************************************************************/
24+
25+
/*
26+
* Wraps around ApproxMC.
27+
*/
28+
#ifndef APXMC_H_
29+
#define APXMC_H_
30+
31+
#include "approxmc/approxmc.h"
32+
#include "stp/Sat/SATSolver.h"
33+
#include <arjun/arjun.h>
34+
35+
#include <unordered_set>
36+
37+
using std::vector;
38+
39+
namespace ApxMC
40+
{
41+
class SATSolver;
42+
}
43+
44+
namespace stp
45+
{
46+
#if defined(__GNUC__) || defined(__clang__)
47+
class __attribute__((visibility("default"))) ApxMC : public SATSolver
48+
#else
49+
class ApxMC : public SATSolver
50+
#endif
51+
52+
{
53+
ApproxMC::AppMC* a;
54+
ArjunNS::Arjun* arjun;
55+
uint64_t seed;
56+
57+
public:
58+
ApxMC(uint64_t unisamp_seed);
59+
60+
~ApxMC();
61+
62+
virtual void setMaxConflicts(int64_t max_confl); // set max solver conflicts
63+
64+
virtual void setMaxTime(int64_t max_time); // set max solver time in seconds
65+
66+
bool addClause(const vec_literals& ps); // Add a clause to the solver.
67+
68+
bool okay() const; // FALSE means solver is in a conflicting state
69+
70+
bool solve(bool& timeout_expired); // Search without assumptions.
71+
72+
virtual uint8_t modelValue(uint32_t x) const;
73+
74+
virtual uint32_t newVar();
75+
76+
void setVerbosity(int v);
77+
78+
unsigned long nVars() const;
79+
80+
void printStats() const;
81+
82+
virtual void enableRefinement(const bool enable);
83+
84+
// nb CryptoMiniSat has different literal values to the other minisats.
85+
virtual lbool true_literal() { return ((uint8_t)1); }
86+
virtual lbool false_literal() { return ((uint8_t)-1); }
87+
virtual lbool undef_literal() { return ((uint8_t)0); }
88+
89+
uint32_t
90+
getFixedCountWithAssumptions(const stp::SATSolver::vec_literals& assumps,
91+
const std::unordered_set<unsigned>& literals);
92+
93+
void solveAndDump();
94+
95+
private:
96+
void* temp_cl;
97+
int64_t max_confl = 0;
98+
int64_t max_time = 0; // seconds
99+
};
100+
} // namespace stp
101+
102+
#endif

lib/STPManager/STP.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ THE SOFTWARE.
3434
#endif
3535

3636
#ifdef USE_UNIGEN
37+
#include "stp/Sat/ApxMC.h"
3738
#include "stp/Sat/UniSamp.h"
3839
#include "stp/Sat/CMSGen.h"
3940
#endif
@@ -124,10 +125,9 @@ SATSolver* STP::get_new_sat_solver()
124125
break;
125126
case UserDefinedFlags::APPROXMC_SOLVER:
126127
#ifdef USE_UNIGEN
127-
newS = new UniSamp(bm->UserFlags.unisamp_seed, bm->UserFlags.num_samples,
128-
bm->UserFlags.samples_generated);
128+
newS = new ApxMC(bm->UserFlags.unisamp_seed);
129129
#else
130-
std::cerr << "UniSamp support was not enabled at configure time."
130+
std::cerr << "ApproxMC support was not enabled at configure time."
131131
<< std::endl;
132132
exit(-1);
133133
#endif

lib/Sat/ApxMC.cpp

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/********************************************************************
2+
* AUTHORS: Arijit Shaw
3+
*
4+
* BEGIN DATE: November, 2023
5+
*
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
********************************************************************/
24+
25+
#include "approxmc/approxmc.h"
26+
#include "stp/Sat/ApxMC.h"
27+
#include <algorithm>
28+
#include <unordered_set>
29+
using std::vector;
30+
31+
using namespace CMSat;
32+
using namespace ApxMC; // namespace in appmc library
33+
34+
namespace stp
35+
{
36+
37+
38+
void ApxMC::enableRefinement(const bool enable)
39+
{
40+
// might break if we simplify with refinement enabled..
41+
// if (enable)
42+
// {
43+
// s->set_no_simplify_at_startup();
44+
// }
45+
}
46+
47+
ApxMC::ApxMC(uint64_t unisamp_seed)
48+
{
49+
50+
a = new ApproxMC::AppMC;
51+
arjun = new ArjunNS::Arjun;
52+
seed = unisamp_seed;
53+
54+
a->set_verbosity(0);
55+
arjun->set_verbosity(0);
56+
// s->log_to_file("stp.cnf");
57+
//s->set_num_threads(num_threads);
58+
//s->set_default_polarity(false);
59+
//s->set_allow_otf_gauss();
60+
temp_cl = (void*)new vector<CMSat::Lit>;
61+
}
62+
63+
ApxMC::~ApxMC()
64+
{
65+
delete a;
66+
vector<CMSat::Lit>* real_temp_cl = (vector<CMSat::Lit>*)temp_cl;
67+
delete real_temp_cl;
68+
}
69+
70+
void ApxMC::setMaxConflicts(int64_t _max_confl)
71+
{
72+
max_confl = _max_confl;
73+
}
74+
75+
void ApxMC::setMaxTime(int64_t _max_time)
76+
{
77+
max_time = _max_time;
78+
}
79+
80+
bool ApxMC::addClause(const vec_literals& ps) // Add a clause to the solver.
81+
{
82+
// Cryptominisat uses a slightly different vec class.
83+
// Cryptominisat uses a slightly different Lit class too.
84+
85+
vector<CMSat::Lit>& real_temp_cl = *(vector<CMSat::Lit>*)temp_cl;
86+
real_temp_cl.clear();
87+
for (int i = 0; i < ps.size(); i++)
88+
{
89+
real_temp_cl.push_back(CMSat::Lit(var(ps[i]), sign(ps[i])));
90+
}
91+
arjun->add_clause(real_temp_cl);
92+
return a->add_clause(real_temp_cl);
93+
}
94+
95+
bool ApxMC::okay() const // FALSE means solver is in a conflicting state
96+
{
97+
//return a->okay();
98+
return true; //TODO AS: implement well
99+
}
100+
101+
bool ApxMC::solve(bool& timeout_expired) // Search without assumptions.
102+
{
103+
104+
/*
105+
* STP uses -1 for a value of "no timeout" -- this means that we only set the
106+
* timeout _in the SAT solver_ if the value is >= 0. This avoids us
107+
* accidentally setting a large limit (or one in the past).
108+
*/
109+
110+
// CMSat::lbool ret = s->solve(); // TODO AS
111+
std::cout << "c [stp->appmc] ApxMC solving instance with " << a->nVars()
112+
<< " variables." << std::endl;
113+
114+
vector<uint32_t> sampling_vars, sampling_vars_orig;
115+
for (uint32_t i = 0; i < a->nVars(); i++)
116+
sampling_vars.push_back(i);
117+
118+
arjun->set_seed(5);
119+
120+
sampling_vars_orig = sampling_vars;
121+
arjun->set_sampl_vars(sampling_vars_orig);
122+
sampling_vars = arjun->run_backwards();
123+
std::cout << "c [appmc->arjun] sampling var size [from arjun] "
124+
<< sampling_vars.size() << "\n";
125+
126+
delete arjun;
127+
128+
//TODO AS: this is debugging as Arjun is not performing correctly
129+
//sampling_vars = sampling_vars_orig;
130+
131+
a->set_sampl_vars(sampling_vars);
132+
133+
auto sol_count = a->count();
134+
135+
std::cout << "c [stp->appmc] Number of Solutions " << sol_count.cellSolCount
136+
<< "*2**" << sol_count.hashCount - 1 << std::endl;
137+
exit(0);
138+
return true;
139+
}
140+
141+
uint8_t ApxMC::modelValue(uint32_t x) const
142+
{
143+
// if (appmc_models[0].size() < sampling_vars.size())
144+
// std::cout << "c [stp->appmc] ERROR! found model size is not large enough\n";
145+
return true;
146+
}
147+
148+
uint32_t ApxMC::newVar()
149+
{
150+
a->new_var();
151+
arjun->new_var();
152+
return a->nVars() - 1;
153+
}
154+
155+
void ApxMC::setVerbosity(int v)
156+
{
157+
a->set_verbosity(0);
158+
arjun->set_verbosity(0);
159+
}
160+
161+
unsigned long ApxMC::nVars() const
162+
{
163+
return a->nVars();
164+
}
165+
166+
void ApxMC::printStats() const
167+
{
168+
// s->printStats();
169+
}
170+
171+
void ApxMC::solveAndDump()
172+
{
173+
bool t;
174+
solve(t);
175+
//s->open_file_and_dump_irred_clauses("clauses.txt");
176+
}
177+
178+
// Count how many literals/bits get fixed subject to the assumptions..
179+
uint32_t ApxMC::getFixedCountWithAssumptions(
180+
const stp::SATSolver::vec_literals& assumps,
181+
const std::unordered_set<unsigned>& literals)
182+
{
183+
/* TODO AS skip all?
184+
const uint64_t conf = 0; // TODO AS: s->get_sum_conflicts();
185+
assert(conf == 0);
186+
187+
188+
// const CMSat::lbool r = s->simplify(); TODO AS
189+
190+
191+
// Add the assumptions are clauses.
192+
vector<CMSat::Lit>& real_temp_cl = *(vector<CMSat::Lit>*)temp_cl;
193+
for (int i = 0; i < assumps.size(); i++)
194+
{
195+
real_temp_cl.clear();
196+
real_temp_cl.push_back(CMSat::Lit(var(assumps[i]), sign(assumps[i])));
197+
a->add_clause(real_temp_cl);
198+
}
199+
200+
201+
//std::cerr << assumps.size() << " assumptions" << std::endl;
202+
203+
std::vector<CMSat::Lit> zero = s->get_zero_assigned_lits();
204+
for (CMSat::Lit l : zero)
205+
{
206+
if (literals.find(l.var()) != literals.end())
207+
assigned++;
208+
}
209+
210+
211+
212+
//std::cerr << assigned << " assignments at end" <<std::endl;
213+
214+
// The assumptions are each single literals (corresponding to bits) that are true/false.
215+
// so in the result they should be all be set
216+
assert(assumps.size() >= 0);
217+
assert(assigned >= static_cast<uint32_t>(assumps.size()));
218+
assert(s->get_sum_conflicts() == conf ); // no searching, so no conflicts.
219+
assert(CMSat::l_False != r); // always satisfiable.
220+
*/
221+
222+
uint32_t assigned = 0;
223+
224+
return assigned;
225+
}
226+
227+
} //end namespace stp

lib/Sat/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ if (USE_UNIGEN)
3535
include_directories(${CMSGEN_INCLUDE_DIRS})
3636
include_directories(${ARJUN_INCLUDE_DIRS})
3737
message(STATUS "Including ApproxMC, CMSGen and UniGen")
38+
set(sat_lib_to_add ${sat_lib_to_add} ApxMC.cpp)
3839
set(sat_lib_to_add ${sat_lib_to_add} UniSamp.cpp)
3940
set(sat_lib_to_add ${sat_lib_to_add} CMSGen.cpp)
4041
endif()

0 commit comments

Comments
 (0)