enumerator.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-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 #ifndef CLASP_ENUMERATOR_H_INCLUDED
00021 #define CLASP_ENUMERATOR_H_INCLUDED
00022 
00023 #ifdef _MSC_VER
00024 #pragma once
00025 #endif
00026 #include <clasp/literal.h>
00027 #include <clasp/constraint.h>
00028 #include <clasp/minimize_constraint.h>
00029 #include <clasp/util/misc_types.h>
00030 
00031 namespace Clasp { 
00032 class Solver;
00033 class SharedContext;
00034 class Enumerator;
00035 class EnumerationConstraint;
00036 
00038 struct Model {
00039         enum Type { model_sat = 0, model_cons = 1, max_value = 1 };
00041         bool     consequences()    const { return (type & model_cons) != 0; }
00043         ValueRep value(Var v)      const { assert(values && v < values->size()); return (*values)[v]; }
00045         bool     isTrue(Literal p) const { return (value(p.var()) & trueValue(p)) != 0; }
00046         uint64                    num;    // running number of this model
00047         const ValueVec*           values; // variable assignment or consequences
00048         const SharedMinimizeData* costs;  // associated costs (or 0)
00049         uint32                    sId :16;// id of solver that found the model
00050         uint32                    type:14;// type of model
00051         uint32                    opt : 1;// whether the model is optimal w.r.t costs (0: unknown)
00052         uint32                    sym : 1;// whether symmetric models are possible
00053 };
00054 
00059 
00061 struct EnumOptions {  
00062         typedef MinimizeMode OptMode;
00063         enum EnumType { enum_auto = 0, enum_bt  = 1, enum_record  = 2, enum_consequences = 4, enum_brave = 5, enum_cautious = 6 };
00064         EnumOptions() : numModels(-1), type(enum_auto), opt(MinimizeMode_t::optimize), project(0), maxSat(false) {}
00065         static Enumerator* createModelEnumerator(const EnumOptions& opts);
00066         static Enumerator* createConsEnumerator(const EnumOptions& opts);
00067         static Enumerator* nullEnumerator();
00068         
00069         Enumerator* createEnumerator() const;
00070         bool        consequences()     const { return (type & enum_consequences) != 0; }
00071         bool        optimize()         const { return ((opt  & MinimizeMode_t::optimize) != 0); }
00072         int      numModels; 
00073         EnumType type;      
00074         OptMode  opt;       
00075         uint32   project;   
00076         SumVec   bound;     
00077         bool     maxSat;    
00078 };
00079 
00080 
00082 
00094 class Enumerator {
00095 public:
00096         typedef EnumerationConstraint*       ConPtr;
00097         typedef const EnumerationConstraint* ConPtrConst;
00098         typedef const SharedMinimizeData*    Minimizer;
00099         typedef EnumOptions::OptMode         OptMode;
00100         explicit Enumerator();
00101         virtual ~Enumerator();
00102 
00103         void   reset();
00104 
00106 
00120         int    init(SharedContext& problem, SharedMinimizeData* min = 0, int limit = 0);
00121         
00123 
00129         bool   start(Solver& s, const LitVec& path = LitVec(), bool disjointPath = false)  const;
00130         
00132 
00141         bool   update(Solver& s)  const;
00142         
00144 
00156         bool   commitModel(Solver& s);
00158         bool   commitSymmetric(Solver& s);
00160 
00169         bool   commitUnsat(Solver& s);
00171 
00177         bool   commitComplete();
00179 
00187         uint8  commit(Solver& s);
00189         void   end(Solver& s) const;
00191         uint64 enumerated()   const { return model_.num; }
00193 
00196         const Model& lastModel()        const { return model_; }
00198         bool         optimize()         const { return mini_ && mini_->mode() != MinimizeMode_t::enumerate && model_.opt == 0; }
00200         bool         tentative()        const { return mini_ && mini_->mode() == MinimizeMode_t::enumOpt && model_.opt == 0; }
00202         virtual int  modelType()        const { return Model::model_sat; }
00204         virtual bool supportsRestarts() const { return true; }
00206         virtual bool supportsParallel() const { return true; }
00208         virtual bool exhaustive()       const { return mini_ && mini_->mode() != MinimizeMode_t::enumerate; }
00210         void         setDisjoint(Solver& s, bool b) const;
00212         void         setIgnoreSymmetric(bool b);
00213         Minimizer    minimizer()                    const { return mini_; }
00214 protected:
00216 
00219         virtual ConPtr doInit(SharedContext& ctx, MinimizeConstraint* m, int numModels) = 0;
00220         virtual void   doReset();
00221         ConPtr         constraint(const Solver& s) const;
00222 private:
00223         Enumerator(const Enumerator&);
00224         Enumerator& operator=(const Enumerator&);
00225         SharedMinimizeData* mini_;
00226         Model               model_;
00227 };
00228 
00230 
00234 class EnumerationConstraint : public Constraint {
00235 public:
00236         typedef EnumerationConstraint* ConPtr;
00237         typedef MinimizeConstraint*    MinPtr;
00239         bool     disjointPath()const { return (flags_ & flag_path_disjoint) != 0u; }
00240         ValueRep state()       const { return static_cast<ValueRep>(flags_ & 3u); }
00242         bool     optimize()    const;
00243         MinPtr   minimizer()   const { return mini_; }
00244         
00245         // Methods used by enumerator
00246         bool start(Solver& s, const LitVec& path, bool disjoint);
00247         void end(Solver& s);
00248         bool update(Solver& s);
00249         void setDisjoint(bool x);
00250         bool integrateBound(Solver& s) const;
00251         bool commitModel(Enumerator& ctx, Solver& s);
00252         bool commitUnsat(Enumerator& ctx, Solver& s);
00253         void setMinimizer(MinPtr min) { mini_ = min; }
00254 protected:
00255         EnumerationConstraint(Solver& s, MinimizeConstraint* min);
00256         virtual ~EnumerationConstraint();
00257         // base interface
00258         virtual void        destroy(Solver* s, bool detach);
00259         virtual PropResult  propagate(Solver&, Literal, uint32&) { return PropResult(true, true); }
00260         virtual void        reason(Solver&, Literal, LitVec&)    {}
00261         virtual bool        simplify(Solver& s, bool reinit);
00262         virtual bool        valid(Solver& s);
00263         // own interface
00264         virtual bool        doUpdate(Solver& s) = 0;
00265         virtual void        doCommitModel(Enumerator&, Solver&) {}
00266         MinimizeConstraint* cloneMinimizer(Solver& s) const;
00267 private:
00268         enum Flag { flag_path_disjoint = 4u };
00269         enum { clear_state_mask = ~uint32(value_true|value_false) };
00270         MinimizeConstraint* mini_;
00271         LitVec              next_;
00272         uint32              flags_ : 4;
00273         uint32              root_  : 28;
00274 };
00276 }
00277 
00278 #endif


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