Alps  1.5.3
AlpsKnowledgeBroker.h
Go to the documentation of this file.
1 /*===========================================================================*
2  * This file is part of the Abstract Library for Parallel Search (ALPS). *
3  * *
4  * ALPS is distributed under the Eclipse Public License as part of the *
5  * COIN-OR repository (http://www.coin-or.org). *
6  * *
7  * Authors: *
8  * *
9  * Yan Xu, Lehigh University *
10  * Ted Ralphs, Lehigh University *
11  * *
12  * Conceptual Design: *
13  * *
14  * Yan Xu, Lehigh University *
15  * Ted Ralphs, Lehigh University *
16  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
17  * Matthew Saltzman, Clemson University *
18  * *
19  * *
20  * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs. *
21  *===========================================================================*/
22 
23 #ifndef AlpsKnowledgeBroker_h_
24 #define AlpsKnowledgeBroker_h_
25 
26 #include <cmath>
27 #include <iosfwd>
28 #include <map>
29 #include <string>
30 
31 #include "CoinMessageHandler.hpp"
32 
33 #include "AlpsSearchStrategy.h"
34 #include "AlpsEnumProcessT.h"
35 #include "AlpsKnowledge.h"
36 #include "AlpsKnowledgePool.h"
37 #include "AlpsMessage.h"
38 #include "AlpsParams.h"
39 #include "AlpsSolutionPool.h"
40 #include "AlpsSubTree.h"
41 #include "AlpsSubTreePool.h"
42 #include "AlpsModel.h"
43 #include "AlpsTime.h"
44 
45 //#############################################################################
46 
49 
50  private:
51 
53  AlpsKnowledgeBroker& operator=(const AlpsKnowledgeBroker&);
54 
56  std::map<int, AlpsKnowledge*> decodeMap_;
57 
58  protected:
59 
61  std::string instanceName_;
62 
65 
68 
76 
79 
81  std::map<AlpsKnowledgeType, AlpsKnowledgePool*>* pools_;
83 
90 
93 
96 
100 
105  /***/
108 
111 
114 
116  int solNum_;
117 
120 
123 
126 
129 
133 
136 
139 
142 
144  double peakMemory_;
145 
149 
155  AlpsSearchStrategy<AlpsSubTree*>* treeSelection_;
156 
158  AlpsSearchStrategy<AlpsTreeNode*>* nodeSelection_;
159 
161  AlpsSearchStrategy<AlpsTreeNode*>* rampUpNodeSelection_;
163 
169  CoinMessageHandler * handler_;
170 
172  CoinMessages messages_;
173 
177 
181 
185 
189 
191  std::string logfile_;
193 
196 
199 
202 
205 
208 
209  public:
210 
213 
215  virtual ~AlpsKnowledgeBroker();
216 
217  //-------------------------------------------------------------------------
230  void registerClass(int name, AlpsKnowledge* userKnowledge) {
231  // Check if alread have one.
232  std::map<int, AlpsKnowledge*>::iterator pos, pos1;
233  pos = decodeMap_.find(name);
234  pos1 = decodeMap_.end();
235 
236  if (pos != pos1) {
237  AlpsKnowledge* kl = pos->second;
238  decodeMap_.erase(pos);
239  delete kl;
240  }
241 
242  decodeMap_[name] = userKnowledge;
243  }
244 
253  const AlpsKnowledge* decoderObject(int name) {
254  return decodeMap_[name];
255  }
257 
258  //------------------------------------------------------
259 
265  virtual void initializeSearch(int argc,
266  char* argv[],
267  AlpsModel& model) = 0;
268 
270  virtual void rootSearch(AlpsTreeNode* root) = 0;
271 
273  virtual void search(AlpsModel *model) {
274  AlpsTreeNode* root = model->createRoot();
275  rootSearch(root);
276  }
278 
279  //------------------------------------------------------
280 
285  AlpsPhase getPhase() { return phase_; }
286  void setPhase(AlpsPhase ph) { phase_ = ph; }
288 
290  AlpsModel *getModel() { return model_; }
291  void setModel(AlpsModel *m) { model_ = m; }
293 
295  int getTreeDepth() { return treeDepth_; }
296 
298  void setPeakMemory(double size) { peakMemory_ = size; }
299 
301  double getPeakMemory() { return peakMemory_; }
302 
308  void setupKnowledgePools();
309 
313  // AlpsKnowledgePool* akp = static_cast<AlpsKnowledgePool*>(kp);
314  pools_->insert
315  (std::pair<AlpsKnowledgeType, AlpsKnowledgePool*>(kt, kp));
316  }
317  else {
318  throw CoinError("Broker doesn't manage this type of knowledge",
319  "addKnowledgePool()", "AlpsKnowledgeBroker");
320  }
321  }
322 
326  return (*pools_)[kt];
327  }
328  else {
329  throw CoinError("Broker doesn't manage this type of knowledge",
330  "getKnowledgePool()", "AlpsKnowledgeBroker");
331  }
332  }
333 
335  virtual int getNumKnowledges(AlpsKnowledgeType kt) const;
336 
339  virtual int getMaxNumKnowledges(AlpsKnowledgeType kt) const {
342  }
343  else {
344  throw CoinError("Broker doesn't manage this type of knowledge",
345  "getMaxNumKnowledges()", "AlpsKnowledgeBroker");
346  }
347  }
348 
351  virtual void setMaxNumKnowledges(AlpsKnowledgeType kt, int num) {
354  }
355  else {
356  throw CoinError("Broker doesn't manage this type of knowledge",
357  "setMaxNumKnowledges()", "AlpsKnowledgeBroker");
358  }
359  }
360 
363  virtual bool hasKnowledge(AlpsKnowledgeType kt) const {
365  return getKnowledgePool(kt)->hasKnowledge();
366  else
367  throw CoinError("Broker doesn't manage this type of knowledge",
368  "hasKnowledge()", "AlpsKnowledgeBroker");
369  }
370 
372  virtual std::pair<AlpsKnowledge*, double>
375  return getKnowledgePool(kt)->getKnowledge();
376  }
377  else {
378  throw CoinError("Broker doesn't manage this type of knowledge",
379  "getKnowledge()", "AlpsKnowledgeBroker");
380  }
381  }
382 
384  virtual void popKnowledge(AlpsKnowledgeType kt) {
387  }
388  else {
389  throw CoinError("Broker doesn't manage this type of knowledge",
390  "popKnowledge()", "AlpsKnowledgeBroker");
391  }
392  }
393 
395  virtual std::pair<AlpsKnowledge*, double>
397 
400  std::vector<std::pair<AlpsKnowledge*,
401  double> >& kls) const {
404  }
405  else {
406  throw CoinError("Broker doesn't manage this type of knowledge",
407  "popKnowledge()", "AlpsKnowledgeBroker");
408  }
409  }
410 
412  virtual void addKnowledge(AlpsKnowledgeType kt,
413  AlpsKnowledge* kl,
414  double value ) {
416  getKnowledgePool(kt)->addKnowledge(kl, value);
417  }
418  else {
419  throw CoinError("Broker doesn't manage this type of knowledge",
420  "popKnowledge()", "AlpsKnowledgeBroker");
421  }
422  }
424 
429  int getNumNodesProcessed() const {
430  return nodeProcessedNum_;
431  }
432 
434  int getNumNodesBranched() const {
435  return nodeBranchedNum_;
436  }
437 
439  int getNumNodesDiscarded() const {
440  return nodeDiscardedNum_;
441  }
442 
444  int getNumNodesPartial() const {
445  return nodePartialNum_;
446  }
447 
450  return systemNodeProcessed_;
451  }
452 
454  virtual int updateNumNodesLeft();
455 
457  virtual AlpsTreeNode* getBestNode() const;
458 
461  return exitStatus_;
462  }
463 
466  exitStatus_ = status;
467  }
468 
471  return timer_;
472  }
473 
476  return subTreeTimer_;
477  }
478 
481  return tempTimer_;
482  }
483 
485  virtual void searchLog() = 0;
487 
492  int getNodeMemSize() { return nodeMemSize_; }
493  void setNodeMemSize(int ms) { nodeMemSize_ = ms; }
495 
501  void setNodeProcessingTime(double npTime) { nodeProcessingTime_ = npTime; }
503 
504  int getLargeSize() const { return largeSize_; }
505 
512  virtual double getIncumbentValue() const = 0;
513 
516  virtual double getBestQuality() const = 0;
517 
519  virtual double getBestEstimateQuality() { return ALPS_OBJ_MAX; }
520 
521  virtual int getNumNodeLeftSystem(){ return nodeLeftNum_; }
522 
525  virtual void printBestSolution(char* outputFile = 0) const = 0;
527 
529  virtual int getProcRank() const { return 0; }
530 
532  virtual int getMasterRank() const { return 0; }
533 
535  virtual AlpsProcessType getProcType() const
536  { return AlpsProcessTypeSerial; } /* Default is serial */
537 
544  AlpsNodeIndex_t nextNodeIndex() { return nextIndex_++; }
545 
548 
550  void setNextNodeIndex(AlpsNodeIndex_t s) { nextIndex_ = s; }
551 
554 
556  void setMaxNodeIndex(AlpsNodeIndex_t s) { maxIndex_ = s; }
558 
563  AlpsSearchStrategy<AlpsSubTree*>* getSubTreeSelection() const {
564  return treeSelection_;
565  }
566  void setSubTreeSelection(AlpsSearchStrategy<AlpsSubTree*>* tc) {
567  if (treeSelection_) delete treeSelection_;
568  treeSelection_ = tc;
569  subTreePool_->setComparison(*treeSelection_);
570  }
571  AlpsSearchStrategy<AlpsTreeNode*>* getNodeSelection() const {
572  return nodeSelection_;
573  }
574  void setNodeSelection(AlpsSearchStrategy<AlpsTreeNode*>* nc) {
575  if (nodeSelection_) delete nodeSelection_;
576  nodeSelection_ = nc;
577  }
578  AlpsSearchStrategy<AlpsTreeNode*>* getRampUpNodeSelection() const {
579  return rampUpNodeSelection_;
580  }
581  void setRampUpNodeSelection(AlpsSearchStrategy<AlpsTreeNode*>* nc) {
582  if (rampUpNodeSelection_) delete rampUpNodeSelection_;
583  rampUpNodeSelection_ = nc;
584  }
586 
590  void passInMessageHandler(CoinMessageHandler * handler);
591 
593  void newLanguage(CoinMessages::Language language);
594  void setLanguage(CoinMessages::Language language)
595  { newLanguage(language); }
596 
598  CoinMessageHandler * messageHandler() const { return handler_; }
599 
601  CoinMessages messages() { return messages_; }
602 
604  CoinMessages * messagesPointer() { return &messages_; }
605 
607  int getMsgLevel() { return msgLevel_; }
608 
610  int getHubMsgLevel() { return hubMsgLevel_; }
611 
614 
616  int getlogFileLevel() { return logFileLevel_; }
617 
619  int getNumNodeLog() const { return numNodeLog_; }
620 
622  void setNumNodeLog(int num) { numNodeLog_ = num; }
624 };
625 #endif
int workerMsgLevel_
The leve of printing message to screen of workers.
CoinMessages messages_
Alps messages.
int getTreeDepth()
Get tree depth.
AlpsNodeIndex_t maxIndex_
The maximum index can been assigned on this process.
void newLanguage(CoinMessages::Language language)
Set language.
virtual void initializeSearch(int argc, char *argv[], AlpsModel &model)=0
Do some initialization for search.
std::map< AlpsKnowledgeType, AlpsKnowledgePool * > * pools_
The collection of pools managed by the knowledge broker.
int hubMsgLevel_
The leve of printing message to screen of hubs.
virtual ~AlpsKnowledgeBroker()
Destructor.
int getNumNodeLog() const
Get times that node log has been printed.
virtual void addKnowledge(AlpsKnowledgeType kt, AlpsKnowledge *kl, double value)
Add a knowledge in the given type of knowledge pools.
int getlogFileLevel()
Return log file level.
virtual void popKnowledge(AlpsKnowledgeType kt)
Remove the a knowledge from the given type of knowledge pools.
int getNumNodesBranched() const
Query the number of node processed by this process.
AlpsNodeIndex_t nextNodeIndex()
Query the next index assigned to a newly created node, and then increment the nextIndex_ by 1...
virtual int getMaxNumKnowledges(AlpsKnowledgeType kt) const
Query the max number of knowledge can be stored in a given type of knowledge pools.
const AlpsKnowledge * decoderObject(int name)
This method returns the pointer to an empty object of the registered class name.
int nodeLeftNum_
The number of nodes left.
AlpsSearchStrategy< AlpsTreeNode * > * nodeSelection_
Node selection criterion.
AlpsExitStatus
Definition: Alps.h:101
int treeDepth_
The depth of the tree.
int systemNodeProcessed_
To record how many nodes processed by the system (used in parallel code).
int nodeBranchedNum_
The number of nodes that have been branched.
void setMaxNodeIndex(AlpsNodeIndex_t s)
Set the upper bound of node indices.
The abstract base class of any user-defined class that Alps has to know about in order to encode/deco...
Definition: AlpsKnowledge.h:51
CoinMessageHandler * handler_
Message handler.
AlpsSearchStrategy< AlpsTreeNode * > * rampUpNodeSelection_
Node selection criterion.
void setNextNodeIndex(AlpsNodeIndex_t s)
Set nextIndex_.
The base class of knowledge broker class.
AlpsTimer & tempTimer()
Query secondary timer.
This class holds one node of the search tree.
Definition: AlpsTreeNode.h:50
void setModel(AlpsModel *m)
int getHubMsgLevel()
Return msg level.
void setRampUpNodeSelection(AlpsSearchStrategy< AlpsTreeNode * > *nc)
virtual void addKnowledge(AlpsKnowledge *nk, double priority)=0
Add a knowledge to pool.
double getPeakMemory()
Get peak memory usage.
AlpsSolutionPool * solPool_
A solution pool containing the solutions found.
CoinMessages * messagesPointer()
Return pointer to messages.
AlpsNodeIndex_t getMaxNodeIndex() const
Queriy the upper bound of node indices.
void setNodeProcessingTime(double npTime)
virtual int updateNumNodesLeft()
Update the number of left nodes on this process.
The subtree pool is used to store subtrees.
AlpsTimer timer_
Main timer.
void setPeakMemory(double size)
Set peak memory usage.
AlpsExitStatus getSolStatus() const
Query search termination status.
int bestSolNode_
The number of nodes pocessed to find the solution.
AlpsSubTree * workingSubTree_
Point to the subtree that being explored.
virtual double getBestQuality() const =0
The process (serial) / the master (parallel) queries the quality of the best solution that it knows...
double nodeProcessingTime_
The approximately CPU time to process a node.
void setExitStatus(AlpsExitStatus status)
Set terminate status.
int logFileLevel_
The degree of log file.
virtual void setMaxNumKnowledges(int num)
Set the quantity limit of knowledges that can be stored in the pool.
virtual bool hasKnowledge() const
Check whether the pool has knowledge.
virtual std::pair< AlpsKnowledge *, double > getKnowledge(AlpsKnowledgeType kt) const
Get a knowledge, but doesn't remove it from the pool.
CoinMessageHandler * messageHandler() const
Return handler.
AlpsSearchStrategy< AlpsTreeNode * > * getRampUpNodeSelection() const
AlpsNodeIndex_t nextIndex_
The index to be assigned to a new search tree node.
int getNumNodesDiscarded() const
Query the number of node processed by this process.
AlpsPhase
Definition: Alps.h:129
AlpsKnowledgeBroker()
Default constructor.
AlpsPhase phase_
Alps phase.
AlpsModel * model_
Pointer to model.
virtual void searchLog()=0
Search statistics log.
AlpsProcessType
This enumerative constant describes the various process types.
int getMasterMsgLevel()
Return msg level.
double peakMemory_
Peak memory usage.
virtual int getMaxNumKnowledges() const
Query the quantity limit of knowledges.
bool needWorkingSubTree_
Indicate whether need a new subtree.
virtual std::pair< AlpsKnowledge *, double > getBestKnowledge(AlpsKnowledgeType kt) const
Get the best knowledge in the given type of knowledge pools.
AlpsSearchStrategy< AlpsSubTree * > * getSubTreeSelection() const
virtual std::pair< AlpsKnowledge *, double > getKnowledge() const =0
Query a knowledge, but doesn't remove it from the pool.
int largeSize_
The size of largest message buffer can be sent or received.
virtual double getBestEstimateQuality()
Get best estimalted quality in system.
void setPhase(AlpsPhase ph)
std::string instanceName_
The instance name.
bool userBalancePeriod_
Has user input balance period.
virtual int getNumKnowledges(AlpsKnowledgeType kt) const
Query the number of knowledge in the given type of a knowledge pool.
void passInMessageHandler(CoinMessageHandler *handler)
Pass in Message handler (not deleted at end).
AlpsSearchStrategy< AlpsSubTree * > * treeSelection_
Tree selection criterion.
virtual int getMasterRank() const
Query the global rank of the Master.
AlpsSearchStrategy< AlpsTreeNode * > * getNodeSelection() const
virtual void getAllKnowledges(AlpsKnowledgeType kt, std::vector< std::pair< AlpsKnowledge *, double > > &kls) const
Get all knowledges in the given type of knowledge pools.
void setupKnowledgePools()
Set up knowledge pools for this broker.
AlpsTimer tempTimer_
Secondary timer.
virtual AlpsTreeNode * createRoot()
Create the root node.
Definition: AlpsModel.h:102
virtual AlpsProcessType getProcType() const
Query the type (master, hub, or worker) of the process.
virtual void search(AlpsModel *model)
Search best solution for a given model.
int numNodeLog_
Times that node log is printed.
virtual void getAllKnowledges(std::vector< std::pair< AlpsKnowledge *, double > > &kls) const
Get a reference to all the knowledges in the pool.
AlpsNodeIndex_t getNextNodeIndex() const
Query the next index assigned to a newly created node.
void setSubTreeSelection(AlpsSearchStrategy< AlpsSubTree * > *tc)
virtual bool hasKnowledge(AlpsKnowledgeType kt) const
Query whether there are knowledges in the given type of knowledge pools.
virtual void popKnowledge()
Remove the queried knowledge from the pool.
virtual int getNumNodeLeftSystem()
int nodeDiscardedNum_
The number of nodes that have been discarded before processing.
virtual void printBestSolution(char *outputFile=0) const =0
The process (serial) / the master (parallel) outputs the best solution that it knows to a file or std...
int nodePartialNum_
The number of nodes that are pregnant.
virtual void rootSearch(AlpsTreeNode *root)=0
Explore the tree rooted as the given root.
#define ALPS_OBJ_MAX
Definition: Alps.h:145
AlpsExitStatus exitStatus_
The status of search when terminated.
virtual int getProcRank() const
Qeury the global rank of process.
virtual void setMaxNumKnowledges(AlpsKnowledgeType kt, int num)
Set the max number of knowledge can be stored in a given type o fknowledge pools. ...
CoinMessages messages()
Return messages.
void setComparison(AlpsSearchStrategy< AlpsSubTree * > &compare)
Set comparison function and resort heap.
int nodeProcessedNum_
The number of nodes that have been processed.
int getNumNodesPartial() const
Query the number of node in the queue that are pregnant.
int getNumNodesProcessedSystem() const
Query the number of node processed by the system.
AlpsKnowledgePool * getKnowledgePool(AlpsKnowledgeType kt) const
Retrieve a knowledge pool in the Knowledge base.
AlpsTimer & timer()
Query timer.
int nodeMemSize_
The approximate memory size (bytes) of a node with full description.
std::string logfile_
The log file.
AlpsTimer subTreeTimer_
Subtree timer.
void setNodeSelection(AlpsSearchStrategy< AlpsTreeNode * > *nc)
int getMsgLevel()
Return msg level.
int AlpsNodeIndex_t
Definition: AlpsTreeNode.h:37
int getNumNodesProcessed() const
Query the number of node processed by this process.
In the solution pool we assume that the lower the priority value the more desirable the solution is...
void setLanguage(CoinMessages::Language language)
void setNumNodeLog(int num)
Get times that node log has been printed.
void addKnowledgePool(AlpsKnowledgeType kt, AlpsKnowledgePool *kp)
Add a knowledge pool into the Knowledge pools.
This class contains the data pertaining to a particular subtree in the search tree.
Definition: AlpsSubTree.h:47
int solNum_
The number of solutions found.
AlpsTimer & subTreeTimer()
Query subtree timer.
virtual AlpsTreeNode * getBestNode() const
Query the best node in the subtree pool.
virtual double getIncumbentValue() const =0
The process queries the objective value of the incumbent that it stores.
void registerClass(int name, AlpsKnowledge *userKnowledge)
Every user derived knowledge class must register.
AlpsKnowledgeType
Definition: Alps.h:88
int msgLevel_
The leve of printing message to screen of the master and general message.
AlpsSubTreePool * subTreePool_
A subtree pool holding a collection of subtrees.