Skip to content

Commit

Permalink
support for full elitism, ref CMA-ES#103
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Benazera committed Feb 5, 2015
1 parent 5127295 commit c4dcee3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
36 changes: 30 additions & 6 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 on restart 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_initial_elitist_on_restart(const bool &e) { _initial_elitist_on_restart = 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,6 +309,8 @@ namespace libcmaes
bool _sep = false; /**< whether to use diagonal covariance matrix. */
bool _vd = false;

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
Expand Down
1 change: 1 addition & 0 deletions cmastrategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ namespace libcmaes
//debug

if (eostrat<TGenoPheno>::_initial_elitist
|| eostrat<TGenoPheno>::_parameters._initial_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),
Expand Down
23 changes: 19 additions & 4 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";
#endif
_solutions._candidates.at(1) = _solutions._initial_candidate;
_solutions._candidates.at(1) = ref_candidate;
}
}

Expand Down

0 comments on commit c4dcee3

Please sign in to comment.