program_builder.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006, 2007, 2012, 2013 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 
00021 #ifndef CLASP_PROGRAM_BUILDER_H_INCLUDED
00022 #define CLASP_PROGRAM_BUILDER_H_INCLUDED
00023 
00024 #ifdef _MSC_VER
00025 #pragma once
00026 #endif
00027 
00028 #include <clasp/literal.h>
00029 #include <clasp/claspfwd.h>
00030 #include <clasp/util/misc_types.h>
00031 #include <map>
00032 #include <iosfwd>
00033 
00034 namespace Clasp {
00035 
00041 
00043 class ProgramBuilder {
00044 public:
00045         typedef SharedMinimizeData SharedMinimize;
00046         typedef SingleOwnerPtr<SharedMinimize, ReleaseObject> MinPtr;
00047         ProgramBuilder();
00048         virtual ~ProgramBuilder();
00050 
00056         bool startProgram(SharedContext& ctx);
00058         bool parseProgram(StreamSource& prg);
00059         bool parseProgram(std::istream& prg);
00061         bool updateProgram();
00063         bool endProgram();
00065 
00068         void             getAssumptions(LitVec& out) const;
00070 
00074         SharedMinimize*  getMinimizeConstraint(SumVec* softBound = 0) const;
00076         void             disposeMinimizeConstraint();
00078         int              type()   const { return doType(); }
00080         bool             frozen() const { return frozen_; }
00082         virtual bool     ok()     const;
00084         SharedContext*   ctx()    const { return ctx_; }
00085 protected:
00086         void addMinLit(WeightLiteral x);
00087         void addMinRule(const WeightLitVec& lits);
00088         void disposeMin();
00089         void setFrozen(bool frozen)  { frozen_ = frozen; }
00090         void setCtx(SharedContext* x){ ctx_    = x; }
00091 private:
00092         typedef SingleOwnerPtr<MinimizeBuilder> MinBuildPtr;
00093         ProgramBuilder(const ProgramBuilder&);
00094         ProgramBuilder& operator=(ProgramBuilder&);
00095         virtual void getMinBound(SumVec& out)      const;
00096         virtual bool doStartProgram()                    = 0;
00097         virtual bool doUpdateProgram()                   = 0;
00098         virtual bool doEndProgram()                      = 0;
00099         virtual void doGetAssumptions(LitVec& out) const = 0;
00100         virtual int  doType() const                      = 0;
00101         virtual bool doParse(StreamSource& prg)          = 0;
00102         SharedContext*      ctx_;
00103         mutable MinBuildPtr min_;
00104         mutable MinPtr      minCon_;
00105         bool                frozen_;
00106 };
00107 
00109 class SatBuilder : public ProgramBuilder {
00110 public:
00111         explicit SatBuilder(bool maxSat = false);
00112         // program definition
00113         
00115 
00122         void prepareProblem(uint32 numVars, wsum_t hardClauseWeight = 0, uint32 clauseHint = 100);
00124         Var numVars() const { return vars_; }
00126 
00138         bool addClause(LitVec& clause, wsum_t cw = 0);
00139 private:
00140         typedef PodVector<uint8>::type VarState;
00141         bool doStartProgram();
00143         bool doParse(StreamSource& prg);
00144         int  doType() const                   { return Problem_t::SAT; }
00145         bool doUpdateProgram()                { return !frozen(); }
00146         void doGetAssumptions(LitVec&) const  { }
00147         bool doEndProgram();
00148         bool satisfied(LitVec& clause);
00149         bool markAssigned();
00150         void markLit(Literal x) { varState_[x.var()] |= 1 + x.sign(); }
00151         VarState varState_;
00152         LitVec   softClauses_;
00153         wsum_t   hardWeight_;
00154         Var      vars_;
00155         uint32   pos_;
00156         bool     maxSat_;
00157 };
00158 
00160 class PBBuilder : public ProgramBuilder {
00161 public:
00162         PBBuilder();
00163         // program definition
00165 
00171         void    prepareProblem(uint32 numVars, uint32 maxProduct, uint32 maxSoft, uint32 constraintHint = 100);
00173         uint32  numVars() const { return nextVar_; }
00175 
00187         bool    addConstraint(WeightLitVec& lits, weight_t bound, bool eq = false, weight_t cw = 0);
00189 
00194         Literal addProduct(LitVec& lits);
00196         bool    addObjective(const WeightLitVec& min);
00198         bool    setSoftBound(wsum_t bound);
00199 private:
00200         typedef std::map<LitVec, Literal> ProductIndex;
00201         bool doStartProgram();
00202         void getMinBound(SumVec& out) const;
00203         int  doType() const                   { return Problem_t::PB; }
00204         bool doUpdateProgram()                { return !frozen(); }
00205         void doGetAssumptions(LitVec&) const  { }
00207         bool doParse(StreamSource& prg);
00208         bool doEndProgram();
00209         bool productSubsumed(LitVec& lits, Literal& subLit);
00210         void addProductConstraints(Literal eqLit, LitVec& lits);
00211         Var  getNextVar();
00212         ProductIndex products_;
00213         uint32       nextVar_;
00214         uint32       maxVar_;
00215         wsum_t       soft_;
00216 };
00217 }
00218 #endif
00219 


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