1#ifndef FSTSEARCHSEQSFRS_H
2#define FSTSEARCHSEQSFRS_H
103template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
115 virtual bool search(
const DIMTYPE target_d, RETURNTYPE &result,
const PSubset sub,
const PCriterion crit, std::ostream& os=std::cout)
override;
125 virtual std::ostream&
print(std::ostream& os)
const override;
131 std::unique_ptr<SUBSET>
sub;
140template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
144 if(
_direction==FORWARD) os <<
"SFRS";
else os <<
"SBRS";
146 os <<
", delta=" <<
_delta;
147 os <<
" [Search_SFRS()";
154template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
164 assert(sub->get_n()>0);
165 assert(target_d<sub->get_n());
169 sub->set_forward_mode(
true);
171 if(_forward) sub->deselect_all();
else sub->select_all();
173 DIMTYPE _d=sub->get_d();
176 if( (!_forward && _d==0) ||
181 sos <<
"---------------------------------------" << std::endl;
182 sos <<
"Starting " << *
this << std::endl;
183 sos <<
"with Criterion: " << *crit << std::endl;
184 sos <<
"with initial subset: " << *sub << std::endl;
185 if(target_d==0) sos <<
"Subset size to be optimized." << std::endl;
else sos <<
"Target subset size set to: " << target_d << std::endl;
186 sos << std::endl << std::flush;
192 RETURNTYPE maxcritval;
196 sub->set_forward_mode(_forward);
199 if(!crit->evaluate(result,sub))
return false;
201 bsubs[_d-1].sub.reset(
new SUBSET(
_n));
202 bsubs[_d-1].sub->stateless_copy(*sub);
203 bsubs[_d-1].critvalue=result;
217 const DIMTYPE _backward_thr=(target_d>0 &&
_delta>0 &&
_delta<target_d)?target_d-
_delta:1;
221 if(!
parent::Step(_forward, result, sub, crit, os))
return false;
226 if((!havemax) || (result>maxcritval) || (result==maxcritval && _d<_d_max)) {
227 maxcritsub->stateless_copy(*sub); maxcritval=result; _d_max=_d; havemax=
true;
231 if(!
bsubs[_d-1].sub) {
232 bsubs[_d-1].sub.reset(
new SUBSET(
_n));
233 bsubs[_d-1].sub->stateless_copy(*sub);
234 bsubs[_d-1].critvalue=result;
237 if(result >
bsubs[_d-1].critvalue) {
238 bsubs[_d-1].sub->stateless_copy(*sub);
239 bsubs[_d-1].critvalue=result;
251 if(!
parent::Step(!_forward, result, sub, crit, os))
return false;
258 if((!havemax) || (result>maxcritval) || (result==maxcritval && _d<_d_max)) {
260 maxcritsub->stateless_copy(*sub); maxcritval=result; _d_max=_d; havemax=
true;
264 if(!
bsubs[_d-1].sub) {
265 bsubs[_d-1].sub.reset(
new SUBSET(
_n));
266 bsubs[_d-1].sub->stateless_copy(*sub);
267 bsubs[_d-1].critvalue=result;
270 if(result >
bsubs[_d-1].critvalue) {
271 bsubs[_d-1].sub->stateless_copy(*sub);
272 bsubs[_d-1].critvalue=result;
281 assert(target_d>=1 && target_d<=
_n);
282 if(!
bsubs[target_d-1].sub)
return false;
283 result=
bsubs[target_d-1].critvalue;
284 sub->stateless_copy(*
bsubs[target_d-1].sub);
286 if(!havemax)
return false;
292 sos <<
"......................................." << std::endl;
293 sos <<
"Search_SFRS() search finished. " << swatch << std::endl;
294 sos <<
"Search result: "<< std::endl << *sub << std::endl <<
"Criterion value: " << result << std::endl << std::endl << std::flush;
300template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
304 assert(d>0 && d<=
_n);
306 result=
bsubs[d-1].critvalue;
307 sub->stateless_copy(*(
bsubs[d-1].sub));
Implements Sequential_Forward_Retreating_Search and Sequential_Backward_Retreating_Search.
Definition search_seq_sfrs.hpp:104
DIMTYPE get_delta() const
the currently configured delta bound (see set_delta())
Definition search_seq_sfrs.hpp:121
Search_SFRS(const PEvaluator evaluator)
evaluator must be non-null; direction defaults to FORWARD (SFRS); call set_search_direction(BACKWARD)...
Definition search_seq_sfrs.hpp:111
parent::parent grandparent
the shared Search base (output detail, result tracker)
Definition search_seq_sfrs.hpp:107
void set_delta(const DIMTYPE delta)
optional bound on how far the search's OWN direction advances the frontier: SFRS stops adding feature...
Definition search_seq_sfrs.hpp:120
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_sfrs.hpp:301
Search_Sequential< RETURNTYPE, DIMTYPE, SUBSET, CRITERION, EVALUATOR > parent
base class alias
Definition search_seq_sfrs.hpp:106
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition search_seq_sfrs.hpp:109
SearchDirection get_search_direction() const
the currently configured direction (see set_search_direction())
Definition search_seq_sfrs.hpp:118
DIMTYPE _n
bsubs array size
Definition search_seq_sfrs.hpp:135
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_sfrs.hpp:155
std::shared_ptr< EVALUATOR > PEvaluator
shared pointer to the sequential-step evaluator
Definition search_seq_sfrs.hpp:108
DIMTYPE _delta
frontier restriction around target_d (0 = unrestricted); see set_delta()
Definition search_seq_sfrs.hpp:133
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_sfrs.hpp:134
std::unique_ptr< SUBSET > maxcritsub
stores the subset with maximum known crit value
Definition search_seq_sfrs.hpp:137
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_seq_sfrs.hpp:110
std::unique_ptr< SUBSET > pivotsub
the subset the next advance continues from: the last subset that became the overall best during the r...
Definition search_seq_sfrs.hpp:136
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_sfrs.hpp:141
SearchDirection _direction
Definition search_seq_sfrs.hpp:127
void set_search_direction(const SearchDirection direction)
FORWARD selects SFRS, BACKWARD selects SBRS (do not change concurrently to running search()).
Definition search_seq_sfrs.hpp:117
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_sfrs.hpp:129
RETURNTYPE critvalue
the subset's criterion value
Definition search_seq_sfrs.hpp:130
std::unique_ptr< SUBSET > sub
the subset itself, or null if no result was recorded at this cardinality
Definition search_seq_sfrs.hpp:131