Skip to content

Commit

Permalink
Merge pull request #67 from bluescarni/nlopt
Browse files Browse the repository at this point in the history
NLopt
  • Loading branch information
darioizzo committed Apr 9, 2017
2 parents 756e893 + 9637411 commit 69da0dd
Show file tree
Hide file tree
Showing 34 changed files with 2,638 additions and 125 deletions.
35 changes: 30 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
project(pagmo)
cmake_minimum_required(VERSION 3.2)

enable_testing()
project(pagmo VERSION 2.0)

cmake_minimum_required(VERSION 3.2)
enable_testing()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules" "${CMAKE_SOURCE_DIR}/cmake_modules/yacma")

Expand All @@ -27,6 +27,9 @@ option(PAGMO_BUILD_PYGMO "Build PyGMO." OFF)
# Build option: enable features depending on Eigen3.
option(PAGMO_WITH_EIGEN3 "Enable features depending on Eigen3 (such as CMAES). Requires Eigen3." OFF)

# Build option: enable NLopt.
option(PAGMO_WITH_NLOPT "Enable wrappers for the NLopt algorithms." OFF)

# Build option: install header.
option(PAGMO_INSTALL_HEADERS "Enable the installation of PaGMO's header files." ON)

Expand Down Expand Up @@ -66,6 +69,13 @@ if(PAGMO_WITH_EIGEN3)
message(STATUS "Eigen version detected: ${EIGEN3_VERSION}")
endif()

# NLopt setup
if(PAGMO_WITH_NLOPT)
find_package(NLOPT REQUIRED)
message(STATUS "NLopt include directory: ${NLOPT_INCLUDE_DIRS}")
message(STATUS "NLopt libraries: ${NLOPT_LIBRARIES}")
endif()

# Python setup.
# NOTE: we do it here because we need to detect the Python bits *before*
# looking for boost.python.
Expand All @@ -90,15 +100,29 @@ add_library(pagmo INTERFACE)
target_link_libraries(pagmo INTERFACE Threads::Threads Boost::boost)
target_include_directories(pagmo INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>)

if(PAGMO_WITH_EIGEN3)
# Link pagmo to eigen3.
add_library(Eigen3::eigen3 INTERFACE IMPORTED)
set_target_properties(Eigen3::eigen3 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${EIGEN3_INCLUDE_DIR}")
target_link_libraries(pagmo INTERFACE Eigen3::eigen3)
# FIXME: this needs to go into config.hpp, once implemented.
target_compile_definitions(pagmo INTERFACE PAGMO_WITH_EIGEN3)
set(PAGMO_ENABLE_EIGEN3 "#define PAGMO_WITH_EIGEN3")
endif()

if(PAGMO_WITH_NLOPT)
# Link pagmo to NLopt.
add_library(NLOPT::nlopt UNKNOWN IMPORTED)
set_target_properties(NLOPT::nlopt PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${NLOPT_INCLUDE_DIRS}")
set_target_properties(NLOPT::nlopt PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LOCATION "${NLOPT_LIBRARIES}")
target_link_libraries(pagmo INTERFACE NLOPT::nlopt)
set(PAGMO_ENABLE_NLOPT "#define PAGMO_WITH_NLOPT")
endif()

# Configure config.hpp.
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/config.hpp.in" "${CMAKE_CURRENT_BINARY_DIR}/include/pagmo/config.hpp")

if(PAGMO_BUILD_TESTS)
add_subdirectory("${CMAKE_SOURCE_DIR}/tests")
endif()
Expand All @@ -113,4 +137,5 @@ endif()

if(PAGMO_INSTALL_HEADERS)
install(DIRECTORY include/ DESTINATION include)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/pagmo/config.hpp" DESTINATION include/pagmo)
endif()
12 changes: 6 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ install:
- if [%BUILD_TYPE%]==[Python35] set PATH=C:\Miniconda35-x64\Scripts;%PATH%
- if [%BUILD_TYPE%]==[Python36] set PATH=C:\Miniconda36-x64\Scripts;%PATH%
- conda config --add channels conda-forge --force
- if [%BUILD_TYPE%]==[Debug] conda create -y --name pagmo python=3.6 cmake boost eigen
- if [%BUILD_TYPE%]==[Python35] conda create -y --name pagmo python=3.5 cmake boost eigen
- if [%BUILD_TYPE%]==[Python36] conda create -y --name pagmo python=3.6 cmake boost eigen
- if [%BUILD_TYPE%]==[Debug] conda create -y --name pagmo python=3.6 cmake boost eigen nlopt
- if [%BUILD_TYPE%]==[Python35] conda create -y --name pagmo python=3.5 cmake boost eigen nlopt
- if [%BUILD_TYPE%]==[Python36] conda create -y --name pagmo python=3.6 cmake boost eigen nlopt
- activate pagmo
- if [%BUILD_TYPE%]==[Python35] conda install -y numpy dill ipyparallel
- if [%BUILD_TYPE%]==[Python36] conda install -y numpy dill ipyparallel

build_script:
- mkdir build
- cd build
- if [%BUILD_TYPE%]==[Debug] cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Debug -DPAGMO_BUILD_TESTS=YES -DPAGMO_BUILD_TUTORIALS=YES -DPAGMO_WITH_EIGEN3=YES ..
- if [%BUILD_TYPE%]==[Debug] cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Debug -DPAGMO_BUILD_TESTS=YES -DPAGMO_BUILD_TUTORIALS=YES -DPAGMO_WITH_EIGEN3=yes -DPAGMO_WITH_NLOPT=yes ..
- if [%BUILD_TYPE%]==[Debug] cmake --build . --config Debug --target install
- if [%BUILD_TYPE%]==[Python35] cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DPAGMO_WITH_EIGEN3=YES -DPAGMO_BUILD_PYGMO=yes ..
- if [%BUILD_TYPE%]==[Python35] cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DPAGMO_WITH_EIGEN3=yes -DPAGMO_WITH_NLOPT=yes -DPAGMO_BUILD_PYGMO=yes ..
- if [%BUILD_TYPE%]==[Python35] cmake --build . --config Release --target install
- if [%BUILD_TYPE%]==[Python36] cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DPAGMO_WITH_EIGEN3=YES -DPAGMO_BUILD_PYGMO=yes ..
- if [%BUILD_TYPE%]==[Python36] cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DPAGMO_WITH_EIGEN3=yes -DPAGMO_WITH_NLOPT=yes -DPAGMO_BUILD_PYGMO=yes ..
- if [%BUILD_TYPE%]==[Python36] cmake --build . --config Release --target install

test_script:
Expand Down
43 changes: 43 additions & 0 deletions cmake_modules/FindNLOPT.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2015-2016, Humanoid Lab, Georgia Tech Research Corporation
# Copyright (c) 2015-2017, Graphics Lab, Georgia Tech Research Corporation
# Copyright (c) 2016-2017, Personal Robotics Lab, Carnegie Mellon University
# This file is provided under the "BSD-style" License

# Find NLOPT
#
# This sets the following variables:
# NLOPT_FOUND
# NLOPT_INCLUDE_DIRS
# NLOPT_LIBRARIES
# NLOPT_DEFINITIONS
# NLOPT_VERSION

find_package(PkgConfig QUIET)

# Check to see if pkgconfig is installed.
pkg_check_modules(PC_NLOPT nlopt QUIET)

# Definitions
set(NLOPT_DEFINITIONS ${PC_NLOPT_CFLAGS_OTHER})

# Include directories
find_path(NLOPT_INCLUDE_DIRS
NAMES nlopt.h
HINTS ${PC_NLOPT_INCLUDEDIR}
PATHS "${CMAKE_INSTALL_PREFIX}/include")

# Libraries
find_library(NLOPT_LIBRARIES
NAMES nlopt nlopt_cxx
HINTS ${PC_NLOPT_LIBDIR})

# Version
set(NLOPT_VERSION ${PC_NLOPT_VERSION})

# Set (NAME)_FOUND if all the variables and the version are satisfied.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(NLOPT
FAIL_MESSAGE DEFAULT_MSG
REQUIRED_VARS NLOPT_INCLUDE_DIRS NLOPT_LIBRARIES
VERSION_VAR NLOPT_VERSION)

40 changes: 40 additions & 0 deletions config.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright 2017 PaGMO development team

This file is part of the PaGMO library.

The PaGMO library is free software; you can redistribute it and/or modify
it under the terms of either:

* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.

or

* the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any
later version.

or both in parallel, as here.

The PaGMO library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the PaGMO library. If not,
see https://www.gnu.org/licenses/. */

#ifndef PAGMO_CONFIG_HPP
#define PAGMO_CONFIG_HPP

// Start of defines instantiated by CMake.
// clang-format off
#define PAGMO_VERSION @pagmo_VERSION@
@PAGMO_ENABLE_EIGEN3@
@PAGMO_ENABLE_NLOPT@
// clang-format on
// End of defines instantiated by CMake.

#endif
4 changes: 3 additions & 1 deletion doc/doxygen/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,9 @@ INCLUDE_FILE_PATTERNS =
# recursively expanded use the := operator instead of the = operator.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.

PREDEFINED = PAGMO_DOXYGEN_INVOKED
PREDEFINED = PAGMO_DOXYGEN_INVOKED \
PAGMO_WITH_EIGEN3 \
PAGMO_WITH_NLOPT

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The
Expand Down
Binary file added doc/doxygen/images/nlopt.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions doc/sphinx/docs/cpp/algorithms/nlopt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NLopt solvers
=============

.. doxygenclass:: pagmo::nlopt
:members:
3 changes: 2 additions & 1 deletion doc/sphinx/docs/cpp/cpp_docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Implemented algorithms
algorithms/moead
algorithms/mbh
algorithms/cstrs_self_adaptive
algorithms/nlopt
algorithms/nsga2
algorithms/pso
algorithms/sade
Expand All @@ -54,13 +55,13 @@ Implemented problems
problems/dtlz
problems/hock_schittkowsky_71
problems/inventory
problems/luksan_vlcek1
problems/translate
problems/decompose
problems/cec2006
problems/cec2009
problems/cec2013
problems/unconstrain
problems/luksan_vlcek1

Implemented islands
^^^^^^^^^^^^^^^^^^^
Expand Down
7 changes: 6 additions & 1 deletion doc/sphinx/docs/python/algorithms/py_algorithms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ Algorithms exposed from C++
-------------------------------------------------------------

.. autoclass:: pygmo.core.cstrs_self_adaptive
:members:
:members:

-------------------------------------------------------------

.. autoclass:: pygmo.core.nlopt
:members:
5 changes: 5 additions & 0 deletions doc/sphinx/docs/python/problems/py_problems.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Problems exposed from C++

-------------------------------------------------------------

.. autoclass:: pygmo.core.luksan_vlcek1
:members:

-------------------------------------------------------------

.. autoclass:: pygmo.core.translate
:members:

Expand Down
10 changes: 8 additions & 2 deletions doc/sphinx/docs/python/tutorials/coding_udp_simple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ be revealed only when calling the malformed method:
...
AttributeError: 'numpy.float64' object has no attribute '__iter__'

In this case, the issue is that the ``fitness()`` method returns a scalar instead of an array-like object (remember that pygmo is also
able to solve multi-objective and constrained problems, thus the fitness value must be, in general, a vector). pygmo will complain
about the wrong return type the first time the ``fitness()`` method is invoked.

Notes on computational speed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -227,6 +231,8 @@ our fitness method into C code.
>>> start_time = time.time(); [prob_jit.fitness(dummy_x) for i in range(1000)]; print(time.time() - start_time) #doctest: +SKIP
0.03771...

With a bit more elbow grease, we can further improve performance:

.. doctest::

>>> from numba import jit, float64
Expand All @@ -250,9 +256,9 @@ our fitness method into C code.
0.01687...


much better right?
Much better, right?

.. note:: For more information on using Numba to speed up your python code see `Numba documentation pages <http://numba.pydata.org/>`_.
.. note:: For more information on using Numba to speed up your python code see the `Numba documentation pages <http://numba.pydata.org/>`_.
In particular, note that only a limited part of NumPy and the python language in general is supported by this use.


4 changes: 3 additions & 1 deletion doc/sphinx/docs/python/tutorials/using_problem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ Let us start:
Lower bounds: [-5, -5, -5, -5, -5]
Upper bounds: [10, 10, 10, 10, 10]
<BLANKLINE>
Has gradient: false
Has gradient: true
User implemented gradient sparsity: false
Expected gradients: 5
Has hessians: false
User implemented hessians sparsity: false
<BLANKLINE>
Function evaluations: 0
Gradient evaluations: 0
<BLANKLINE>
Thread safety: basic
<BLANKLINE>
Expand Down
25 changes: 14 additions & 11 deletions doc/sphinx/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ C++

Pagmo is a header-only library which has the following third party dependencies:

* `Boost <http://www.boost.org/>`_, headers only (needs the libraries only if you intend to compile the python bindings)
* `Eigen <http://eigen.tuxfamily.org/index.php?title=Main_Page>`_, headers only (optional)
* `Boost <http://www.boost.org/>`_, **mandatory**, header-only (needs the libraries only if you
intend to compile the python bindings)
* `Eigen <http://eigen.tuxfamily.org/index.php?title=Main_Page>`_, optional, header-only
* `NLopt <http://ab-initio.mit.edu/wiki/index.php/NLopt>`_, optional, requires linking


After making sure the dependencies above are installed in your system and their headers visible to your compiler, you can download
After making sure the dependencies above are installed in your system, you can download the
pagmo source via the ``git`` command

.. code-block:: bash
Expand All @@ -28,7 +30,7 @@ and configure your build using ``cmake``. When done, type (in your build directo
make install
The headers will be installed in the ``CMAKE_INSTALL_PREFIX/include directory``. To check that all went well
The headers will be installed in the ``CMAKE_INSTALL_PREFIX/include`` directory. To check that all went well
compile the :ref:`quick-start example <getting_started_c++>`.

-----------------------------------------------------------------------
Expand All @@ -38,8 +40,8 @@ Python
The python module correponding to pagmo is called pygmo
It can be installed either directly from ``conda`` or ``pip`` or by building the module from source.

Installing with pip
^^^^^^^^^^^^^^^^^^^
Installation with pip/conda
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The python package pygmo (python binding of the C++ code) can be installed using ``pip`` or ``conda``:

.. code-block:: bash
Expand All @@ -53,13 +55,14 @@ or
conda config --add channels conda-forge
conda install pygmo
Building the module
^^^^^^^^^^^^^^^^^^^
Installation from source
^^^^^^^^^^^^^^^^^^^^^^^^

To build the module you need to have the boost python libraries installed and to activate the ``BUILD_PYGMO`` option from within ``cmake``.
To build the module from source you need to have the Boost.Python libraries installed and to activate the cmake
``PAGMO_BUILD_PYGMO`` option.

Check carefully what python version is detected and what libraries are linked to. In particular select the correct boost_python
according to the python version (2 or 3) you want to compile the module for.
Check carefully what python version is detected and what libraries are linked to. In particular, select the correct Boost.Python
version according to the python version (2 or 3) you want to compile the module for.

The ``CMAKE_INSTALL_PREFIX`` will be used to construct the final location of headers and Python module after install.

Expand Down
15 changes: 12 additions & 3 deletions doc/sphinx/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ After following the :ref:`install` you will be able to compile and run your firs
:language: c++
:linenos:

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

.. code-block:: bash
g++ -std=c++11 getting_started.cpp -pthread
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:

.. code-block:: bash
g++ -O2 -DNDEBUG -std=c++11 getting_started.cpp -pthread -lnlopt
-----------------------------------------------------------------------

Expand All @@ -36,7 +45,7 @@ If you have successfully installed pygmo following the :ref:`install` you can tr
:language: python
:linenos:

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

.. code-block:: bash
Expand Down

0 comments on commit 69da0dd

Please sign in to comment.