Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d43cb2b

Browse files
committed
Added DecisionTree and RandomForest Tuners
1 parent cd8102a commit d43cb2b

6 files changed

+289
-17
lines changed

src/MorpheusOracle_DecisionTree.hpp

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@ class DecisionTree {
9999
load_tree(filename);
100100
}
101101

102+
DecisionTree(const DecisionTree& tree)
103+
: nfeatures_(tree.nfeatures()),
104+
nclasses_(tree.nclasses()),
105+
nodecount_(tree.nodecount()),
106+
maxdepth_(tree.maxdepth()),
107+
classes_(tree.cclasses()),
108+
feature_names_(tree.cfeature_names()),
109+
left_child_(tree.cleft_child()),
110+
right_child_(tree.cright_child()),
111+
threshold_(tree.cthreshold()),
112+
features_(tree.cfeatures()),
113+
values_(tree.cvalues()) {}
114+
115+
DecisionTree& operator=(const DecisionTree& tree) {
116+
nfeatures_ = tree.nfeatures();
117+
nclasses_ = tree.nclasses();
118+
nodecount_ = tree.nodecount();
119+
maxdepth_ = tree.maxdepth();
120+
classes_ = tree.cclasses();
121+
feature_names_ = tree.cfeature_names();
122+
left_child_ = tree.cleft_child();
123+
right_child_ = tree.cright_child();
124+
threshold_ = tree.cthreshold();
125+
features_ = tree.cfeatures();
126+
values_ = tree.cvalues();
127+
return *this;
128+
}
129+
102130
void load_tree(const std::string& filename) {
103131
Morpheus::Oracle::load_tree(filename, *this);
104132
}
@@ -150,15 +178,15 @@ class DecisionTree {
150178
print_matrix(values(), nodecount(), nclasses(), std::cout, offset + 2);
151179
}
152180

153-
size_type nfeatures() { return nfeatures_; }
154-
size_type nclasses() { return nclasses_; }
155-
size_type nodecount() { return nodecount_; }
156-
size_type maxdepth() { return maxdepth_; }
181+
size_type nfeatures() const { return nfeatures_; }
182+
size_type nclasses() const { return nclasses_; }
183+
size_type nodecount() const { return nodecount_; }
184+
size_type maxdepth() const { return maxdepth_; }
157185

158-
void set_nfeatures(size_t nfeatures) { nfeatures_ = nfeatures; }
159-
void set_nclasses(size_t nclasses) { nclasses_ = nclasses; }
160-
void set_nodecount(size_t nodecount) { nodecount_ = nodecount; }
161-
void set_maxdepth(size_t maxdepth) { maxdepth_ = maxdepth; }
186+
void set_nfeatures(const size_t nfeatures) { nfeatures_ = nfeatures; }
187+
void set_nclasses(const size_t nclasses) { nclasses_ = nclasses; }
188+
void set_nodecount(const size_t nodecount) { nodecount_ = nodecount; }
189+
void set_maxdepth(const size_t maxdepth) { maxdepth_ = maxdepth; }
162190

163191
index_type& classes(size_t i) { return classes_(i); }
164192
index_type& left_child(size_t i) { return left_child_(i); }
@@ -168,6 +196,16 @@ class DecisionTree {
168196
value_type& values(size_t i, size_t j) { return values_(i, j); }
169197
string_type& feature_names(size_t i) { return feature_names_[i]; }
170198

199+
const index_type& cclasses(size_t i) const { return classes_(i); }
200+
const index_type& cleft_child(size_t i) const { return left_child_(i); }
201+
const index_type& cright_child(size_t i) const { return right_child_(i); }
202+
const value_type& cthreshold(size_t i) const { return threshold_(i); }
203+
const value_type& cfeatures(size_t i) const { return features_(i); }
204+
const value_type& cvalues(size_t i, size_t j) const { return values_(i, j); }
205+
const string_type& cfeature_names(size_t i) const {
206+
return feature_names_[i];
207+
}
208+
171209
index_vector& classes() { return classes_; }
172210
index_vector& left_child() { return left_child_; }
173211
index_vector& right_child() { return right_child_; }
@@ -176,6 +214,14 @@ class DecisionTree {
176214
scalar_vector2d& values() { return values_; }
177215
string_vector& feature_names() { return feature_names_; }
178216

217+
const index_vector& cclasses() const { return classes_; }
218+
const index_vector& cleft_child() const { return left_child_; }
219+
const index_vector& cright_child() const { return right_child_; }
220+
const scalar_vector& cthreshold() const { return threshold_; }
221+
const scalar_vector& cfeatures() const { return features_; }
222+
const scalar_vector2d& cvalues() const { return values_; }
223+
const string_vector& cfeature_names() const { return feature_names_; }
224+
179225
/*! \cond */
180226
private:
181227
size_type nfeatures_, nclasses_, nodecount_, maxdepth_;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* MorpheusOracle_DecisionTreeTuner.hpp
3+
*
4+
* EPCC, The University of Edinburgh
5+
*
6+
* (c) 2022 The University of Edinburgh
7+
*
8+
* Contributing Authors:
9+
* Christodoulos Stylianou ([email protected])
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*/
23+
24+
#ifndef MORPHEUSORACLE_DECISIONTREETUNER_HPP
25+
#define MORPHEUSORACLE_DECISIONTREETUNER_HPP
26+
27+
#include <MorpheusOracle_DecisionTree.hpp>
28+
29+
#include <Morpheus_Core.hpp>
30+
31+
namespace Morpheus {
32+
namespace Oracle {
33+
34+
/**
35+
* \addtogroup tuners Tuners
36+
*
37+
*/
38+
39+
class DecisionTreeTuner {
40+
public:
41+
/*! Enum value specifying the state of the tuner when the optimum format
42+
* has not been yet selected*/
43+
enum format_state { INVALID_FORMAT_STATE = -1 };
44+
45+
/**
46+
* @brief Construct a new DecisionTreeTuner object
47+
*
48+
* @param tree A DecisionTree to be used by the tuner.
49+
*/
50+
DecisionTreeTuner(const DecisionTree& tree)
51+
: tree_(tree), format_id_(INVALID_FORMAT_STATE) {}
52+
53+
void reload(const std::string& filename) { tree_.load_tree(filename); }
54+
55+
/**
56+
* @brief Resets the state of the tuner to the initial state.
57+
*
58+
*/
59+
void reset() { format_id_ = INVALID_FORMAT_STATE; }
60+
61+
/**
62+
* @brief Provides the index of the optimum format selected by the tuner. Note
63+
* that until the tuner finishes the tuning process, the format ID is set to
64+
* \p INVALID_FORMAT_STATE.
65+
*
66+
* @return size_t Optimum format index.
67+
*/
68+
size_t format_id() const { return format_id_; }
69+
70+
/*! \cond */
71+
private:
72+
DecisionTree tree_;
73+
size_t format_id_;
74+
/*! \endcond */
75+
};
76+
77+
/*! \} // end of tuners group
78+
*/
79+
} // namespace Oracle
80+
} // namespace Morpheus
81+
82+
#endif // MORPHEUSORACLE_DECISIONTREETUNER_HPP

src/MorpheusOracle_RandomForest.hpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ class RandomForest {
9090
load_forest(fmetadata, ftrees);
9191
}
9292

93+
RandomForest(const RandomForest& forest)
94+
: nfeatures_(forest.nfeatures()),
95+
nclasses_(forest.nclasses()),
96+
noutputs_(forest.noutputs()),
97+
classes_(forest.cclasses()),
98+
feature_names_(forest.cfeature_names()),
99+
estimators_(forest.cestimators()) {}
100+
101+
RandomForest& operator=(const RandomForest& forest) {
102+
nfeatures_ = forest.nfeatures();
103+
nclasses_ = forest.nclasses();
104+
noutputs_ = forest.noutputs();
105+
classes_ = forest.cclasses();
106+
feature_names_ = forest.cfeature_names();
107+
estimators_ = forest.cestimators();
108+
return *this;
109+
}
110+
93111
void load_forest(const std::string& fmetadata, const string_vector& ftrees) {
94112
Morpheus::Oracle::load_forest(fmetadata, ftrees, *this);
95113
}
@@ -99,7 +117,6 @@ class RandomForest {
99117
index_vector voters(estimators().size(), 0);
100118
for (size_type i = 0; i < estimators().size(); i++) {
101119
voters[i] = estimators(i).recurse(sample);
102-
std::cout << "Voter " << i << ": " << voters[i] << std::endl;
103120
}
104121

105122
// Majority voting to determine the selected class
@@ -133,22 +150,32 @@ class RandomForest {
133150
}
134151
}
135152

136-
size_type nfeatures() { return nfeatures_; }
137-
size_type nclasses() { return nclasses_; }
138-
size_type noutputs() { return noutputs_; }
153+
size_type nfeatures() const { return nfeatures_; }
154+
size_type nclasses() const { return nclasses_; }
155+
size_type noutputs() const { return noutputs_; }
139156

140-
void set_nfeatures(size_t nfeatures) { nfeatures_ = nfeatures; }
141-
void set_nclasses(size_t nclasses) { nclasses_ = nclasses; }
142-
void set_noutputs(size_t noutputs) { noutputs_ = noutputs; }
157+
void set_nfeatures(const size_t nfeatures) { nfeatures_ = nfeatures; }
158+
void set_nclasses(const size_t nclasses) { nclasses_ = nclasses; }
159+
void set_noutputs(const size_t noutputs) { noutputs_ = noutputs; }
143160

144161
index_type& classes(size_t i) { return classes_[i]; }
145162
tree_type& estimators(size_t i) { return estimators_[i]; }
146163
string_type& feature_names(size_t i) { return feature_names_[i]; }
147164

165+
const index_type& cclasses(size_t i) const { return classes_[i]; }
166+
const tree_type& cestimators(size_t i) const { return estimators_[i]; }
167+
const string_type& cfeature_names(size_t i) const {
168+
return feature_names_[i];
169+
}
170+
148171
index_vector& classes() { return classes_; }
149172
tree_vector& estimators() { return estimators_; }
150173
string_vector& feature_names() { return feature_names_; }
151174

175+
const index_vector& cclasses() const { return classes_; }
176+
const tree_vector& cestimators() const { return estimators_; }
177+
const string_vector& cfeature_names() const { return feature_names_; }
178+
152179
/*! \cond */
153180
private:
154181
size_type nfeatures_, nclasses_, noutputs_;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* MorpheusOracle_RandomForestTuner.hpp
3+
*
4+
* EPCC, The University of Edinburgh
5+
*
6+
* (c) 2022 The University of Edinburgh
7+
*
8+
* Contributing Authors:
9+
* Christodoulos Stylianou ([email protected])
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*/
23+
24+
#ifndef MORPHEUSORACLE_RANDOMFORESTTUNER_HPP
25+
#define MORPHEUSORACLE_RANDOMFORESTTUNER_HPP
26+
27+
#include <MorpheusOracle_RandomForest.hpp>
28+
29+
#include <Morpheus_Core.hpp>
30+
31+
namespace Morpheus {
32+
namespace Oracle {
33+
34+
/**
35+
* \addtogroup tuners Tuners
36+
*
37+
*/
38+
39+
class RandomForestTuner {
40+
public:
41+
/*! Enum value specifying the state of the tuner when the optimum format
42+
* has not been yet selected*/
43+
enum format_state { INVALID_FORMAT_STATE = -1 };
44+
45+
/**
46+
* @brief Construct a new RandomForestTuner object
47+
*
48+
* @param forest A RandomForest to by used by the tuner.
49+
*/
50+
RandomForestTuner(const RandomForest& forest)
51+
: forest_(forest), format_id_(INVALID_FORMAT_STATE) {}
52+
53+
void reload(const std::string& fmetadata,
54+
const std::vector<std::string>& ftrees) {
55+
forest_.load_forest(fmetadata, ftrees);
56+
}
57+
/**
58+
* @brief Resets the state of the tuner to the initial state.
59+
*
60+
*/
61+
void reset() { format_id_ = INVALID_FORMAT_STATE; }
62+
63+
/**
64+
* @brief Provides the index of the optimum format selected by the tuner. Note
65+
* that until the tuner finishes the tuning process, the format ID is set to
66+
* \p INVALID_FORMAT_STATE.
67+
*
68+
* @return size_t Optimum format index.
69+
*/
70+
size_t format_id() const { return format_id_; }
71+
72+
/*! \cond */
73+
private:
74+
RandomForest forest_;
75+
size_t format_id_;
76+
/*! \endcond */
77+
};
78+
79+
/*! \} // end of tuners group
80+
*/
81+
} // namespace Oracle
82+
} // namespace Morpheus
83+
84+
#endif // MORPHEUSORACLE_RANDOMFORESTTUNER_HPP

src/Morpheus_Oracle.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
#include <MorpheusOracle_Macros.hpp>
3030
#include <MorpheusOracle_RunFirstTuner.hpp>
31-
#include <MorpheusOracle_DecisionTree.hpp>
32-
#include <MorpheusOracle_RandomForest.hpp>
31+
#include <MorpheusOracle_DecisionTreeTuner.hpp>
32+
#include <MorpheusOracle_RandomForestTuner.hpp>
3333
#include <MorpheusOracle_TuneMultiply.hpp>
3434
#include <MorpheusOracle_TypeTraits.hpp>
3535
#include <MorpheusOracle_Loader.hpp>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* MorpheusOracle_Fwd_RandomForestTuner.hpp
3+
*
4+
* EPCC, The University of Edinburgh
5+
*
6+
* (c) 2022 The University of Edinburgh
7+
*
8+
* Contributing Authors:
9+
* Christodoulos Stylianou ([email protected])
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*/
23+
24+
#ifndef MORPHEUSORACLE_FWD_RANDOMFORESTTUNER_HPP
25+
#define MORPHEUSORACLE_FWD_RANDOMFORESTTUNER_HPP
26+
27+
namespace Morpheus {
28+
namespace Oracle {
29+
class RandomForestTuner;
30+
} // namespace Oracle
31+
} // namespace Morpheus
32+
33+
#endif // MORPHEUSORACLE_FWD_RANDOMFORESTTUNER_HPP

0 commit comments

Comments
 (0)