Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
result_tracker_dupless.hpp
Go to the documentation of this file.
1#ifndef FSTRESULTTRACKERDUPLESS_H
2#define FSTRESULTTRACKERDUPLESS_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"
55#include "result_tracker.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
91template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET>
92class Result_Tracker_Dupless : public Clonable_Impl<Result_Tracker_Dupless<RETURNTYPE,IDXTYPE,DIMTYPE,SUBSET>,Result_Tracker<RETURNTYPE,SUBSET>,CLONE_INHERIT,CLONE_INHERIT,CLONE_DEEP> {
93public:
96 typedef typename parent::ResultRec ResultRec;
97 typedef typename parent::PResultRec PResultRec;
98 typedef std::shared_ptr<SUBSET> PSubset;
100 Result_Tracker_Dupless(const IDXTYPE capacity_limit=0):_margin(0),_restrict_to_d(0),_capacity_limit(capacity_limit) {notify("Result_Tracker_Dupless constructor.");}
101 Result_Tracker_Dupless(const Result_Tracker_Dupless& rtd):parent_impl(rtd), _margin(rtd._margin), _restrict_to_d(rtd._restrict_to_d), _capacity_limit(rtd._capacity_limit) {notify("Result_Tracker_Dupless copy-constructor.");}
102 virtual ~Result_Tracker_Dupless() {notify("Result_Tracker_Dupless destructor.");}
103
123 virtual bool add(const RETURNTYPE value, const PSubset sub) override;
124 virtual void clear() override {results.clear();}
125 virtual std::size_t size() const override {return results.size();}
126 virtual std::size_t size(const RETURNTYPE margin) const;
127 virtual bool get_first(PResultRec &r) override;
128 virtual bool get_next(PResultRec &r) override;
129 virtual void join(Result_Tracker<RETURNTYPE,SUBSET> &src) override;
130
131 void set_inclusion_margin(const RETURNTYPE margin=0) {_margin=margin;}
133
134
135
136 virtual std::ostream& print(std::ostream& os) const override {os << "Result_Tracker_Dupless(limit=" << _capacity_limit << ", margin=" << _margin << ") size " << size(); return os;}
137protected:
138 std::list<ResultRec> results;
139 RETURNTYPE _margin;
141 const IDXTYPE _capacity_limit;
142
143public:
144 typedef typename std::list<ResultRec>::const_iterator CONSTRESULTSITER;
145 typedef typename std::list<ResultRec>::iterator RESULTSITER;
146 RESULTSITER begin() {return results.begin();}
147 RESULTSITER end() {return results.end();}
148
149private:
151};
152
153
154
155template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET>
157{
158 assert(_margin>=0);
159 assert(sub);
160 if(_restrict_to_d>0 && sub->get_d()!=_restrict_to_d) return false;
161 ResultRec rr; rr.value=value; rr.sub.reset(new SUBSET(*sub));
162 bool added=false;
163 if(!results.empty())
164 {
165 RESULTSITER iter=results.begin();
166 if(_margin>0 && iter->value-_margin>value)
167 { // the new result is outside the _margin given by the so-far best result value. Will not be added
168 }
169 else // sort-in the new result, avoiding duplicates + possibly trim the tail of list that falls below _margin, if the new result becomes the new best
170 {
171 if(value>iter->value) {
172 results.push_front(rr); added=true;
173 while(_margin>0 && results.back().value<value-_margin) results.pop_back(); // trim the tail of results that fell below _margin
174 while(_capacity_limit>0 && results.size()>_capacity_limit) results.pop_back(); // if there is a limit, trim the tail so that the list size remains restricted
175 } else { // sort the new result in, trimming not necessary
176 bool dupl=false;
177 while(iter!=results.end() && (value<iter->value || (value==iter->value && sub->get_d()>iter->sub->get_d()))) iter++; // skip all results that rank better: higher value, or equal value with smaller cardinality
178 while(!dupl && iter!=results.end() && (value==iter->value && sub->get_d()==iter->sub->get_d())) if(sub->equivalent(*(iter->sub))) dupl=true; else iter++; // within the block of equal value and equal cardinality: is an equivalent subset already held?
179 if(!dupl) {
180 results.insert(iter,rr); added=true;
181 while(_capacity_limit>0 && results.size()>_capacity_limit) results.pop_back(); // if there is a limit, trim the tail so that the list size remains restricted
182 }
183 }
184 }
185 } else {
186 results.push_back(rr); added=true;
187 }
188 return added;
189}
190
191template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET>
193{
194 assert(margin>=0);
195 if(results.empty()) return 0;
196 CONSTRESULTSITER iter=results.begin();
197 std::size_t count=0;
198 RETURNTYPE threshold=iter->value-margin;
199 while(iter!=results.end() && iter->value>=threshold) {count++; iter++;}
200 return count;
201}
202
203template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET>
205{
206 if(results.empty()) return false;
207 getiter=results.begin();
208 r=&(*getiter);
209 ++getiter;
210 return true;
211}
212
213template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET>
215{
216 if(getiter==results.end()) return false;
217 r=&(*getiter);
218 ++getiter;
219 return true;
220}
221
222template<class RETURNTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET>
224{
225 if(results.empty()) // simple copy
226 {
227 PResultRec pr;
228 for(bool got=src.get_first(pr);got==true;got=src.get_next(pr)) results.push_back(*pr);
229 }
230 else // join
231 {
232 RESULTSITER iter=begin();
233 PResultRec pr;
234 bool got=src.get_first(pr);
235 while(got)
236 {
237 assert(pr->sub);
238 while(iter!=end() && (iter->value>pr->value || (iter->value==pr->value && iter->sub->get_d()<pr->sub->get_d()))) iter++;
239 bool dupl=false;
240 while(!dupl && iter!=end() && (iter->value==pr->value && iter->sub->get_d()==pr->sub->get_d())) if(pr->sub->equivalent(*(iter->sub))) dupl=true; else iter++;
241 if(!dupl) results.insert(iter,*pr);
242 got=src.get_next(pr);
243 }
244 }
245 if(!results.empty()) { // make joined list coherent with _capacity_limit and _margin restrictions
246 RETURNTYPE threshold=results.front().value-_margin;
247 while(_capacity_limit>0 && results.size()>_capacity_limit) results.pop_back();
248 while(_margin>0 && results.back().value<threshold) results.pop_back();
249 }
250}
251
252} // namespace
253#endif // FSTRESULTTRACKERDUPLESS_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
void set_inclusion_margin(const RETURNTYPE margin=0)
restrict adding to only subsets with criterion value within given distance (margin) from best known
Definition result_tracker_dupless.hpp:131
virtual void join(Result_Tracker< RETURNTYPE, SUBSET > &src) override
merges the results held by src into this tracker, keeping the ordering and skipping duplicates exactl...
Definition result_tracker_dupless.hpp:223
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
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition result_tracker_dupless.hpp:136
virtual bool add(const RETURNTYPE value, const PSubset sub) override
Offers one (value, subset) result for inclusion; sub is copied, the caller keeps ownership of its own...
Definition result_tracker_dupless.hpp:156
DIMTYPE _restrict_to_d
set via set_inclusion_cardinality_restriction(); 0 = no subset-size restriction
Definition result_tracker_dupless.hpp:140
Result_Tracker_Dupless(const Result_Tracker_Dupless &rtd)
copies the configuration (margin, cardinality restriction, capacity limit) only; the results list is ...
Definition result_tracker_dupless.hpp:101
const IDXTYPE _capacity_limit
set by the constructor; 0 = unlimited
Definition result_tracker_dupless.hpp:141
Result_Tracker< RETURNTYPE, SUBSET > parent
the abstract Result_Tracker interface this class implements
Definition result_tracker_dupless.hpp:94
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
parent::PResultRec PResultRec
see Result_Tracker::PResultRec
Definition result_tracker_dupless.hpp:97
Result_Tracker_Dupless(const IDXTYPE capacity_limit=0)
Definition result_tracker_dupless.hpp:100
RETURNTYPE _margin
set via set_inclusion_margin(); 0 = no margin restriction
Definition result_tracker_dupless.hpp:139
Clonable_Impl< Result_Tracker_Dupless, parent, CLONE_INHERIT, CLONE_INHERIT, CLONE_DEEP > parent_impl
implements stateless_clone() via the copy-constructor (CLONE_DEEP); clone()/sharing_clone() are inher...
Definition result_tracker_dupless.hpp:95
void set_inclusion_cardinality_restriction(const DIMTYPE d)
for d>0 restricts adding only to subsets of size d, for d=0 subset size restriction is removed
Definition result_tracker_dupless.hpp:132
virtual bool get_first(PResultRec &r) override
starts iteration at the best held result; false if nothing is held. The returned pointer refers into ...
Definition result_tracker_dupless.hpp:204
std::list< ResultRec >::iterator RESULTSITER
mutable iterator over results, as returned by begin()/end()
Definition result_tracker_dupless.hpp:145
RESULTSITER begin()
iterator to the best (first) held result
Definition result_tracker_dupless.hpp:146
virtual bool get_next(PResultRec &r) override
hands out the next result in descending-value order; false when the iteration is exhausted....
Definition result_tracker_dupless.hpp:214
std::shared_ptr< SUBSET > PSubset
Definition result_tracker_dupless.hpp:98
RESULTSITER getiter
iteration cursor for get_first()/get_next() (single shared cursor, not reentrant)
Definition result_tracker_dupless.hpp:150
virtual void clear() override
discards all recorded results
Definition result_tracker_dupless.hpp:124
virtual std::size_t size() const override
number of results currently held (std::size_t rather than 'long': on LLP64 platforms 'long' is 32 bit...
Definition result_tracker_dupless.hpp:125
Abstract class, defines interface for classes that enable collecting multiple results.
Definition result_tracker.hpp:91
ResultRec * PResultRec
raw pointer to a ResultRec, as used by get_first()/get_next() iteration
Definition result_tracker.hpp:103
virtual bool get_next(PResultRec &r)=0
hands out the next held result; false once the iteration is exhausted. A tracker has a single shared ...
virtual bool get_first(PResultRec &r)=0
starts an iteration over the held results, best first; false if the tracker holds nothing....
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 classes that enable collecting multiple results.
Structure to hold [subset,criterion value] temporary solutions in the course of search.
Definition result_tracker.hpp:99
RETURNTYPE value
the criterion value of the recorded subset
Definition result_tracker.hpp:100
PSubset sub
the recorded subset itself
Definition result_tracker.hpp:101
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.