Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
result_tracker_regularizer.hpp
Go to the documentation of this file.
1#ifndef FSTRESULTTRACKERREGULARIZER_H
2#define FSTRESULTTRACKERREGULARIZER_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 <list>
53#include "error.hpp"
54#include "global.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
103template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
104class Result_Tracker_Regularizer : public Clonable_Impl<Result_Tracker_Regularizer<RETURNTYPE,IDXTYPE,DIMTYPE,SUBSET,CRITERION>,Result_Tracker_Dupless<RETURNTYPE,IDXTYPE,DIMTYPE,SUBSET>,CLONE_INHERIT,CLONE_INHERIT,CLONE_DEEP> {
105public:
108 typedef typename parent::ResultRec ResultRec;
110 typedef std::shared_ptr<SUBSET> PSubset;
111 typedef std::shared_ptr<CRITERION> PCriterion;
112 Result_Tracker_Regularizer(const IDXTYPE capacity_limit=0):parent_impl(capacity_limit),_searching(false) {notify("Result_Tracker_Regularizer constructor.");}
113 Result_Tracker_Regularizer(const Result_Tracker_Regularizer& rtr):parent_impl(rtr),_searching(false) {notify("Result_Tracker_Regularizer copy-constructor.");}
114 virtual ~Result_Tracker_Regularizer() {notify("Result_Tracker_Regularizer destructor.");}
115
116 // NOTE: crit is expected to be already initialize()d before use here
130 virtual bool optimize_within_margin(const RETURNTYPE margin, RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub, const PCriterion crit);
131
139 virtual bool optimize_within_first_equivalence_group(RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub, const PCriterion crit);
148 virtual bool optimize_within_next_equivalence_group(RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub);
149
150
151
152 virtual std::ostream& print(std::ostream& os) const override {os << "Result_Tracker_Regularizer(limit=" << parent::_capacity_limit << ", margin=" << parent::_margin << ") size " << parent::results.size(); return os;}
153
154private:
155 bool optimize_within_equivalence_group(RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub);
157 RETURNTYPE _prevmax;
160};
161
162
163
164template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
165bool Result_Tracker_Regularizer<RETURNTYPE, IDXTYPE, DIMTYPE, SUBSET, CRITERION>::optimize_within_margin(const RETURNTYPE margin, RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub, const PCriterion crit)
166{
167 assert(margin>=0);
168 if(parent::results.empty()) return false;
169 bool stop=false;
170
171 typename parent::CONSTRESULTSITER iter=parent::results.begin();
172 typename parent::CONSTRESULTSITER sel=iter;
173 RETURNTYPE bestval, val;
174
175 // NOTE: crit is expected to be already initialize()d before use here
176 if(!crit->evaluate(bestval,sel->sub)) return false;
177 notify("sel: val1=",sel->value,", val2=",bestval,", getd ",sel->sub->get_d(),".");
178
179 const RETURNTYPE threshold=sel->value-margin;
180 iter++;
181 while(!stop && iter!=parent::results.end())
182 {
183 if(iter->value<threshold) stop=true; else
184 {
185 // NOTE: crit is expected to be already initialize()d before use here
186 if(!crit->evaluate(val,iter->sub)) return false;
187 notify("iter: val1=",iter->value,", val2=",val,", getd ",iter->sub->get_d(),".");
188 if(val>bestval || (val==bestval && sel->sub->get_d()>iter->sub->get_d())) {bestval=val; sel=iter; notify("best updated.");}
189 iter++;
190 }
191 }
192 result1=sel->value; result2=bestval; sub=sel->sub;
193 return true;
194}
195
196template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
198{
199 // precondition: _iter points to a valid record of the tracked list and _crit is set - both public entry points guarantee this before calling in
200 _prevmax=_iter->value;
201 RETURNTYPE val, maxval1, maxval2; PSubset maxsub=_iter->sub;
202 maxval1=_iter->value;
203 if(!_crit->evaluate(maxval2,_iter->sub)) {_searching=false; return false;}
204 _iter++;
205 while(_iter!=parent::results.end() && _iter->value==_prevmax)
206 {
207 if(!_crit->evaluate(val,_iter->sub)) {_searching=false; return false;}
208 if(val>maxval2 || (val==maxval2 && maxsub->get_d()>_iter->sub->get_d())) {maxval2=val; maxsub=_iter->sub;}
209 _iter++;
210 }
211 if(_iter==parent::results.end()) _searching=false; else _searching=true;
212 result1=maxval1; result2=maxval2; sub=maxsub;
213 return true;
214}
215
216template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
218{ // returns the subset from the first equivalence group (=subsets with equal primary criterion value) yielding the highest crit value
219 assert(crit);
220 assert(sub);
221 if(parent::results.empty()) {_searching=false; return false;}
222 _crit=crit;
223 _iter=parent::results.begin();
224 return optimize_within_equivalence_group(result1,result2,sub);
225}
226
227template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
229{ // returns the subset from the next equivalence group (=subsets with equal primary criterion value which is the next highest in sequence) yielding the highest crit value
230 assert(_crit);
231 assert(sub);
232 if(!_searching) return false;
233 return optimize_within_equivalence_group(result1,result2,sub);
234}
235
236} // namespace
237#endif // FSTRESULTTRACKERREGULARIZER_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Collects multiple results, avoiding duplicates.
Definition result_tracker_dupless.hpp:92
RESULTSITER end()
iterator past the last held result
Definition result_tracker_dupless.hpp:147
parent::ResultRec ResultRec
see Result_Tracker::ResultRec
Definition result_tracker_dupless.hpp:96
std::list< ResultRec > results
held results, sorted descending by value (ties broken toward the smaller subset); see add() for the e...
Definition result_tracker_dupless.hpp:138
const IDXTYPE _capacity_limit
set by the constructor; 0 = unlimited
Definition result_tracker_dupless.hpp:141
std::list< ResultRec >::const_iterator CONSTRESULTSITER
read-only iterator over results, for use by subclasses/friends inspecting the collected results
Definition result_tracker_dupless.hpp:144
RETURNTYPE _margin
set via set_inclusion_margin(); 0 = no margin restriction
Definition result_tracker_dupless.hpp:139
Collects multiple results. Enables eventual selection of alternative solution based on secondary crit...
Definition result_tracker_regularizer.hpp:104
bool _searching
true while an equivalence-group scan is in progress, i.e. optimize_within_next_equivalence_group() ma...
Definition result_tracker_regularizer.hpp:159
virtual bool optimize_within_next_equivalence_group(RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub)
Continues the scan started by optimize_within_first_equivalence_group(): returns the best subset of t...
Definition result_tracker_regularizer.hpp:228
virtual bool optimize_within_margin(const RETURNTYPE margin, RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub, const PCriterion crit)
Re-ranks the tracked results within margin of the best tracked (primary) value by a secondary crit,...
Definition result_tracker_regularizer.hpp:165
std::shared_ptr< CRITERION > PCriterion
shared pointer to a criterion (the secondary criterion used to re-rank tracked results)
Definition result_tracker_regularizer.hpp:111
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition result_tracker_regularizer.hpp:110
RETURNTYPE _prevmax
the primary value of the equivalence group currently being scanned; helper of optimize_within_equival...
Definition result_tracker_regularizer.hpp:157
Result_Tracker_Dupless< RETURNTYPE, IDXTYPE, DIMTYPE, SUBSET > parent
base class alias
Definition result_tracker_regularizer.hpp:106
Result_Tracker_Regularizer(const Result_Tracker_Regularizer &rtr)
the copy starts with a fresh (non-searching) equivalence-group cursor; _crit/_prevmax/_iter are not c...
Definition result_tracker_regularizer.hpp:113
Clonable_Impl< Result_Tracker_Regularizer, parent, CLONE_INHERIT, CLONE_INHERIT, CLONE_DEEP > parent_impl
implements stateless_clone() via Clonable_Impl; clone()/sharing_clone() defer to Result_Tracker's thr...
Definition result_tracker_regularizer.hpp:107
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition result_tracker_regularizer.hpp:152
parent::CONSTRESULTSITER _iter
cursor into the tracked results list, carried across optimize_within_*_equivalence_group() calls so s...
Definition result_tracker_regularizer.hpp:158
ResultRec * PResultRec
raw pointer to a ResultRec
Definition result_tracker_regularizer.hpp:109
bool optimize_within_equivalence_group(RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub)
building block for optimize_within_*_equivalence_group
Definition result_tracker_regularizer.hpp:197
virtual bool optimize_within_first_equivalence_group(RETURNTYPE &result1, RETURNTYPE &result2, PSubset &sub, const PCriterion crit)
Starts a group-by-group scan: returns the subset of the first equivalence group (the tracked subsets ...
Definition result_tracker_regularizer.hpp:217
PCriterion _crit
the secondary criterion set by optimize_within_first_equivalence_group(), reused by subsequent optimi...
Definition result_tracker_regularizer.hpp:156
Result_Tracker_Regularizer(const IDXTYPE capacity_limit=0)
capacity_limit forwarded to Result_Tracker_Dupless (0 = unlimited)
Definition result_tracker_regularizer.hpp:112
parent::ResultRec ResultRec
one tracked [value,subset] result record, as defined by Result_Tracker
Definition result_tracker_regularizer.hpp:108
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
Enables collecting multiple results.
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.