Alps  2.0.2
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  * Aykut Bulut, Lehigh University *
11  * Ted Ralphs, Lehigh University *
12  * *
13  * Conceptual Design: *
14  * *
15  * Yan Xu, Lehigh University *
16  * Ted Ralphs, Lehigh University *
17  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
18  * Matthew Saltzman, Clemson University *
19  * *
20  * *
21  * Copyright (C) 2001-2023, Lehigh University, Yan Xu, Aykut Bulut, and *
22  * Ted Ralphs. *
23  * All Rights Reserved. *
24  *===========================================================================*/
25 
26 
27 #ifndef AlpsTreeNode_h_
28 #define AlpsTreeNode_h_
29 
30 #include <algorithm>
31 #include <utility>
32 #include <vector>
33 
34 #include "CoinError.hpp"
35 #include "CoinSort.hpp"
36 
37 #include "Alps.h"
38 #include "AlpsKnowledge.h"
39 #include "AlpsNodeDesc.h"
40 
41 class AlpsSubTree;
43 
44 //#############################################################################
50 //#############################################################################
51 
52 class AlpsTreeNode : public AlpsKnowledge {
53  protected:
55  //AlpsSubTree* subTree_;
56 
58  bool active_;
59 
62 
64  int depth_;
65 
67  double solEstimate_;
68 
70  double quality_;
71 
74 
77 
80 
81 #if defined(ALPS_MAX_CHILD_NUM) // *FIXME* : Do we want ifdefs?
82 
83  AlpsTreeNode* children_[ALPS_MAX_CHILD_NUM];
84 #else
86 #endif
87 
90  int explicit_;
91 
94 
97 
99  // 0: default; 1: in subtree to be sent: 2: in subtree's node pool
101 
102  public:
104  :
106  active_(false),
107  index_(-1),
108  depth_(-1),
110  quality_(-ALPS_OBJ_MAX), // Smaller than default incumbentValue
111  parent_(0),
112  parentIndex_(-1),
113  numChildren_(0),
114 #if defined(ALPS_MAX_CHILD_NUM) // *FIXME* : Do we want ifdefs?
115  // AlpsTreeNode* children_[ALPS_MAX_CHILD_NUM];
116 #else
117  children_(0),
118 #endif
119  explicit_(0),
120  desc_(0),
122  sentMark_(0)
123  { }
124 
125  virtual ~AlpsTreeNode() {
126  assert(numChildren_ == 0);
127  //std::cout << "---- delete Alps part of node " << index_ << std::endl;
128 #if ! defined(ALPS_MAX_CHILD_NUM)
129  if (children_ != 0) {
130  delete [] children_;
131  children_ = 0;
132  }
133 #endif
134  if (desc_ != 0) {
135  delete desc_;
136  desc_ = 0;
137  }
138  }
139 
140  bool operator<(const AlpsTreeNode& compNode)
141  { return quality_ < compNode.getQuality(); }
142 
145  AlpsNodeDesc* getDesc() const { return desc_; }
146  void setDesc(AlpsNodeDesc* desc) { desc_ = desc; }
147 
150  /* FIXME: I think that we probably want the argument to be a diff'd
151  description, but this is open to debate. Maybe we should
152  overload this method and have a version that creates a diff'd
153  description, as well as one that creates an explicit
154  description. */
155  virtual AlpsTreeNode* createNewTreeNode(AlpsNodeDesc*& desc) const = 0;
156 
160  inline AlpsNodeStatus getStatus() const { return status_; }
162  inline void setStatus(const AlpsNodeStatus stat) { status_ = stat; }
164 
166  inline bool isCandidate() const {
168  return status_ == AlpsNodeStatusCandidate; }
169  inline bool isEvaluated() const {
170  return status_ == AlpsNodeStatusEvaluated; }
171  inline bool isPregnant() const {
172  return status_ == AlpsNodeStatusPregnant; }
173  inline bool isBranched() const {
174  return status_ == AlpsNodeStatusBranched; }
175  inline bool isFathomed() const {
176  return status_ == AlpsNodeStatusFathomed; }
177  inline bool isDiscarded() const {
178  return status_ == AlpsNodeStatusDiscarded; }
180 
182  inline bool isActive() const { return active_; }
184  inline void setActive(const bool yesno) { active_ = yesno; }
186 
188  inline AlpsNodeIndex_t getIndex() const { return index_; }
190  inline void setIndex(const AlpsNodeIndex_t index) { index_ = index; }
192 
194  inline int getDepth() const { return depth_; }
196  inline void setDepth(const int depth) { depth_ = depth; }
198 
200  inline double getSolEstimate() const { return solEstimate_; }
202  inline void setSolEstimate(double est) { solEstimate_ = est; }
204 
206  inline double getQuality() const { return quality_; }
208  inline void setQuality(double quality) { quality_ = quality; }
210 
212  inline int getNumChildren() const { return numChildren_; }
214  inline void setNumChildren(const int numChildren) {
215  numChildren_ = numChildren;
216 #if ! defined(ALPS_MAX_CHILD_NUM)
217  if (children_ != 0) {
218  delete [] children_;
219  children_ = 0;
220  }
222 #endif
223  }
224  // Change by s
225  inline void modifyNumChildren(const int s) { numChildren_ += s; }
227 
228  // *FIXME* : Sanity check. Maybe we should check that the argument is in
229  // the correct range, but how do we do that? This makes the code
230  // inefficient so it should be done with #ifdefs so it can be compiled
231  // out. But in that case, we should move this code to the implementation
232  // file and it won't get inlined anymore.
233 
235  inline AlpsTreeNode* getChild(const int i) const { return children_[i]; }
237 
238  //FIXME: Compiler complains about this second declaration. Not sure how to
239  //declare a const and a non-const version of a function with the same
240  //arguments...
241  // /** Returns a const pointer to the ith child. */
242  // const AlpsTreeNode* getChild(const int i) const { return children_[i]; }
243  inline void setChild(const int i, AlpsTreeNode* node)
244  { children_[i] = node; }
246 
250  void removeChild(AlpsTreeNode*& child);
251 
253  void addChild(AlpsTreeNode*& child);
254 
258  void removeDescendants();
259 
261  //inline AlpsSubTree* getSubTree() const { return subTree_; }
262  //inline void setSubTree(AlpsSubTree* tree) { subTree_ = tree; }
263 
265  inline AlpsTreeNode* getParent() const { return parent_; }
267  inline void setParent(AlpsTreeNode* parent) { parent_ = parent; }
269 
271  inline AlpsNodeIndex_t getParentIndex() const { return parentIndex_; }
273  inline void setParentIndex(AlpsNodeIndex_t index)
274  { parentIndex_ = index; }
276 
279  inline int getExplicit() const { return explicit_; }
281  inline void setExplicit(int fp) { explicit_ = fp; }
283 
285  virtual void convertToExplicit() {}
287  virtual void convertToRelative() {}
289 
291  inline int getSentMark() const { return sentMark_; }
293  inline void setSentMark(const int tf) { sentMark_ = tf; }
295 
325  virtual int process(bool isRoot = false, bool rampUp = false) = 0;
326 
336  virtual std::vector< CoinTriple<AlpsNodeDesc*, AlpsNodeStatus, double> >
337  branch() = 0;
338 
340  using AlpsKnowledge::encode;
342  virtual AlpsReturnStatus encode(AlpsEncoded * encoded) const;
344  virtual AlpsReturnStatus decodeToSelf(AlpsEncoded & encoded);
345 
346 private:
348  AlpsTreeNode(AlpsTreeNode const &);
351 };
352 #endif
AlpsTreeNode::getStatus
AlpsNodeStatus getStatus() const
Definition: AlpsTreeNode.h:160
AlpsTreeNode::removeChild
void removeChild(AlpsTreeNode *&child)
Remove the pointer to given child from the list of children.
AlpsTreeNode::getDesc
AlpsNodeDesc * getDesc() const
Definition: AlpsTreeNode.h:145
AlpsTreeNode::active_
bool active_
The subtree own this node.
Definition: AlpsTreeNode.h:58
AlpsTreeNode::index_
AlpsNodeIndex_t index_
The unique index of the tree node (across the whole search tree).
Definition: AlpsTreeNode.h:61
AlpsKnowledge.h
AlpsTreeNode::setActive
void setActive(const bool yesno)
Definition: AlpsTreeNode.h:184
AlpsTreeNode::modifyDesc
AlpsNodeDesc * modifyDesc()
Access the desc so that can modify it.
Definition: AlpsTreeNode.h:144
AlpsTreeNode::numChildren_
int numChildren_
The number of children.
Definition: AlpsTreeNode.h:79
AlpsTreeNode::setIndex
void setIndex(const AlpsNodeIndex_t index)
Definition: AlpsTreeNode.h:190
AlpsNodeStatusPregnant
@ AlpsNodeStatusPregnant
Definition: Alps.h:201
AlpsTreeNode::isBranched
bool isBranched() const
Definition: AlpsTreeNode.h:173
AlpsReturnStatus
AlpsReturnStatus
Definition: Alps.h:261
AlpsTreeNode::setDesc
void setDesc(AlpsNodeDesc *desc)
Definition: AlpsTreeNode.h:146
AlpsTreeNode::desc_
AlpsNodeDesc * desc_
The actual description of the tree node.
Definition: AlpsTreeNode.h:93
AlpsTreeNode::explicit_
int explicit_
Indicate whether the node description is explicit(1) or relative(0).
Definition: AlpsTreeNode.h:90
AlpsNodeStatusBranched
@ AlpsNodeStatusBranched
Definition: Alps.h:202
AlpsNodeStatus
AlpsNodeStatus
The possible stati for the search nodes.
Definition: Alps.h:198
AlpsTreeNode::modifyNumChildren
void modifyNumChildren(const int s)
Definition: AlpsTreeNode.h:225
AlpsTreeNode::~AlpsTreeNode
virtual ~AlpsTreeNode()
Definition: AlpsTreeNode.h:125
AlpsKnowledge::operator=
AlpsKnowledge & operator=(AlpsKnowledge const &)
Disable copy assignment operator.
AlpsTreeNode::quality_
double quality_
The quality of this node.
Definition: AlpsTreeNode.h:70
AlpsTreeNode::setChild
void setChild(const int i, AlpsTreeNode *node)
Definition: AlpsTreeNode.h:243
AlpsEncoded
Definition: AlpsEncoded.h:64
AlpsTreeNode::isPregnant
bool isPregnant() const
Definition: AlpsTreeNode.h:171
AlpsTreeNode::sentMark_
int sentMark_
Various mark used in splitting and passing subtrees.
Definition: AlpsTreeNode.h:100
AlpsNodeStatusCandidate
@ AlpsNodeStatusCandidate
Definition: Alps.h:199
AlpsNodeStatusEvaluated
@ AlpsNodeStatusEvaluated
Definition: Alps.h:200
AlpsNodeStatusFathomed
@ AlpsNodeStatusFathomed
Definition: Alps.h:203
AlpsTreeNode::isCandidate
bool isCandidate() const
Query functions about specific stati.
Definition: AlpsTreeNode.h:167
AlpsTreeNode::setSolEstimate
void setSolEstimate(double est)
Definition: AlpsTreeNode.h:202
AlpsTreeNode::AlpsTreeNode
AlpsTreeNode()
Definition: AlpsTreeNode.h:103
AlpsTreeNode::setQuality
void setQuality(double quality)
Definition: AlpsTreeNode.h:208
AlpsTreeNode
This class holds one node of the search tree.
Definition: AlpsTreeNode.h:52
AlpsTreeNode::setStatus
void setStatus(const AlpsNodeStatus stat)
get status.
Definition: AlpsTreeNode.h:162
AlpsTreeNode::depth_
int depth_
The depth of the node (in the whole tree – the root is at depth 0).
Definition: AlpsTreeNode.h:64
Alps.h
AlpsTreeNode::getQuality
double getQuality() const
Query/set the quality of the node.
Definition: AlpsTreeNode.h:207
AlpsNodeStatusDiscarded
@ AlpsNodeStatusDiscarded
Definition: Alps.h:204
AlpsTreeNode::status_
AlpsNodeStatus status_
The current status of the node.
Definition: AlpsTreeNode.h:96
AlpsTreeNode::getSentMark
int getSentMark() const
Various marks used in parallel code.
Definition: AlpsTreeNode.h:292
AlpsTreeNode::getIndex
AlpsNodeIndex_t getIndex() const
Query/set node identifier (unique within subtree).
Definition: AlpsTreeNode.h:189
AlpsNodeDesc
This is an abstract base class for subproblem data to be stored in a tree node.
Definition: AlpsNodeDesc.h:45
AlpsTreeNode::getParent
AlpsTreeNode * getParent() const
Get/set subtree.
Definition: AlpsTreeNode.h:266
AlpsTreeNode::getParentIndex
AlpsNodeIndex_t getParentIndex() const
Get/set the index of the parent of the node.
Definition: AlpsTreeNode.h:272
AlpsTreeNode::getChild
AlpsTreeNode * getChild(const int i) const
Query/set pointer to the ith child.
Definition: AlpsTreeNode.h:236
AlpsTreeNode::parentIndex_
AlpsNodeIndex_t parentIndex_
The index of parent of the tree node.
Definition: AlpsTreeNode.h:76
AlpsKnowledge
The abstract base class of Alps knowledges generated during the search.
Definition: AlpsKnowledge.h:63
AlpsTreeNode::children_
AlpsTreeNode ** children_
Definition: AlpsTreeNode.h:85
ALPS_OBJ_MAX
#define ALPS_OBJ_MAX
Definition: Alps.h:288
AlpsKnowledgeTypeNode
@ AlpsKnowledgeTypeNode
Definition: Alps.h:226
AlpsKnowledgeBroker
The base class of knowledge broker class.
Definition: AlpsKnowledgeBroker.h:52
AlpsKnowledge::encode
AlpsEncoded * encode() const
Encode the content of this into an AlpsEncoded object and return a pointer to it.
AlpsTreeNode::getNumChildren
int getNumChildren() const
Query/set what the number of children.
Definition: AlpsTreeNode.h:213
AlpsTreeNode::isDiscarded
bool isDiscarded() const
Definition: AlpsTreeNode.h:177
AlpsTreeNode::getDepth
int getDepth() const
Query/set what depth the search tree node is at.
Definition: AlpsTreeNode.h:195
AlpsNodeDesc.h
AlpsTreeNode::setSentMark
void setSentMark(const int tf)
Definition: AlpsTreeNode.h:293
AlpsTreeNode::getSolEstimate
double getSolEstimate() const
Query/set the solution estimate of the node.
Definition: AlpsTreeNode.h:201
AlpsTreeNode::setParentIndex
void setParentIndex(AlpsNodeIndex_t index)
Definition: AlpsTreeNode.h:273
AlpsTreeNode::setNumChildren
void setNumChildren(const int numChildren)
Definition: AlpsTreeNode.h:214
AlpsTreeNode::setParent
void setParent(AlpsTreeNode *parent)
Definition: AlpsTreeNode.h:267
AlpsTreeNode::setExplicit
void setExplicit(int fp)
Definition: AlpsTreeNode.h:281
AlpsTreeNode::convertToRelative
virtual void convertToRelative()
Definition: AlpsTreeNode.h:287
AlpsTreeNode::isActive
bool isActive() const
Query/set node in-process indicator.
Definition: AlpsTreeNode.h:183
AlpsSubTree
This class contains the data pertaining to a particular subtree in the search tree.
Definition: AlpsSubTree.h:51
AlpsTreeNode::addChild
void addChild(AlpsTreeNode *&child)
Add a child to the list of children for this node.
AlpsTreeNode::solEstimate_
double solEstimate_
The solution estimate.
Definition: AlpsTreeNode.h:67
AlpsTreeNode::isFathomed
bool isFathomed() const
Definition: AlpsTreeNode.h:175
AlpsTreeNode::createNewTreeNode
virtual AlpsTreeNode * createNewTreeNode(AlpsNodeDesc *&desc) const =0
The purpose of this function is be able to create the children of a node after branching.
AlpsTreeNode::convertToExplicit
virtual void convertToExplicit()
Convert explicit description to difference, and vise-vesa.
Definition: AlpsTreeNode.h:286
AlpsTreeNode::operator<
bool operator<(const AlpsTreeNode &compNode)
Definition: AlpsTreeNode.h:140
AlpsTreeNode::parent_
AlpsTreeNode * parent_
The parent of the tree node.
Definition: AlpsTreeNode.h:73
AlpsKnowledge::decodeToSelf
virtual AlpsReturnStatus decodeToSelf(AlpsEncoded &encoded)
Decode the given AlpsEncoded object into this.
AlpsTreeNode::removeDescendants
void removeDescendants()
Removes all the descendants of the node.
AlpsTreeNode::getExplicit
int getExplicit() const
Get/set the indication of whether the node has full or differencing description.
Definition: AlpsTreeNode.h:280
AlpsNodeIndex_t
int AlpsNodeIndex_t
Definition: Alps.h:174
AlpsTreeNode::setDepth
void setDepth(const int depth)
Definition: AlpsTreeNode.h:196
AlpsTreeNode::isEvaluated
bool isEvaluated() const
Definition: AlpsTreeNode.h:169