cppOpt

written in C++ source documentation

A header-only C++ library which allows the numerical optimisation of any given problem, function or program without knowing the “function” of the problem simply by smartly testing certain values.

Usage example minimising x^2 with the evolutionary algorithm

#include <cmath>
#include <iostream>

#include "cppOpt.h"

using namespace cppOpt;
using namespace std;

Define the calculation to be optimised.

auto toOptimize = [](OptCalculation<double>& optCalculation) {
    //defined x^2 as function to be optimised
    optCalculation.result = pow(optCalculation.get_parameter("X"), 2);
};

The main program. Defining the optimisation boundaries and algorithm.

int main()
{
    OptBoundaries<double> optBoundaries;
    optBoundaries.add_boundary({-5.0, 5.0, "X"});

    unsigned int maxCalculations = 300;

    OptTarget optTarget = OptTarget::MINIMIZE;

    double coolingFactor = 0.95;

    unsigned int nIndividualsStart = 50;

    unsigned int nIndividualsSelection = 10;

    unsigned int nIndividualsOffspring = 3;

    double mutation = 0.1;

    OptCoordinator<double, false> coordinator(
        maxCalculations,
        toOptimize,
        optTarget,
        0);

    coordinator.add_child(make_unique<OptEvolutionary<double>>(
        optBoundaries,
        optTarget,
        0,
        coolingFactor,
        nIndividualsStart,
        nIndividualsSelection,
        nIndividualsOffspring,
        mutation));

    coordinator.run_optimisation();

    OptCalculation<double> best = coordinator.get_best_calculation();
    cout << best.to_string_header() << endl;
    cout << best.to_string_values() << endl;

    return 0;
}