gapp is a library of genetic algorithm implementations in C++ for solving single- and multi-objective optimization problems. The algorithms are highly customizable, with all of their parts possibly defined by the user, but the library also includes GAs for several commonly used encoding types, frequently used crossover and mutation methods for each of these encodings, several stop conditions, and other utilities that can be used.
#include <gapp/gapp.hpp>
#include <iostream>
using namespace gapp;
class SinX : public FitnessFunction<RealGene, 1>
{
FitnessVector invoke(const Chromosome<RealGene>& x) const override { return { std::sin(x[0]) }; }
};
int main()
{
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];
}
Possible console output:
The maximum of sin(x) in [0.0, 3.14] is at x = 1.57079
The following are needed for building and using the library:
- C++20 compiler (gcc 11.0, clang 15.0, msvc 14.30 or later)
- CMake 3.21 or later
- Catch2 3.3 or later (optional, only needed for the tests)
See install-guide.md for a more detailed installation and usage guide.
The library uses CMake as its build system. Assuming you have all of the requirements listed above, the steps for installing the library in Release config are:
git clone https://github.com/KRM7/gapp.git --branch v0.2.0
cd gapp/build
cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF
cmake --build . --config Release
sudo cmake --install . --config Release
Alternatively, you can also use the install script that is provided with the library, which will install all available configurations:
git clone https://github.com/KRM7/gapp.git --branch v0.2.0
sudo bash ./gapp/tools/install.sh
Once the library is installed, you can import it using find_package
and then link
against the gapp::gapp
target provided by the library:
find_package(gapp REQUIRED)
target_link_libraries(YourTarget PRIVATE gapp::gapp)
The documentation for the library can be found in the docs directory:
- Introduction
- Fitness functions
- Encodings
- Algorithms
- Genetic operators
- Stop conditions
- Metrics
- Miscellaneous
The API documentation is available here.
The examples directory contains several examples for using the library.