1#ifndef FSTSEARCHSEQSFFS_H
2#define FSTSEARCHSEQSFFS_H
98template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
110 virtual bool search(
const DIMTYPE target_d, RETURNTYPE &result,
const PSubset sub,
const PCriterion crit, std::ostream& os=std::cout)
override;
120 virtual std::ostream&
print(std::ostream& os)
const override;
126 std::unique_ptr<SUBSET>
sub;
135template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
139 if(
_direction==FORWARD) os <<
"SFFS";
else os <<
"SBFS";
141 os <<
", delta=" <<
_delta;
142 os <<
" [Search_SFFS()";
149template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
161 assert(sub->get_n()>0);
162 assert(target_d<sub->get_n());
166 sub->set_forward_mode(
true);
168 if(_forward) sub->deselect_all();
else sub->select_all();
170 DIMTYPE _d=sub->get_d();
171 if( (!_forward && _d==0) ||
176 sos <<
"---------------------------------------" << std::endl;
177 sos <<
"Starting " << *
this << std::endl;
178 sos <<
"with Criterion: " << *crit << std::endl;
179 sos <<
"with initial subset: " << *sub << std::endl;
180 if(target_d==0) sos <<
"Subset size to be optimized." << std::endl;
else sos <<
"Target subset size set to: " << target_d << std::endl;
181 sos << std::endl << std::flush;
189 RETURNTYPE maxcritval;
193 sub->set_forward_mode(_forward);
195 if(!crit->evaluate(result,sub))
return false;
197 bsubs[_d-1].sub.reset(
new SUBSET(
_n));
198 bsubs[_d-1].sub->stateless_copy(*sub);
199 bsubs[_d-1].critvalue=result;
212 const DIMTYPE _backward_thr=(target_d>0 &&
_delta>0 &&
_delta<target_d)?target_d-
_delta:1;
216 if(!
parent::Step(_forward, result, sub, crit, os))
return false;
221 if((!havemax) || (result>maxcritval) || (result==maxcritval && _d<_d_max)) {
222 maxcritsub->stateless_copy(*sub); maxcritval=result; _d_max=_d; havemax=
true;
226 if(!
bsubs[_d-1].sub) {
227 bsubs[_d-1].sub.reset(
new SUBSET(
_n));
228 bsubs[_d-1].sub->stateless_copy(*sub);
229 bsubs[_d-1].critvalue=result;
232 if(result >
bsubs[_d-1].critvalue) {
233 bsubs[_d-1].sub->stateless_copy(*sub);
234 bsubs[_d-1].critvalue=result;
248 if(!
parent::Step(!_forward, result, sub, crit, os))
return false;
252 if((!havemax) || (result>maxcritval) || (result==maxcritval && _d<_d_max)) {
253 maxcritsub->stateless_copy(*sub); maxcritval=result; _d_max=_d; havemax=
true;
257 if(!
bsubs[_d-1].sub) {
258 bsubs[_d-1].sub.reset(
new SUBSET(
_n));
259 bsubs[_d-1].sub->stateless_copy(*sub);
260 bsubs[_d-1].critvalue=result;
263 if(result >
bsubs[_d-1].critvalue) {
264 bsubs[_d-1].sub->stateless_copy(*sub);
265 bsubs[_d-1].critvalue=result;
268 }
else backtrack=
false;
275 assert(target_d>=1 && target_d<=
_n);
276 if(!
bsubs[target_d-1].sub)
return false;
277 result=
bsubs[target_d-1].critvalue;
278 sub->stateless_copy(*
bsubs[target_d-1].sub);
280 if(!havemax)
return false;
286 sos <<
"......................................." << std::endl;
287 sos <<
"Search_SFFS() search finished. " << swatch << std::endl;
288 sos <<
"Search result: "<< std::endl << *sub << std::endl <<
"Criterion value: " << result << std::endl << std::endl << std::flush;
294template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
298 assert(d>0 && d<=
_n);
300 result=
bsubs[d-1].critvalue;
301 sub->stateless_copy(*(
bsubs[d-1].sub));
Implements Sequential_Forward_Floating_Search and Sequential_Backward_Floating_Search.
Definition search_seq_sffs.hpp:99
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition search_seq_sffs.hpp:104
DIMTYPE get_delta() const
the currently configured delta bound (see set_delta())
Definition search_seq_sffs.hpp:116
parent::parent grandparent
the shared Search base (output detail, result tracker)
Definition search_seq_sffs.hpp:102
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_seq_sffs.hpp:105
std::unique_ptr< SUBSET > pivotsub
the subset the next forward step continues from: the deepest backtracking result that improved its ca...
Definition search_seq_sffs.hpp:131
std::unique_ptr< SUBSET > maxcritsub
stores the subset with maximum known crit value
Definition search_seq_sffs.hpp:132
DIMTYPE _n
bsubs array size
Definition search_seq_sffs.hpp:130
void set_search_direction(const SearchDirection direction)
FORWARD selects SFFS, BACKWARD selects SBFS (do not change concurrently to a running search()).
Definition search_seq_sffs.hpp:112
void set_delta(const DIMTYPE delta)
optional parameter to restrict search extent: bounds the floating frontier to at most target_d+delta ...
Definition search_seq_sffs.hpp:115
Search_Sequential< RETURNTYPE, DIMTYPE, SUBSET, CRITERION, EVALUATOR > parent
base class alias
Definition search_seq_sffs.hpp:101
SearchDirection _direction
Definition search_seq_sffs.hpp:122
SearchDirection get_search_direction() const
the currently configured direction (see set_search_direction())
Definition search_seq_sffs.hpp:113
Search_SFFS(const PEvaluator evaluator)
evaluator must be non-null; direction defaults to FORWARD (SFFS); call set_search_direction(BACKWARD)...
Definition search_seq_sffs.hpp:106
std::shared_ptr< EVALUATOR > PEvaluator
shared pointer to the sequential-step evaluator
Definition search_seq_sffs.hpp:103
bool get_result(const DIMTYPE d, RETURNTYPE &result, PSubset &sub)
retrieves temporary solution of cardinality d that had been updated in the course of the last search ...
Definition search_seq_sffs.hpp:295
virtual bool search(const DIMTYPE target_d, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os=std::cout) override
returns found subset + criterion value
Definition search_seq_sffs.hpp:150
std::unique_ptr< OneSubset[]> bsubs
per-cardinality best-known result found in the course of the last search() (index d-1); read by get_r...
Definition search_seq_sffs.hpp:129
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the search and its configuration; base default writes nothi...
Definition search_seq_sffs.hpp:136
DIMTYPE _delta
frontier restriction around target_d (0 = unrestricted); see set_delta()
Definition search_seq_sffs.hpp:128
Search< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
base class alias
Definition search_seq.hpp:107
Search_Sequential(const PEvaluator evaluator)
evaluator must be non-null
Definition search_seq.hpp:111
bool Step(const bool forward, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os=std::cout)
Performs one sequential step: adds (forward) or removes (!forward) the best g-tuple of features,...
Definition search_seq.hpp:146
PEvaluator _evaluator
the sequential-step evaluator injected at construction; drives Step()'s candidate generation and best...
Definition search_seq.hpp:139
bool initial_subset_enabled() const
true if enable_initial_subset() was called (and not since disabled by disable_initial_subset())
Definition search_seq.hpp:136
DIMTYPE get_generalization_level() const
the currently configured generalization level (see set_generalization_level())
Definition search_seq.hpp:131
bool output_normal() const
true unless output detail is SILENT
Definition search.hpp:159
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
OutputDetail get_output_detail() const
the currently configured output detail level
Definition search.hpp:158
PResultTracker _tracker
the assigned result tracker, or null (see enable_result_tracking()/result_tracker_active())
Definition search.hpp:179
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
Simple stopwatch to measure run time of search algorithms.
Definition stopwatch.hpp:92
FST4 specific error passing class.
Definition error.hpp:61
Message accumulator for Search::report() - collects one progress message exactly as an std::ostringst...
Definition search.hpp:87
FST4 specific error passing class.
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
SearchDirection
Direction a sequential search proceeds in.
Definition search_seq.hpp:91
Defines interface for sequential search method implementations.
Simple tool to measure run time.
Structure to hold [subset,criterion value] temporary solutions in the course of search.
Definition search_seq_sffs.hpp:124
RETURNTYPE critvalue
the subset's criterion value
Definition search_seq_sffs.hpp:125
std::unique_ptr< SUBSET > sub
the subset itself, or null if no result was recorded at this cardinality
Definition search_seq_sffs.hpp:126