Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
candidate_evaluator_sequential.hpp
Go to the documentation of this file.
1#ifndef FSTCANDIDATEEVALUATORSEQUENTIAL_H
2#define FSTCANDIDATEEVALUATORSEQUENTIAL_H
3 /*
20=========================================================================
21Copyright and license:
22 * FST4 software (with the exception of any bundled or externally linked
23 third-party components) is Copyright (C) 2000-present Institute of
24 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
25 * FST4 is free software: you can redistribute it and/or modify it under
26 the terms of the GNU Affero General Public License as published by the
27 Free Software Foundation, version 3. The full text of the license is in
28 the file LICENSE in the distribution root and at
29 https://www.gnu.org/licenses/agpl-3.0.html.
30 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
31 WARRANTY; without even the implied warranty of MERCHANTABILITY or
32 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
33 License for more details.
34 * Closed-source use without the AGPL's copyleft obligations requires a
35 commercial license from UTIA; in case of interest write to
36 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
37 options, the contributor license agreement that every contribution
38 requires, and the contacts (technical questions and pull requests:
39 somol@utia.cas.cz, with "FST" in the subject line).
40========================================================================== */
41
42#include <memory>
43#include <cassert>
44#include <iostream>
45#include "error.hpp"
46#include "global.hpp"
48
49namespace FST {
50
61template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
62class Candidate_Evaluator_Sequential : public Candidate_Evaluator<RETURNTYPE,DIMTYPE,SUBSET,CRITERION> {
63public:
65 typedef typename parent::PCriterion PCriterion;
66 typedef typename parent::PSubset PSubset;
68 typedef typename parent::Generator Generator;
72
73 Candidate_Evaluator_Sequential() : _best_available(false), _bestval(RETURNTYPE()), _bestfeature(0), _evaluated(0), _failed(false) {notify("Candidate_Evaluator_Sequential constructor.");}
74 virtual ~Candidate_Evaluator_Sequential() {notify("Candidate_Evaluator_Sequential destructor.");}
75
76 virtual unsigned int get_max_threads() const override {return 1;}
77 virtual bool is_threaded() const override {return false;}
78
79 virtual void initialize(const PCriterion &crit, const DIMTYPE max_candidates, ResultCallback on_result=ResultCallback()) override
80 {
81 assert(crit);
82 _crit=crit;
83 _on_result=on_result;
84 _best_available=false;
85 _evaluated=0;
86 _failed=false;
87 }
88
89 virtual bool add_candidate(const PSubset &sub, const DIMTYPE feature=0) override
90 {
91 assert(_crit);
92 assert(sub);
93 RETURNTYPE val;
94 if(!_crit->evaluate(val,sub)) {_failed=true; return false;}
95 ++_evaluated;
96 if(_on_result) _on_result(val,sub,feature);
97 if(!_best_available || val>_bestval) {
98 if(!_bestsub || _bestsub->get_n()!=sub->get_n()) _bestsub.reset(new SUBSET(sub->get_n()));
99 _bestsub->stateless_copy(*sub);
100 _bestval=val;
101 _bestfeature=feature;
102 _best_available=true;
103 }
104 return true;
105 }
106
108 virtual bool collect() override {return _evaluated>0 && !_failed;}
109
110 virtual bool getBestResult(RETURNTYPE &result, PSubset &sub, DIMTYPE &feature) override
111 {
112 if(!_best_available || _failed) return false; // a failed batch hands out nothing, as in the threaded engine
113 result=_bestval;
114 sub=_bestsub;
115 feature=_bestfeature;
116 return true;
117 }
118
119 virtual bool evaluate_stream(Generator generator, const PCriterion &crit, const PResultTracker tracker, BetterPredicate better, BestCallback on_best, RETURNTYPE &result, const PSubset bestsub) override
120 {
121 assert(generator);
122 assert(crit);
123 assert(better);
124 assert(bestsub);
125 assert(bestsub->get_n()>0);
126 PSubset tmp_sub(new SUBSET(bestsub->get_n()));
127 RETURNTYPE val;
128 bool best_available=false;
129 unsigned long long ordinal=0; // 64-bit to match BestCallback (see the LLP64 note there)
130 while(generator(*tmp_sub)) {
131 if(!crit->evaluate(val,tmp_sub)) return false;
132 if(tracker) tracker->add(val,tmp_sub);
133 if(!best_available || better(val,tmp_sub,result,bestsub)) {
134 result=val;
135 bestsub->stateless_copy(*tmp_sub);
136 best_available=true;
137 if(on_best) on_best(-1,ordinal,val,tmp_sub);
138 }
139 ++ordinal;
140 }
141 return best_available;
142 }
143
144 virtual std::ostream& print(std::ostream& os) const override {os << "Candidate_Evaluator_Sequential()"; return os;}
145
146protected:
151 RETURNTYPE _bestval;
152 DIMTYPE _bestfeature;
153 DIMTYPE _evaluated;
154 bool _failed;
155};
156
157} // namespace
158#endif // FSTCANDIDATEEVALUATORSEQUENTIAL_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.
Definition candidate_evaluator_sequential.hpp:62
virtual void initialize(const PCriterion &crit, const DIMTYPE max_candidates, ResultCallback on_result=ResultCallback()) override
starts a new batch; max_candidates must bound the number of add_candidate() calls to follow
Definition candidate_evaluator_sequential.hpp:79
DIMTYPE _bestfeature
the feature tag passed to add_candidate() for _bestsub
Definition candidate_evaluator_sequential.hpp:152
virtual unsigned int get_max_threads() const override
1 in the sequential implementation
Definition candidate_evaluator_sequential.hpp:76
PCriterion _crit
the criterion supplied to initialize(), evaluated in-line by add_candidate()
Definition candidate_evaluator_sequential.hpp:147
parent::BetterPredicate BetterPredicate
see Candidate_Evaluator::BetterPredicate
Definition candidate_evaluator_sequential.hpp:71
virtual bool is_threaded() const override
true for the threaded engine (any --threads value), false for the sequential one – distinguishes the ...
Definition candidate_evaluator_sequential.hpp:77
virtual bool evaluate_stream(Generator generator, const PCriterion &crit, const PResultTracker tracker, BetterPredicate better, BestCallback on_best, RETURNTYPE &result, const PSubset bestsub) override
Evaluates candidates produced by generator until it reports exhaustion, keeping the best one.
Definition candidate_evaluator_sequential.hpp:119
bool _failed
set when a criterion evaluate() call returned false; makes collect() and getBestResult() report failu...
Definition candidate_evaluator_sequential.hpp:154
parent::Generator Generator
see Candidate_Evaluator::Generator
Definition candidate_evaluator_sequential.hpp:68
DIMTYPE _evaluated
number of candidates evaluated in the current batch (collect() requires at least one)
Definition candidate_evaluator_sequential.hpp:153
parent::PSubset PSubset
shared pointer to a candidate feature subset
Definition candidate_evaluator_sequential.hpp:66
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the engine; base default writes nothing
Definition candidate_evaluator_sequential.hpp:144
virtual bool collect() override
nothing is left pending in this engine (every candidate was evaluated inside add_candidate()),...
Definition candidate_evaluator_sequential.hpp:108
RETURNTYPE _bestval
criterion value of _bestsub
Definition candidate_evaluator_sequential.hpp:151
parent::ResultCallback ResultCallback
see Candidate_Evaluator::ResultCallback
Definition candidate_evaluator_sequential.hpp:69
virtual bool add_candidate(const PSubset &sub, const DIMTYPE feature=0) override
adds one candidate to the current batch; sub is copied, feature is an opaque tag handed back by the c...
Definition candidate_evaluator_sequential.hpp:89
bool _best_available
true once at least one candidate has been evaluated successfully in the current batch
Definition candidate_evaluator_sequential.hpp:150
PSubset _bestsub
the best candidate seen so far in the current batch (only the best is retained, not the whole batch)
Definition candidate_evaluator_sequential.hpp:149
virtual bool getBestResult(RETURNTYPE &result, PSubset &sub, DIMTYPE &feature) override
completes the batch first, then hands out its best candidate (sub is set to the engine's own subset o...
Definition candidate_evaluator_sequential.hpp:110
ResultCallback _on_result
the per-result callback supplied to initialize(), fired synchronously from add_candidate()
Definition candidate_evaluator_sequential.hpp:148
parent::BestCallback BestCallback
see Candidate_Evaluator::BestCallback
Definition candidate_evaluator_sequential.hpp:70
parent::PResultTracker PResultTracker
shared pointer to the result tracker fed during evaluate_stream()
Definition candidate_evaluator_sequential.hpp:67
parent::PCriterion PCriterion
shared pointer to the criterion this engine evaluates candidates against
Definition candidate_evaluator_sequential.hpp:65
Candidate_Evaluator< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
the abstract Candidate_Evaluator interface this class implements
Definition candidate_evaluator_sequential.hpp:64
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
std::shared_ptr< Result_Tracker< RETURNTYPE, SUBSET > > PResultTracker
Definition candidate_evaluator.hpp:90
std::function< bool(SUBSET &)> Generator
fills sub with the next candidate, returns false when no candidate remains (in the threaded implement...
Definition candidate_evaluator.hpp:92
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion this engine evaluates candidates against
Definition candidate_evaluator.hpp:88
std::function< bool(const RETURNTYPE, const PSubset &, const RETURNTYPE, const PSubset &)> BetterPredicate
returns true if (value,subset) is better than (best value,best subset) - encodes per-search tie-break...
Definition candidate_evaluator.hpp:99
std::function< void(const int, const unsigned long long, const RETURNTYPE, const PSubset &)> BestCallback
Definition candidate_evaluator.hpp:97
std::shared_ptr< SUBSET > PSubset
shared pointer to a candidate feature subset
Definition candidate_evaluator.hpp:89
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
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.