Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
search_ranking_bif.hpp
Go to the documentation of this file.
1#ifndef FSTSEARCHRANKINGBIF_H
2#define FSTSEARCHRANKINGBIF_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>
52#include <sstream>
53#include <list>
54#include "error.hpp"
55#include "global.hpp"
56#include "stopwatch.hpp"
57#include "search_ranking.hpp"
58
59/*============== Template parameter type naming conventions ==============
60--------- Numeric types: -------------------------------------------------
61DATATYPE - data sample values - usually real numbers (but may be integers
62 in text processing etc.)
63REALTYPE - must be real numbers - for representing intermediate results of
64 calculations like mean, covariance etc.
65IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
66 extent depends on numbers of samples in data
67DIMTYPE - index values for enumeration of features (dimensions), or classes (not
68 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
69 but be aware of expressions like _classes*_features*_features !
70 in linearized representations of feature matrices for all classes
71BINTYPE - feature selection marker type - represents ca. <10 different feature
72 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
73RETURNTYPE - criterion value: real value, but may be extended in future to support
74 multiple values
75--------- Class types: ---------------------------------------------------
76SUBSET - class of class type Subset
77CLASSIFIER - class implementing interface defined in abstract class Classifier
78EVALUATOR - class implementing interface defined in abstract class Sequential_Step
79DISTANCE - class implementing interface defined in abstract class Distance
80DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
81INTERVALCONTAINER - class of class type Data_Intervaller
82CONTAINER - STL container of class type Data_Interval
83========================================================================== */
84
85namespace FST {
86
99template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
100class Search_Ranking_BIF : public Search_Ranking<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>{
101public:
103 typedef std::shared_ptr<CRITERION> PCriterion;
104 typedef std::shared_ptr<SUBSET> PSubset;
108 explicit Search_Ranking_BIF(PEvaluatorEngine engine=PEvaluatorEngine()):Search_Ranking<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>(engine) {notify("Search_Ranking_BIF constructor.");}
109 virtual ~Search_Ranking_BIF() {notify("Search_Ranking_BIF destructor.");}
110
120 virtual bool evaluate_individuals(const DIMTYPE n, const PCriterion crit, std::ostream& os=std::cout) override;
121
122 virtual std::ostream& print(std::ostream& os) const override {
123 os << "Individual feature ranking [";
124 if(parent::_engine->is_threaded()) os << "Search_Ranking_BIF(threads="<<parent::_engine->get_max_threads()<<")";
125 else os << "Search_Ranking_BIF()";
126 if(parent::result_tracker_active()) os << " with " << *parent::_tracker;
127 os<<"]"; return os;
128 }
129};
130
131template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
132bool Search_Ranking_BIF<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>::evaluate_individuals(const DIMTYPE n, const PCriterion crit, std::ostream& os) // returns found subset + criterion value
133{
134 notify("Search_Ranking_BIF::evaluate_individuals().");
135 assert(crit);
136 assert(n>0);
137 assert(parent::_engine);
138 std::shared_ptr<SUBSET> tempsub(new SUBSET(n)); // tempsub forward_mode assumed
139 tempsub->deselect_all();
140 parent::bifs.clear();
141
142 // sorted insertion + progress output, fired once per feature in feature
143 // order by either engine (the sequential one fires it interleaved with the
144 // evaluations themselves); an equally valued feature is inserted in front of
145 // those already ranked
146 typename parent::EVALUATORENGINE::ResultCallback on_result=[this,&os](const RETURNTYPE val, const PSubset&, const DIMTYPE feat) {
147 typename parent::OneFeature ofbuf;
148 typename parent::FEATURELIST::iterator iter;
149 ofbuf.feature=feat; ofbuf.critval=val;
150 for(iter=this->bifs.begin(); iter!=this->bifs.end() && ofbuf.critval<(*iter).critval; iter++) {}
151 this->bifs.insert(iter,ofbuf);
152 if(parent::output_normal()) this->report(os, msgbuf() << "f:"<<feat<<", critval="<<val<<std::endl<<std::flush);
153 };
154 parent::_engine->initialize(crit, n, on_result);
155 for(DIMTYPE i=0;i<n;i++) { // compute individual values, store in list
156 if(i>0) tempsub->deselect(i-1);
157 tempsub->select(i);
158 if(!parent::_engine->add_candidate(tempsub,i)) return false;
159 }
160 if(!parent::_engine->collect()) return false;
161
162 if(parent::output_normal()) this->report(os, msgbuf() << std::endl<<std::flush);
164 msgbuf sos; sos << "parent::bifs: ";
165 for(typename parent::FEATURELIST::iterator iter=this->bifs.begin(); iter!=this->bifs.end(); iter++) sos << (*iter).feature << " ";
166 sos << std::endl << std::flush;
167 this->report(os,sos);
168 }
169 return true;
170}
171
172
173} // namespace
174#endif // FSTSEARCHRANKINGBIF_H ///:~
Implements Best Individual Features, i.e., individual feature ranking.
Definition search_ranking_bif.hpp:100
Search_Ranking_BIF(PEvaluatorEngine engine=PEvaluatorEngine())
Constructs the search with the given candidate-evaluation engine; a default-constructed engine falls ...
Definition search_ranking_bif.hpp:108
virtual bool evaluate_individuals(const DIMTYPE n, const PCriterion crit, std::ostream &os=std::cout) override
Scores every single feature individually under crit and ranks them into parent::bifs (descending),...
Definition search_ranking_bif.hpp:132
Search_Ranking< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
base class: the individual-ranking search skeleton (final best-dimensionality scan,...
Definition search_ranking_bif.hpp:102
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_ranking_bif.hpp:122
parent::PEvaluatorEngine PEvaluatorEngine
the candidate-evaluation engine type, inherited from parent
Definition search_ranking_bif.hpp:105
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion being optimized
Definition search_ranking_bif.hpp:103
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_ranking_bif.hpp:104
FEATURELIST bifs
the individual-feature ranking; empty until evaluate_individuals() is called
Definition search_ranking.hpp:146
PEvaluatorEngine _engine
Definition search_ranking.hpp:139
Search_Ranking(PEvaluatorEngine engine=PEvaluatorEngine())
Definition search_ranking.hpp:121
std::shared_ptr< EVALUATORENGINE > PEvaluatorEngine
Definition search_ranking.hpp:119
bool output_normal() const
true unless output detail is SILENT
Definition search.hpp:159
void report(std::ostream &os, const msgbuf &m) const
delivers one pre-formatted progress message: to the observer if attached, otherwise as syncout text t...
Definition search.hpp:170
bool result_tracker_active() const
true if a result tracker was assigned via enable_result_tracking() and not since disabled
Definition search.hpp:155
PResultTracker _tracker
the assigned result tracker, or null (see enable_result_tracking()/result_tracker_active())
Definition search.hpp:179
bool output_detailed() const
true only at the DETAILED output level
Definition search.hpp:160
Message accumulator for Search::report() - collects one progress message exactly as an std::ostringst...
Definition search.hpp:87
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 individual feature ranking method implementations.
Simple tool to measure run time.
Structure to hold [feature,criterion value] pair while ranking features.
Definition search_ranking.hpp:141
RETURNTYPE critval
the feature's individual criterion value, as evaluated by evaluate_individuals()
Definition search_ranking.hpp:143
DIMTYPE feature
the feature's index
Definition search_ranking.hpp:142
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.