<commit_before>/**
 *
 *  \file evolution_operation.cc
 *  \remark This file is part of VITA.
 *
 *  Copyright (C) 2011-2013 EOS <NAME>.
 *
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this file,
 *  You can obtain one at http://mozilla.org/MPL/2.0/
 *
 */

#include "evolution_operation.h"
#include "environment.h"
#include "evolution.h"

namespace vita
{
  operation_strategy::operation_strategy(const evolution *const evo,
                                         summary *const stats)
    : evo_(evo), stats_(stats)
  {
    assert(evo);
    assert(stats);
  }

  standard_op::standard_op(const evolution *const evo, summary *const s)
    : operation_strategy(evo, s)
  {
  }

  ///
  /// \return the offspring.
  ///
  /// This is a quite standard crossover + mutation operator.
  ///
  std::vector<individual> standard_op::run(const std::vector<index_t> &parent)
  {
    assert(parent.size() >= 2);

    const population &pop(evo_->population());
    const environment &env(pop.env());

    assert(env.p_cross);
    assert(env.p_mutation);
    assert(env.brood_recombination);

    const index_t r1(parent[0]), r2(parent[1]);

    if (random::boolean(*env.p_cross))
    {
      individual off(individual::crossover(pop[r1], pop[r2]));
      ++stats_->crossovers;

      // This should be an original contribution of Vita... but it's hard
      // to be sure.
      // It remembers of the hereditary repulsion constraint (I guess you could
      // call it signature repulsion) and seems:
      // * to maintain diversity during the exploration phase;
      // * to optimize the exploitation phase.
      while (pop[r1].signature() == off.signature() ||
             pop[r2].signature() == off.signature())
        stats_->mutations += off.mutation();

      if (*env.brood_recombination > 0)
      {
        fitness_t fit_off(evo_->fast_fitness(off));

        unsigned i(0);
        do
        {
          individual tmp(individual::crossover(pop[r1], pop[r2]));
          ++stats_->crossovers;

          while (pop[r1].signature() == tmp.signature() ||
                 pop[r2].signature() == tmp.signature())
            stats_->mutations += tmp.mutation();

          const fitness_t fit_tmp(evo_->fast_fitness(tmp));
          if (fit_tmp > fit_off)
          {
            off     =     tmp;
            fit_off = fit_tmp;
          }
        } while (++i < *env.brood_recombination);
      }

      assert(off.debug());
      return {off};
    }
    else // !crossover
    {
      individual off(pop[random::boolean() ? r1 : r2]);
      stats_->mutations += off.mutation();

      assert(off.debug());
      return {off};
    }
  }
}  // namespace vita
<commit_msg>[cleanup] evolution_operation.cc<commit_after>/**
 *
 *  \file evolution_operation.cc
 *  \remark This file is part of VITA.
 *
 *  Copyright (C) 2011-2013 EOS <NAME>.
 *
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this file,
 *  You can obtain one at http://mozilla.org/MPL/2.0/
 *
 */

#include "evolution_operation.h"
#include "environment.h"
#include "evolution.h"

namespace vita
{
  operation_strategy::operation_strategy(const evolution *const evo,
                                         summary *const stats)
    : evo_(evo), stats_(stats)
  {
    assert(evo);
    assert(stats);
  }

  standard_op::standard_op(const evolution *const evo, summary *const s)
    : operation_strategy(evo, s)
  {
  }

  ///
  /// \return the offspring.
  ///
  /// This is a quite standard crossover + mutation operator.
  ///
  std::vector<individual> standard_op::run(const std::vector<index_t> &parent)
  {
    assert(parent.size() >= 2);

    const population &pop(evo_->population());
    const environment &env(pop.env());

    assert(env.p_cross);
    assert(env.p_mutation);
    assert(env.brood_recombination);

    const index_t r1(parent[0]), r2(parent[1]);

    if (random::boolean(*env.p_cross))
    {
      individual off(individual::crossover(pop[r1], pop[r2]));
      ++stats_->crossovers;

      // This could be an original contribution of Vita... but it's hard
      // to be sure.
      // It remembers of the hereditary repulsion constraint (I guess you could
      // call it signature repulsion) and seems:
      // * to maintain diversity during the exploration phase;
      // * to optimize the exploitation phase.
      while (pop[r1].signature() == off.signature() ||
             pop[r2].signature() == off.signature())
        stats_->mutations += off.mutation();

      if (*env.brood_recombination > 0)
      {
        fitness_t fit_off(evo_->fast_fitness(off));

        unsigned i(0);
        do
        {
          individual tmp(individual::crossover(pop[r1], pop[r2]));

          while (pop[r1].signature() == tmp.signature() ||
                 pop[r2].signature() == tmp.signature())
            stats_->mutations += tmp.mutation();

          const fitness_t fit_tmp(evo_->fast_fitness(tmp));
          if (fit_tmp > fit_off)
          {
            off     =     tmp;
            fit_off = fit_tmp;
          }
        } while (++i < *env.brood_recombination);

        stats_->crossovers += i;
      }

      assert(off.debug());
      return {off};
    }
    else // !crossover
    {
      individual off(pop[random::boolean() ? r1 : r2]);
      stats_->mutations += off.mutation();

      assert(off.debug());
      return {off};
    }
  }
}  // namespace vita

{'fraction_non_alphanumeric': 0.13964773544212797, 'fraction_numerical': 0.010424155283968369, 'mean_word_length': 2.3283492822966507, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '790805', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1693, 'n_words': 424}