Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
search_branch_and_bound_improved.hpp
Go to the documentation of this file.
1#ifndef FSTSEARCHBRANCHANDBOUNDIMPROVED_H
2#define FSTSEARCHBRANCHANDBOUNDIMPROVED_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
53#include "error.hpp"
54#include "global.hpp"
55#include "result_tracker.hpp"
56
57/*============== Template parameter type naming conventions ==============
58--------- Numeric types: -------------------------------------------------
59DATATYPE - data sample values - usually real numbers (but may be integers
60 in text processing etc.)
61REALTYPE - must be real numbers - for representing intermediate results of
62 calculations like mean, covariance etc.
63IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
64 extent depends on numbers of samples in data
65DIMTYPE - index values for enumeration of features (dimensions), or classes (not
66 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
67 but be aware of expressions like _classes*_features*_features !
68 in linearized representations of feature matrices for all classes
69BINTYPE - feature selection marker type - represents ca. <10 different feature
70 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
71RETURNTYPE - criterion value: real value, but may be extended in future to support
72 multiple values
73--------- Class types: ---------------------------------------------------
74SUBSET - class of class type Subset
75CLASSIFIER - class implementing interface defined in abstract class Classifier
76EVALUATOR - class implementing interface defined in abstract class Sequential_Step
77DISTANCE - class implementing interface defined in abstract class Distance
78DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
79INTERVALCONTAINER - class of class type Data_Intervaller
80CONTAINER - STL container of class type Data_Interval
81========================================================================== */
82
83namespace FST {
84
110
111template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
112class Search_Branch_And_Bound_Improved : public Search_Branch_And_Bound<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>{
113public:
116 typedef typename parent::PSubset PSubset;
117 typedef typename parent::PNode PNode;
118 typedef typename parent::Node Node;
119 typedef typename parent::NodeType NodeType;
121 explicit Search_Branch_And_Bound_Improved(PEvaluatorEngine engine=PEvaluatorEngine()):Search_Branch_And_Bound<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>(engine) {notify("Search_Branch_And_Bound_Improved constructor.");}
122 virtual ~Search_Branch_And_Bound_Improved() {notify("Search_Branch_And_Bound_Improved destructor.");}
123
124 // for search() implementation see parent class
125
126 virtual std::ostream& print(std::ostream& os) const override {
127 if(parent::get_engine()->is_threaded()) os << "Improved Branch & Bound (IBB) [Search_Branch_And_Bound_Improved(threads="<<parent::get_engine()->get_max_threads()<<")]";
128 else os << "Improved Branch & Bound (IBB) [Search_Branch_And_Bound_Improved()]";
129 return os;
130 }
131
132protected:
133 // the following may be overriden in descendant Branch and Bound specialization classes
134 virtual void initialize(const DIMTYPE d, const DIMTYPE n, const PCriterion crit) override {}
135 virtual void pre_evaluate_availables() override;
136 virtual void post_process_tree_level() override{}
137 virtual bool cut_possible() override;
138};
139
140template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
142{
143 assert(parent::get_criterion());
144 assert(parent::get_engine());
145 const PSubset &currentset=parent::get_currentset();
146 // pre-evaluate available features before next tree level construction;
147 // computed values are written back into the corresponding _available node,
148 // keyed by the feature tag (Node::feature indexes _available by position)
149 typename parent::EVALUATORENGINE::ResultCallback on_result=[this](const RETURNTYPE value, const PSubset&, const DIMTYPE feat) {
150 PNode avail;
151 if(this->getAvailable(feat,avail)) {
152 avail->value=value;
153 avail->type=parent::COMPUTED;
154 }
155 };
156 parent::get_engine()->initialize(parent::get_criterion(), parent::get_n(), on_result);
157 PNode avail;
158 for(bool got=this->getFirstAvailable(avail);got;got=this->getNextAvailable(avail))
159 {
160 assert(currentset->selected_raw(avail->feature));
161 currentset->deselect_raw(avail->feature);
162 if(!parent::get_engine()->add_candidate(currentset,avail->feature)) throw FST::fst_error("Criterion evaluation failure.");
163 currentset->select_raw(avail->feature);
164 }
165 // fail as loudly as the sequential path's add_candidate throw above: a criterion
166 // exception thrown on a worker thread is rethrown right here on the master, and a
167 // failed evaluation marks the whole batch failed - in which case no on_result
168 // callback fired, every available stays UNKNOWN with a stale value, and the level
169 // sort-in would feed cut_possible() garbage (it expects COMPUTED nodes)
170 if(!parent::get_engine()->collect()) throw FST::fst_error("Criterion evaluation failure.");
171}
172
173template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
175{
177 // the node's value is always COMPUTED here: IBB evaluates every available feature
178 // in pre_evaluate_availables() before the level is built, so - unlike in FBB - no
179 // node can reach this point carrying a merely predicted value
180 Node &currentnode=parent::get_current_node(); assert(currentnode.type==parent::COMPUTED);
181 if(currentnode.value<=parent::get_bound_value()) {
182 return true;
183 }
184 }
185 return false;
186}
187
188} // namespace
189#endif // FSTSEARCHBRANCHANDBOUNDIMPROVED_H ///:~
Implements Improved Branch and Bound (IBB) method, i.e., with fully computed node ordering.
Definition search_branch_and_bound_improved.hpp:112
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the search and its configuration; base default writes nothi...
Definition search_branch_and_bound_improved.hpp:126
virtual bool cut_possible() override
reports whether the current node's (always computed) value fails to exceed the bound,...
Definition search_branch_and_bound_improved.hpp:174
virtual void initialize(const DIMTYPE d, const DIMTYPE n, const PCriterion crit) override
IBB keeps no structures of its own beyond the base class's tree, so there is nothing to set up.
Definition search_branch_and_bound_improved.hpp:134
parent::NodeType NodeType
UNKNOWN/RANDOM/PREDICTED/COMPUTED node-value provenance, see Search_Branch_And_Bound.
Definition search_branch_and_bound_improved.hpp:119
parent::PSubset PSubset
shared pointer to a feature subset
Definition search_branch_and_bound_improved.hpp:116
parent::PCriterion PCriterion
shared pointer to the criterion the search optimizes
Definition search_branch_and_bound_improved.hpp:115
Search_Branch_And_Bound_Improved(PEvaluatorEngine engine=PEvaluatorEngine())
engine forwarded to Search_Branch_And_Bound; defaults to sequential in-line evaluation if not supplie...
Definition search_branch_and_bound_improved.hpp:121
parent::PNode PNode
pointer to a search-tree Node, see Search_Branch_And_Bound
Definition search_branch_and_bound_improved.hpp:117
virtual void pre_evaluate_availables() override
evaluates the criterion for every available feature (one batch through the evaluation engine) so that...
Definition search_branch_and_bound_improved.hpp:141
parent::PEvaluatorEngine PEvaluatorEngine
shared pointer to the injected evaluation-strategy engine
Definition search_branch_and_bound_improved.hpp:120
Search_Branch_And_Bound< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
base class alias
Definition search_branch_and_bound_improved.hpp:114
virtual void post_process_tree_level() override
nothing to do: every node of the new level already carries a COMPUTED value from pre_evaluate_availab...
Definition search_branch_and_bound_improved.hpp:136
parent::Node Node
search-tree node record (feature, value, type, valid), see Search_Branch_And_Bound
Definition search_branch_and_bound_improved.hpp:118
PSubset & get_currentset()
mutable accessor for _currentset, for use by descendant B&B variants' hooks
Definition search_branch_and_bound.hpp:198
Node * PNode
pointer to a Node, as used by the getFirst.../getNext... accessors below
Definition search_branch_and_bound.hpp:142
PCriterion const & get_criterion() const
accessor for _criterion, for use by descendant B&B variants' hooks
Definition search_branch_and_bound.hpp:180
Search_Branch_And_Bound(PEvaluatorEngine engine=PEvaluatorEngine())
Constructs the search, optionally injecting a candidate-evaluation engine.
Definition search_branch_and_bound.hpp:121
DIMTYPE n
no. of all features
Definition search_branch_and_bound.hpp:176
Node & get_current_node()
the node at the current level k presently being visited (_tree[k].back()), i.e. about to be tested fo...
Definition search_branch_and_bound.hpp:200
PEvaluatorEngine const & get_engine() const
accessor for the injected evaluation strategy (sequential-inline or thread-pooled),...
Definition search_branch_and_bound.hpp:181
bool getAvailable(DIMTYPE idx, PNode &avail)
direct lookup of _available[idx] by feature index, regardless of its valid flag; returns false if idx...
Definition search_branch_and_bound.hpp:195
bool getFirstAvailable(PNode &avail)
begins iteration over _available in feature-index order, skipping withdrawn (invalid) entries,...
Definition search_branch_and_bound.hpp:193
DIMTYPE d
no. of features to be selected
Definition search_branch_and_bound.hpp:177
std::shared_ptr< EVALUATORENGINE > PEvaluatorEngine
shared pointer to an EVALUATORENGINE, as accepted by the constructor and returned by get_engine()
Definition search_branch_and_bound.hpp:115
bool is_bound_valid() const
true once a valid bound has been established, see _boundvalid
Definition search_branch_and_bound.hpp:184
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_branch_and_bound.hpp:113
bool getNextAvailable(PNode &avail)
advances the getFirstAvailable() iteration, again skipping invalid entries; returns false once exhaus...
Definition search_branch_and_bound.hpp:194
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion under optimization
Definition search_branch_and_bound.hpp:112
NodeType
Marks how a Node's value was obtained: UNKNOWN = not yet assigned; RANDOM = reserved but currently un...
Definition search_branch_and_bound.hpp:134
DIMTYPE get_n() const
total number of features, see n
Definition search_branch_and_bound.hpp:187
RETURNTYPE get_bound_value() const
the current best (bound) criterion value; meaningful only once is_bound_valid()
Definition search_branch_and_bound.hpp:183
FST4 specific error passing class.
Definition error.hpp:61
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Defines interface for classes that enable collecting multiple results.
Implements Branch and Bound template method as basis for more advanced B&B implementations.
Structure representing search tree node.
Definition search_branch_and_bound.hpp:136
RETURNTYPE value
the node's criterion value: exact once type==COMPUTED, an ordering estimate while type==PREDICTED,...
Definition search_branch_and_bound.hpp:138
DIMTYPE feature
the feature index this node represents removing from its ancestor set (meaningless in the single root...
Definition search_branch_and_bound.hpp:137
NodeType type
how value was obtained, see NodeType
Definition search_branch_and_bound.hpp:139
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.