1#ifndef FSTSEARCHSEQOS_H
2#define FSTSEARCHSEQOS_H
100template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
114 virtual bool search(
const DIMTYPE target_d, RETURNTYPE &result,
const PSubset sub,
const PCriterion crit, std::ostream& os=std::cout)
override;
119 virtual std::ostream&
print(std::ostream& os)
const override;
166template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
172 os <<
", delta=" <<
_delta;
173 os <<
" [Search_OS()";
180template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
185 assert(target_d>0 && target_d<=sub->get_n());
189 if(target_d!=0 && sub->get_d()!=target_d) {
194 std::vector<DIMTYPE> ranked;
195 if(!bif->getFirstFeature(value, f))
return false;
197 assert(f<=sub->get_n());
199 }
while(bif->getNextFeature(value, f));
200 if(sub->get_d()<target_d) {
201 for(
typename std::vector<DIMTYPE>::const_iterator it=ranked.begin();it!=ranked.end() && sub->get_d()!=target_d;++it) sub->select(*it);
203 for(
typename std::vector<DIMTYPE>::const_reverse_iterator it=ranked.rbegin();it!=ranked.rend() && sub->get_d()!=target_d;++it) sub->deselect(*it);
205 if(sub->get_d()!=target_d)
return false;
211template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
221 assert(sub->get_n()>0);
222 assert(target_d<sub->get_n());
231 sos <<
"---------------------------------------" << std::endl;
232 sos <<
"Starting " << *
this << std::endl;
233 sos <<
"with Criterion: " << *crit << std::endl;
234 sos <<
"with initial subset: " << *sub << std::endl;
235 if(target_d==0) sos <<
"Subset size to be optimized." << std::endl;
else sos <<
"Target subset size set to: " << target_d << std::endl;
236 sos << std::endl << std::flush;
241 DIMTYPE _n=sub->get_n();
251 if(!bif->evaluate_individuals(_n, crit, os))
return false;
253 inisub->stateless_copy(*sub);
254 for(DIMTYPE d=1;d<=_n;d++) {
257 if(d<_n) {
if(!
search_one(value,
tmpsub,crit,os))
return false;}
else {
if(!crit->evaluate(value,
tmpsub))
return false;}
258 if(d==1 || value>result) {
260 sub->stateless_copy(*
tmpsub);
266 if(sub->get_d()!=target_d) {
269 if(!bif->evaluate_individuals(_n,crit,os))
return false;
273 if(!
search_one(result,sub,crit,os))
return false;
277 sos <<
"......................................." << std::endl;
278 sos <<
"Search_OS() search finished. " << swatch << std::endl;
279 sos <<
"Search result: "<< std::endl << *sub << std::endl <<
"Criterion value: " << result << std::endl << std::endl << std::flush;
285template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION,
class EVALUATOR>
288 notify(
"Search_OS::search_one().");
291 assert(sub->get_n()>0);
292 assert(sub->get_d()>0);
295 if( (sub->get_d()==0) ||
298 DIMTYPE _n=sub->get_n();
306 sub->set_forward_mode(
true);
312 if(!crit->evaluate(result,sub))
return false;
317 DIMTYPE _d=sub->get_d();
320 bool pivot_top_avail=
false;
321 bool pivot_bottom_avail=
false;
330 assert(_d_piv==sub->get_d());
331 pivotsub->stateless_copy(*sub); bestval=result;
332 pivot_top_avail=pivot_bottom_avail=
false;
338 assert(_d==sub->get_d());
339 assert(sub->get_d()!=0);
345 _d=sub->get_d(); swinged=
true;
348 assert(_d==sub->get_d());
350 pivot_bottom_avail=
true;
355 pivot_bottom_avail=
false;
358 assert(_d==sub->get_d());
361 assert(_d==sub->get_d());
363 assert(_d==sub->get_d());
368 if(result>bestval) swing=RESTART;
371 swing=UP; swinged=
false;
373 assert(_d==sub->get_d());
376 assert(_d==sub->get_d());
377 if(pivot_top_avail) {assert(
pivot_top->get_d()!=0); sub->stateless_copy(*
pivot_top); _d=sub->get_d();}
379 assert(_d==sub->get_d());
382 _d=sub->get_d(); swinged=
true;
385 assert(_d==sub->get_d());
387 pivot_top_avail=
true;
392 pivot_top_avail=
false;
396 assert(_d==sub->get_d());
399 assert(_d==sub->get_d());
401 assert(_d==sub->get_d());
406 if(result>bestval) swing=RESTART;
409 swing=DOWN; swinged=
false;
412 assert(_d==sub->get_d());
Implements Oscillating_Search.
Definition search_seq_os.hpp:101
std::shared_ptr< EVALUATOR > PEvaluator
shared pointer to the sequential-step evaluator
Definition search_seq_os.hpp:107
std::unique_ptr< SUBSET > pivot_bottom
the subset the last DOWN swing ended on, used the same way to resume a deeper DOWN swing without repe...
Definition search_seq_os.hpp:158
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_os.hpp:212
std::unique_ptr< SUBSET > pivot_top
the subset the last UP swing ended on; when the cycle depth grows, the next UP swing resumes from her...
Definition search_seq_os.hpp:157
std::unique_ptr< SUBSET > inisub
target_d==0 only: an untouched copy of the caller's initial subset, re-used as the starting point for...
Definition search_seq_os.hpp:160
std::unique_ptr< SUBSET > pivotsub
the current pivot: the best-known subset for the ongoing search_one() call, around which DOWN/UP swin...
Definition search_seq_os.hpp:156
SwingPhase
Oscillating-cycle phase for the OS swing state machine driving search_one()'s inline switch.
Definition search_seq_os.hpp:132
Search_Ranking_BIF< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > SearchBIF
Best Individual Feature ranking search, used to grow/shrink an initial subset to the requested cardin...
Definition search_seq_os.hpp:105
void set_delta(const DIMTYPE delta)
oscillating cycle depth
Definition search_seq_os.hpp:116
virtual bool adjust_initial_subset(const DIMTYPE target_d, const PSubset sub, const PSearchBIF bif, std::ostream &os)
Brings sub to exactly target_d features before the oscillation starts: an undersized subset is supple...
Definition search_seq_os.hpp:181
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_os.hpp:167
std::shared_ptr< SUBSET > tmpsub
target_d==0 only: the working subset one cardinality is searched in, before it is compared with the b...
Definition search_seq_os.hpp:161
Search_OS(const PEvaluator evaluator)
evaluator must be non-null; unlike other sequential searches, initial_subset_enabled() is turned ON b...
Definition search_seq_os.hpp:110
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_seq_os.hpp:109
Search_Sequential< RETURNTYPE, DIMTYPE, SUBSET, CRITERION, EVALUATOR > parent
base class alias
Definition search_seq_os.hpp:103
parent::parent grandparent
the shared Search base (output detail, result tracker)
Definition search_seq_os.hpp:104
bool track
whether result tracking is enabled, cached per search() call and read throughout search_one()
Definition search_seq_os.hpp:163
virtual bool search_one(RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os)
standard OS initialized by sub, target dimensionality sub->get_d()
Definition search_seq_os.hpp:286
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition search_seq_os.hpp:108
std::shared_ptr< SearchBIF > PSearchBIF
shared pointer to a SearchBIF instance
Definition search_seq_os.hpp:106
DIMTYPE _delta
current oscillating cycle depth (max swing extent); see set_delta()
Definition search_seq_os.hpp:154
DIMTYPE get_delta() const
the currently configured oscillating cycle depth (see set_delta())
Definition search_seq_os.hpp:117
Implements Best Individual Features, i.e., individual feature ranking.
Definition search_ranking_bif.hpp:100
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
void enable_initial_subset()
makes search() start from the caller-supplied contents of its sub argument instead of the concrete me...
Definition search_seq.hpp:134
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
bool output_detailed() const
true only at the DETAILED output level
Definition search.hpp:160
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
Implements Best Individual Features, i.e., individual feature ranking.
Defines interface for sequential search method implementations.
Simple tool to measure run time.