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

- File List
- File Members

# /home/coin/SVN-release/Bcps-0.91.0/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 Common 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-2009, 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
    00129     int systemNodeProcessed_;
    00130
    00132     int nodeLeftNum_;
    00133
    00135     int treeDepth_;
    00136
    00138     int bestSolDepth_;
    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     int getBestSolDepth() { return bestSolDepth_; }
    00299
    00301     void setBestSolDepth(int depth) { bestSolDepth_ = depth; }
    00302
    00304     void setPeakMemory(double size) { peakMemory_ = size; }
    00305
    00307     double getPeakMemory() { return peakMemory_; }
    00308
    00314     void setupKnowledgePools();
    00315
    00317     inline void addKnowledgePool(AlpsKnowledgeType kt, AlpsKnowledgePool* kp) {
    00318         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00319             // AlpsKnowledgePool* akp = static_cast<AlpsKnowledgePool*>(kp);
    00320             pools_->insert
    00321                 (std::pair<AlpsKnowledgeType, AlpsKnowledgePool*>(kt, kp));
    00322         }
    00323         else {
    00324             throw CoinError("Broker doesn't manage this type of knowledge",
    00325                             "addKnowledgePool()", "AlpsKnowledgeBroker");
    00326         }
    00327     }
    00328
    00330     inline  AlpsKnowledgePool* getKnowledgePool(AlpsKnowledgeType kt) const {
    00331         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00332             return (*pools_)[kt];
    00333         }
    00334         else {
    00335             throw CoinError("Broker doesn't manage this type of knowledge",
    00336                             "getKnowledgePool()", "AlpsKnowledgeBroker");
    00337         }
    00338     }
    00339
    00341     virtual int getNumKnowledges(AlpsKnowledgeType kt) const;
    00342
    00345     virtual int getMaxNumKnowledges(AlpsKnowledgeType kt) const {
    00346         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00347             return getKnowledgePool(kt)->getMaxNumKnowledges();
    00348         }
    00349         else {
    00350             throw CoinError("Broker doesn't manage this type of knowledge",
    00351                             "getMaxNumKnowledges()", "AlpsKnowledgeBroker");
    00352         }
    00353     }
    00354
    00357     virtual void setMaxNumKnowledges(AlpsKnowledgeType kt, int num) {
    00358         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00359             getKnowledgePool(kt)->setMaxNumKnowledges(num);
    00360         }
    00361         else {
    00362             throw CoinError("Broker doesn't manage this type of knowledge",
    00363                             "setMaxNumKnowledges()", "AlpsKnowledgeBroker");
    00364         }
    00365     }
    00366
    00369     virtual bool hasKnowledge(AlpsKnowledgeType kt) const {
    00370         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree)
    00371             return getKnowledgePool(kt)->hasKnowledge();
    00372         else
    00373             throw CoinError("Broker doesn't manage this type of knowledge",
    00374                             "hasKnowledge()", "AlpsKnowledgeBroker");
    00375     }
    00376
    00378     virtual std::pair<AlpsKnowledge*, double>
    00379         getKnowledge(AlpsKnowledgeType kt) const {
    00380         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00381             return getKnowledgePool(kt)->getKnowledge();
    00382         }
    00383         else {
    00384             throw CoinError("Broker doesn't manage this type of knowledge",
    00385                             "getKnowledge()", "AlpsKnowledgeBroker");
    00386         }
    00387     }
    00388
    00390     virtual void popKnowledge(AlpsKnowledgeType kt) {
    00391         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00392             getKnowledgePool(kt)->popKnowledge();
    00393         }
    00394         else {
    00395             throw CoinError("Broker doesn't manage this type of knowledge",
    00396                             "popKnowledge()", "AlpsKnowledgeBroker");
    00397         }
    00398     }
    00399
    00401     virtual std::pair<AlpsKnowledge*, double>
    00402         getBestKnowledge(AlpsKnowledgeType kt) const;
    00403
    00405     virtual void getAllKnowledges (AlpsKnowledgeType kt,
    00406                                    std::vector<std::pair<AlpsKnowledge*,
    00407                                    double> >& kls)  const {
    00408         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00409             getKnowledgePool(kt)->getAllKnowledges(kls);
    00410         }
    00411         else {
    00412             throw CoinError("Broker doesn't manage this type of knowledge",
    00413                             "popKnowledge()", "AlpsKnowledgeBroker");
    00414         }
    00415     }
    00416
    00418     virtual void addKnowledge(AlpsKnowledgeType kt,
    00419                               AlpsKnowledge* kl,
    00420                               double value ) {
    00421         if(kt == AlpsKnowledgeTypeSolution || kt == AlpsKnowledgeTypeSubTree) {
    00422             getKnowledgePool(kt)->addKnowledge(kl, value);
    00423         }
    00424         else {
    00425             throw CoinError("Broker doesn't manage this type of knowledge",
    00426                             "popKnowledge()", "AlpsKnowledgeBroker");
    00427         }
    00428     }
    00430
    00435     int getNumNodesProcessed() const {
    00436         return nodeProcessedNum_;
    00437     }
    00438
    00440     int getNumNodesBranched() const {
    00441         return nodeBranchedNum_;
    00442     }
    00443
    00445     int getNumNodesDiscarded() const {
    00446         return nodeDiscardedNum_;
    00447     }
    00448
    00450     int getNumNodesProcessedSystem() const {
    00451         return systemNodeProcessed_;
    00452     }
    00453
    00455     virtual int updateNumNodesLeft();
    00456
    00458     virtual AlpsTreeNode* getBestNode() const;
    00459
    00461     AlpsExitStatus getSolStatus() const {
    00462         return exitStatus_;
    00463     }
    00464
    00466     void setExitStatus(AlpsExitStatus status) {
    00467         exitStatus_ = status;
    00468     }
    00469
    00471     AlpsTimer & timer() {
    00472         return timer_;
    00473     }
    00474
    00476     AlpsTimer & subTreeTimer() {
    00477         return subTreeTimer_;
    00478     }
    00479
    00481     AlpsTimer & tempTimer() {
    00482         return tempTimer_;
    00483     }
    00484
    00486     virtual void searchLog() = 0;
    00488
    00493     int getNodeMemSize() { return nodeMemSize_; }
    00494     void setNodeMemSize(int ms) { nodeMemSize_ = ms; }
    00496
    00501     double getNodeProcessingTime() { return nodeProcessingTime_; }
    00502     void setNodeProcessingTime(double npTime) { nodeProcessingTime_ = npTime; }
    00504
    00505     int getLargeSize() const { return largeSize_; }
    00506
    00513     virtual double getIncumbentValue() const = 0;
    00514
    00517     virtual double getBestQuality() const = 0;
    00518
    00520     virtual double getBestEstimateQuality() { return ALPS_OBJ_MAX; }
    00521
    00522     virtual int getNumNodeLeftSystem(){ return nodeLeftNum_; }
    00523
    00526     virtual void printBestSolution(char* outputFile = 0) const = 0;
    00528
    00530     virtual int getProcRank() const { return 0; }
    00531
    00533     virtual int getMasterRank() const { return 0; }
    00534
    00536     virtual AlpsProcessType getProcType() const
    00537     { return AlpsProcessTypeSerial; } /* Default is serial */
    00538
    00545     AlpsNodeIndex_t nextNodeIndex() { return nextIndex_++; }
    00546
    00548     AlpsNodeIndex_t getNextNodeIndex() const { return nextIndex_; }
    00549
    00551     void setNextNodeIndex(AlpsNodeIndex_t s) { nextIndex_ = s; }
    00552
    00554     AlpsNodeIndex_t getMaxNodeIndex() const { return maxIndex_; }
    00555
    00557     void setMaxNodeIndex(AlpsNodeIndex_t s) { maxIndex_ = s; }
    00559
    00564     AlpsSearchStrategy<AlpsSubTree*>* getSubTreeSelection() const {
    00565         return treeSelection_;
    00566     }
    00567     void setSubTreeSelection(AlpsSearchStrategy<AlpsSubTree*>* tc) {
    00568         if (treeSelection_) delete treeSelection_;
    00569         treeSelection_ = tc;
    00570         subTreePool_->setComparison(*treeSelection_);
    00571     }
    00572     AlpsSearchStrategy<AlpsTreeNode*>* getNodeSelection() const {
    00573         return nodeSelection_;
    00574     }
    00575     void setNodeSelection(AlpsSearchStrategy<AlpsTreeNode*>* nc) {
    00576         if (nodeSelection_) delete nodeSelection_;
    00577         nodeSelection_ = nc;
    00578     }
    00579     AlpsSearchStrategy<AlpsTreeNode*>* getRampUpNodeSelection() const {
    00580         return rampUpNodeSelection_;
    00581     }
    00582     void setRampUpNodeSelection(AlpsSearchStrategy<AlpsTreeNode*>* nc) {
    00583         if (rampUpNodeSelection_) delete rampUpNodeSelection_;
    00584         rampUpNodeSelection_ = nc;
    00585     }
    00587
    00591     void passInMessageHandler(CoinMessageHandler * handler);
    00592
    00594     void newLanguage(CoinMessages::Language language);
    00595     void setLanguage(CoinMessages::Language language)
    00596     { newLanguage(language); }
    00597
    00599     CoinMessageHandler * messageHandler() const { return handler_; }
    00600
    00602     CoinMessages messages() { return messages_; }
    00603
    00605     CoinMessages * messagesPointer() { return &messages_; }
    00606
    00608     int getMsgLevel() { return msgLevel_; }
    00609
    00611     int getHubMsgLevel() { return hubMsgLevel_; }
    00612
    00614     int getMasterMsgLevel() { return workerMsgLevel_; }
    00615
    00617     int getlogFileLevel() { return logFileLevel_; }
    00618
    00620     int getNumNodeLog() const { return numNodeLog_; }
    00621
    00623     void setNumNodeLog(int num) { numNodeLog_ = num; }
    00625 };
    00626 #endif