Skip to content

Commit

Permalink
Create 10_fp_context.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Aug 17, 2023
1 parent 9b024ad commit f8782e2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/10_fp_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Example showing how to change the tolerances used for floating-point comparisons in the GA. */

#include "gapp.hpp" // include everything
#include <iostream>
#include <cmath>

using namespace gapp;

class SinX : public FitnessFunction<RealGene, 1>
{
FitnessVector invoke(const Chromosome<RealGene>& x) const override { return { std::sin(x[0]) }; }
};

int main()
{
std::cout << "The default absolute tolerance used is " << math::Tolerances::abs() << "\n";
std::cout << "The default relative tolerance around 1.0 is " << math::Tolerances::rel(1.0) << "\n";

{
auto solutions = RCGA{}.solve(SinX{}, Bounds{ 0.0, 3.14 });
std::cout << "The maximum of sin(x) in [0.0, 3.14] is at x = " << solutions[0].chromosome[0] << "\n";
}

{
math::ScopedTolerances _(/* abs = */ 0.1, /* rel = */ 0.1);

auto solutions = RCGA{}.solve(SinX{}, Bounds{ 0.0, 3.14 });
std::cout << "The maximum of sin(x) in [0.0, 3.14] is at x = " << solutions[0].chromosome[0] << "\n";
}

{
math::ScopedTolerances _(/* abs = */ 0.0, /* rel = */ 0.0);

auto solutions = RCGA{}.solve(SinX{}, Bounds{ 0.0, 3.14 });
std::cout << "The maximum of sin(x) in [0.0, 3.14] is at x = " << solutions[0].chromosome[0] << "\n";
}
}

0 comments on commit f8782e2

Please sign in to comment.