Quick start¶
Contents
C++¶
After following the Installation guide you will be able to compile and run your first C++ pagmo_plugins_nonfree 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 | #include <pagmo/algorithm.hpp>
#include <pagmo/archipelago.hpp>
#include <pagmo/problem.hpp>
#include <pagmo/problems/rosenbrock.hpp>
#include <pagmo_plugins_nonfree/pagmo_plugins_nonfree.hpp>
using namespace pagmo;
int main()
{
// 1 - Instantiate a pagmo problem constructing it from the Rosenbrock
// UDP (user defined problem).
problem prob{rosenbrock(30)};
// 2 - Instantiate a pagmo_plugins_nonfree algorithm, in this case SNOPT. This assumes a library libsnopt7_c.so is
// found in the path "/usr/local/lib/". Otherwise you will get a runtime error.
algorithm algo(snopt7(false, "/usr/local/lib/libsnopt7_c.so"));
// 3 - Instantiate an archipelago with 16 islands having each 1 individual (the initial guess)
archipelago archi{16, algo, prob, 1};
// 4 - Run the snopt7 calls in parallel on the 16 separate islands.
archi.evolve(1);
// 5 - Wait for the snopt7 calls to be finished
archi.wait();
// 6 - Print the fitness of the best solution in each island
for (const auto &isl : archi) {
print(isl.get_population().champion_f(), "\n");
}
}
|
Place it into a getting_started.cpp
text file and compile it (for example) with:
g++ -I ~/.local/include -I /usr/include/eigen3/ -O2 -DNDEBUG -std=c++11 getting_started.cpp -pthread -lboost_system -lboost_filesystem -ldl
Note that we have made some assumptions in the above line. 1) your pagmo was installed with the eigen option activated, 2) you installed the headers in a local folder in your user directory.
Python¶
If you have successfully installed pygmo_plugins_non_free 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 22 23 24 | import pygmo as pg
import pygmo_plugins_nonfree as ppn
# 1 - Instantiate a pygmo problem constructing it from a UDP
# (user defined problem).
prob = pg.problem(pg.schwefel(30))
# 2 - Instantiate a pagmo_plugins_nonfree algorithm, in this case SNOPT.
# Here we assume the library name is libsnopt7_c.so, in Windows it would probably be
# snopt7_c.dll or similar.
algo = pg.algorithm(ppn.snopt7(false, "/usr/local/lib/libsnopt7_c.so"))
# 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 and its affiliated packages the most.