Ultra 0.9.4
Loading...
Searching...
No Matches
symbolic_regression02.cc
/*
* \file
* \remark This file is part of ULTRA.
*
* \copyright Copyright (C) 2024 EOS di Manlio Morini.
*
* \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/
*
* \see https://github.com/morinim/ultra/wiki/symbolic_regression
*/
/* CHANGES IN THIS FILE MUST BE APPLIED TO THE LINKED WIKI PAGE */
#include "kernel/ultra.h"
int main()
{
using namespace ultra;
// DATA SAMPLE
// (the target function is `x + sin(x)`)
std::istringstream training(R"(
-9.456,-10.0
-8.989, -8.0
-5.721, -6.0
-3.243, -4.0
-2.909, -2.0
0.000, 0.0
2.909, 2.0
3.243, 4.0
5.721, 6.0
8.989, 8.0
)");
// READING INPUT DATA
src::problem prob(training);
// SETTING UP SYMBOLS
prob.insert<real::sin>();
prob.insert<real::cos>();
prob.insert<real::add>();
prob.insert<real::sub>();
prob.insert<real::div>();
prob.insert<real::mul>();
// SEARCHING
prob.params.evolution.generations = 50;
src::search s(prob);
// This is a callback function invoked at the end of every generation and
// useful to gather statistical data.
s.after_generation([](const auto &pop, const auto &)
{
for (const auto &i : pop)
std::cout << out::python_language << i << '\n';
std::cout << "--------------------------------------\n";
});
const auto result(s.run());
std::cout << "\nCANDIDATE SOLUTION\n"
<< out::c_language << result.best_individual()
<< "\n\nFITNESS\n" << *result.best_measurements().fitness << '\n';
}
Definition real.h:148
Definition real.h:227
Definition real.h:256
Definition real.h:729
Definition real.h:763
Definition real.h:824
Definition gp/src/problem.h:66
Definition gp/src/search.h:79
search & after_generation(after_generation_callback_t)
Definition gp/src/search.h:435
std::ostream & python_language(std::ostream &o)
Definition individual_format.cc:81
std::ostream & c_language(std::ostream &o)
Definition individual_format.cc:57
unsigned generations
Definition parameters.h:119