- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/coin/svn-release/Alps-1.4.8/Alps/src/AlpsKnowledgeBroker.h

Go to the documentation of this file.

    00001 /*===========================================================================*
    00002  * This file is part of the Abstract Library for Parallel Search (ALPS).     *
    00003  *                                                                           *
    00004  * ALPS is distributed under the Eclipse Public License as part of the       *
    00005  * COIN-OR repository (http://www.coin-or.org).                              *
    00006  *                                                                           *
    00007  * Authors:                                                                  *
    00008  *                                                                           *
    00009  *          Yan Xu, Lehigh University                                        *
    00010  *          Ted Ralphs, Lehigh University                                    *
    00011  *                                                                           *
    00012  * Conceptual Design:                                                        *
    00013  *                                                                           *
    00014  *          Yan Xu, Lehigh University                                        *
    00015  *          Ted Ralphs, Lehigh University                                    *
    00016  *          Laszlo Ladanyi, IBM T.J. Watson Research Center                  *
    00017  *          Matthew Saltzman, Clemson University                             *
    00018  *                                                                           *
    00019  *                                                                           *
    00020  * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs.       *
    00021  *===========================================================================*/
    00022
    00023 #ifndef AlpsKnowledgeBroker_h_
    00024 #define AlpsKnowledgeBroker_h_
    00025
    00026 #include <cmath>
    00027 #include <iosfwd>
    00028 #include <map>
    00029 #include <string>
    00030
    00031 #include "CoinMessageHandler.hpp"
    00032
    00033 #include "AlpsSearchStrategy.h"
    00034 #include "AlpsEnumProcessT.h"
    00035 #include "AlpsKnowledge.h"
    00036 #include "AlpsKnowledgePool.h"
    00037 #include "AlpsMessage.h"
    00038 #include "AlpsParams.h"
    00039 #include "AlpsSolutionPool.h"
    00040 #include "AlpsSubTree.h"
    00041 #include "AlpsSubTreePool.h"
    00042 #include "AlpsModel.h"
    00043 #include "AlpsTime.h"
    00044
    00045 //#############################################################################
    00046
    00048 class AlpsKnowledgeBroker {
    00049
    00050  private:
    00051
    00052     AlpsKnowledgeBroker(const AlpsKnowledgeBroker&);
    00053     AlpsKnowledgeBroker& operator=(const AlpsKnowledgeBroker&);
    00054
    00056     std::map<int, AlpsKnowledge*> decodeMap_;
    00057
    00058  protected:
    00059
    00061     std::string instanceName_;
    00062
    00064     AlpsModel *model_;
    00065
    00067     AlpsPhase phase_;
    00068
    00075     AlpsSubTreePool* subTreePool_;
    00076
    00078     AlpsSolutionPool* solPool_;
    00079
    00081     std::map<AlpsKnowledgeType, AlpsKnowledgePool*>* pools_;
    00083
    00089     AlpsSubTree* workingSubTree_;
    00090
    00092     bool needWorkingSubTree_;
    00093
    00095     AlpsNodeIndex_t nextIndex_;
    00096
    00098     AlpsNodeIndex_t maxIndex_;
    00100
    00105     /***/
    00107     AlpsTimer timer_;
    00108
    00110     AlpsTimer subTreeTimer_;
    00111
    00113     AlpsTimer tempTimer_;
    00114
    00116     int solNum_;
    00117
    00119     int nodeProcessedNum_;
    00120
    00122     int nodeBranchedNum_;
    00123
    00125     int nodeDiscardedNum_;
    00126
    00128     int nodePartialNum_;
    00129
    00132     int systemNodeProcessed_;
    00133
    00135     int nodeLeftNum_;
    00136
    00138     int treeDepth_;
    00139
    00141     int bestSolNode_;
    00142
    00144     double peakMemory_;
    00145
    00147     AlpsExitStatus exitStatus_;
    00149
    00155     AlpsSearchStrategy<AlpsSubTree*>* treeSelection_;
    00156
    00158     AlpsSearchStrategy<AlpsTreeNode*>* nodeSelection_;
    00159
    00161     AlpsSearchStrategy<AlpsTreeNode*>* rampUpNodeSelection_;
    00163
    00169     CoinMessageHandler * handler_;
    00170
    00172     CoinMessages messages_;
    00173
    00176     int msgLevel_;
    00177
    00180     int hubMsgLevel_;
    00181
    00184     int workerMsgLevel_;
    00185
    00188     int logFileLevel_;
    00189
    00191     std::string logfile_;
    00193
    00195     int nodeMemSize_;
    00196
    00198     double nodeProcessingTime_;
    00199
    00201     int largeSize_;
    00202
    00204     bool userBalancePeriod_;
    00205
    00207     int numNodeLog_;
    00208
    00209  public:
    00210
    00212     AlpsKnowledgeBroker();
    00213
    00215     virtual ~AlpsKnowledgeBroker();
    00216
    00217     //-------------------------------------------------------------------------
    00230     void registerClass(int name, AlpsKnowledge* userKnowledge) {
    00231         // Check if alread have one.
    00232         std::map<int, AlpsKnowledge*>::iterator pos, pos1;
    00233         pos = decodeMap_.find(name);
    00234         pos1 = decodeMap_.end();
    00235
    00236         if (pos != pos1) {
    00237             AlpsKnowledge* kl = pos->second;
    00238             decodeMap_.erase(pos);
    00239             delete kl;
    00240         }
    00241
    00242         decodeMap_[name] = userKnowledge;
    00243     }
    00244
    00253     const AlpsKnowledge* decoderObject(int name) {
    00254         return decodeMap_[name];
    00255     }
    00257
    00258     //------------------------------------------------------
    00259
    00265     virtual void initializeSearch(int argc,
    00266                                   char* argv[],
    00267                                   AlpsModel& model) = 0;
    00268
    00270     virtual void rootSearch(AlpsTreeNode* root) = 0;
    00271
    00273     virtual void search(AlpsModel *model) {
    00274         AlpsTreeNode* root = model->createRoot();
    00275         rootSearch(root);
    00276     }
    00278
    00279     //------------------------------------------------------
    00280
    00285     AlpsPhase getPhase() { return phase_; }
    00286     void setPhase(AlpsPhase ph) { phase_ = ph; }
    00288
    00290     AlpsModel *getModel() { return model_; }
    00291     void setModel(AlpsModel *m) { model_ = m; }
    00293
    00295     int getTreeDepth() { return treeDepth_; }
    00296
    00298     void setPeakMemory(double size) { peakMemory_ = size; }
    00299
    00301     double getPeakMemory() { return peakMemory_; }
    00302
    00308     void setupKnowledgePools();
    00309
    00311     inline void addKnowledgePool(AlpsKnowledgeType kt, AlpsKnowledgePool* kp) {
    00312         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00313             // AlpsKnowledgePool* akp = static_cast<AlpsKnowledgePool*>(kp);
    00314             pools_->insert
    00315                 (std::pair<AlpsKnowledgeType, AlpsKnowledgePool*>(kt, kp));
    00316         }
    00317         else {
    00318             throw CoinError("Broker doesn't manage this type of knowledge",
    00319                             "addKnowledgePool()", "AlpsKnowledgeBroker");
    00320         }
    00321     }
    00322
    00324     inline  AlpsKnowledgePool* getKnowledgePool(AlpsKnowledgeType kt) const {
    00325         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00326             return (*pools_)[kt];
    00327         }
    00328         else {
    00329             throw CoinError("Broker doesn't manage this type of knowledge",
    00330                             "getKnowledgePool()", "AlpsKnowledgeBroker");
    00331         }
    00332     }
    00333
    00335     virtual int getNumKnowledges(AlpsKnowledgeType kt) const;
    00336
    00339     virtual int getMaxNumKnowledges(AlpsKnowledgeType kt) const {
    00340         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00341             return getKnowledgePool(kt)->getMaxNumKnowledges();
    00342         }
    00343         else {
    00344             throw CoinError("Broker doesn't manage this type of knowledge",
    00345                             "getMaxNumKnowledges()", "AlpsKnowledgeBroker");
    00346         }
    00347     }
    00348
    00351     virtual void setMaxNumKnowledges(AlpsKnowledgeType kt, int num) {
    00352         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00353             getKnowledgePool(kt)->setMaxNumKnowledges(num);
    00354         }
    00355         else {
    00356             throw CoinError("Broker doesn't manage this type of knowledge",
    00357                             "setMaxNumKnowledges()", "AlpsKnowledgeBroker");
    00358         }
    00359     }
    00360
    00363     virtual bool hasKnowledge(AlpsKnowledgeType kt) const {
    00364         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree)
    00365             return getKnowledgePool(kt)->hasKnowledge();
    00366         else
    00367             throw CoinError("Broker doesn't manage this type of knowledge",
    00368                             "hasKnowledge()", "AlpsKnowledgeBroker");
    00369     }
    00370
    00372     virtual std::pair<AlpsKnowledge*, double>
    00373         getKnowledge(AlpsKnowledgeType kt) const {
    00374         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00375             return getKnowledgePool(kt)->getKnowledge();
    00376         }
    00377         else {
    00378             throw CoinError("Broker doesn't manage this type of knowledge",
    00379                             "getKnowledge()", "AlpsKnowledgeBroker");
    00380         }
    00381     }
    00382
    00384     virtual void popKnowledge(AlpsKnowledgeType kt) {
    00385         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00386             getKnowledgePool(kt)->popKnowledge();
    00387         }
    00388         else {
    00389             throw CoinError("Broker doesn't manage this type of knowledge",
    00390                             "popKnowledge()", "AlpsKnowledgeBroker");
    00391         }
    00392     }
    00393
    00395     virtual std::pair<AlpsKnowledge*, double>
    00396         getBestKnowledge(AlpsKnowledgeType kt) const;
    00397
    00399     virtual void getAllKnowledges (AlpsKnowledgeType kt,
    00400                                    std::vector<std::pair<AlpsKnowledge*,
    00401                                    double> >& kls)  const {
    00402         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00403             getKnowledgePool(kt)->getAllKnowledges(kls);
    00404         }
    00405         else {
    00406             throw CoinError("Broker doesn't manage this type of knowledge",
    00407                             "popKnowledge()", "AlpsKnowledgeBroker");
    00408         }
    00409     }
    00410
    00412     virtual void addKnowledge(AlpsKnowledgeType kt,
    00413                               AlpsKnowledge* kl,
    00414                               double value ) {
    00415         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00416             getKnowledgePool(kt)->addKnowledge(kl, value);
    00417         }
    00418         else {
    00419             throw CoinError("Broker doesn't manage this type of knowledge",
    00420                             "popKnowledge()", "AlpsKnowledgeBroker");
    00421         }
    00422     }
    00424
    00429     int getNumNodesProcessed() const {
    00430         return nodeProcessedNum_;
    00431     }
    00432
    00434     int getNumNodesBranched() const {
    00435         return nodeBranchedNum_;
    00436     }
    00437
    00439     int getNumNodesDiscarded() const {
    00440         return nodeDiscardedNum_;
    00441     }
    00442
    00444     int getNumNodesPartial() const {
    00445         return nodePartialNum_;
    00446     }
    00447
    00449     int getNumNodesProcessedSystem() const {
    00450         return systemNodeProcessed_;
    00451     }
    00452
    00454     virtual int updateNumNodesLeft();
    00455
    00457     virtual AlpsTreeNode* getBestNode() const;
    00458
    00460     AlpsExitStatus getSolStatus() const {
    00461         return exitStatus_;
    00462     }
    00463
    00465     void setExitStatus(AlpsExitStatus status) {
    00466         exitStatus_ = status;
    00467     }
    00468
    00470     AlpsTimer & timer() {
    00471         return timer_;
    00472     }
    00473
    00475     AlpsTimer & subTreeTimer() {
    00476         return subTreeTimer_;
    00477     }
    00478
    00480     AlpsTimer & tempTimer() {
    00481         return tempTimer_;
    00482     }
    00483
    00485     virtual void searchLog() = 0;
    00487
    00492     int getNodeMemSize() { return nodeMemSize_; }
    00493     void setNodeMemSize(int ms) { nodeMemSize_ = ms; }
    00495
    00500     double getNodeProcessingTime() { return nodeProcessingTime_; }
    00501     void setNodeProcessingTime(double npTime) { nodeProcessingTime_ = npTime; }
    00503
    00504     int getLargeSize() const { return largeSize_; }
    00505
    00512     virtual double getIncumbentValue() const = 0;
    00513
    00516     virtual double getBestQuality() const = 0;
    00517
    00519     virtual double getBestEstimateQuality() { return ALPS_OBJ_MAX; }
    00520
    00521     virtual int getNumNodeLeftSystem(){ return nodeLeftNum_; }
    00522
    00525     virtual void printBestSolution(char* outputFile = 0) const = 0;
    00527
    00529     virtual int getProcRank() const { return 0; }
    00530
    00532     virtual int getMasterRank() const { return 0; }
    00533
    00535     virtual AlpsProcessType getProcType() const
    00536     { return AlpsProcessTypeSerial; } /* Default is serial */
    00537
    00544     AlpsNodeIndex_t nextNodeIndex() { return nextIndex_++; }
    00545
    00547     AlpsNodeIndex_t getNextNodeIndex() const { return nextIndex_; }
    00548
    00550     void setNextNodeIndex(AlpsNodeIndex_t s) { nextIndex_ = s; }
    00551
    00553     AlpsNodeIndex_t getMaxNodeIndex() const { return maxIndex_; }
    00554
    00556     void setMaxNodeIndex(AlpsNodeIndex_t s) { maxIndex_ = s; }
    00558
    00563     AlpsSearchStrategy<AlpsSubTree*>* getSubTreeSelection() const {
    00564         return treeSelection_;
    00565     }
    00566     void setSubTreeSelection(AlpsSearchStrategy<AlpsSubTree*>* tc) {
    00567         if (treeSelection_) delete treeSelection_;
    00568         treeSelection_ = tc;
    00569         subTreePool_->setComparison(*treeSelection_);
    00570     }
    00571     AlpsSearchStrategy<AlpsTreeNode*>* getNodeSelection() const {
    00572         return nodeSelection_;
    00573     }
    00574     void setNodeSelection(AlpsSearchStrategy<AlpsTreeNode*>* nc) {
    00575         if (nodeSelection_) delete nodeSelection_;
    00576         nodeSelection_ = nc;
    00577     }
    00578     AlpsSearchStrategy<AlpsTreeNode*>* getRampUpNodeSelection() const {
    00579         return rampUpNodeSelection_;
    00580     }
    00581     void setRampUpNodeSelection(AlpsSearchStrategy<AlpsTreeNode*>* nc) {
    00582         if (rampUpNodeSelection_) delete rampUpNodeSelection_;
    00583         rampUpNodeSelection_ = nc;
    00584     }
    00586
    00590     void passInMessageHandler(CoinMessageHandler * handler);
    00591
    00593     void newLanguage(CoinMessages::Language language);
    00594     void setLanguage(CoinMessages::Language language)
    00595     { newLanguage(language); }
    00596
    00598     CoinMessageHandler * messageHandler() const { return handler_; }
    00599
    00601     CoinMessages messages() { return messages_; }
    00602
    00604     CoinMessages * messagesPointer() { return &messages_; }
    00605
    00607     int getMsgLevel() { return msgLevel_; }
    00608
    00610     int getHubMsgLevel() { return hubMsgLevel_; }
    00611
    00613     int getMasterMsgLevel() { return workerMsgLevel_; }
    00614
    00616     int getlogFileLevel() { return logFileLevel_; }
    00617
    00619     int getNumNodeLog() const { return numNodeLog_; }
    00620
    00622     void setNumNodeLog(int num) { numNodeLog_ = num; }
    00624 };
    00625 #endif