Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
search_seq.hpp
Go to the documentation of this file.
1#ifndef FSTSEARCHSEQ_H
2#define FSTSEARCHSEQ_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 "error.hpp"
54#include "global.hpp"
55#include "search.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
92 UNDEFINED=0,
93 FORWARD=1,
94 BACKWARD=2
95};
96
104template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION, class EVALUATOR>
105class Search_Sequential : public Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>{ // abstract class
106public:
107 typedef Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION> parent;
108 typedef std::shared_ptr<EVALUATOR> PEvaluator;
109 typedef std::shared_ptr<CRITERION> PCriterion;
110 typedef std::shared_ptr<SUBSET> PSubset;
111 Search_Sequential(const PEvaluator evaluator):Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>(), _generalization_level(1), _initial_subset_enabled(false) {assert(evaluator); _evaluator=evaluator; notify("Search_Sequential constructor.");}
112 virtual ~Search_Sequential() {notify("Search_Sequential destructor.");}
113
114 // NOTE: crit is expected to be already initialize()d before use here
115 virtual bool search(const DIMTYPE target_d, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream& os) override =0;
116
128 inline bool Step(const bool forward, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream& os=std::cout);
129
130 void set_generalization_level(const DIMTYPE g) {if(g>=1) _generalization_level=g;}
132
133 // to be used only if the search is intended to be started from a pre-specified initial subset, otherwise the standard FORWARD methods start from empty set and BACKWARD methods start from full set
137
138protected:
142};
143
144
145template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION, class EVALUATOR>
146bool Search_Sequential<RETURNTYPE,DIMTYPE,SUBSET,CRITERION,EVALUATOR>::Step(const bool forward, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream& os) // modifies sub, returns the evaluator's success flag
147{
148 assert(_evaluator);
149 assert(sub);
150 assert(crit);
151
152 if(parent::output_detailed()) this->report(os, msgbuf() << "Search_Sequential::Step(): sub->get_d()="<<sub->get_d()<<" forward="<<forward <<" sub=" << *sub<<std::endl);
153
154 bool success=true;
155 bool _mode_change=(sub->get_forward_mode()!=forward);
156
157 if(_mode_change) sub->set_forward_mode(forward);
158 if(!_evaluator->evaluate_candidates(result, sub, crit, _generalization_level,os)) success=false;
159 if(_mode_change) sub->set_forward_mode(!forward);
160
161 return success;
162}
163
164
165} // namespace
166#endif // FSTSEARCHSEQ_H ///:~
Abstract class, defines interface for sequential search method implementations, provides common imple...
Definition search_seq.hpp:105
Search< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
base class alias
Definition search_seq.hpp:107
std::shared_ptr< EVALUATOR > PEvaluator
shared pointer to the sequential-step evaluator that implements Step()'s candidate generation and sel...
Definition search_seq.hpp:108
void set_generalization_level(const DIMTYPE g)
optional parameter to enlarge the number of considered candidates (g is the number of features added/...
Definition search_seq.hpp:130
Search_Sequential(const PEvaluator evaluator)
evaluator must be non-null
Definition search_seq.hpp:111
bool Step(const bool forward, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os=std::cout)
Performs one sequential step: adds (forward) or removes (!forward) the best g-tuple of features,...
Definition search_seq.hpp:146
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition search_seq.hpp:109
PEvaluator _evaluator
the sequential-step evaluator injected at construction; drives Step()'s candidate generation and best...
Definition search_seq.hpp:139
virtual bool search(const DIMTYPE target_d, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os) override=0
returns the found subset of target_d features + its criterion value; target_d==0 asks the method to p...
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_seq.hpp:110
void enable_initial_subset()
makes search() start from the caller-supplied contents of its sub argument instead of the concrete me...
Definition search_seq.hpp:134
bool initial_subset_enabled() const
true if enable_initial_subset() was called (and not since disabled by disable_initial_subset())
Definition search_seq.hpp:136
DIMTYPE _generalization_level
number of features added/removed together in one Step() call (see set_generalization_level())
Definition search_seq.hpp:140
bool _initial_subset_enabled
by default all concrete search methods ignore the contents of supplied 'PSubset set' and initialize s...
Definition search_seq.hpp:141
void disable_initial_subset()
reverts to the default: incoming sub contents are ignored by search()
Definition search_seq.hpp:135
DIMTYPE get_generalization_level() const
the currently configured generalization level (see set_generalization_level())
Definition search_seq.hpp:131
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 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
SearchDirection
Direction a sequential search proceeds in.
Definition search_seq.hpp:91
Defines interface for search method implementations.
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.