<commit_before>/**
 *  \file
 *  \remark This file is part of VITA.
 *
 *  \copyright Copyright (C) 2014 EOS di <NAME>.
 *
 *  \license
 *  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 <cstdlib>
#include <sstream>

#include "kernel/ga/i_ga.h"
#include "kernel/ga/interpreter.h"
#include "kernel/ga/ga_evaluator.h"
#include "kernel/ga/ga_search.h"
#include "kernel/evolution.h"
#include "kernel/problem.h"

#if !defined(MASTER_TEST_SET)
#define BOOST_TEST_MODULE t_ga
#include <boost/test/unit_test.hpp>

using namespace boost;

#include "factory_fixture5.h"
#endif

BOOST_FIXTURE_TEST_SUITE(t_ga, F_FACTORY5)

BOOST_AUTO_TEST_CASE(Penalty)
{
  
}

BOOST_AUTO_TEST_CASE(Evaluator)
{
  auto f = [](const std::vector<double> &v)
           { return std::accumulate(v.begin(), v.end(), 0.0); };

  auto eva(vita::make_evaluator<vita::i_ga>(f));

  vita::fitness_t eva_prev;
  double v_prev;

  for (unsigned i(0); i < 1000; ++i)
  {
    vita::i_ga ind(env, sset);
    BOOST_REQUIRE(ind.debug());

    const auto eva_ret(eva(ind));
    BOOST_REQUIRE_LE(eva_ret, 0.0);

    const auto v(f(ind));

    if (i)
    {
      if (v_prev < v)
        BOOST_REQUIRE_LT(eva_prev, eva_ret);
      else
        BOOST_REQUIRE_GE(eva_prev, eva_ret);
    }

    v_prev = v;
    eva_prev = eva_ret;
  }
}

BOOST_AUTO_TEST_CASE(Evolution)
{
  env.individuals = 100;
  env.verbosity = 0;

  auto eva(vita::make_evaluator<vita::i_ga>(
    [](const std::vector<double> &v)
    { return std::accumulate(v.begin(), v.end(), 0.0); }));

  vita::evolution<vita::i_ga, vita::alps_es> evo1(env, sset, eva);
  BOOST_REQUIRE(evo1.debug(true));

  const auto s1(evo1.run(1));

  BOOST_CHECK_GT(s1.best->ind[0], 8.0);
  BOOST_CHECK_GT(s1.best->ind[1], 95.0);
  BOOST_CHECK_GT(s1.best->ind[2], 990.0);
  BOOST_CHECK_GT(s1.best->ind[3], 9980.0);

  vita::evolution<vita::i_ga, vita::std_es> evo2(env, sset, eva);
  BOOST_REQUIRE(evo2.debug(true));

  const auto s2(evo2.run(1));

  BOOST_CHECK_GT(s2.best->ind[0], 8.0);
  BOOST_CHECK_GT(s2.best->ind[1], 95.0);
  BOOST_CHECK_GT(s2.best->ind[2], 980.0);
  BOOST_CHECK_GT(s2.best->ind[3], 9980.0);
}

BOOST_AUTO_TEST_CASE(Search)
{
  env.individuals = 100;
  //env.verbosity = 0;

  vita::problem<> prob;
  prob.env = env;
  prob.sset = std::move(sset);

  vita::ga_search<vita::i_ga, vita::de_es> s(prob);
  BOOST_REQUIRE(s.debug(true));

  s.set_evaluator(vita::make_unique_evaluator<vita::i_ga>(
                    [](const std::vector<double> &v)
                    { return std::accumulate(v.begin(), v.end(), 0.0); }));

  const auto res(s.run());

  BOOST_CHECK_GT(res[0], 8.0);
  BOOST_CHECK_GT(res[1], 95.0);
  BOOST_CHECK_GT(res[2], 990.0);
  BOOST_CHECK_GT(res[3], 9980.0);
}

BOOST_AUTO_TEST_SUITE_END()<commit_msg>[TEST] New test for GA penalty function<commit_after>/**
 *  \file
 *  \remark This file is part of VITA.
 *
 *  \copyright Copyright (C) 2014 EOS di <NAME>.
 *
 *  \license
 *  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 <cstdlib>
#include <sstream>

#include "kernel/ga/i_ga.h"
#include "kernel/ga/interpreter.h"
#include "kernel/ga/ga_evaluator.h"
#include "kernel/ga/ga_search.h"
#include "kernel/evolution.h"
#include "kernel/problem.h"

#if !defined(MASTER_TEST_SET)
#define BOOST_TEST_MODULE t_ga
#include <boost/test/unit_test.hpp>

using namespace boost;

#include "factory_fixture5.h"
#endif

BOOST_FIXTURE_TEST_SUITE(t_ga, F_FACTORY5)

BOOST_AUTO_TEST_CASE(Penalty)
{
  vita::i_ga ind(env, sset);
  BOOST_REQUIRE(ind.debug());

  vita::interpreter<vita::i_ga> intr(&ind);

  BOOST_REQUIRE_EQUAL(intr.penalty(), 0);

  ind[0] = 20;
  BOOST_REQUIRE_EQUAL(intr.penalty(), 1);

  ind[1] = 200;
  BOOST_REQUIRE_EQUAL(intr.penalty(), 2);

  ind[2] = 2000;
  BOOST_REQUIRE_EQUAL(intr.penalty(), 3);

  ind[3] = 20000;
  BOOST_REQUIRE_EQUAL(intr.penalty(), 4);
}

BOOST_AUTO_TEST_CASE(Evaluator)
{
  auto f = [](const std::vector<double> &v)
           { return std::accumulate(v.begin(), v.end(), 0.0); };

  auto eva(vita::make_evaluator<vita::i_ga>(f));

  vita::fitness_t eva_prev;
  double v_prev;

  for (unsigned i(0); i < 1000; ++i)
  {
    vita::i_ga ind(env, sset);
    BOOST_REQUIRE(ind.debug());

    const auto eva_ret(eva(ind));
    BOOST_REQUIRE_LE(eva_ret, 0.0);

    const auto v(f(ind));

    if (i)
    {
      if (v_prev < v)
        BOOST_REQUIRE_LT(eva_prev, eva_ret);
      else
        BOOST_REQUIRE_GE(eva_prev, eva_ret);
    }

    v_prev = v;
    eva_prev = eva_ret;
  }
}

BOOST_AUTO_TEST_CASE(Evolution)
{
  env.individuals = 100;
  env.verbosity = 0;

  auto eva(vita::make_evaluator<vita::i_ga>(
    [](const std::vector<double> &v)
    { return std::accumulate(v.begin(), v.end(), 0.0); }));

  vita::evolution<vita::i_ga, vita::alps_es> evo1(env, sset, eva);
  BOOST_REQUIRE(evo1.debug(true));

  const auto s1(evo1.run(1));

  BOOST_CHECK_GT(s1.best->ind[0], 8.0);
  BOOST_CHECK_GT(s1.best->ind[1], 95.0);
  BOOST_CHECK_GT(s1.best->ind[2], 990.0);
  BOOST_CHECK_GT(s1.best->ind[3], 9980.0);

  vita::evolution<vita::i_ga, vita::std_es> evo2(env, sset, eva);
  BOOST_REQUIRE(evo2.debug(true));

  const auto s2(evo2.run(1));

  BOOST_CHECK_GT(s2.best->ind[0], 8.0);
  BOOST_CHECK_GT(s2.best->ind[1], 95.0);
  BOOST_CHECK_GT(s2.best->ind[2], 980.0);
  BOOST_CHECK_GT(s2.best->ind[3], 9980.0);
}
/*
BOOST_AUTO_TEST_CASE(Search)
{
  env.individuals = 100;
  //env.verbosity = 0;

  vita::problem<> prob;
  prob.env = env;
  prob.sset = std::move(sset);

  vita::ga_search<vita::i_ga, vita::de_es> s(prob);
  BOOST_REQUIRE(s.debug(true));

  s.set_evaluator(vita::make_unique_evaluator<vita::i_ga>(
                    [](const std::vector<double> &v)
                    { return std::accumulate(v.begin(), v.end(), 0.0); }));

  const auto res(s.run());

  BOOST_CHECK_GT(res[0], 8.0);
  BOOST_CHECK_GT(res[1], 95.0);
  BOOST_CHECK_GT(res[2], 990.0);
  BOOST_CHECK_GT(res[3], 9980.0);
}
*/
BOOST_AUTO_TEST_SUITE_END()
{'fraction_non_alphanumeric': 0.17415730337078653, 'fraction_numerical': 0.036757624398073833, 'mean_word_length': 3.9452380952380954, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3898429', 'n_tokens_mistral': 2860, 'n_tokens_neox': 2662, 'n_words': 457}