solver_types.cpp
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-2011, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #include <clasp/solver_types.h>
00021 #include <clasp/solver.h>
00022 #include <clasp/clause.h>
00023 #include <new>
00024 namespace Clasp {
00026 // SolverStats
00028 bool SolverStats::enableStats(const SolverStats& o) {
00029         if (o.extra && !enableExtended()) { return false; }
00030         if (o.jumps && !enableJump())     { return false; }
00031         return true;
00032 }
00033 bool SolverStats::enableExtended() { 
00034         if (!extra) { extra = new (std::nothrow) ExtendedStats(); }
00035         return extra != 0;
00036 }
00037 bool SolverStats::enableJump() {
00038         if (!jumps) { jumps = new (std::nothrow) JumpStats(); }
00039         return jumps != 0;
00040 }
00041 void SolverStats::enableQueue(uint32 size) { 
00042         if (queue && queue->maxSize()!=size) { queue->destroy(); queue = 0; }
00043         if (!queue)                          { queue = SumQueue::create(size); }
00044 }
00045 void SolverStats::reset() {
00046         CoreStats::reset();
00047         if (queue) queue->resetGlobal();
00048         if (extra) extra->reset();
00049         if (jumps) jumps->reset();
00050 }
00051 void SolverStats::accu(const SolverStats& o) {
00052         CoreStats::accu(o);
00053         if (extra && o.extra) extra->accu(*o.extra);
00054         if (jumps && o.jumps) jumps->accu(*o.jumps);
00055 }
00056 void SolverStats::swapStats(SolverStats& o) {
00057         std::swap(static_cast<CoreStats&>(*this), static_cast<CoreStats&>(o));
00058         std::swap(extra, o.extra);
00059         std::swap(jumps, o.jumps);
00060 }
00061 double SolverStats::operator[](const char* path) const {
00062         bool ext = matchStatPath(path, "extra");
00063         if (ext || matchStatPath(path, "jumps")) {
00064                 if (!*path)      { return -2.0; }
00065                 if (ext && extra){ return (*extra)[path]; }
00066                 if (!ext&& jumps){ return (*jumps)[path]; }
00067                 return -3.0; 
00068         }
00069         return CoreStats::operator[](path);
00070 }
00071 const char* SolverStats::subKeys(const char* path) const {
00072         bool ext = matchStatPath(path, "extra");
00073         if (ext || matchStatPath(path, "jumps")) {
00074                 return ext ? ExtendedStats::keys(path) : JumpStats::keys(path); 
00075         }
00076         return 0;
00077 }
00079 // ClauseHead
00081 ClauseHead::Info::Info(const Clasp::ClauseInfo& init) {
00082         data.act  = init.activity();
00083         data.key  = !init.tagged() ? 0 : TAGGED_CLAUSE;
00084         data.lbd  = std::min(init.lbd(), uint32(ClauseHead::MAX_LBD));
00085         data.type = init.type();
00086 }
00087 
00088 ClauseHead::ClauseHead(const ClauseInfo& init) : info_(init){
00089         static_assert(sizeof(ClauseHead)<=32, "Unsupported Alignment");
00090         head_[2]  = negLit(0);
00091 }
00092 
00093 void ClauseHead::attach(Solver& s) {
00094         assert(head_[0] != head_[1] && head_[1] != head_[2]);
00095         s.addWatch(~head_[0], ClauseWatch(this));
00096         s.addWatch(~head_[1], ClauseWatch(this));
00097 }
00098 
00099 void ClauseHead::detach(Solver& s) {
00100         s.removeWatch(~head_[0], this);
00101         s.removeWatch(~head_[1], this);
00102 }
00103 
00104 bool ClauseHead::locked(const Solver& s) const {
00105         return (s.isTrue(head_[0]) && s.reason(head_[0]) == this)
00106           ||   (s.isTrue(head_[1]) && s.reason(head_[1]) == this);
00107 }
00108 
00109 bool ClauseHead::satisfied(const Solver& s) {
00110         return s.isTrue(head_[0]) || s.isTrue(head_[1]) || s.isTrue(head_[2]);
00111 }
00112 
00113 bool ClauseHead::toImplication(Solver& s) {
00114         ConstraintType t  = ClauseHead::type();
00115         uint32 sz         = isSentinel(head_[1]) ? 1 : 2 + (!s.isFalse(head_[2]) || s.level(head_[2].var()) > 0);
00116         ClauseRep rep     = ClauseRep::create(head_, sz, ClauseInfo(t).setLbd(2).setTagged(tagged()));
00117         bool   implicit   = s.allowImplicit(rep);
00118         bool   locked     = ClauseHead::locked(s) && s.decisionLevel() > 0;
00119         rep.prep          = 1;
00120         if ((locked || !implicit) && sz > 1) { return false; }
00121         s.add(rep, false);
00122         detach(s);
00123         return true;
00124 }
00126 // SmallClauseAlloc
00128 SmallClauseAlloc::SmallClauseAlloc() : blocks_(0), freeList_(0) { }
00129 SmallClauseAlloc::~SmallClauseAlloc() { 
00130         Block* r = blocks_;
00131         while (r) {
00132                 Block* t = r;
00133                 r = r->next;
00134                 ::operator delete(t);
00135         }
00136 }
00137 
00138 void SmallClauseAlloc::allocBlock() {
00139         Block* r = (Block*)::operator new(sizeof(Block));
00140         for (uint32 i = 0; i < Block::num_chunks-1; ++i) {
00141                 r->chunk[i].next = &r->chunk[i+1];
00142         }
00143         r->chunk[Block::num_chunks-1].next = freeList_;
00144         freeList_ = r->chunk;
00145         r->next   = blocks_;
00146         blocks_   = r;
00147 }
00148 
00149 }


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:40