/*! * \author Ruben Martins - ruben@sat.inesc-id.pt * * @section LICENSE * * Open-WBO, Copyright (c) 2013-2018, Ruben Martins, Vasco Manquinho, Ines Lynce * PBLib, Copyright (c) 2012-2013 Peter Steinke * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ #ifndef Enc_Adder_h #define Enc_Adder_h #ifdef SIMP #include "simp/SimpSolver.h" #else #include "core/Solver.h" #endif #include "Encodings.h" #include "core/SolverTypes.h" #include #include #include #include namespace openwbo { class Adder : public Encodings { public: Adder() { hasEncoding = false; } ~Adder() {} // Encode constraint. void encode(Solver *S, vec &lits, vec &coeffs, uint64_t rhs); // Update constraint. void update(Solver *S, uint64_t rhs); // Returns true if the encoding was built, otherwise returns false; bool hasCreatedEncoding() { return hasEncoding; } void encodeInc(Solver *S, vec &lits, vec &coeffs, uint64_t rhs, vec &assumptions); void updateInc(Solver *S, uint64_t rhs, vec& assumptions); protected: vec _output; vec clause; std::vector > _buckets; void FA_extra ( Solver *S, Lit xc, Lit xs, Lit a, Lit b, Lit c ); Lit FA_carry ( Solver *S, Lit a, Lit b, Lit c ); Lit FA_sum ( Solver *S, Lit a, Lit b, Lit c ); Lit HA_carry ( Solver *S, Lit a, Lit b); Lit HA_sum ( Solver *S, Lit a, Lit b ); void adderTree (Solver *S, std::vector< std::queue< Lit > > & buckets, vec< Lit >& result ); void lessThanOrEqual (Solver *S, vec< Lit > & xs, std::vector< uint64_t > & ys); void numToBits ( std::vector & bits, uint64_t n, uint64_t number ); uint64_t ld64(const uint64_t x); void lessThanOrEqualInc (Solver *S, vec< Lit > & xs, std::vector< uint64_t > & ys, vec& assumptions); #define wbsplit(half,wL,wR, ws,bs, wsL,bsL, wsR,bsR) \ wsL.clear(); bsL.clear(); wsR.clear(); bsR.clear(); \ int ii = 0; \ int wsSizeHalf = ws.size()/2; \ for(; ii < wsSizeHalf; ii++) { \ wsL.push(ws[ii]); \ bsL.push(bs[ii]); \ wL += ws[ii]; \ } \ for(; ii < ws.size(); ii++) { \ wsR.push(ws[ii]); \ bsR.push(bs[ii]); \ wR += ws[ii]; \ } void genWarnersFull(Lit& a, Lit& b, Lit& c, Lit& carry, Lit& sum, int comp, Solver* S, vec& lits); void genWarnersHalf(Lit& a, Lit& b, Lit& carry, Lit& sum, int comp, Solver* S, vec& lits); void genWarners(vec& weights, vec& blockings, uint64_t max, int k, int comp, Solver* S, const Lit zero, vec& lits, vec& linkingVar); void genWarners0(vec& weights, vec& blockings, uint64_t max,uint64_t k, int comp, Solver *S, vec& lits, vec& linkingVar); void lessthan(vec& linking, uint64_t k, vec& cc, Solver* S, vec& lits); vec cc; vec linkingVar; void wbSort(vec& weights, vec& blockings, vec& sweights, vec& sblockings) { sweights.clear(); sblockings.clear(); for(int i = 0; i < weights.size(); i++) { sweights.push(weights[i]); sblockings.push(blockings[i]); } } // koshi 20140121 void wbFilter(uint64_t UB, Solver* S,vec& lits, vec& weights, vec& blockings, vec& sweights, vec& sblockings) { sweights.clear(); sblockings.clear(); for(int i = 0; i < weights.size(); i++) { if (weights[i] < UB) { sweights.push(weights[i]); sblockings.push(blockings[i]); } else { lits.clear(); lits.push(~blockings[i]); S->addClause(lits); } } } }; } #endif