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

Decomposition.h

00001 // Decomposition.h -- The interface to the super class of all the
00002 // `decomposition' algorithms.
00003 
00004 /*
00005  * Copyright (C) 1997-1998, 2003-2005 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: Decomposition_8h-source.html,v 1.1 2005/05/25 12:37:18 tudor Exp $
00029 
00030 #ifndef _CSP_KERNEL_Decomposition_H
00031 #define _CSP_KERNEL_Decomposition_H
00032 
00033 
00034 CSP_NAMESPACE_BEGIN(csp);
00035 
00036 
00037 class CSP_API Driver;
00038 
00039 
00043 class CSP_API Decomposition
00044 {
00045   public:
00047     static const timeval NoTimeLimit;
00048 
00050     static const ulonglong NoNodeLimit;
00051 
00056     static const bool KeepChanges = true;
00057 
00062     static const bool DiscardChanges = false;
00063 
00068     static const bool ReselectAllowed = true;
00069 
00075     static const bool ReselectProhibited = false;
00076 
00078     typedef bool (*SolutionCallback)(Decomposition&, bool&);
00079 
00081     typedef bool (*EvaluationCallback)(Decomposition&, bool&);
00082 
00084     typedef bool (*RefutationCallback)(Decomposition&, bool&);
00085 
00087     typedef hash_set<Variable*> ActiveVariablesSet;
00088 
00090     typedef hash_set<Constraint*> ActiveConstraintsSet;
00091 
00092     enum Statistics
00093     {
00095         NoStatistics = 0,
00096 
00099         AverageUninstantiatedVariables = 1,
00100 
00103         AverageModifiedVariables = 2,
00104 
00107         AverageDomainSize = 4,
00108 
00111         AverageActiveConstraints = 8,
00112 
00115         AverageVariableDegree = 16,
00116 
00119         AverageConstrainedness = 32,
00120 
00123         AverageConstraintDensity = 64,
00124 
00127         AverageConstraintTightness = 128,
00128 
00131         AverageLooseConstraints = 256,
00132 
00135         AverageLeafDepth = 512,
00136 
00139         Nodes = 1024,
00140 
00142         Leaves = 2048,
00143 
00146         ExtraPropagationStatistics = 4096,
00147 
00150         VariableSelectionCounters = 8192,
00151 
00154         LastPathSelections = 16384,
00155 
00157         Backdoor = 32768,
00158 
00160         BranchingAssignments = 65536,
00161 
00163         AllStatistics = UINT_MAX
00164     };
00165 
00166     enum Features
00167     {
00169         NoFeatures = 0,
00170 
00174         StaticOptimismBucketing = 1,
00175 
00179         DynamicOptimismBucketing = 2,
00180 
00186         GreedyVariableProcessing = 8,
00187 
00192         LooseConstraintsDeactivation = 16,
00193 
00199         FailFirstnessAnalysis = 32,
00200 
00208         AggregateScoreValueSelection = 64,
00209 
00211         AllFeatures = UINT_MAX
00212     };
00213 
00214     struct Refutation
00215     {
00216         Refutation() { init(); }
00217 
00218         void init()
00219         {
00220             m_ccks = ULONGLONG_MAX;
00221             m_nodes = ULONGLONG_MAX;
00222             m_assignments = ULONGLONG_MAX;
00223             m_lowerBound = 0;
00224             m_resources = true;
00225             m_depth = INT_MAX;
00226             m_isOptimal = false;
00227         }
00228 
00234         vector<vector<id_t> > m_variables;
00235 
00241         vector<vector<id_t> > m_values;
00242 
00245         ulonglong m_ccks;
00246 
00249         ulonglong m_nodes;
00250 
00253         ulonglong m_assignments;
00254 
00257         ulonglong m_lowerBound;
00258 
00261         Resources m_resources;
00262 
00264         int m_depth;
00265 
00268         bool m_isOptimal;
00269     };
00270 
00272     virtual ~Decomposition();
00273 
00275     const wstring name() const {
00276         return m_fullName + L" (" + m_shortName + L")"; }
00277 
00279     const wstring& fullName() const { return m_fullName; }
00280 
00282     const wstring& shortName() const { return m_shortName; }
00283 
00285     const Problem& problem() const { return m_problem; }
00286 
00288     Problem& problem() { return m_problem; }
00289 
00291     const Filter& filter() const { return m_filter; }
00292 
00294     Filter& filter() { return m_filter; }
00295 
00297     const Retraction& retraction() const { return m_retraction; }
00298 
00300     Retraction& retraction() { return m_retraction; }
00301 
00303     int depth() const { return m_depth; }
00304 
00315     void keepBestValues(double percentage);
00316 
00322     double percentageOfInstantiatedVariables() const;
00323 
00332     void getModifiedVariables(
00333         int depth,
00334         vector<Variable*>& modifiedVariables) const;
00335 
00344     ulonglong solutions() const { return m_solutions.back(); }
00345 
00356     bool registerHeuristic(VariableOH& heuristic, bool back = true);
00357 
00365     bool unregisterHeuristic(VariableOH& heuristic);
00366 
00368     size_t variableOrderingHeuristics() const
00369     {
00370         return m_variableOH.size();
00371     }
00372 
00379     VariableOH& variableOrderingHeuristic(size_t n) const
00380     {
00381         return *m_variableOH[n];
00382     }
00383 
00394     bool registerHeuristic(ValueOH& heuristic, bool back = true);
00395 
00403     bool unregisterHeuristic(ValueOH& heuristic);
00404 
00406     size_t valueOrderingHeuristics() const
00407     {
00408         return m_valueOH.size();
00409     }
00410 
00417     ValueOH& valueOrderingHeuristic(size_t n) const
00418     {
00419         return *m_valueOH[n];
00420     }
00421 
00425     const vlist_type& uninstantiatedVariables() const
00426     {
00427         return m_uninstantiatedVariables;
00428     }
00429 
00437     void getInstantiatedVariables(hash_set<const Variable*>& c) const;
00438 
00440     const shash_type& solutionsLeft() const { return m_solutionsLeft; }
00441 
00456     void getKnownSolutions(
00457         const hash_set<const Variable*>& instantiatedVariables,
00458         const shash_type& solutionsLeft,
00459         shash_type& goodSolutions,
00460         shash_type& badSolutions) const;
00461 
00473     void getPerfectValues(
00474         const Variable& variable,
00475         hash_set<const Value*>& values,
00476         const shash_type& solutions) const;
00477 
00497     void setSolutionCallback(SolutionCallback solutionCallback)
00498     {
00499         m_solutionCallbacks[m_solutionCallbacks.size() - 1] =
00500             solutionCallback;
00501     }
00502 
00508     SolutionCallback solutionCallback() const
00509     {
00510         return m_solutionCallbacks[m_solutionCallbacks.size() - 1];
00511     }
00512 
00525     void setEvaluationCallback(EvaluationCallback evaluationCallback)
00526     {
00527         m_evaluationCallbacks[m_evaluationCallbacks.size() - 1] =
00528             evaluationCallback;
00529     }
00530 
00536     EvaluationCallback evaluationCallback() const
00537     {
00538         return m_evaluationCallbacks[m_evaluationCallbacks.size() - 1];
00539     }
00540 
00554     void setRefutationCallback(RefutationCallback refutationCallback)
00555     {
00556         m_refutationCallbacks[m_refutationCallbacks.size() - 1] =
00557             refutationCallback;
00558     }
00559 
00565     RefutationCallback refutationCallback() const
00566     {
00567         return m_refutationCallbacks[m_refutationCallbacks.size() - 1];
00568     }
00569 
00592     ulonglong search(
00593         const bool yield = false,
00594         const vlist_type& instantiables = s_all);
00595 
00606     void buildActiveSets(
00607         const vlist_type& variables,
00608         ActiveVariablesSet& avs,
00609         ActiveConstraintsSet& acs) const;
00610 
00621     int depthOfDeepestModifiedVariable(const hash_set<id_t>& wanted) const;
00622 
00632     size_t branching(
00633         Variable& variable,
00634         hash_map<const Value*, size_t>& costs,
00635         int level,
00636         bool minimum = true);
00637 
00646     void postPropagationDomainSizes(
00647         Variable& variable,
00648         hash_map<const Value*, size_t>& domainSizes,
00649         bool minimum = true);
00650 
00652     ulonglong totalNodes() const;
00653 
00658     double staticOptimismPercentage() const
00659     {
00660         return m_staticOptimismPercentage;
00661     }
00662 
00674     void setStaticOptimismPercentage(double percentage)
00675     {
00676         m_staticOptimismPercentage = percentage;
00677     }
00678 
00683     double dynamicOptimismPercentage() const
00684     {
00685         return m_dynamicOptimismPercentage;
00686     }
00687 
00698     void setDynamicOptimismPercentage(double percentage)
00699     {
00700         m_dynamicOptimismPercentage = percentage;
00701     }
00702 
00706     const BitFlags& statistics() const { return m_statistics; }
00707 
00715     bool setStatistics(const BitFlags::Flags flags);
00716 
00719     const BitFlags& features() const { return m_features; }
00720 
00728     bool setFeatures(const BitFlags::Flags flags);
00729 
00731     const PropagationStatistics& propagationStatistics() const
00732     {
00733         return m_propagationStatistics;
00734     }
00735 
00737     const Resources& resources() const { return m_resources; }
00738 
00740     Resources& resources() { return m_resources; }
00741 
00744     const vector<long double>& averageUninstantiatedVariables() const
00745     {
00746         return m_averageUninstantiatedVariables;
00747     }
00748 
00751     const vector<long double>& averageModifiedVariables() const
00752     {
00753         return m_averageModifiedVariables;
00754     }
00755 
00757     const vector<long double>& averageDomainSize() const
00758     {
00759         return m_averageDomainSize;
00760     }
00761 
00763     const vector<long double>& averageMinimumDomainSize() const
00764     {
00765         return m_averageMinimumDomainSize;
00766     }
00767 
00769     const vector<long double>& averageMaximumDomainSize() const
00770     {
00771         return m_averageMaximumDomainSize;
00772     }
00773 
00776     const vector<long double>& averageActiveConstraints() const
00777     {
00778         return m_averageActiveConstraints;
00779     }
00780 
00783     const vector<long double>& averageVariableDegree() const
00784     {
00785         return m_averageVariableDegree;
00786     }
00787 
00790     const vector<long double>& averageConstrainedness() const
00791     {
00792         return m_averageConstrainedness;
00793     }
00794 
00797     const vector<long double>& averageConstraintDensity() const
00798     {
00799         return m_averageConstraintDensity;
00800     }
00801 
00804     const vector<long double>& averageConstraintTightness() const
00805     {
00806         return m_averageConstraintTightness;
00807     }
00808 
00811     const vector<long double>& averageLooseConstraints() const
00812     {
00813         return m_averageLooseConstraints;
00814     }
00815 
00820     const vector<hash_map<Variable*, ulonglong> >&
00821     variableSelectionCounters() const
00822     {
00823         return m_variableSelectionCounters;
00824     }
00825 
00827     const vector<vector<pair<Variable*, Domain*> > >& lastPathSelections() const
00828     {
00829         return m_lastPathSelections;
00830     }
00831 
00837     const vector<pair <Variable*, Domain*> >& backdoor() const
00838     {
00839         return m_backdoor;
00840     }
00841 
00847     const vector<vector<pair <Variable*, Domain*> > >&
00848     branchingAssignments() const
00849     {
00850         return m_branchingAssignments;
00851     }
00852 
00854     inline Driver* driver() { return m_driver; }
00855 
00857     inline const Driver* driver() const { return m_driver; }
00858 
00865     inline const timeval& timeLimit() const { return m_timeLimit; }
00866 
00868     bool timeLimitEnabled() const;
00869 
00877     timeval setTimeLimit(const timeval& timeLimit);
00878 
00896     bool timeLimitExceeded();
00897 
00904     inline const ulonglong nodeLimit() const { return m_nodeLimit; }
00905 
00907     bool nodeLimitEnabled() const { return m_nodeLimit != NoNodeLimit; }
00908 
00916     ulonglong setNodeLimit(const ulonglong nodeLimit);
00917 
00924     bool nodeLimitExceeded();
00925 
00933     ulonglong setRefutationsLimit(ulonglong refutationsLimit);
00934 
00936     void setRefutationNodesUpperBound(ulonglong nodes)
00937     {
00938         m_ffaRefutationNodesUpperBound = nodes;
00939     }
00940 
00955     void recordRefutationParameters(
00956         const ulonglong ccks,
00957         const ulonglong nodes,
00958         const Resources& resources,
00959         const int depth);
00960 
00970     void currentRefutation(
00971         vector<vector<id_t> >& variables,
00972         vector<vector<id_t> >& values);
00973 
00987     bool shortestRefutation(Refutation& refutation) const;
00988 
00991     ulonglong shortestRefutationNodes() const
00992     {
00993         return m_ffaShortestRefutation.m_nodes;
00994     }
00995 
00999     void setRefutationDepthUpperBound(int depthUpperBound);
01000 
01003     void setIterativeDeepening(bool iterativeDeepening)
01004     {
01005         m_ffaIterativeDeepening = iterativeDeepening;
01006     }
01007 
01010     void setLookAheadLevel(int lookAheadLevel)
01011     {
01012         m_ffaLookAheadLevel = lookAheadLevel;
01013     }
01014 
01017     void setOptimalRefutationCaching(bool caching)
01018     {
01019         m_ffaOptimalRefutationCaching = caching;
01020     }
01021 
01025     void setConsistencyBeforeSearch(bool consistencyBeforeSearch);
01026 
01031     size_t ffaDynamicUpperBoundThreshold() const
01032     {
01033         return m_ffaDynamicUpperBoundThreshold;
01034     }
01035 
01040     void setFFADynamicUpperBoundThreshold(size_t nVariables)
01041     {
01042         m_ffaDynamicUpperBoundThreshold = nVariables;
01043     }
01044 
01046     int ffaProgressReportingDepthLimit() const
01047     {
01048         return m_ffaProgressReportingDepthLimit;
01049     }
01050 
01052     void setFFAProgressReportingDepthLimit(int limit)
01053     {
01054         m_ffaProgressReportingDepthLimit = limit;
01055     }
01056 
01059     size_t ffaLowerBoundLookAheadLevel() const
01060     {
01061         return m_ffaLowerBoundLookAheadLevel;
01062     }
01063 
01066     void setFFALowerBoundLookAheadLevel(size_t level)
01067     {
01068         m_ffaLowerBoundLookAheadLevel = level;
01069     }
01070 
01074     double setFFAOptimismPercentage() const
01075     {
01076         return m_ffaOptimismPercentage;
01077     }
01078 
01082     void setFFAOptimismPercentage(double percentage)
01083     {
01084         m_ffaOptimismPercentage = percentage;
01085     }
01086 
01091     int maximumSearchDepth() const;
01092 
01102     long double constrainedness(
01103         const ActiveVariablesSet& avs,
01104         const ActiveConstraintsSet& acs) const;
01105 
01112     ulonglong nodes(int depth) const;
01113 
01120     ulonglong leaves(int depth) const;
01121 
01131     const vector<vector<ulonglong> >& mistakePointNodes(int depth) const
01132     {
01133         assert(depth < (int)m_nodes.size());
01134         return m_nodes[depth];
01135     }
01136 
01146     const vector<vector<ulonglong> >& mistakePointLeaves(int depth) const
01147     {
01148         assert(depth < (int)m_leaves.size());
01149         return m_leaves[depth];
01150     }
01151 
01158     ulonglong backtracks(int depth) const;
01159 
01164     int solutionDepth() const
01165     {
01166         assert(m_solutionDepth == (int)m_solutionHeuristicMistakes.size() - 1);
01167         return m_solutionDepth;
01168     }
01169 
01176     const vector<ulonglong>& solutionHeuristicMistakes() const
01177     {
01178         return m_solutionHeuristicMistakes;
01179     }
01180 
01187     const vector<ulonglong>& globalHeuristicMistakes() const
01188     {
01189         return m_globalHeuristicMistakes;
01190     }
01191 
01202     ulonglong refutationSizeLowerBound(int lookAheadLevel);
01203 
01205     ulonglong initialRefutationSizeLowerBound() const {
01206         return m_ffaInitialRefutationSizeLowerBound; }
01207 
01214     void generateProblemGraph(wostream& wos) const;
01215 
01223     void generateProblemGraph(const wstring& stem) const;
01224 
01237     bool scheduleProblemGraphGeneration(const wstring& stem, int depth);
01238 
01249     bool scheduleSearchTreeGeneration(const wstring& stem, int depth);
01250 
01251   protected:
01258     virtual bool init();
01259 
01264     virtual void done();
01265 
01270     void eureka();
01271 
01278     bool hideBadValues(Variable& variable) const;
01279 
01287     ulonglong totalValues(vlist_type& variables) const;
01288 
01295     virtual void decompose() = 0;
01296 
01324     bool propagate(
01325         const bool keepChanges = KeepChanges,
01326         const vlist_type& instantiables = s_all);
01327 
01343     bool propagateAndSearch(const vlist_type& instantiable = s_all);
01344 
01354     bool canYield() const;
01355 
01410     Variable* selectVariable(
01411         const bool reselectOk = ReselectAllowed,
01412         const vlist_type& undesirables = s_empty);
01413 
01423     Variable& selectVariable(Variable& variable);
01424 
01435     size_t selectAllVariables(vlist_type* selection = 0);
01436 
01444     void unselectVariable(Variable& variable);
01445 
01459     Value* selectValue(Variable& variable, Domain& domain);
01460 
01467     void unselectValue(Domain& domain, Value* value)
01468     {
01469         insert(domain, value);
01470     }
01471 
01473     void saveState()
01474     {
01475         csp::Filter::saveState(m_uninstantiatedVariables);
01476         csp::Filter::saveState(m_instantiatedVariables[m_depth]);
01477     }
01478 
01480     void restoreState()
01481     {
01482         csp::Filter::restoreState(m_instantiatedVariables[m_depth]);
01483         csp::Filter::restoreState(m_uninstantiatedVariables);
01484     }
01485 
01508     bool moreWork();
01509 
01518     bool moreSolutionsNeeded() const { return m_moreSolutionsNeeded.back(); }
01519 
01529     bool discardableSubtree();
01530 
01538     bool atJumpDepth() const { return m_depth == m_jumpDepth; }
01539 
01558     void restrict(const vlist_type& variables);
01559 
01568     void unrestrict();
01569 
01574     void printStack();
01575 
01585     Decomposition(
01586         const wstring& fullName,
01587         const wstring& shortName,
01588         Problem& problem,
01589         Filter& filter,
01590         Retraction& retraction,
01591         Randomizer& randomizer,
01592         bool consistencyBeforeSearch = true);
01593 
01595     Problem& m_problem;
01596 
01599     vector<SolutionCallback> m_solutionCallbacks;
01600 
01604     vector<EvaluationCallback> m_evaluationCallbacks;
01605 
01611     vector<RefutationCallback> m_refutationCallbacks;
01612 
01614     bool m_consistencyBeforeSearch;
01615 
01616   private:
01621     friend class Driver;
01622 
01624     Decomposition(const Decomposition&);
01625 
01627     Decomposition& operator=(const Decomposition&);
01628 
01629     static const bool UnselectableVariables = true;
01630     static const bool SelectedVariables = false;
01631 
01632     static const bool WillingToYield = true;
01633     static const bool UnwillingToYield = false;
01634 
01637     bool selectVariables();
01638 
01640     void unselectVariables();
01641 
01646     void goModifiedOrUnmodified2OneValue(
01647         Variable& variable,
01648         const bool instantiable,
01649         vlist_type& modifiedVariables,
01650         vlist_type& instantiatedVariables);
01651 
01656     void goModifiedOrUnmodified2Modified(
01657         Variable& variable,
01658         vlist_type& modifiedVariables);
01659 
01664     void goModifiedOrUnmodified2Unmodified(Variable& variable);
01665 
01696     void prePropagation(const vlist_type& instantiables = s_all);
01697 
01714     void postPropagation(bool endSearch);
01715 
01721     void activateConstraints(clist_type& cl);
01722 
01727     void deactivateConstraints();
01728 
01741     void makeChangesPermanent();
01742 
01749     void saveTemporalFlags();
01750 
01756     void shiftTemporalFlags();
01757 
01768     void restoreTemporalFlags(const bool type = SelectedVariables);
01769 
01780     void discardTemporalFlags(const bool type = SelectedVariables);
01781 
01802     bool autoSelectVariables();
01803 
01805     bool temporaryPropagate();
01806 
01812     void deinstantiateVariableList(const vlist_type& vl);
01813 
01820     template <class Iterator>
01821     void accumulateConstraints(
01822         const Iterator vb,
01823         const Iterator ve,
01824         ActiveConstraintsSet& acs) const
01825     {
01826         for (Iterator vi = vb; vi != ve; ++vi)
01827         {
01828             const Variable& variable = **vi;
01829             Variable::constraint_iterator ci = variable.beginConstraints();
01830             Variable::constraint_iterator cie = variable.endConstraints();
01831             for (; ci != cie; ++ci)
01832             {
01833                 Constraint& constraint = **ci;
01834                 if (acs.find(&constraint) == acs.end())
01835                     acs.insert(&constraint);
01836             }
01837         }
01838     }
01839 
01847     void getNonHiddenVariables(
01848         vhash_type& nonHiddenVariables) const;
01849 
01859     void getNonHiddenConstraints(
01860         const vhash_type& nonHiddenVariables,
01861         chash_type& nonHiddenConstraints) const;
01862 
01871     bool isSolution(bool debugging = false) const;
01872 
01874     void saveVariables(
01875         vector<Variable*>& variables,
01876         vector<Domain*>& domains,
01877         vector<vector<Constraint*>*>& constraints);
01878 
01884     void compareVariables(
01885         const vector<Variable*>& variables,
01886         const vector<Domain*>& domains,
01887         vector<vector<Constraint*>*>& constraints);
01888 
01899     bool validBackdoorReassignment(
01900         Variable& variable,
01901         Value& value,
01902         const vector<Constraint*>& constraints) const;
01903 
01912     void updateStatistics();
01913 
01920     void onFringe(bool isSolution);
01921 
01923     void updateNodeCount();
01924 
01926     void updateLeafCount();
01927 
01929     void updateAverageUninstantiatedVariables();
01930 
01932     void updateAverageModifiedVariables();
01933 
01935     void updateAverageDomainSize();
01936 
01938     void updateAverageActiveConstraints(
01939         const ActiveConstraintsSet& acs);
01940 
01942     void updateAverageVariableDegree(
01943         const ActiveVariablesSet& avs);
01944 
01946     void updateAverageConstrainedness(
01947         const ActiveVariablesSet& avs,
01948         const ActiveConstraintsSet& acs);
01949 
01951     void updateAverageConstraintDensity(
01952         const ActiveVariablesSet& avs,
01953         const ActiveConstraintsSet& acs);
01954 
01956     void updateAverageConstraintTightness(
01957         const ActiveConstraintsSet& acs);
01958 
01960     void updateAverageLooseConstraints(
01961         const ActiveConstraintsSet& acs);
01962 
01969     void incrementSelectionCounter(Variable& variable);
01970 
01972     void updateHeuristicMistakeStatistics();
01973 
01975     void updateSolutions();
01976 
01981     void computeBackdoor();
01982 
01984     void computeBackdoorKey();
01985 
01987     void computeBranchingAssignments();
01988 
01990     void callBackdoorHooks() const;
01991 
01993     void callBackdoorKeyHooks() const;
01994 
01997     void callBranchingHooks() const;
01998 
02006     void updateLastPathSelections(Variable* variable);
02007 
02014     void trimLastPathSelections(int depth);
02015 
02022     void sortDomains(const vlist_type& variables);
02023 
02028     void decomposeWrapper();
02029 
02034     void generateSearchTree(const wstring& stem) const;
02035 
02037     wstring searchTreeNodeLabel();
02038 
02045     void addSearchTreeNode(bool isLeaf, bool isSolution);
02046 
02048     void addSearchTreeArc();
02049 
02054     void assertDomainsAreSorted() const;
02055 
02057     typedef vector<size_t> marks_type;
02058 
02062     ulonglong dynamicNodesUpperBound();
02063 
02066     void decomposeForOptimalRefutation();
02067 
02070     void kwayDecompose();
02071 
02074     ulonglong uncertainAssignments(const marks_type& marks) const;
02075 
02078     ulonglong refutationAssignments() const;
02079 
02083     void computeMinimumValueCosts(
02084         Variable& variable,
02085         Domain& copy,
02086         hash_map<const Value*, size_t>& costs,
02087         int level,
02088         bool minimum = true);
02089 
02092     size_t minimumVariableCost(
02093         Domain& domain,
02094         hash_map<const Value*, size_t>& costs,
02095         int lookAheadLevel) const;
02096 
02104     bool tooManyAssignments(
02105         const marks_type& marks,
02106         size_t extraAssignments) const;
02107 
02109     bool partialRefutationIsConsistent() const;
02110 
02112     void mark(marks_type& marks, vector<vector<id_t> >& data);
02113 
02115     void undo(const marks_type& marks, vector<vector<id_t> >& data);
02116 
02119     void saveBest(
02120         const marks_type& marks,
02121         vector<vector<id_t> >& data,
02122         vector<vector<id_t> >& best);
02123 
02126     void restoreBest(
02127         vector<vector<id_t> >& data,
02128         vector<vector<id_t> >& best);
02129 
02131     const wstring optimalRefutationCacheKey(int maxDepth) const;
02132 
02134     void clearOptimalRefutationsCache();
02135 
02137     void printOptimalRefutationsCacheStatistics();
02138 
02141     void updatePercentage(
02142         double completed,
02143         double total,
02144         csp::Resources& resources,
02145         bool print = true);
02146 
02148     const wstring m_fullName;
02149 
02151     const wstring m_shortName;
02152 
02154     Filter& m_filter;
02155 
02157     Retraction& m_retraction;
02158 
02160     Randomizer& m_randomizer;
02161 
02163     vlist_type m_uninstantiatedVariables;
02164 
02166     int m_depth;
02167 
02169     int m_jumpDepth;
02170 
02173     wstring m_problemGraphFileStem;
02174 
02178     int m_problemGraphGenerationDepth;
02179 
02182     wstring m_searchTreeFileStem;
02183 
02187     int m_searchTreeGenerationDepth;
02188 
02191     wostringstream m_searchTreeNodes;
02192 
02195     wostringstream m_searchTreeArcs;
02196 
02199     vector<size_t> m_searchTreeStack;
02200 
02202     size_t m_searchTreeNodeIndex;
02203 
02205     double m_staticOptimismPercentage;
02206 
02208     double m_dynamicOptimismPercentage;
02209 
02216     vector<vlist_type> m_modifiedVariables;
02217 
02228     vector<vlist_type> m_instantiatedVariables;
02229 
02231     size_t m_totalInstantiatedVariables;
02232 
02237     vector<vlist_type> m_unselectableVariables;
02238 
02246     vector<vlist_type> m_selectedVariables;
02247 
02249     vector<clist_type> m_inactiveConstraints;
02250 
02252     vector<clist_type> m_permanentlyInactiveConstraints;
02253 
02256     vector<list<size_t> > m_selectedVariablesDomainSizes;
02257 
02258     typedef struct flags_t
02259     {
02260         bool m_past; // `true' for past variables.
02261         bool m_current; // `true' for current variables.
02262         bool m_future; // `true' for future variables.
02263     };
02264 
02266     vector<vector<flags_t> > m_selectedVariablesTemporalFlags;
02267 
02269     vector<vector<flags_t> > m_unselectableVariablesTemporalFlags;
02270 
02272     Resources m_resources;
02273 
02275     bool m_timeLimitExceeded;
02276 
02278     timeval m_timeLimit;
02279 
02281     bool m_nodeLimitExceeded;
02282 
02284     ulonglong m_nodeLimit;
02285 
02294     vector<bool> m_moreSolutionsNeeded;
02295 
02302     vector<ulonglong> m_solutions;
02303 
02305     Driver* m_driver;
02306 
02308     size_t m_index;
02309 
02317     bool m_propagationIsRestorable;
02318 
02326     vector<vector<vector<ulonglong> > > m_nodes;
02327 
02335     vector<vector<vector<ulonglong> > > m_leaves;
02336 
02343     vector<ulonglong> m_currentHeuristicMistakes;
02344 
02349     vector<ulonglong> m_solutionHeuristicMistakes;
02350 
02355     vector<ulonglong> m_globalHeuristicMistakes;
02356 
02358     vector<long double> m_averageUninstantiatedVariables;
02359 
02364     vector<long double> m_averageModifiedVariables;
02365 
02367     hash_map<const Variable*, size_t> m_originalDomainSizes;
02368 
02370     vector<long double> m_averageDomainSize;
02371 
02373     vector<long double> m_averageMinimumDomainSize;
02374 
02376     vector<long double> m_averageMaximumDomainSize;
02377 
02379     vector<long double> m_averageActiveConstraints;
02380 
02382     vector<long double> m_averageVariableDegree;
02383 
02400     vector<long double> m_averageConstrainedness;
02401 
02403     vector<long double> m_averageConstraintDensity;
02404 
02406     vector<long double> m_averageConstraintTightness;
02407 
02409     vector<long double> m_averageLooseConstraints;
02410 
02415     vector<hash_map<Variable*, ulonglong> >
02416         m_variableSelectionCounters;
02417 
02423     vector<vector<pair<Variable*, Domain*> > > m_lastPathSelections;
02424 
02426     vector<pair<Variable*, Domain*> > m_backdoor;
02427 
02429     vector<vector<pair<Variable*, Domain*> > > m_branchingAssignments;
02430 
02449     vector<VariableOH*> m_variableOH;
02450 
02452     vector<ValueOH*> m_valueOH;
02453 
02455     vector<vlist_type> m_hiddenVariables;
02456 
02458     hash_map<Variable*, Domain*> m_unwantedValues;
02459 
02463     shash_type m_solutionsLeft;
02464 
02466     BitFlags m_features;
02467 
02469     BitFlags m_statistics;
02470 
02473     BitFlags m_previousFeatures;
02474 
02477     BitFlags m_previousStatistics;
02478 
02480     PropagationStatistics m_propagationStatistics;
02481 
02484     bool m_searching;
02485 
02487     int m_maximumSearchDepth;
02488 
02490     int m_solutionDepth;
02491 
02493     int* m_previousIndentationLevelPlaceholder;
02494 
02496     static const vlist_type s_empty;
02497 
02499     static const vlist_type s_all;
02500 
02503     int m_ffaDepthUpperBound;
02504 
02506     bool m_ffaKWayDecomposition;
02507 
02509     int m_ffaDepthLimit;
02510 
02515     bool m_ffaIterativeDeepening;
02516 
02518     int m_ffaLookAheadLevel;
02519 
02521     bool m_ffaDepthLimitReached;
02522 
02525     size_t m_ffaLowerBoundLookAheadLevel;
02526 
02530     size_t m_ffaDynamicUpperBoundThreshold;
02531 
02533     ulonglong m_ffaInitialRefutationSizeLowerBound;
02534 
02538     ulonglong m_ffaRefutationExtraCostLowerBound;
02539 
02543     bool m_ffaTooManyAssignments;
02544 
02546     int m_ffaRefutationDepth;
02547 
02549     ulonglong m_ffaRefutationsFound;
02550 
02552     ulonglong m_ffaRefutationsLimit;
02553 
02559     vector<vector<id_t> > m_ffaRefutationVariables;
02560 
02569     vector<vector<id_t> > m_ffaRefutationValues;
02570 
02572     vector<ulonglong> m_ffaRefutationMinimumAssignments;
02573 
02575     Refutation m_ffaShortestRefutation;
02576 
02581     ulonglong m_ffaRefutationNodesUpperBound;
02582 
02585     bool m_ffaOptimalRefutationCaching;
02586 
02589     struct OptimalRefutation
02590     {
02591         OptimalRefutation(
02592             vector<vector<id_t> >* variables,
02593             vector<vector<id_t> >* values,
02594             ulonglong assignments,
02595             int depthLimit,
02596             size_t nVariables) :
02597             m_variables(variables),
02598             m_values(values),
02599             m_assignments(assignments),
02600             m_depthLimit(depthLimit),
02601             m_nVariables(nVariables),
02602             m_hits(0),
02603             m_quasiHits(0) {}
02604 
02605         vector<vector<id_t> >* m_variables;
02606         vector<vector<id_t> >* m_values;
02607         ulonglong m_assignments;
02608         int m_depthLimit;
02609         size_t m_nVariables;
02610         ulonglong m_hits;
02611         ulonglong m_quasiHits;
02612     };
02613 
02614     struct OptimalRefutationLessThan
02615     {
02616         bool operator()(
02617             const OptimalRefutation* or1,
02618             const OptimalRefutation* or2) const
02619         {
02620             return or1->m_assignments > or2->m_assignments;
02621         }
02622     };
02623 
02624 #if defined(__GNUG__)
02625 
02626     struct StringEqual
02627     {
02628         bool operator()(const char* s1, const char* s2) const
02629         {
02630             return strcmp(s1, s2) == 0;
02631         }
02632     };
02633 
02636     typedef hash_map<
02637             const char*,
02638             OptimalRefutation*,
02639             hash<const char*>,
02640             StringEqual> OptimalRefutationsCache;
02641 #elif defined(_MSC_VER)
02642     // Comparison object for character pointers.
02643     struct StringCompare
02644     {
02645         enum
02646         {
02647             bucket_size = 32, // 0 < bucket_size
02648             min_buckets = 32
02649         };
02650 
02651         size_t operator()(const char* s) const
02652         {
02653             size_t hashval = 0;
02654             const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
02655             while (*p != '\0')
02656                 hashval += *p++;
02657             return hashval;
02658         }
02659 
02660         bool operator()(const char* s1, const char* s2) const
02661         {
02662             return strcmp(s1, s2) < 0;
02663         }
02664     };
02665 
02668     typedef hash_map<
02669             const char*,
02670             OptimalRefutation*,
02671             StringCompare> OptimalRefutationsCache;
02672 #endif
02673 
02675     OptimalRefutationsCache m_ffaOptimalRefutationsCache;
02676 
02678     ulonglong m_ffaOptimalRefutationsCacheLookups;
02679 
02681     ulonglong m_ffaOptimalRefutationsCacheHits;
02682 
02685     ulonglong m_ffaOptimalRefutationsCacheQuasiHits;
02686 
02692     vector<pair<pair<double, double>, Resources*> > m_ffaAlternativesLeft;
02693 
02695     int m_ffaProgressReportingDepthLimit;
02696 
02698     double m_ffaOptimismPercentage;
02699 };
02700 
02701 
02702 CSP_NAMESPACE_END(csp);
02703 
02704 
02705 #endif // _CSP_KERNEL_Decomposition_H
02706 
02707 // Local Variables:
02708 // mode: C++
02709 // End:

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