scen-tree_struct.hpp
Go to the documentation of this file.
1 
14 #ifndef SCENTREE_STRUCT_HPP
15 #define SCENTREE_STRUCT_HPP
16 
17 #include <iostream>
18 #include <vector>
19 #include <cassert>
20 
21 namespace FlopSmiEx {
22 using std::vector;
23 using std::cout;
24 using std::cerr;
25 using std::endl;
26 
29 protected:
30  int nmbScens;
31  vector<int> leaves;
32 
33 public:
34  int nmbStages;
35 
37  ScenTreeStruct(int const nScens, int const nStages = 0)
38  : nmbScens(nScens), leaves(nScens, -1), nmbStages(nStages)
39  {}
41  virtual ~ScenTreeStruct() {}
42 
44 
47  virtual int get_parent_node(int n) const = 0;
48 
50  int get_nmb_stages() const { return nmbStages; }
51 
53  int get_nmb_scens() const { return static_cast<int>(leaves.size()); }
54 
56  virtual double get_scen_prob(int const sc) const = 0;
57 
59  virtual int const * get_scen_nodes(int const sc) = 0;
60 
62  inline int const * get_core_scen() { return get_scen_nodes(0); }
63 
65 
74  virtual int const * get_next_scen(int &scen, int &parentScen,
75  int &branchStage, double &prob) = 0;
76 };
77 
78 
80 class BinTreeStruct : public ScenTreeStruct {
81 protected:
82  int * scenNodeNmb;
83  int nextScen;
84  double scenProb;
85 
88  int set_scen_nodes(int const sc);
89 
90 public:
92 
95  BinTreeStruct(int const T);
96 
98  delete[] scenNodeNmb;
99  }
100 
101  int get_parent_node(int n) const {
102  return (n-1) / 2; // This gives: get_parent_node(0) = 0
103  }
104 
105  double get_scen_prob(int const sc) const { return scenProb; }
106 
107  int const * get_scen_nodes(int const sc) {
108  set_scen_nodes(sc);
109  return scenNodeNmb;
110  }
111 
112  int const * get_next_scen(int &scen, int &parentScen,
113  int &branchStage, double &prob);
114 };
115 
116 
118 class TwoStageTree : public ScenTreeStruct {
119 protected:
120  int nmbScens;
121  vector<int> leaves;
122  vector<double> probs;
123  int scenNodeNmb[2];
124  int nextScen;
125 
128  int set_scen_nodes(int const sc) {
129  scenNodeNmb[1] = 1 + sc; // scenNodeNmb[0] == 0, the root
130  return 1;
131  }
132 
133 public:
134  int nmbStages;
135 
137  TwoStageTree(int const nScens);
138 
141 
143  int get_parent_node(int n) const { return 0; }
144 
146  void set_scen_prob(double * const pr);
147 
149  double get_scen_prob(int const sc) const { return probs[sc]; }
150 
152  int const * get_scen_nodes(int const sc) {
153  set_scen_nodes(sc);
154  return scenNodeNmb;
155  }
156 
158 
167  int const * get_next_scen(int &scen, int &parentScen,
168  int &branchStage, double &prob);
169 };
170 
171 
172 } // namespace FlopSmiEx
173 #endif
int const * get_core_scen()
Get the vector of nodes of a core scenario - just a wrapper.
Class for a 2-stage tree (bush)
int get_parent_node(int n) const
Get the parent of a given node.
vector< int > leaves
list of leaf nodes - they define scenarios
int nextScen
next scenario to be processed by gen_next_scen
int get_nmb_scens() const
Get the number of scenarios = the number of leaves.
int nmbScens
number of scenarios
int const * get_scen_nodes(int const sc)
Get the vector of nodes of a given scenarios.
int nmbStages
number of stages, again counted from zero
double scenProb
probability of each scenario (equiprobable)
int nmbStages
number of stages, again counted from zero
ScenTreeStruct(int const nScens, int const nStages=0)
Constructor.
TwoStageTree(int const nScens)
Constructor.
virtual int const * get_next_scen(int &scen, int &parentScen, int &branchStage, double &prob)=0
Get vector of nodes of a next scenario in the list.
int const * get_next_scen(int &scen, int &parentScen, int &branchStage, double &prob)
Get vector of nodes of a next scenario in the list.
vector< double > probs
scenario probabilities
int nmbScens
number of scenarios
int * scenNodeNmb
vector of nodes of a scenario - for internal use
int const * get_scen_nodes(int const sc)
Get the vector of nodes of a given scenarios.
double get_scen_prob(int const sc) const
Get probability of a given scenarios.
virtual int get_parent_node(int n) const =0
Get the parent of a given node.
int nextScen
next scenario to be processed by gen_next_scen
BinTreeStruct(int const T)
Constructor.
vector< int > leaves
list of leaf nodes - they define scenarios
Class for balanced binary trees.
virtual ~ScenTreeStruct()
Destructor.
void set_scen_prob(double *const pr)
Set scenario probabilities (if non-equiprobable)
virtual double get_scen_prob(int const sc) const =0
Get probability of a given scenarios.
int const * get_next_scen(int &scen, int &parentScen, int &branchStage, double &prob)
Get vector of nodes of a next scenario in the list.
double get_scen_prob(int const sc) const
Get probability of a given scenarios.
virtual int const * get_scen_nodes(int const sc)=0
Get the vector of nodes of a given scenarios.
int set_scen_nodes(int const sc)
Fill scenNodeNmb with nodes of a given scenarios.
int get_parent_node(int n) const
Get the parent of a given node.
int set_scen_nodes(int const sc)
Fill scenNodeNmb with nodes of a given scenarios.
Base class for scenario-trees.
int scenNodeNmb[2]
vector of nodes of a scenario - for internal use
int get_nmb_stages() const
Get the number of stages.