Main Page | Class Hierarchy | Class List | File List | Class Members

ValueOH.h

00001 // ValueOH.h -- The interface to the super class of all the value
00002 // ordering heuristics.
00003 
00004 /*
00005  * Copyright (C) 1998,2004 Tudor Hulubei <tudor@hulubei.net>.
00006  *
00007  * This library is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU Lesser General Public License as
00009  * published by the Free Software Foundation; either version 2, or (at
00010  * your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA,
00020  * 02111-1307, USA.
00021  */
00022 
00023 /*
00024  * NOTE: This is an interal header file.
00025  * You should not attempt to use it directly.
00026  */
00027 
00028 // $Id: ValueOH_8h-source.html,v 1.1 2005/05/25 12:37:18 tudor Exp $
00029 
00030 #ifndef _CSP_KERNEL_ValueOH_H
00031 #define _CSP_KERNEL_ValueOH_H
00032 
00033 
00034 CSP_NAMESPACE_BEGIN(csp);
00035 
00036 
00037 class StrictWeakValueOrdering
00038 {
00039   public:
00040     virtual ~StrictWeakValueOrdering() {}
00041     virtual bool operator()(
00042         const Value* /* value0 */,
00043         const Value* /* value1 */) = 0;
00044 };
00045 
00046 
00048 class StrictWeakValueOrderingWrapper
00049 {
00050   public:
00051     StrictWeakValueOrderingWrapper(
00052         StrictWeakValueOrdering& implementation) :
00053         m_implementation(implementation) {}
00054 
00056     bool operator()(
00057         const Value* value0,
00058         const Value* value1)
00059     {
00060         return m_implementation(value0, value1);
00061     }
00062 
00063   private:
00064     StrictWeakValueOrdering& m_implementation;
00065 };
00066 
00067 
00071 class CSP_API ValueOH :
00072     public OrderingHeuristic
00073 {
00074   public:
00085     ValueOH(
00086         const wstring& name,
00087         Decomposition& decomposition,
00088         const vector<pair<double, double> >& intervals,
00089         double quality = 1,
00090         bool analysis = false);
00091 
00092     virtual ~ValueOH() {}
00093 
00104     virtual void init(const vlist_type& /*uninstantiatedVariables*/)
00105     {
00106         m_previousVariable = 0;
00107         m_mistakeCount = 0;
00108     }
00109 
00111     virtual void done() {}
00112 
00119     void solutionFound(const Solution& solution);
00120 
00128     virtual void dynamicInit(
00129         Variable& /*variable*/,
00130         Domain& /*selectable*/) {}
00131 
00138     virtual void dynamicDone(Variable& /*variable*/) {}
00139 
00157     virtual void select(
00158         Domain& selectable,
00159         Domain& selected,
00160         Variable& variable) = 0;
00161 
00173     virtual bool better(const Value& value0, const Value& value1) /*= 0*/;
00174 
00185     virtual bool equal(const Value& value0, const Value& value1) /*= 0*/;
00186 
00198     virtual void sort(
00199         Domain& original,
00200         Domain& sorted,
00201         Variable& variable) /*= 0*/;
00202 
00212     virtual long double score(Variable& variable, const Value& value) = 0;
00213 
00218     void setDistance(size_t distance) { m_distance = distance; }
00219 
00221     bool analysisFlag() const { return m_analysis; }
00222 
00228     virtual wostream& print(wostream& wos) const;
00229 
00230     friend CSP_API wostream& operator<<(
00231         wostream& wos, const ValueOH& valueOH)
00232     {
00233         return valueOH.print(wos);
00234     }
00235 
00236   protected:
00248     void selectByQuality(
00249         const Variable& variable,
00250         Domain& selectable,
00251         Domain& selected,
00252         StrictWeakValueOrdering& better,
00253         StrictWeakValueOrdering& equal);
00254 
00265     void selectByDistance(
00266         const Variable& variable,
00267         Domain& selectable,
00268         Domain& selected,
00269         StrictWeakValueOrdering& better);
00270 
00283     void sort(
00284         Domain& original,
00285         Domain& sorted,
00286         Variable& variable,
00287         StrictWeakValueOrdering& better,
00288         StrictWeakValueOrdering& equal);
00289 
00299     int distance(
00300         vector<Value*>& preferred,
00301         hash_set<const Value*>& perfect) const;
00302 
00311     void printPerfectPositions(
00312         vector<Value*>& preferred,
00313         hash_set<const Value*>& perfect) const;
00314 
00320     bool m_analysis;
00321 
00323     size_t m_distance;
00324 
00325   protected:
00327     wostringstream& printScores(
00328         wostringstream& wos,
00329         Variable& variable,
00330         vector<Value*>& preferred);
00331 
00332   private:
00334     ValueOH(const ValueOH&);
00335 
00337     ValueOH& operator=(const ValueOH&);
00338 
00347     void updateStatistics(
00348         vector<Value*>& preferred,
00349         hash_set<const Value*>& perfect);
00350 
00361     int analyze(const Variable& variable, vector<Value*>& preferred);
00362 
00371     void sanityChecks(
00372         vector<Value*>& preferred,
00373         hash_set<const Value*>& perfect);
00374 
00382     void getNewlyInstantiatedVariables(
00383         hash_set<const Variable*>& newlyInstantiatedVariables) const;
00384 
00393     bool inKnownSubtree(
00394         const hash_set<const Variable*>& variables,
00395         const hash_map<const Variable*, const Value*>& values) const;
00396 
00404     void rememberGoodSubtree(shash_type& goodSolutions);
00405 
00407     void forgetKnownGoodSubtree();
00408 
00411     bool inKnownGoodSubtree() const;
00412 
00415     void rememberBadSubtree();
00416 
00418     void forgetKnownBadSubtree();
00419 
00421     bool inKnownBadSubtree() const;
00422 
00424     vector<ulonglong> m_usefulCalls;
00425 
00427     vector<ulonglong> m_uselessCalls;
00428 
00434     vector<double> m_averageQuality;
00435 
00437     vector<pair<double, double> > m_qualityRange;
00438 
00444     hash_set<const Variable*> m_goodVariables;
00445 
00447     hash_map<const Variable*, const Value*> m_goodValues;
00448 
00451     shash_type m_goodSolutions;
00452 
00459     hash_set<const Variable*> m_badVariables;
00460 
00462     hash_map<const Variable*, const Value*> m_badValues;
00463 
00466     hash_set<const Variable*> m_instantiatedVariables;
00467 
00469     int m_mistakeCount;
00470 
00472     const Variable* m_previousVariable;
00473 };
00474 
00475 
00476 CSP_NAMESPACE_END(csp);
00477 
00478 
00479 #endif // _CSP_KERNEL_ValueOH_H
00480 
00481 // Local Variables:
00482 // mode: C++
00483 // End:

Generated on Wed May 25 12:21:15 2005 for csp.kdevelop by  doxygen 1.3.9.1