Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
search.hpp
Go to the documentation of this file.
1#ifndef FSTSEARCH_H
2#define FSTSEARCH_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 <iostream>
51#include <sstream>
52#include <string>
53#include <ctime>
54#include "error.hpp"
55#include "global.hpp"
56#include "result_tracker.hpp"
57
58/*============== Template parameter type naming conventions ==============
59--------- Numeric types: -------------------------------------------------
60DATATYPE - data sample values - usually real numbers (but may be integers
61 in text processing etc.)
62REALTYPE - must be real numbers - for representing intermediate results of
63 calculations like mean, covariance etc.
64IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
65 extent depends on numbers of samples in data
66DIMTYPE - index values for enumeration of features (dimensions), or classes (not
67 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
68 but be aware of expressions like _classes*_features*_features !
69 in linearized representations of feature matrices for all classes
70BINTYPE - feature selection marker type - represents ca. <10 different feature
71 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
72RETURNTYPE - criterion value: real value, but may be extended in future to support
73 multiple values
74--------- Class types: ---------------------------------------------------
75SUBSET - class of class type Subset
76CLASSIFIER - class implementing interface defined in abstract class Classifier
77EVALUATOR - class implementing interface defined in abstract class Sequential_Step
78DISTANCE - class implementing interface defined in abstract class Distance
79DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
80INTERVALCONTAINER - class of class type Data_Intervaller
81CONTAINER - STL container of class type Data_Interval
82========================================================================== */
83
84namespace FST {
85
87class msgbuf {
88public:
89 template<class T> msgbuf& operator<<(const T& t) {_s<<t; return *this;}
90 msgbuf& operator<<(std::ostream& (*manip)(std::ostream&)) {_s<<manip; return *this;}
91 std::ostream& stream() {return _s;}
92 std::string str() const {return _s.str();}
93private:
94 std::ostringstream _s;
95};
96
105template<class RETURNTYPE, class SUBSET>
107public:
108 typedef std::shared_ptr<SUBSET> PSubset;
109 virtual ~Search_Observer() {}
110 virtual void on_progress(const std::string &msg) {}
111 virtual void on_solution(const RETURNTYPE value, const SUBSET &sub, const std::string &msg) {}
112};
113
114template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
115class Search;
116
117template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
118std::ostream& operator<<(std::ostream& os, const Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>& sr);
119
121template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
122class Search { // abstract class
124 friend std::ostream& operator<< <>(std::ostream& os, const Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>& sr);
125public:
126 typedef std::shared_ptr<CRITERION> PCriterion;
127 typedef std::shared_ptr<SUBSET> PSubset;
128 typedef std::shared_ptr<Result_Tracker<RETURNTYPE,SUBSET> > PResultTracker;
129 Search(): _detail(NORMAL) {}
130 virtual ~Search() {}
131
151 virtual bool search(const DIMTYPE target_d, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream& os) =0;
152
155 bool result_tracker_active() const {return _tracker.get()!=0;}
156
157 void set_output_detail(OutputDetail detail) {_detail=detail;}
159 bool output_normal() const {return _detail==NORMAL || _detail==DETAILED;}
160 bool output_detailed() const {return _detail==DETAILED;}
161
163 typedef std::shared_ptr<OBSERVER> PObserver;
164 void set_observer(PObserver observer) {_observer=observer;}
165
166 virtual std::ostream& print(std::ostream& os) const {return os;}
167
168protected:
170 void report(std::ostream& os, const msgbuf &m) const {
171 if(_observer) _observer->on_progress(m.str());
172 else {std::ostringstream sos; sos<<m.str(); syncout::print(os,sos);}
173 }
174
175 void report_solution(std::ostream& os, const RETURNTYPE value, const SUBSET &sub, const msgbuf &m) const {
176 if(_observer) _observer->on_solution(value,sub,m.str());
177 else {std::ostringstream sos; sos<<m.str(); syncout::print(os,sos);}
178 }
182};
183
185template<class RETURNTYPE, typename DIMTYPE, class SUBSET, class CRITERION>
186std::ostream& operator<<(std::ostream& os, const Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>& sr) {
187 return sr.print(os);
188}
189
190} // namespace
191#endif // FSTSEARCH_H ///:~
Observer interface for search progress reporting.
Definition search.hpp:106
virtual void on_progress(const std::string &msg)
one pre-formatted progress message
Definition search.hpp:110
virtual void on_solution(const RETURNTYPE value, const SUBSET &sub, const std::string &msg)
a new (sub-)solution has been reached (msg = its pre-formatted text form); copy sub if kept beyond th...
Definition search.hpp:111
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset (not used by the interface itself; kept for observer implementatio...
Definition search.hpp:108
abstract class, defines interface for search method implementations
Definition search.hpp:122
virtual std::ostream & print(std::ostream &os) const
writes a human-readable identification of the search and its configuration; base default writes nothi...
Definition search.hpp:166
virtual bool search(const DIMTYPE target_d, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os)=0
Runs the search; the central entry point of every search method.
bool output_normal() const
true unless output detail is SILENT
Definition search.hpp:159
std::shared_ptr< OBSERVER > PObserver
shared pointer to an OBSERVER, as set via set_observer()
Definition search.hpp:163
PObserver _observer
the attached progress observer, or null (see set_observer(), report(), report_solution())
Definition search.hpp:181
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 result_tracker_active() const
true if a result tracker was assigned via enable_result_tracking() and not since disabled
Definition search.hpp:155
void set_output_detail(OutputDetail detail)
sets the amount of information that is logged to output stream throughout the course of search
Definition search.hpp:157
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search.hpp:127
void set_observer(PObserver observer)
redirects search progress reporting away from the default syncout text output to observer
Definition search.hpp:164
void disable_result_tracking()
detaches any result tracker (result_tracker_active() then returns false)
Definition search.hpp:154
OutputDetail get_output_detail() const
the currently configured output detail level
Definition search.hpp:158
std::shared_ptr< Result_Tracker< RETURNTYPE, SUBSET > > PResultTracker
shared pointer to the result tracker enabled via enable_result_tracking()
Definition search.hpp:128
PResultTracker _tracker
the assigned result tracker, or null (see enable_result_tracking()/result_tracker_active())
Definition search.hpp:179
OutputDetail _detail
the output detail level set via set_output_detail(); read by output_normal()/output_detailed()
Definition search.hpp:180
void enable_result_tracking(PResultTracker tracker)
assigns a result tracker to the search process, i.e., enables logging of all evaluated subsets for fu...
Definition search.hpp:153
Search_Observer< RETURNTYPE, SUBSET > OBSERVER
the observer interface type this search reports through (see Search_Observer)
Definition search.hpp:162
void report_solution(std::ostream &os, const RETURNTYPE value, const SUBSET &sub, const msgbuf &m) const
like report(), for sites announcing a new (sub-)solution; fires on_solution instead
Definition search.hpp:175
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition search.hpp:126
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
msgbuf & operator<<(std::ostream &(*manip)(std::ostream &))
applies a stream manipulator (std::endl, std::flush) to the accumulator
Definition search.hpp:90
std::ostringstream _s
the underlying accumulator every operator<< overload writes into
Definition search.hpp:94
msgbuf & operator<<(const T &t)
appends t via std::ostringstream's own operator<<, like building any ostream message
Definition search.hpp:89
std::ostream & stream()
for legacy print(std::ostream&)-style helpers appending into the message
Definition search.hpp:91
std::string str() const
the accumulated message text, as passed to Search_Observer::on_progress()/on_solution()
Definition search.hpp:92
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
OutputDetail
search algorithms' output extent
Definition global.hpp:67
@ NORMAL
informs about new found solutions and time elapsed etc.
Definition global.hpp:69
@ DETAILED
includes much detail including variable contents etc. (may render output unreadably cluttered)
Definition global.hpp:70
std::ostream & operator<<(std::ostream &os, const Data_Intervaller< CONTAINER, IDXTYPE > &ia)
Streams every contained interval as "si:<startidx>, c:<count>" lines to os.
Definition data_intervaller.hpp:152
Defines interface for classes that enable 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.