Skip to content

Commit

Permalink
Merge branch 'elitism_103' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Benazera committed Feb 11, 2015
2 parents 72ed434 + 1ab0ba1 commit cb7afac
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
38 changes: 31 additions & 7 deletions cmaparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,35 @@ namespace libcmaes
inline bool get_lazy_update() { return _lazy_update; }

/**
* \brief sets initial elitist scheme: restart if best encountered solution is not
* the final solution and reinjects the best solution until the population
* has better fitness, in its majority
* @param e whether to activate the initial elitist scheme
* \brief sets elitism:
* 0 -> no elitism
* 1 -> elitism: reinjects the best-ever seen solution
* 2 -> initial elitism: reinject x0 as long as it is not improved upon
* 3 -> initial elitism on restart: restart if best encountered solution is not the
* the final solution and reinjects the best solution until the population
* has better fitness, in its majority
*/
inline void set_elitist(const bool &e) { _elitist = e; }

inline void set_elitism(const int &e)
{
if (e == 0)
_elitist = _initial_elitist = _initial_elitist_on_restart;
else if (e == 1)
{
_elitist = true;
_initial_elitist = _initial_elitist_on_restart = false;
}
else if (e == 2)
{
_initial_elitist = true;
_elitist = _initial_elitist_on_restart = false;
}
else if (e == 3)
{
_initial_elitist_on_restart = true;
_elitist = _initial_elitist = false;
}
}

/**
* \brief all stopping criteria are active by default, this allows to control
* them
Expand Down Expand Up @@ -287,7 +309,9 @@ namespace libcmaes
bool _sep = false; /**< whether to use diagonal covariance matrix. */
bool _vd = false;

bool _elitist = false; /**< activate the restart from and re-injection of the best seen solution if not the final one. */
bool _elitist = false; /**< re-inject the best-ever seen solution. */
bool _initial_elitist = false; /**< re-inject x0. */
bool _initial_elitist_on_restart = false; /**< activate the restart from and re-injection of the best seen solution if not the final one. */

// stopping criteria
std::map<int,bool> _stoppingcrit; /**< control list of stopping criteria. */
Expand Down
2 changes: 1 addition & 1 deletion cmasolutions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace libcmaes
_median_fvalues.erase(_median_fvalues.begin());

// store best seen candidate.
if (_niter == 0 || _candidates.at(0).get_fvalue() < _best_seen_candidate.get_fvalue())
if ((_niter == 0 && !_best_seen_candidate.get_x_size()) || _candidates.at(0).get_fvalue() < _best_seen_candidate.get_fvalue())
{
_best_seen_candidate = _candidates.at(0);
_best_seen_iter = _niter;
Expand Down
9 changes: 6 additions & 3 deletions cmastrategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,13 @@ namespace libcmaes
//debug

if (eostrat<TGenoPheno>::_initial_elitist
|| eostrat<TGenoPheno>::_parameters._initial_elitist
|| eostrat<TGenoPheno>::_parameters._elitist
|| eostrat<TGenoPheno>::_parameters._initial_fvalue)
{
eostrat<TGenoPheno>::_solutions._initial_candidate = Candidate(eostrat<TGenoPheno>::_func(eostrat<TGenoPheno>::_parameters._gp.pheno(eostrat<TGenoPheno>::_solutions._xmean).data(),eostrat<TGenoPheno>::_parameters._dim),
eostrat<TGenoPheno>::_solutions._xmean);
eostrat<TGenoPheno>::_solutions._best_seen_candidate = eostrat<TGenoPheno>::_solutions._initial_candidate;
this->update_fevals(1);
}

Expand All @@ -361,14 +364,14 @@ namespace libcmaes
eostrat<TGenoPheno>::edm();

// test on final value wrt. to best candidate value and number of iterations in between.
if (eostrat<TGenoPheno>::_parameters._elitist)
if (eostrat<TGenoPheno>::_parameters._initial_elitist_on_restart)
{
if (eostrat<TGenoPheno>::_parameters._elitist
if (eostrat<TGenoPheno>::_parameters._initial_elitist_on_restart
&& eostrat<TGenoPheno>::_solutions._best_seen_candidate.get_fvalue()
< eostrat<TGenoPheno>::_solutions.best_candidate().get_fvalue()
&& eostrat<TGenoPheno>::_niter - eostrat<TGenoPheno>::_solutions._best_seen_iter >= 3) // elitist
{
LOG_IF(INFO,!eostrat<TGenoPheno>::_parameters._quiet) << "Starting elitist: bfvalue=" << eostrat<TGenoPheno>::_solutions._best_seen_candidate.get_fvalue() << " / biter=" << eostrat<TGenoPheno>::_solutions._best_seen_iter << std::endl;
LOG_IF(INFO,!eostrat<TGenoPheno>::_parameters._quiet) << "Starting elitist on restart: bfvalue=" << eostrat<TGenoPheno>::_solutions._best_seen_candidate.get_fvalue() << " / biter=" << eostrat<TGenoPheno>::_solutions._best_seen_iter << std::endl;
this->set_initial_elitist(true);

// reinit solution and re-optimize.
Expand Down
25 changes: 20 additions & 5 deletions esostrategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,35 @@ namespace libcmaes
_solutions._candidates_uh = nvcandidates;
}

// if initial elitist, reinject initial solution as needed.
if (_initial_elitist)
// if an elitist is active, reinject initial solution as needed.
if (_niter > 0 && (_parameters._elitist || _parameters._initial_elitist || (_initial_elitist && _parameters._initial_elitist_on_restart)))
{
// get reference values.
double ref_fvalue = std::numeric_limits<double>::max();
Candidate ref_candidate;

if (_parameters._initial_elitist_on_restart || _parameters._initial_elitist)
{
ref_fvalue = _solutions._initial_candidate.get_fvalue();
ref_candidate = _solutions._initial_candidate;
}
else if (_parameters._elitist)
{
ref_fvalue = _solutions._best_seen_candidate.get_fvalue();
ref_candidate = _solutions._best_seen_candidate;
}

// reinject intial solution if half or more points have value above that of the initial point candidate.
int count = 0;
for (int r=0;r<candidates.cols();r++)
if (_solutions._candidates.at(r).get_fvalue() < _solutions._initial_candidate.get_fvalue())
if (_solutions._candidates.at(r).get_fvalue() < ref_fvalue)
++count;
if (count/2.0 < candidates.cols()/2)
{
#ifdef HAVE_DEBUG
std::cout << "reinjecting initial solution\n";
std::cout << "reinjecting solution=" << ref_fvalue << std::endl;
#endif
_solutions._candidates.at(1) = _solutions._initial_candidate;
_solutions._candidates.at(1) = ref_candidate;
}
}

Expand Down

0 comments on commit cb7afac

Please sign in to comment.