Quick start

C++

After following the Installation guide you will be able to compile and run your first C++ pagmo program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

#include <pagmo/algorithm.hpp>
#include <pagmo/algorithms/sade.hpp>
#include <pagmo/archipelago.hpp>
#include <pagmo/problem.hpp>
#include <pagmo/problems/schwefel.hpp>

using namespace pagmo;

int main()
{
    // 1 - Instantiate a pagmo problem constructing it from a UDP
    // (user defined problem).
    problem prob{schwefel(30)};

    // 2 - Instantiate a pagmo algorithm
    algorithm algo{sade(100)};

    // 3 - Instantiate an archipelago with 16 islands having each 20 individuals
    archipelago archi{16, algo, prob, 20};

    // 4 - Run the evolution in parallel on the 16 separate islands 10 times.
    archi.evolve(10);

    // 5 - Wait for the evolutions to be finished
    archi.wait_check();

    // 6 - Print the fitness of the best solution in each island
    for (const auto &isl : archi) {
        std::cout << isl.get_population().champion_f()[0] << '\n';
    }
}

Place it into a getting_started.cpp text file and compile it (for example) with:

g++ -O2 -DNDEBUG -std=c++11 getting_started.cpp -pthread

If you installed pagmo with support for optional 3rd party libraries, you might need to add additional switches to the command-line invocation of the compiler. For instance, if you enabled the optional NLopt support, you will have to link your executable to the nlopt library:

g++ -O2 -DNDEBUG -std=c++11 getting_started.cpp -pthread -lnlopt

Python

If you have successfully installed pygmo following the Installation guide you can try the following script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import pygmo as pg

# 1 - Instantiate a pygmo problem constructing it from a UDP
# (user defined problem).
prob = pg.problem(pg.schwefel(30))

# 2 - Instantiate a pagmo algorithm
algo = pg.algorithm(pg.sade(gen=100))

# 3 - Instantiate an archipelago with 16 islands having each 20 individuals
archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20)

# 4 - Run the evolution in parallel on the 16 separate islands 10 times.
archi.evolve(10)

# 5 - Wait for the evolutions to be finished
archi.wait()

# 6 - Print the fitness of the best solution in each island
res = [isl.get_population().champion_f for isl in archi]
print(res)

Place it into a getting_started.py text file and run it with:

python getting_started.py

We recommend the use of Jupyter or Ipython to enjoy pygmo the most.