1#ifndef FSTSEARCHMONTECARLO_H
2#define FSTSEARCHMONTECARLO_H
111template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION>
114 typedef Search<RETURNTYPE,DIMTYPE,SUBSET,CRITERION>
parent;
128 virtual bool search(
const DIMTYPE target_d, RETURNTYPE &result,
const PSubset sub,
const PCriterion crit, std::ostream& os=std::cout)
override;
138 virtual std::ostream&
print(std::ostream& os)
const override;
150template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION>
154 os <<
"Random search [Search_Monte_Carlo(max_threads="<<
_engine->get_max_threads();
161 os <<
"Random search [Search_Monte_Carlo(";
173template<
class RETURNTYPE,
typename DIMTYPE,
class SUBSET,
class CRITERION>
180 const DIMTYPE n=sub->get_n();
187 sos <<
"---------------------------------------" << std::endl;
188 sos <<
"Starting " << *
this << std::endl;
189 sos <<
"with Criterion: " << *crit << std::endl;
190 if(target_d==0) sos <<
"Subset size to be optimized." << std::endl;
else sos <<
"Target subset size set to: " << target_d << std::endl;
191 sos << std::endl << std::flush;
196 unsigned long long _trials=0;
197 unsigned long _time=0;
201 if(_d_to_tmp<1 || _d_to_tmp>n) _d_to_tmp=n;
202 if(_d_from_tmp<1) _d_from_tmp=1;
else if(_d_from_tmp>_d_to_tmp) _d_from_tmp=_d_to_tmp;
203 assert(_d_from_tmp>=1 && _d_from_tmp<=_d_to_tmp && _d_to_tmp<=n);
213 if(
_d_prob>0.0)
do {cand.make_random_subset(
_d_prob);}
while(cand.get_d()==0);
214 else cand.make_random_subset(_d_from_tmp,_d_to_tmp);
215 }
else cand.make_random_subset(target_d);
222 return val>bestval || (val==bestval && cand->get_d()<bestsub->get_d());
227 std::ostream *pos=&os;
230 on_best=[
this,psw,pos](
const int ,
const unsigned long long ordinal,
const RETURNTYPE val,
const PSubset &bsub) {
232 sos <<
"new MAXCRIT="<<val<<
", trial=" << ordinal <<
", " << *bsub << std::endl << *psw << std::endl << std::endl << std::flush;
241 sos <<
"......................................." << std::endl;
242 if(
_engine->is_threaded()) sos <<
"Search_Monte_Carlo_Threaded() search finished after "<<_trials<<
" trials. " << swatch << std::endl;
243 else sos <<
"Search_Monte_Carlo() search finished after "<<_trials<<
" trials. " << swatch << std::endl;
244 sos <<
"Search result: "<< std::endl << *sub << std::endl <<
"Criterion value: " << result << std::endl << std::endl << std::flush;
Abstract candidate-evaluation strategy: the seam through which one search algorithm body evaluates ca...
Implements sequential in-line evaluation of a criterion on a set of subset candidates.
Implements sequential in-line evaluation of a criterion on a set of subset candidates.
Definition candidate_evaluator_sequential.hpp:62
Abstract candidate-evaluation strategy (sequential-inline vs. thread-pooled).
Definition candidate_evaluator.hpp:86
std::function< bool(SUBSET &)> Generator
fills sub with the next candidate, returns false when no candidate remains (in the threaded implement...
Definition candidate_evaluator.hpp:92
std::function< bool(const RETURNTYPE, const PSubset &, const RETURNTYPE, const PSubset &)> BetterPredicate
returns true if (value,subset) is better than (best value,best subset) - encodes per-search tie-break...
Definition candidate_evaluator.hpp:99
std::function< void(const int, const unsigned long long, const RETURNTYPE, const PSubset &)> BestCallback
Definition candidate_evaluator.hpp:97
Repeatedly samples random subsets to eventually yield the one with highest criterion value.
Definition search_monte_carlo.hpp:112
unsigned int _time_limit_check_freq
number of trials before next time check
Definition search_monte_carlo.hpp:144
Candidate_Evaluator< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > EVALUATORENGINE
the injected evaluation-strategy interface (sequential in-line or thread-pooled)
Definition search_monte_carlo.hpp:117
std::shared_ptr< CRITERION > PCriterion
shared pointer to the criterion the search optimizes
Definition search_monte_carlo.hpp:115
void reset_cardinality_randomization()
clears both ways of randomizing the subset size (the constructor calls this); with neither of them in...
Definition search_monte_carlo.hpp:136
virtual bool search(const DIMTYPE target_d, RETURNTYPE &result, const PSubset sub, const PCriterion crit, std::ostream &os=std::cout) override
samples random subsets of target_d features (of randomized size if target_d==0: from the configured r...
Definition search_monte_carlo.hpp:174
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset
Definition search_monte_carlo.hpp:116
float _d_prob
active d_prob must be from (0,1]; generates random subset of random size where the mean size over a s...
Definition search_monte_carlo.hpp:147
Search_Monte_Carlo(const unsigned long long trials_limit=1000, const unsigned long time_limit=0, const unsigned int time_limit_check_freq=1, PEvaluatorEngine engine=PEvaluatorEngine())
engine defaults to sequential in-line evaluation if not supplied; see set_stopping_condition() for tr...
Definition search_monte_carlo.hpp:120
void set_cardinality_randomization(const DIMTYPE d_from, const DIMTYPE d_to)
generate subset size first from [d_from,d_to], then randomly choose exactly that number of features
Definition search_monte_carlo.hpp:134
std::shared_ptr< EVALUATORENGINE > PEvaluatorEngine
Definition search_monte_carlo.hpp:118
void set_cardinality_randomization(const float d_prob)
for each feature roll the dice and select it with probability d_prob
Definition search_monte_carlo.hpp:135
DIMTYPE _d_from
lower bound of the random subset size range; see set_cardinality_randomization(const DIMTYPE,...
Definition search_monte_carlo.hpp:145
PEvaluatorEngine _engine
injected evaluation strategy (sequential-inline or thread-pooled); defaulted in the constructor if no...
Definition search_monte_carlo.hpp:141
void set_stopping_condition(const unsigned long long trials_limit=1000, const unsigned long time_limit=0, const unsigned int time_limit_check_freq=100)
set maximum number of random trials or maximum time in seconds or both; trials_limit and time_limit b...
Definition search_monte_carlo.hpp:131
Search< RETURNTYPE, DIMTYPE, SUBSET, CRITERION > parent
base class alias
Definition search_monte_carlo.hpp:114
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_monte_carlo.hpp:151
unsigned long _time_limit
max time allowed to be spent in seconds
Definition search_monte_carlo.hpp:142
DIMTYPE _d_to
upper bound of the random subset size range
Definition search_monte_carlo.hpp:146
unsigned long long _trials_limit
max no of candidates evaluated (64-bit: counts candidates, see the LLP64 note on Candidate_Evaluator:...
Definition search_monte_carlo.hpp:143
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
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
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
double time_elapsed() const
Returns the number of whole seconds elapsed since construction (wall-clock via time(),...
Definition stopwatch.hpp:99
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
Defines interface for classes that enable collecting multiple results.
Defines interface for search method implementations.
Simple tool to measure run time.