Quick start#
C++#
After following the Installation you will be able to compile and run your first C++ pagmo_plugins_nonfree program. Let us have a look at a mininal cpp file and its corresponding CMakeLists.txt:
1#include <pagmo/algorithm.hpp>
2#include <pagmo/archipelago.hpp>
3#include <pagmo/problem.hpp>
4#include <pagmo/problems/rosenbrock.hpp>
5#include <pagmo_plugins_nonfree/pagmo_plugins_nonfree.hpp>
6
7using namespace pagmo;
8
9int main()
10{
11 // 1 - Instantiate a pagmo problem constructing it from the Rosenbrock
12 // UDP (user defined problem).
13 problem prob{rosenbrock(30)};
14
15 // 2 - Instantiate a pagmo_plugins_nonfree algorithm, in this case SNOPT. This assumes a library libsnopt7_c.so is
16 // found in the path "/usr/local/lib/". Otherwise you will get a runtime error.
17 algorithm algo(snopt7(false, "/usr/local/lib/libsnopt7_c.so"));
18
19 // 3 - Instantiate an archipelago with 16 islands having each 1 individual (the initial guess)
20 archipelago archi{16, algo, prob, 1};
21
22 // 4 - Run the snopt7 calls in parallel on the 16 separate islands.
23 archi.evolve(1);
24
25 // 5 - Wait for the snopt7 calls to be finished
26 archi.wait();
27
28 // 6 - Print the fitness of the best solution in each island
29 for (const auto &isl : archi) {
30 print(isl.get_population().champion_f(), "\n");
31 }
32}
1cmake_minimum_required(VERSION 3.20)
2project(getting_started_example LANGUAGES CXX)
3set(CMAKE_CXX_STANDARD 17)
4set(CMAKE_CXX_STANDARD_REQUIRED ON)
5
6# Find pagmo and the plugins package installed by this project.
7find_package(pagmo REQUIRED)
8find_package(pagmo_plugins_nonfree REQUIRED)
9
10add_executable(getting_started getting_started.cpp)
11
12# Link the pagmo_plugins_nonfree exported target and pagmo
13target_link_libraries(getting_started PRIVATE Pagmo_plugins_nonfree::pagmo_plugins_nonfree Pagmo::pagmo)
Make a folder and place there the C++ code into a getting_started.cpp text
file and the CMake code into a CMakeLists.txt text file. Then,
from the command line, you can build and run the example with:
$ mkdir build && cd build
$ cmake ..
$ cmake --build .
Python#
If you have successfully installed pygmo_plugins_non_free following the Installation you can try the following script:
1import pygmo as pg
2import pygmo_plugins_nonfree as ppn
3
4# 1 - Instantiate a pygmo problem constructing it from a UDP
5# (user defined problem).
6prob = pg.problem(pg.schwefel(30))
7
8# 2 - Instantiate a pagmo_plugins_nonfree algorithm, in this case SNOPT.
9# Here we assume the library name is libsnopt7_c.so, in Windows it would probably be
10# snopt7_c.dll or similar.
11algo = pg.algorithm(ppn.snopt7(false, "/usr/local/lib/libsnopt7_c.so"))
12
13# 3 - Instantiate an archipelago with 16 islands having each 20 individuals
14archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20)
15
16# 4 - Run the evolution in parallel on the 16 separate islands 10 times.
17archi.evolve(10)
18
19# 5 - Wait for the evolutions to be finished
20archi.wait()
21
22# 6 - Print the fitness of the best solution in each island
23res = [isl.get_population().champion_f for isl in archi]
24print(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 and its affiliated packages the most.