Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
seq_step_straight.hpp
Go to the documentation of this file.
1#ifndef FSTSEARCHSEQSTEPSTRAIGHT_H
2#define FSTSEARCHSEQSTEPSTRAIGHT_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 "seq_step.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
102template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
103class Sequential_Step_Straight : public Sequential_Step<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>{
104public:
105 typedef Sequential_Step<RETURNTYPE,DIMTYPE,SUBSET,CRITERION> parent;
106 typedef std::shared_ptr<CRITERION> PCriterion;
107 typedef std::shared_ptr<SUBSET> PSubset;
109 typedef std::shared_ptr<EVALUATORENGINE> PEvaluatorEngine;
111 explicit Sequential_Step_Straight(PEvaluatorEngine engine=PEvaluatorEngine()):Sequential_Step<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>(),_engine(engine) {
113 notify("Sequential_Step_Straight constructor.");
114 }
115 virtual ~Sequential_Step_Straight() {notify("Sequential_Step_Straight destructor.");}
116
117 // NOTE: crit is expected to be already initialize()d before use here
118 inline bool evaluate_candidates(RETURNTYPE &result, const PSubset sub, const PCriterion crit, const DIMTYPE _generalization_level=1, std::ostream& os=std::cout) override;
119
121 virtual std::ostream& print(std::ostream& os) const override {
122 if(_engine->is_threaded()) os << "Sequential_Step_Straight(threads="<<_engine->get_max_threads()<<")";
123 else os << "Sequential_Step_Straight()";
124 if(parent::result_tracker_active()) os << " with " << *parent::_tracker;
125 return os;
126 }
127protected:
129};
130
131template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
132bool Sequential_Step_Straight<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>::evaluate_candidates(RETURNTYPE &result, const PSubset sub, const PCriterion crit, const DIMTYPE _generalization_level, std::ostream& os)
133{
134 assert(sub);
135 assert(crit);
136 assert(_generalization_level>=1 && _generalization_level<=sub->get_n());
137 assert(_engine);
138 // a per-result callback is installed only when a result tracker is attached:
139 // without one the candidates are evaluated exactly as before, they are just
140 // not recorded anywhere
141 typename EVALUATORENGINE::ResultCallback on_result;
144 on_result=[tracker](const RETURNTYPE val, const PSubset &csub, const DIMTYPE) {tracker->add(val,csub);};
145 }
146 // the candidate count is needed only by the threaded engine, to size its result
147 // storage; the sequential engine streams candidates and never needs it.
148 // getNoOfCandidateSubsets() computes C(n,g) incrementally in 64 bits, so
149 // generalized steps (g>=2) are supported threaded as well; it throws only if the
150 // count genuinely exceeds DIMTYPE - i.e., when the step loop itself would be
151 // infeasible anyway
152 const DIMTYPE max_candidates = _engine->is_threaded() ? sub->getNoOfCandidateSubsets(_generalization_level,false/*reverse*/) : 1;
153 _engine->initialize(crit, max_candidates, on_result);
154
155 bool b=sub->getFirstCandidateSubset(_generalization_level,false/*reverse*/);
156 if(!b) return false; // no candidate subsets can be traversed for whatever reason
157 if(parent::output_detailed()) {std::ostringstream sos; sos << "Step...: sub=" << *sub << std::endl << std::endl << std::flush; syncout::print(os,sos);}
158 if(!_engine->add_candidate(sub)) return false;
159
160 b=sub->getNextCandidateSubset(); // evaluate the remaining candidates
161 while(b) {
162 if(parent::output_detailed()) {std::ostringstream sos; sos << "Step...: sub=" << *sub << std::endl << std::endl << std::flush; syncout::print(os,sos);}
163 if(!_engine->add_candidate(sub)) return false;
164 b=sub->getNextCandidateSubset();
165 }
166
167 PSubset psub;
168 DIMTYPE dummyfeature;
169 if(!_engine->getBestResult(result,psub,dummyfeature)) return false;
170 assert(psub);
171 assert(psub->get_n()==sub->get_n());
172 sub->stateless_copy(*psub);
173 return true;
174}
175
176
177} // namespace
178#endif // FSTSEARCHSEQSTEPSTRAIGHT_H ///:~
Abstract candidate-evaluation strategy: the seam through which one search algorithm body evaluates ca...
Implements sequential in-line evaluation of a criterion on a set of subset candidates.
Implements sequential in-line evaluation of a criterion on a set of subset candidates.
Definition candidate_evaluator_sequential.hpp:62
Abstract candidate-evaluation strategy (sequential-inline vs. thread-pooled).
Definition candidate_evaluator.hpp:86
std::function< void(const RETURNTYPE, const PSubset &, const DIMTYPE)> ResultCallback
fired once per evaluated batch candidate: (criterion value, candidate, feature tag as passed to add_c...
Definition candidate_evaluator.hpp:94
Implements standard sequential selection step in sequential search type of methods.
Definition seq_step_straight.hpp:103
Sequential_Step_Straight(PEvaluatorEngine engine=PEvaluatorEngine())
engine defaults to sequential in-line evaluation (Candidate_Evaluator_Sequential) if not supplied; pa...
Definition seq_step_straight.hpp:111
Candidate_Evaluator< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > EVALUATORENGINE
the injected evaluation-strategy interface (sequential in-line or thread-pooled)
Definition seq_step_straight.hpp:108
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition seq_step_straight.hpp:106
PEvaluatorEngine _engine
injected evaluation strategy (sequential-inline or thread-pooled); defaulted in the constructor if no...
Definition seq_step_straight.hpp:128
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition seq_step_straight.hpp:107
bool evaluate_candidates(RETURNTYPE &result, const PSubset sub, const PCriterion crit, const DIMTYPE _generalization_level=1, std::ostream &os=std::cout) override
evaluates every subset offered by sub->get*CandidateSubset() and leaves the highest-valued one in sub...
Definition seq_step_straight.hpp:132
Sequential_Step< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
base class alias
Definition seq_step_straight.hpp:105
virtual std::ostream & print(std::ostream &os) const override
writes "Sequential_Step_Straight(threads=N)" if the injected engine is threaded, else "Sequential_Ste...
Definition seq_step_straight.hpp:121
std::shared_ptr< EVALUATORENGINE > PEvaluatorEngine
Definition seq_step_straight.hpp:109
std::shared_ptr< Result_Tracker< RETURNTYPE, SUBSET > > PResultTracker
shared pointer to the result tracker enabled via enable_result_tracking()
Definition seq_step.hpp:103
PResultTracker _tracker
the assigned result tracker, or null (see enable_result_tracking()/result_tracker_active())
Definition seq_step.hpp:140
bool output_detailed() const
true only at the DETAILED output level
Definition seq_step.hpp:138
bool result_tracker_active() const
true if a result tracker was assigned via enable_result_tracking() and not since disabled
Definition seq_step.hpp:133
static void print(std::ostream &os, std::ostringstream &sos)
Writes sos's buffered content to os, serializing concurrent writers under FST_THREADED via a shared m...
Definition global.hpp:86
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 implementations of one selection step in sequential search type of methods.
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.