|
| 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 |
0 commit comments