AlpsTreeNode.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 AlpsTreeNode_h_
24 #define AlpsTreeNode_h_
25 
26 #include <algorithm>
27 #include <utility>
28 #include <vector>
29 
30 #include "CoinError.hpp"
31 #include "CoinSort.hpp"
32 
33 #include "Alps.h"
34 #include "AlpsKnowledge.h"
35 #include "AlpsNodeDesc.h"
36 
37 typedef int AlpsNodeIndex_t;
38 
40 class AlpsSubTree;
41 
42 //#############################################################################
48 //#############################################################################
49 
50 class AlpsTreeNode : public AlpsKnowledge {
51  private:
52  AlpsTreeNode(const AlpsTreeNode&);
54 
55  protected:
57  //AlpsSubTree* subTree_;
58 
60  bool active_;
61 
64 
66  int depth_;
67 
69  double solEstimate_;
70 
72  double quality_;
73 
76 
79 
82 
83 #if defined(ALPS_MAX_CHILD_NUM) // *FIXME* : Do we want ifdefs?
84 
85  AlpsTreeNode* children_[ALPS_MAX_CHILD_NUM];
86 #else
88 #endif
89 
92  int explicit_;
93 
96 
99 
102  // Need broker to get incumbent value and add solution when process().
104 
106  // 0: default; 1: in subtree to be sent: 2: in subtree's node pool
107  int sentMark_;
108 
109  public:
111  :
112  active_(false),
113  index_(-1),
114  depth_(-1),
116  quality_(ALPS_OBJ_MAX), // Smaller than default incumbentValue
117  parent_(0),
118  parentIndex_(-1),
119  numChildren_(0),
120 #if defined(ALPS_MAX_CHILD_NUM) // *FIXME* : Do we want ifdefs?
121  // AlpsTreeNode* children_[ALPS_MAX_CHILD_NUM];
122 #else
123  children_(0),
124 #endif
125  explicit_(0),
126  desc_(0),
128  knowledgeBroker_(0),
129  sentMark_(0)
131 
132  virtual ~AlpsTreeNode() {
133  assert(numChildren_ == 0);
134  //std::cout << "---- delete Alps part of node " << index_ << std::endl;
135 #if ! defined(ALPS_MAX_CHILD_NUM)
136  if (children_ != 0) {
137  delete [] children_;
138  children_ = 0;
139  }
140 #endif
141  if (desc_ != 0) {
142  delete desc_;
143  desc_ = 0;
144  }
145  }
146 
147  bool operator<(const AlpsTreeNode& compNode)
148  { return quality_ < compNode.getQuality(); }
149 
152  AlpsNodeDesc* getDesc() const { return desc_; }
153  void setDesc(AlpsNodeDesc* desc) { desc_ = desc; }
154 
157  { return knowledgeBroker_; }
159  { knowledgeBroker_ = kb; }
160 
163  /* FIXME: I think that we probably want the argument to be a diff'd
164  description, but this is open to debate. Maybe we should
165  overload this method and have a version that creates a diff'd
166  description, as well as one that creates an explicit
167  description. */
168  virtual AlpsTreeNode* createNewTreeNode(AlpsNodeDesc*& desc) const = 0;
169 
171  inline AlpsNodeStatus getStatus() const { return status_; }
173  inline void setStatus(const AlpsNodeStatus stat) { status_ = stat; }
175 
177  inline bool isCandidate() const {
179  return status_ == AlpsNodeStatusCandidate; }
180  inline bool isEvaluated() const {
181  return status_ == AlpsNodeStatusEvaluated; }
182  inline bool isPregnant() const {
183  return status_ == AlpsNodeStatusPregnant; }
184  inline bool isBranched() const {
185  return status_ == AlpsNodeStatusBranched; }
186  inline bool isFathomed() const {
187  return status_ == AlpsNodeStatusFathomed; }
188  inline bool isDiscarded() const {
189  return status_ == AlpsNodeStatusDiscarded; }
191 
193  inline bool isActive() const { return active_; }
195  inline void setActive(const bool yesno) { active_ = yesno; }
197 
199  inline AlpsNodeIndex_t getIndex() const { return index_; }
201  inline void setIndex(const AlpsNodeIndex_t index) { index_ = index; }
203 
205  inline int getDepth() const { return depth_; }
207  inline void setDepth(const int depth) { depth_ = depth; }
209 
211  inline double getSolEstimate() const { return solEstimate_; }
213  inline void setSolEstimate(double est) { solEstimate_ = est; }
215 
217  inline double getQuality() const { return quality_; }
219  inline void setQuality(double quality) { quality_ = quality; }
221 
223  inline int getNumChildren() const { return numChildren_; }
225  inline void setNumChildren(const int numChildren) {
226  numChildren_ = numChildren;
227 #if ! defined(ALPS_MAX_CHILD_NUM)
228  if (children_ != 0) {
229  delete [] children_;
230  children_ = 0;
231  }
233 #endif
234  }
235  // Change by s
236  inline void modifyNumChildren(const int s) { numChildren_ += s; }
238 
239  // *FIXME* : Sanity check. Maybe we should check that the argument is in
240  // the correct range, but how do we do that? This makes the code
241  // inefficient so it should be done with #ifdefs so it can be compiled
242  // out. But in that case, we should move this code to the implementation
243  // file and it won't get inlined anymore.
244 
246  inline AlpsTreeNode* getChild(const int i) const { return children_[i]; }
248 
249  //FIXME: Compiler complains about this second declaration. Not sure how to
250  //declare a const and a non-const version of a function with the same
251  //arguments...
252  // /** Returns a const pointer to the ith child. */
253  // const AlpsTreeNode* getChild(const int i) const { return children_[i]; }
254  inline void setChild(const int i, AlpsTreeNode* node)
255  { children_[i] = node; }
257 
261  void removeChild(AlpsTreeNode*& child);
262 
264  void addChild(AlpsTreeNode*& child);
265 
269  void removeDescendants();
270 
272  //inline AlpsSubTree* getSubTree() const { return subTree_; }
273  //inline void setSubTree(AlpsSubTree* tree) { subTree_ = tree; }
274 
276  inline AlpsTreeNode* getParent() const { return parent_; }
278  inline void setParent(AlpsTreeNode* parent) { parent_ = parent; }
280 
282  inline AlpsNodeIndex_t getParentIndex() const { return parentIndex_; }
284  inline void setParentIndex(AlpsNodeIndex_t index)
285  { parentIndex_ = index; }
287 
290  inline int getExplicit() const { return explicit_; }
292  inline void setExplicit(int fp) { explicit_ = fp; }
294 
296  virtual void convertToExplicit() {}
298  virtual void convertToRelative() {}
300 
302  inline int getSentMark() const { return sentMark_; }
304  inline void setSentMark(const int tf) { sentMark_ = tf; }
306 
336  virtual int process(bool isRoot = false, bool rampUp = false) = 0;
337 
347  virtual std::vector< CoinTriple<AlpsNodeDesc*, AlpsNodeStatus, double> >
348  branch() = 0;
349 
350  protected:
351 
353  AlpsReturnStatus encodeAlps(AlpsEncoded *encoded) const;
354 
356  AlpsReturnStatus decodeAlps(AlpsEncoded &encoded);
357 
358 };
359 #endif
void addChild(AlpsTreeNode *&child)
Add a child to the list of children for this node.
void setActive(const bool yesno)
Query/set node in-process indicator.
Definition: AlpsTreeNode.h:195
The base class of knowledge broker class.
bool isDiscarded() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:188
int getExplicit() const
Get/set the indication of whether the node has full or differencing description.
Definition: AlpsTreeNode.h:291
int explicit_
Indicate whether the node description is explicit(1) or relative(0).
Definition: AlpsTreeNode.h:92
AlpsNodeDesc * getDesc() const
Definition: AlpsTreeNode.h:152
AlpsNodeIndex_t index_
The unique index of the tree node (across the whole search tree).
Definition: AlpsTreeNode.h:63
double getSolEstimate() const
Query/set the solution estimate of the node.
Definition: AlpsTreeNode.h:212
bool isEvaluated() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:180
double solEstimate_
The solution estimate.
Definition: AlpsTreeNode.h:69
AlpsReturnStatus
Definition: Alps.h:118
AlpsNodeIndex_t getParentIndex() const
Get/set the index of the parent of the node.
Definition: AlpsTreeNode.h:283
void setNumChildren(const int numChildren)
Query/set what the number of children.
Definition: AlpsTreeNode.h:225
AlpsNodeStatus
The possible stati for the search nodes.
Definition: Alps.h:61
void setChild(const int i, AlpsTreeNode *node)
Returns a const pointer to the ith child.
Definition: AlpsTreeNode.h:254
AlpsKnowledgeBroker * knowledgeBroker_
A pointer to the knowledge broker of the process where this node is processed.
Definition: AlpsTreeNode.h:103
void setParent(AlpsTreeNode *parent)
Get/set subtree.
Definition: AlpsTreeNode.h:278
AlpsTreeNode & operator=(const AlpsTreeNode &)
AlpsNodeIndex_t parentIndex_
The index of parent of the tree node.
Definition: AlpsTreeNode.h:78
AlpsNodeDesc * modifyDesc()
Access the desc so that can modify it.
Definition: AlpsTreeNode.h:151
This class contains the data pertaining to a particular subtree in the search tree.
Definition: AlpsSubTree.h:47
bool operator<(const AlpsTreeNode &compNode)
Definition: AlpsTreeNode.h:147
AlpsKnowledgeBroker * getKnowledgeBroker() const
Functions to access/set the knwoledge broker.
Definition: AlpsTreeNode.h:156
void setQuality(double quality)
Query/set the quality of the node.
Definition: AlpsTreeNode.h:219
bool isBranched() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:184
bool isPregnant() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:182
virtual void convertToExplicit()
Convert explicit description to difference, and vise-vesa.
Definition: AlpsTreeNode.h:297
This data structure is to contain the packed form of an encodable knowledge.
Definition: AlpsEncoded.h:25
void removeDescendants()
Removes all the descendants of the node.
int getNumChildren() const
Query/set what the number of children.
Definition: AlpsTreeNode.h:224
void setSolEstimate(double est)
Query/set the solution estimate of the node.
Definition: AlpsTreeNode.h:213
AlpsTreeNode * getChild(const int i) const
Query/set pointer to the ith child.
Definition: AlpsTreeNode.h:247
void setStatus(const AlpsNodeStatus stat)
Query/set the current status.
Definition: AlpsTreeNode.h:173
int getSentMark() const
Various marks used in parallel code.
Definition: AlpsTreeNode.h:303
void modifyNumChildren(const int s)
Query/set what the number of children.
Definition: AlpsTreeNode.h:236
This class holds one node of the search tree.
Definition: AlpsTreeNode.h:50
#define ALPS_OBJ_MAX
Definition: Alps.h:145
virtual AlpsTreeNode * createNewTreeNode(AlpsNodeDesc *&desc) const =0
The purpose of this function is be able to create the children of a node after branching.
int sentMark_
Various mark used in splitting and passing subtrees.
Definition: AlpsTreeNode.h:107
int AlpsNodeIndex_t
Definition: AlpsTreeNode.h:37
The abstract base class of any user-defined class that Alps has to know about in order to encode/deco...
Definition: AlpsKnowledge.h:51
double quality_
The quality of this node.
Definition: AlpsTreeNode.h:72
virtual ~AlpsTreeNode()
Definition: AlpsTreeNode.h:132
void setDesc(AlpsNodeDesc *desc)
Definition: AlpsTreeNode.h:153
AlpsTreeNode * parent_
The parent of the tree node.
Definition: AlpsTreeNode.h:75
AlpsNodeStatus getStatus() const
Query/set the current status.
Definition: AlpsTreeNode.h:172
AlpsTreeNode ** children_
Definition: AlpsTreeNode.h:87
void setIndex(const AlpsNodeIndex_t index)
Query/set node identifier (unique within subtree).
Definition: AlpsTreeNode.h:201
void setParentIndex(AlpsNodeIndex_t index)
Get/set the index of the parent of the node.
Definition: AlpsTreeNode.h:284
bool isActive() const
Query/set node in-process indicator.
Definition: AlpsTreeNode.h:194
int getDepth() const
Query/set what depth the search tree node is at.
Definition: AlpsTreeNode.h:206
int numChildren_
The number of children.
Definition: AlpsTreeNode.h:81
AlpsTreeNode * getParent() const
Get/set subtree.
Definition: AlpsTreeNode.h:277
bool active_
The subtree own this node.
Definition: AlpsTreeNode.h:60
void removeChild(AlpsTreeNode *&child)
Remove the pointer to given child from the list of children.
AlpsNodeIndex_t getIndex() const
Query/set node identifier (unique within subtree).
Definition: AlpsTreeNode.h:200
AlpsNodeStatus status_
The current status of the node.
Definition: AlpsTreeNode.h:98
bool isFathomed() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:186
int depth_
The depth of the node (in the whole tree – the root is at depth 0).
Definition: AlpsTreeNode.h:66
virtual void convertToRelative()
Convert explicit description to difference, and vise-vesa.
Definition: AlpsTreeNode.h:298
void setDepth(const int depth)
Query/set what depth the search tree node is at.
Definition: AlpsTreeNode.h:207
void setSentMark(const int tf)
Various marks used in parallel code.
Definition: AlpsTreeNode.h:304
void setKnowledgeBroker(AlpsKnowledgeBroker *kb)
Definition: AlpsTreeNode.h:158
A class to refer to the description of a search tree node.
Definition: AlpsNodeDesc.h:35
bool isCandidate() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:178
AlpsNodeDesc * desc_
The actual description of the tree node.
Definition: AlpsTreeNode.h:95
void setExplicit(int fp)
Get/set the indication of whether the node has full or differencing description.
Definition: AlpsTreeNode.h:292
void setType(KnowledgeType t)
Definition: AlpsKnowledge.h:72
double getQuality() const
Query/set the quality of the node.
Definition: AlpsTreeNode.h:218