Hello World

Writing our first Mantella-based application

Create a new source file called hello_world.cpp on your computer.

For our first Mantella-based application, we want to initialise a pre-defined optimisation problem from the black-box optimisation benchmark, optimise it with a decent optimisation algorithm and print the best found parameter to the console.

#include <mantella>

int main() {
  // 1. Setup the optimisation problem.
  unsigned int numberOfDimensions = 2;
  mant::bbob::SphereFunction optimisationProblem(numberOfDimensions);

  // 2. Run the solver.
  mant::HookeJeevesAlgorithm optimisationAlgorithm;
  optimisationAlgorithm.optimise(optimisationProblem);

  // 3. Get your result!
  std::cout << "bestParameter:\n" << optimisationAlgorithm.getBestParameter() << std::endl;

  return 0;
}
c++ -std=c++11 -march=native -O3 hello_world.cpp -larmadillo -lmantella -o hello_world

# In case Armadillo and Mantella weren't installed/placed into standard included/searched system directories, use the following:
# c++ -std=c++11 -march=native -O3 hello_world.cpp -L/path/to/armaillo/lib -L/path/to/mantella/lib -larmadillo -lmantella -I/path/to/armadillo/include -I/path/to/mantella/include -o hello_world
./hello_world

As we didn’t yet specify an termination criteria, the optimisation process will take 1 second per default. The actual output might may vary due to randomly initialised problems.

Possible output:

bestParameter:
   2.7208
  -0.8456

Understanding what we just did

Our first line was to include the central Mantella include file <mantella>, which adds all internal Mantella headers, providing all of Mantella’s features.

#include <mantella>

Then we created a 2-dimensional instance of the sphere function from the black-box optimisation benchmark …

  // 1. Setup the optimisation problem.
  unsigned int numberOfDimensions = 2;
  mant::bbob::SphereFunction optimisationProblem(numberOfDimensions);

… and used the Hooke-Jeeves algorithm to optimise the given problem.

  // 2. Run the solver.
  mant::HookeJeevesAlgorithm optimisationAlgorithm;
  optimisationAlgorithm.optimise(optimisationProblem);

After the optimisation process finished, we printed out the best found parameter by using the .getBestParameter() Getter. The.optimise(...) method itself does not return any value, but fills various Getter defined in the optimisation algorithms base class.

  // 3. Get your result!
  std::cout << "bestParameter:\n" << optimisationAlgorithm.getBestParameter() << std::endl;

Regarding the compiler options, we added -std=c++11 to ensure C++11 is supported (if not set per default by your compiler) and -march=native -O3 to make use of SIMD instructions (SSE2, SSE3, AVX, …) and compiler optimisation.

It is also crucial to add -larmadillo before -lmantella, as Mantella depends on Armadillo C++.