Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adds bindings for PagMo2 NSGA-III #160

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pygmo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ Python3_add_library(core MODULE WITH_SOABI
)

target_link_libraries(core PRIVATE Pagmo::pagmo Boost::boost Boost::serialization)
target_link_directories(core PRIVATE "/home/paul/work/UoM/wrg/code/moea/pagmo2/build")
# NOTE: quench warnings from Boost when building the library.
target_compile_definitions(core PRIVATE BOOST_ALLOW_DEPRECATED_HEADERS)
target_include_directories(core SYSTEM PRIVATE "${pybind11_INCLUDE_DIR}")
target_include_directories(core SYSTEM PRIVATE "${pybind11_INCLUDE_DIR}" "/home/paul/work/UoM/wrg/code/moea/pagmo2/include")
target_compile_definitions(core PRIVATE "${pybind11_DEFINITIONS}")
target_compile_options(core PRIVATE
"$<$<CONFIG:Debug>:${PYGMO_CXX_FLAGS_DEBUG}>"
Expand Down
3 changes: 3 additions & 0 deletions pygmo/docstrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2453,6 +2453,9 @@ See also the docs of the relevant C++ method :cpp:func:`pagmo::nsga2::get_log`.
)";
}

std::string nsga3_docstring(){ return R"(nsga3_docstring)"; }
std::string nsga3_get_log_docstring(){ return R"(nsga3_get_log_docstring)"; }

std::string gaco_set_bfe_docstring()
{
return R"(set_bfe(b)
Expand Down
2 changes: 2 additions & 0 deletions pygmo/docstrings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ std::string moead_gen_get_log_docstring();
std::string nsga2_set_bfe_docstring();
std::string nsga2_docstring();
std::string nsga2_get_log_docstring();
std::string nsga3_docstring();
std::string nsga3_get_log_docstring();
std::string nspso_set_bfe_docstring();
std::string nspso_docstring();
std::string nspso_get_log_docstring();
Expand Down
21 changes: 21 additions & 0 deletions pygmo/expose_algorithms_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <pagmo/algorithms/maco.hpp>
#include <pagmo/algorithms/moead_gen.hpp>
#include <pagmo/algorithms/nsga2.hpp>
#include <pagmo/algorithms/nsga3.hpp>
#include <pagmo/algorithms/nspso.hpp>
#include <pagmo/algorithms/null_algorithm.hpp>
#include <pagmo/algorithms/pso.hpp>
Expand Down Expand Up @@ -71,6 +72,26 @@ void expose_algorithms_1(py::module &m, py::class_<pagmo::algorithm> &algo, py::
nsga2_.def("get_seed", &pagmo::nsga2::get_seed, generic_uda_get_seed_docstring().c_str());
nsga2_.def("set_bfe", &pagmo::nsga2::set_bfe, nsga2_set_bfe_docstring().c_str(), py::arg("b"));

// NSGA3
auto nsga3_ = expose_algorithm<pagmo::nsga3>(m, algo, a_module, "nsga3", nsga3_docstring().c_str());
nsga3_.def(py::init<unsigned, double, double, double, double, size_t, unsigned, bool>(),
py::arg("gen") = 1u, py::arg("cr") = 1.0, py::arg("eta_cr") = 30.0, py::arg("mut") = 0.10,
py::arg("eta_mut") = 20.0, py::arg("divisions") = 12u, py::arg("seed"), py::arg("use_memory") );

nsga3_.def(
"get_log",
[](const pagmo::nsga3 &a) -> py::list {
py::list retval;
for (const auto &t : a.get_log()) {
retval.append(py::make_tuple(std::get<0>(t), std::get<1>(t),
vector_to_ndarr<py::array_t<double>>(std::get<2>(t))));
}
return retval;
},
nsga3_get_log_docstring().c_str());

nsga3_.def("get_seed", &pagmo::nsga3::get_seed, generic_uda_get_seed_docstring().c_str());

// GACO
auto gaco_ = expose_algorithm<pagmo::gaco>(m, algo, a_module, "gaco", gaco_docstring().c_str());
gaco_.def(
Expand Down
43 changes: 43 additions & 0 deletions pygmo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,48 @@ def runTest(self):
log = uda.get_log()


class nsga3_test_case(_ut.TestCase):
"""Test case for the UDA NSGA-III"""

def runTest(self):
import numpy as np
from .core import algorithm, dtlz, ideal, nsga3, population
from pickle import loads, dumps
import random

dtlz1_p92_g20_ideal = [3.36287e-06, 8.54994e-06, 1.33931e-04]
nsga3_seed = 32

# Test evolve population with DTLZ1 problem
uda = nsga3(gen=20, cr=1.0, eta_cr=30.0, mut=0.10, eta_mut=20.0, divisions=12, seed=nsga3_seed, use_memory=False)
udp = dtlz(prob_id=1, dim=10, fdim=3)
pop = population(udp, size=92, seed=23)
alg = algorithm(uda)
alg.set_verbosity(2) # Required for log test below
out = alg.evolve(pop)
g20_ideal = ideal(out.get_f())
assert np.allclose(dtlz1_p92_g20_ideal, g20_ideal)

# Test serialisation
self.assertEqual(str(alg), str(loads(dumps(alg))))

# Test get_seed()
self.assertEqual(uda.get_seed(), nsga3_seed)

# Test log retrieval
inst = alg.extract(nsga3)
rlog = inst.get_log()
self.assertTrue(isinstance(rlog, list))
self.assertEqual(len(rlog), 20) # ngen
entry = random.choice(rlog)
self.assertTrue(isinstance(entry, tuple))
self.assertEqual(len(entry), 3) # gen, fevals, ideal
self.assertTrue(isinstance(entry[0], int))
self.assertTrue(isinstance(entry[1], int))
self.assertTrue(isinstance(entry[2], np.ndarray))
self.assertEqual(entry[2].shape, (3,)) # nobjs


class gaco_test_case(_ut.TestCase):
"""Test case for the UDA gaco"""

Expand Down Expand Up @@ -3435,6 +3477,7 @@ def run_test_suite(level=0):
suite.addTest(lennard_jones_test_case())
suite.addTest(de_test_case())
suite.addTest(nsga2_test_case())
suite.addTest(nsga3_test_case())
suite.addTest(gaco_test_case())
suite.addTest(gwo_test_case())
suite.addTest(de1220_test_case())
Expand Down