├── .github ├── ISSUE_TEMPLATE │ └── 0_bug.yml └── workflows │ └── build.yml ├── .readthedocs.yaml ├── AUTHORS ├── CITATION.bib ├── CMakeLists.txt ├── CMakePresets.json ├── COPYING ├── COPYRIGHT ├── ChangeLog ├── NEWS.md ├── README.md ├── TODO ├── cmake ├── FindGuile.cmake ├── FindOctave.cmake ├── NLoptConfig.cmake.in ├── NLoptConfigVersion.cmake.in ├── generate-cpp.cmake ├── generate-fortran.cmake ├── toolchain-i686-w64-mingw32.cmake └── toolchain-x86_64-w64-mingw32.cmake ├── doc ├── README ├── docs │ ├── Citing_NLopt.md │ ├── NLopt_Algorithms.md │ ├── NLopt_C-plus-plus_Reference.md │ ├── NLopt_Deprecated_API_Reference.md │ ├── NLopt_FAQ.md │ ├── NLopt_Fortran_Reference.md │ ├── NLopt_Guile_Reference.md │ ├── NLopt_Installation.md │ ├── NLopt_Introduction.md │ ├── NLopt_Java_Reference.md │ ├── NLopt_License_and_Copyright.md │ ├── NLopt_Matlab_Reference.md │ ├── NLopt_Python_Reference.md │ ├── NLopt_R_Reference.md │ ├── NLopt_Reference.md │ ├── NLopt_Tutorial.md │ ├── NLopt_manual.md │ ├── NLopt_on_Windows.md │ ├── images │ │ ├── NLopt-example-constraints.png │ │ └── Nlopt-logo.png │ └── index.md └── requirements.txt ├── mkdocs.yml ├── nlopt.pc.in ├── nlopt_config.h.in ├── src ├── algs │ ├── ags │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── ags.cc │ │ ├── ags.h │ │ ├── data_types.hpp │ │ ├── evolvent.cc │ │ ├── evolvent.hpp │ │ ├── local_optimizer.cc │ │ ├── local_optimizer.hpp │ │ ├── solver.cc │ │ ├── solver.hpp │ │ └── tst.cc │ ├── auglag │ │ ├── README │ │ ├── auglag.c │ │ └── auglag.h │ ├── bobyqa │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── README.orig │ │ ├── bobyqa.c │ │ └── bobyqa.h │ ├── cdirect │ │ ├── README │ │ ├── cdirect.c │ │ ├── cdirect.h │ │ └── hybrid.c │ ├── cobyla │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── README.orig │ │ ├── cobyla.c │ │ └── cobyla.h │ ├── cquad │ │ ├── README │ │ ├── cquad.c │ │ └── cquad.h │ ├── crs │ │ ├── README │ │ ├── crs.c │ │ └── crs.h │ ├── direct │ │ ├── AUTHORS │ │ ├── COPYING │ │ ├── DIRect.c │ │ ├── DIRparallel.c │ │ ├── DIRserial.c │ │ ├── DIRsubrout.c │ │ ├── README │ │ ├── direct-internal.h │ │ ├── direct.h │ │ ├── direct_wrap.c │ │ ├── tstc.c │ │ └── userguide.pdf │ ├── esch │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── esch.c │ │ └── esch.h │ ├── isres │ │ ├── README │ │ ├── isres.c │ │ └── isres.h │ ├── luksan │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── luksan.h │ │ ├── mssubs.c │ │ ├── mssubs.for │ │ ├── plip.c │ │ ├── plip.for │ │ ├── plip.txt │ │ ├── plis.c │ │ ├── plis.for │ │ ├── plis.txt │ │ ├── pnet.c │ │ ├── pnet.for │ │ ├── pnet.txt │ │ ├── pssubs.c │ │ ├── pssubs.for │ │ └── v999-07.pdf │ ├── mlsl │ │ ├── README │ │ ├── mlsl.c │ │ └── mlsl.h │ ├── mma │ │ ├── README │ │ ├── ccsa_quadratic.c │ │ ├── mma.c │ │ └── mma.h │ ├── neldermead │ │ ├── README │ │ ├── neldermead.h │ │ ├── nldrmd.c │ │ └── sbplx.c │ ├── newuoa │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── README.orig │ │ ├── newuoa.c │ │ └── newuoa.h │ ├── praxis │ │ ├── README │ │ ├── praxis.c │ │ └── praxis.h │ ├── slsqp │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── slsqp.c │ │ └── slsqp.h │ ├── stogo │ │ ├── COPYRIGHT │ │ ├── README │ │ ├── global.cc │ │ ├── global.h │ │ ├── linalg.cc │ │ ├── linalg.h │ │ ├── local.cc │ │ ├── local.h │ │ ├── paper.pdf │ │ ├── prog.cc │ │ ├── rosen.h │ │ ├── stogo.cc │ │ ├── stogo.h │ │ ├── stogo_config.h │ │ ├── techreport.pdf │ │ ├── testfun.h │ │ ├── testros.cc │ │ ├── tools.cc │ │ ├── tools.h │ │ ├── tst.cc │ │ └── tstc.c │ └── subplex │ │ ├── README │ │ ├── subplex.c │ │ └── subplex.h ├── api │ ├── deprecated.c │ ├── f77api.c │ ├── f77funcs.h │ ├── f77funcs_.h │ ├── general.c │ ├── nlopt-in.hpp │ ├── nlopt-internal.h │ ├── nlopt.3 │ ├── nlopt.h │ ├── nlopt_minimize.3 │ ├── nlopt_minimize_constrained.3 │ ├── optimize.c │ └── options.c ├── octave │ ├── CMakeLists.txt │ ├── NLOPT_AUGLAG.m │ ├── NLOPT_AUGLAG_EQ.m │ ├── NLOPT_GD_MLSL.m │ ├── NLOPT_GD_MLSL_LDS.m │ ├── NLOPT_GD_STOGO.m │ ├── NLOPT_GD_STOGO_RAND.m │ ├── NLOPT_GN_AGS.m │ ├── NLOPT_GN_CRS2_LM.m │ ├── NLOPT_GN_DIRECT.m │ ├── NLOPT_GN_DIRECT_L.m │ ├── NLOPT_GN_DIRECT_L_NOSCAL.m │ ├── NLOPT_GN_DIRECT_L_RAND.m │ ├── NLOPT_GN_DIRECT_L_RAND_NOSCAL.m │ ├── NLOPT_GN_DIRECT_NOSCAL.m │ ├── NLOPT_GN_ESCH.m │ ├── NLOPT_GN_ISRES.m │ ├── NLOPT_GN_MLSL.m │ ├── NLOPT_GN_MLSL_LDS.m │ ├── NLOPT_GN_ORIG_DIRECT.m │ ├── NLOPT_GN_ORIG_DIRECT_L.m │ ├── NLOPT_G_MLSL.m │ ├── NLOPT_G_MLSL_LDS.m │ ├── NLOPT_LD_AUGLAG.m │ ├── NLOPT_LD_AUGLAG_EQ.m │ ├── NLOPT_LD_CCSAQ.m │ ├── NLOPT_LD_LBFGS.m │ ├── NLOPT_LD_MMA.m │ ├── NLOPT_LD_SLSQP.m │ ├── NLOPT_LD_TNEWTON.m │ ├── NLOPT_LD_TNEWTON_PRECOND.m │ ├── NLOPT_LD_TNEWTON_PRECOND_RESTART.m │ ├── NLOPT_LD_TNEWTON_RESTART.m │ ├── NLOPT_LD_VAR1.m │ ├── NLOPT_LD_VAR2.m │ ├── NLOPT_LN_AUGLAG.m │ ├── NLOPT_LN_AUGLAG_EQ.m │ ├── NLOPT_LN_BOBYQA.m │ ├── NLOPT_LN_COBYLA.m │ ├── NLOPT_LN_NELDERMEAD.m │ ├── NLOPT_LN_NEWUOA.m │ ├── NLOPT_LN_NEWUOA_BOUND.m │ ├── NLOPT_LN_PRAXIS.m │ ├── NLOPT_LN_SBPLX.m │ ├── dummy.c │ ├── mkconstants.sh │ ├── nlopt_minimize.m │ ├── nlopt_minimize_constrained.m │ ├── nlopt_optimize-mex.c │ ├── nlopt_optimize-oct.cc │ └── nlopt_optimize.m ├── swig │ ├── CMakeLists.txt │ ├── METADATA.in │ ├── glob_java.cmake │ ├── nlopt-exceptions.i │ ├── nlopt-guile.i │ ├── nlopt-java.i │ ├── nlopt-python.i │ ├── nlopt.i │ └── numpy.i └── util │ ├── mt19937ar.c │ ├── mt19937ar_test.c │ ├── nlopt-getopt.c │ ├── nlopt-getopt.h │ ├── nlopt-util.h │ ├── qsort_r.c │ ├── redblack.c │ ├── redblack.h │ ├── redblack_test.c │ ├── rescale.c │ ├── soboldata.h │ ├── sobolseq.c │ ├── sobolseq_test.c │ ├── stop.c │ └── timer.c └── test ├── CMakeLists.txt ├── box.c ├── cpp_functor.cxx ├── lorentzfit.c ├── myconstraint.m ├── myfunc.m ├── t_bounded.cxx ├── t_except.cxx ├── t_fbound.cxx ├── t_fortran.f90 ├── t_guile.scm ├── t_java.java ├── t_matlab.m ├── t_memoize.py ├── t_octave.m ├── t_python.py ├── t_tutorial.cxx ├── testfuncs.c ├── testfuncs.h └── testopt.c /.github/ISSUE_TEMPLATE/0_bug.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: Create a bug report. 3 | labels: 4 | - bug 5 | body: 6 | - type: textarea 7 | id: what 8 | attributes: 9 | label: What happened? 10 | description: What should have happened instead? Please provide as many details as possible. 11 | validations: 12 | required: true 13 | - type: textarea 14 | id: info 15 | attributes: 16 | label: How to reproduce the issue? 17 | description: Please add the minimal code that helps replicating the issue. 18 | render: shell 19 | validations: 20 | required: true 21 | - type: textarea 22 | id: version 23 | attributes: 24 | label: Version 25 | description: | 26 | Please tell us the version you are using. 27 | 28 | ```python 29 | import nlopt 30 | print(nlopt.__version__) 31 | ``` 32 | validations: 33 | required: true 34 | - type: dropdown 35 | id: oscheck 36 | attributes: 37 | label: Operating System 38 | description: Please provide the operating system running. 39 | options: 40 | - unknown 41 | - Windows 42 | - Linux 43 | - MacOS 44 | - all 45 | - other 46 | validations: 47 | required: true 48 | - type: dropdown 49 | id: sourcecheck 50 | attributes: 51 | label: Installation media 52 | description: Please provide the installation media. 53 | options: 54 | - unknown 55 | - pip 56 | - conda 57 | - from source 58 | - other 59 | validations: 60 | required: true 61 | - type: textarea 62 | id: context 63 | attributes: 64 | label: Additional Context 65 | description: Include any additional information that you think would be valuable. 66 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | on: [push, pull_request] 3 | jobs: 4 | linux: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - uses: actions/checkout@v4 8 | - name: Build 9 | run: | 10 | sudo apt-get update -y && sudo apt-get install python3-dev python3-numpy guile-3.0-dev octave-dev cmake gfortran g++-mingw-w64-x86-64 gfortran-mingw-w64-x86-64 11 | pip install mkdocs python-markdown-math --user 12 | PATH=$PATH:~/.local/bin mkdocs build 13 | mkdir build && pushd build 14 | cmake -DCMAKE_INSTALL_PREFIX=~/.local -DNLOPT_FORTRAN=ON -DCMAKE_C_FLAGS='-std=c89 -pedantic -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Wshadow' -DCMAKE_CXX_FLAGS='-Wall -Wextra -pedantic -Wshadow' -DSWIG_COMPILE_FLAGS="-Wno-shadow -Wno-unused-parameter -Wno-missing-field-initializers" -DNLOPT_TESTS=ON .. 15 | make install -j4 && ctest -j4 --output-on-failure 16 | rm -rf * ~/.local 17 | cmake -DCMAKE_INSTALL_PREFIX=~/.local -DNLOPT_PYTHON=OFF -DNLOPT_OCTAVE=OFF -DNLOPT_GUILE=OFF -DNLOPT_FORTRAN=ON -DCMAKE_TOOLCHAIN_FILE=$PWD/../cmake/toolchain-x86_64-w64-mingw32.cmake -DNLOPT_TESTS=ON .. 18 | make install -j4 19 | 20 | macos: 21 | runs-on: macos-latest 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Build 25 | run: | 26 | brew install swig guile 27 | mkdir build && pushd build 28 | cmake -DCMAKE_INSTALL_PREFIX=~/.local -DNLOPT_FORTRAN=OFF -DNLOPT_TESTS=ON .. 29 | make install -j3 && ctest --output-on-failure 30 | 31 | windows: 32 | runs-on: windows-latest 33 | steps: 34 | - uses: actions/checkout@v4 35 | - uses: actions/setup-python@v5 36 | with: 37 | python-version: '3.12' 38 | - name: Build 39 | shell: cmd 40 | run: | 41 | pip install numpy 42 | cmake -DCMAKE_INSTALL_PREFIX=%cd:\=/%/install -DNLOPT_TESTS=ON . 43 | cmake --build . --config Release --target install 44 | xcopy %cd%\install\bin\nlopt.dll %cd%\src\swig\python 45 | xcopy %cd%\install\Lib\site-packages\_nlopt*.pyd %cd%\src\swig\python 46 | xcopy %cd%\install\bin\nlopt.dll %cd%\src\swig\Release 47 | dir /p %cd%\src\swig 48 | ctest -C Release --output-on-failure --timeout 100 49 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | build: 4 | os: ubuntu-22.04 5 | tools: 6 | python: "3.12" 7 | 8 | mkdocs: 9 | configuration: mkdocs.yml 10 | 11 | python: 12 | install: 13 | - requirements: doc/requirements.txt 14 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | NLopt was originally written/packaged by: 2 | 3 | Steven G. Johnson 4 | 5 | with subsequent contributions by Julien Schueller and other contributors 6 | that can be found in the git history, summarized here: 7 | 8 | https://github.com/stevengj/nlopt/graphs/contributors 9 | 10 | See the subdirectories for the authors of the original nonlinear optimization 11 | libraries utilized by NLopt. 12 | -------------------------------------------------------------------------------- /CMakePresets.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "configurePresets": [ 4 | { 5 | "name": "linux-debug", 6 | "binaryDir": "${sourceDir}/build", 7 | "generator": "Unix Makefiles", 8 | "cacheVariables": { 9 | "CMAKE_BUILD_TYPE": "RelWithDebInfo", 10 | "CMAKE_C_FLAGS": "-Wall -Wextra -Wpedantic -Wshadow", 11 | "CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wshadow -D_GLIBCXX_ASSERTIONS -fno-inline", 12 | "CMAKE_INSTALL_PREFIX": "${sourceDir}/build/install", 13 | "SWIG_COMPILE_FLAGS": "-O1 -Wno-unused-parameter -Wno-shadow" 14 | } 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | NLopt combines several free/open-source nonlinear optimization 2 | libraries by various authors. See the COPYING, COPYRIGHT, and README 3 | files in the subdirectories for the original copyright and licensing 4 | information of these packages. 5 | 6 | The compiled NLopt library, i.e. the combined work of all of the 7 | included optimization routines, is licensed under the conjunction of 8 | all of these licensing terms. By default, the most restrictive terms 9 | are for the code in the "luksan" directory, which is licensed under 10 | the GNU Lesser General Public License (GNU LGPL), version 2.1 or 11 | later (see luksan/COPYRIGHT). That means that, by default, the compiled 12 | NLopt library is governed by the terms of the LGPL. 13 | 14 | --------------------------------------------------------------------------- 15 | 16 | However, NLopt also offers the option to be built without the code in 17 | the "luksan" directory. In this case, NLopt, including any modifications 18 | to the abovementioned packages, are licensed under the standard "MIT License:" 19 | 20 | Copyright (c) 2007-2024 Massachusetts Institute of Technology 21 | 22 | Permission is hereby granted, free of charge, to any person obtaining 23 | a copy of this software and associated documentation files (the 24 | "Software"), to deal in the Software without restriction, including 25 | without limitation the rights to use, copy, modify, merge, publish, 26 | distribute, sublicense, and/or sell copies of the Software, and to 27 | permit persons to whom the Software is furnished to do so, subject to 28 | the following conditions: 29 | 30 | The above copyright notice and this permission notice shall be 31 | included in all copies or substantial portions of the Software. 32 | 33 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 34 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 35 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 36 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 37 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 38 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 39 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 40 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2020 Massachusetts Institute of Technology 2 | * and other contributors 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | 24 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | See https://github.com/stevengj/nlopt for a detailed version history. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Docs](https://readthedocs.org/projects/nlopt/badge/?version=latest)](http://nlopt.readthedocs.io/en/latest/) 2 | [![Build Status](https://github.com/stevengj/nlopt/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/stevengj/nlopt/actions/workflows/build.yml) 3 | 4 | NLopt is a library for nonlinear local and global optimization, for 5 | functions with and without gradient information. It is designed as 6 | a simple, unified interface and packaging of several free/open-source 7 | nonlinear optimization libraries. 8 | 9 | The latest release can be downloaded from the [NLopt releases](https://github.com/stevengj/nlopt/releases) page on Github, and the 10 | [NLopt manual](https://nlopt.readthedocs.io/en/latest/) is hosted on readthedocs. 11 | 12 | To build it refer to the [installation instructions](https://nlopt.readthedocs.io/en/latest/#download-and-installation). 13 | 14 | To use in your C/C++ programs see the [C reference manual](https://nlopt.readthedocs.io/en/latest/NLopt_Reference/). 15 | 16 | There are also interfaces for [C++](https://nlopt.readthedocs.io/en/latest/NLopt_C-plus-plus_Reference/), [Fortran](https://nlopt.readthedocs.io/en/latest/NLopt_Fortran_Reference/), [Python](https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference/), [Matlab or GNU Octave](https://nlopt.readthedocs.io/en/latest/NLopt_Matlab_Reference/), [OCaml](https://bitbucket.org/mkur/nlopt-ocaml), 17 | [GNU Guile](https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference/), [GNU R](https://www.ucl.ac.uk/~uctpjyy/nloptr.html), [Lua](https://github.com/rochus-keller/LuaNLopt), [Perl](https://metacpan.org/pod/Math::NLopt), [Rust](https://github.com/jesskfullwood/rust-nlopt), [Ruby](https://github.com/ankane/nlopt-ruby), [Julia](https://github.com/JuliaOpt/NLopt.jl), and [Java](https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference/). Interfaces for other languages may 18 | be added in the future. 19 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Add nonlinearly constrained multistart algorithm. 2 | 3 | Add option to control subspace size (mf) in LBFGS etcetera. 4 | 5 | STOGO has bitrotted - doesn't return proper NLopt codes, doesn't 6 | support forced stop, etcetera. Needs to be fixed. 7 | 8 | If local_optimizer is set, STOGO should use it instead of its 9 | own BFGS. 10 | 11 | Wrappers for GNU R, Java, Perl, Ocaml, C#, ... 12 | 13 | Updates from Jones, D.R., Martins, J.R.R.A., (2021) "The DIRECT algorithm: 25 years Later." J Glob Optim 79, 521–566. -------------------------------------------------------------------------------- /cmake/FindGuile.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2008, 2014 OpenCog.org (http://opencog.org) 2 | # 3 | # Redistribution and use is allowed according to the terms of the BSD license. 4 | # For details see the accompanying COPYING-CMAKE-SCRIPTS file. 5 | 6 | # - Try to find Guile; Once done this will define 7 | # 8 | # GUILE_FOUND - system has the GUILE library 9 | # GUILE_INCLUDE_DIRS - the GUILE include directory 10 | # GUILE_LIBRARIES - The libraries needed to use GUILE 11 | # GUILE_VERSION_STRING - Version 12 | # GUILE_SITE_DIR - site dir 13 | # GUILE_EXTENSION_DIR - extension dir 14 | # GUILE_ROOT_DIR - prefix dir 15 | 16 | # Look for the header file 17 | # Look for guile-2.2 first, then 2.0, then 1.8 18 | # Macports for OSX puts things in /opt/local 19 | find_path (GUILE_INCLUDE_DIR libguile.h 20 | PATH_SUFFIXES 21 | guile/3.0 22 | guile/2.2 23 | guile/2.0 24 | guile/1.8 25 | libguile 26 | guile 27 | HINTS /opt/local/include 28 | ) 29 | 30 | # Look for the library 31 | find_library (GUILE_LIBRARY NAMES guile-3.0 guile-2.2 guile-2.0 guile 32 | HINTS 33 | /opt/local/lib 34 | ) 35 | 36 | 37 | set (GUILE_LIBRARIES ${GUILE_LIBRARY}) 38 | set (GUILE_INCLUDE_DIRS ${GUILE_INCLUDE_DIR}) 39 | 40 | find_path (GMP_INCLUDE_DIR gmp.h) 41 | if (GMP_INCLUDE_DIR) 42 | list (APPEND GUILE_INCLUDE_DIRS ${GMP_INCLUDE_DIR}) 43 | endif () 44 | 45 | # check guile's version if we're using cmake >= 2.6 46 | if (GUILE_INCLUDE_DIR) 47 | SET(GUILE_VERSION_MAJOR 0) 48 | SET(GUILE_VERSION_MINOR 0) 49 | SET(GUILE_VERSION_PATCH 0) 50 | 51 | IF(NOT EXISTS "${GUILE_INCLUDE_DIR}/libguile/version.h") 52 | MESSAGE(FATAL_ERROR "Found ${GUILE_INCLUDE_DIR}/libguile.h but not version.h; check your guile installation!") 53 | ENDIF(NOT EXISTS "${GUILE_INCLUDE_DIR}/libguile/version.h") 54 | 55 | # Extract the libguile version from the 'version.h' file 56 | SET(GUILE_MAJOR_VERSION 0) 57 | FILE(READ "${GUILE_INCLUDE_DIR}/libguile/version.h" _GUILE_VERSION_H_CONTENTS) 58 | 59 | STRING(REGEX MATCH "#define SCM_MAJOR_VERSION[ ]+([0-9])" _MATCH "${_GUILE_VERSION_H_CONTENTS}") 60 | SET(GUILE_VERSION_MAJOR ${CMAKE_MATCH_1}) 61 | STRING(REGEX MATCH "#define SCM_MINOR_VERSION[ ]+([0-9]+)" _MATCH "${_GUILE_VERSION_H_CONTENTS}") 62 | SET(GUILE_VERSION_MINOR ${CMAKE_MATCH_1}) 63 | STRING(REGEX MATCH "#define SCM_MICRO_VERSION[ ]+([0-9]+)" _MATCH "${_GUILE_VERSION_H_CONTENTS}") 64 | SET(GUILE_VERSION_PATCH ${CMAKE_MATCH_1}) 65 | 66 | SET(GUILE_VERSION_STRING "${GUILE_VERSION_MAJOR}.${GUILE_VERSION_MINOR}.${GUILE_VERSION_PATCH}") 67 | 68 | endif () 69 | 70 | find_program(GUILE_EXECUTABLE 71 | NAMES guile3.0 guile2.2 guile2.0 guile 72 | ) 73 | 74 | find_program(GUILE_CONFIG_EXECUTABLE 75 | NAMES guile-config3.0 guile-config2.2 guile-config2.0 guile-config 76 | ) 77 | 78 | 79 | if (GUILE_CONFIG_EXECUTABLE) 80 | execute_process (COMMAND ${GUILE_CONFIG_EXECUTABLE} info prefix 81 | OUTPUT_VARIABLE GUILE_ROOT_DIR 82 | OUTPUT_STRIP_TRAILING_WHITESPACE) 83 | 84 | execute_process (COMMAND ${GUILE_CONFIG_EXECUTABLE} info sitedir 85 | OUTPUT_VARIABLE GUILE_SITE_DIR 86 | OUTPUT_STRIP_TRAILING_WHITESPACE) 87 | 88 | execute_process (COMMAND ${GUILE_CONFIG_EXECUTABLE} info extensiondir 89 | OUTPUT_VARIABLE GUILE_EXTENSION_DIR 90 | OUTPUT_STRIP_TRAILING_WHITESPACE) 91 | endif () 92 | 93 | # IF(GUILE_FOUND AND GUILE_VERSION_MAJOR EQUAL 2) 94 | # ADD_DEFINITIONS(-DHAVE_GUILE2) 95 | # ENDIF(GUILE_FOUND AND GUILE_VERSION_MAJOR EQUAL 2) 96 | 97 | # handle REQUIRED and QUIET options 98 | include (FindPackageHandleStandardArgs) 99 | find_package_handle_standard_args (Guile REQUIRED_VARS GUILE_EXECUTABLE GUILE_ROOT_DIR GUILE_INCLUDE_DIRS GUILE_LIBRARIES VERSION_VAR GUILE_VERSION_STRING) 100 | 101 | 102 | mark_as_advanced (GUILE_INCLUDE_DIR GUILE_LIBRARY) 103 | -------------------------------------------------------------------------------- /cmake/NLoptConfig.cmake.in: -------------------------------------------------------------------------------- 1 | 2 | # Tell the user project where to find our headers and libraries 3 | 4 | set (NLOPT_VERSION "@NLOPT_VERSION_STRING@") 5 | 6 | set (NLOPT_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/@NLOPT_INSTALL_INCLUDEDIR@") 7 | set (NLOPT_LIBRARY_DIRS "@CMAKE_INSTALL_PREFIX@/@NLOPT_INSTALL_LIBDIR@") 8 | 9 | # Allows loading NLOPT settings from another project 10 | set (NLOPT_CONFIG_FILE "${CMAKE_CURRENT_LIST_FILE}") 11 | 12 | # List of compilation flags -DTOTO to export 13 | set (NLOPT_DEFINITIONS "@NLOPT_DEFINITIONS@") 14 | 15 | # Our library dependencies (contains definitions for IMPORTED targets) 16 | include ("${CMAKE_CURRENT_LIST_DIR}/NLoptLibraryDepends.cmake") 17 | 18 | # These are IMPORTED targets created by NLOPTLibraryDepends.cmake 19 | set (NLOPT_LIBRARIES "@NLOPT_LIBRARIES@") 20 | 21 | -------------------------------------------------------------------------------- /cmake/NLoptConfigVersion.cmake.in: -------------------------------------------------------------------------------- 1 | 2 | set (PACKAGE_VERSION "@NLOPT_VERSION_STRING@") 3 | 4 | # Check whether the requested PACKAGE_FIND_VERSION is compatible 5 | if ("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") 6 | set (PACKAGE_VERSION_COMPATIBLE FALSE) 7 | else () 8 | set (PACKAGE_VERSION_COMPATIBLE TRUE) 9 | if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") 10 | set (PACKAGE_VERSION_EXACT TRUE) 11 | endif () 12 | endif () 13 | -------------------------------------------------------------------------------- /cmake/generate-cpp.cmake: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.13) 2 | 3 | # generate nlopt.hpp from nlopt-in.hpp 4 | file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "") 5 | file (STRINGS ${API_SOURCE_DIR}/nlopt-in.hpp NLOPT_HPP_LINES) 6 | foreach (NLOPT_HPP_LINE ${NLOPT_HPP_LINES}) 7 | list(LENGTH NLOPT_HPP_LINE line_len) 8 | # handling trailing backlashes in "file (STRINGS" is a little tricky 9 | if (line_len VERSION_LESS 8) 10 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${NLOPT_HPP_LINE}\n") 11 | else () 12 | set (prev_inst FALSE) 13 | foreach(NLOPT_HPP_SUBLINE ${NLOPT_HPP_LINE}) 14 | # test is we need to add the eaten semicolon 15 | if (NLOPT_HPP_SUBLINE MATCHES "\\)$" OR NLOPT_HPP_SUBLINE MATCHES "return") 16 | set (new_inst TRUE) 17 | else () 18 | set (new_inst FALSE) 19 | endif () 20 | if (NOT prev_inst) 21 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${NLOPT_HPP_SUBLINE}") 22 | if (new_inst) 23 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp ";") 24 | endif () 25 | list (FIND NLOPT_HPP_LINE "${NLOPT_HPP_SUBLINE}" index) 26 | math (EXPR index "${index} + 1") 27 | list (LENGTH NLOPT_HPP_LINE total) 28 | if (NOT index STREQUAL total) 29 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " \\") 30 | endif () 31 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "\n") 32 | endif () 33 | set (prev_inst ${new_inst}) 34 | endforeach () 35 | endif () 36 | if (NLOPT_HPP_LINE MATCHES "GEN_ENUMS_HERE") 37 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " enum algorithm {\n") 38 | file (STRINGS ${API_SOURCE_DIR}/nlopt.h NLOPT_H_LINES REGEX " NLOPT_[A-Z0-9_]+") 39 | foreach (NLOPT_H_LINE ${NLOPT_H_LINES}) 40 | string (REGEX REPLACE "NLOPT_" "" ENUM_LINE ${NLOPT_H_LINE}) 41 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${ENUM_LINE}\n") 42 | if (NLOPT_H_LINE MATCHES "NLOPT_NUM_ALGORITHMS") 43 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n enum result {\n") 44 | elseif (NLOPT_H_LINE MATCHES "NLOPT_NUM_RESULTS") 45 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n") 46 | endif () 47 | endforeach () 48 | endif () 49 | endforeach () 50 | -------------------------------------------------------------------------------- /cmake/generate-fortran.cmake: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.13) 2 | 3 | # generate nlopt.f from nlopt.h enums 4 | file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f "") 5 | file (STRINGS ${API_SOURCE_DIR}/nlopt.h NLOPT_H_LINES REGEX " NLOPT_[A-Z0-9_]+") 6 | set (i 0) 7 | foreach (NLOPT_H_LINE ${NLOPT_H_LINES}) 8 | if (NOT NLOPT_H_LINE MATCHES "NLOPT_NUM_") 9 | string (REGEX REPLACE ".*NLOPT_([A-Z0-9_]+).*" "\\1" ENUM_STRING ${NLOPT_H_LINE}) 10 | string (REGEX REPLACE ".*NLOPT_[A-Z0-9_]+ = (-?[0-9]+).*" "\\1" ENUM_VAL ${NLOPT_H_LINE}) 11 | if (ENUM_VAL MATCHES "^-?[0-9]+$") 12 | set (i ${ENUM_VAL}) 13 | endif () 14 | set (ENUM_LINE " integer NLOPT_${ENUM_STRING}\n parameter (NLOPT_${ENUM_STRING}=${i})\n") 15 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f "${ENUM_LINE}") 16 | math (EXPR i "${i} + 1") 17 | endif () 18 | endforeach () 19 | -------------------------------------------------------------------------------- /cmake/toolchain-i686-w64-mingw32.cmake: -------------------------------------------------------------------------------- 1 | set (CMAKE_SYSTEM_NAME Windows) 2 | 3 | # specify the cross compiler 4 | set (CMAKE_C_COMPILER i686-w64-mingw32-gcc) 5 | set (CMAKE_CXX_COMPILER i686-w64-mingw32-g++) 6 | 7 | # where is the target environment 8 | set (CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32) 9 | 10 | # search for programs in the build host directories 11 | set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | # for libraries and headers in the target directories 13 | set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 14 | set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 15 | set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 16 | 17 | # Make sure Qt can be detected by CMake 18 | set (QT_BINARY_DIR /usr/i686-w64-mingw32/bin /usr/bin) 19 | set (QT_INCLUDE_DIRS_NO_SYSTEM ON) 20 | 21 | # set the resource compiler (RHBZ #652435) 22 | set (CMAKE_RC_COMPILER i686-w64-mingw32-windres) 23 | set (CMAKE_MC_COMPILER i686-w64-mingw32-windmc) 24 | 25 | # override boost thread component suffix as mingw-w64-boost is compiled with threadapi=win32 26 | set (Boost_THREADAPI win32) 27 | 28 | # These are needed for compiling lapack (RHBZ #753906) 29 | set (CMAKE_Fortran_COMPILER i686-w64-mingw32-gfortran) 30 | set (CMAKE_AR:FILEPATH i686-w64-mingw32-ar) 31 | set (CMAKE_RANLIB:FILEPATH i686-w64-mingw32-ranlib) 32 | 33 | -------------------------------------------------------------------------------- /cmake/toolchain-x86_64-w64-mingw32.cmake: -------------------------------------------------------------------------------- 1 | set (CMAKE_SYSTEM_NAME Windows) 2 | 3 | # specify the cross compiler 4 | set (CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) 5 | set (CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) 6 | 7 | # where is the target environment 8 | set (CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32) 9 | 10 | # search for programs in the build host directories 11 | set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | # for libraries and headers in the target directories 13 | set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 14 | set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 15 | set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 16 | 17 | # Make sure Qt can be detected by CMake 18 | set (QT_BINARY_DIR /usr/x86_64-w64-mingw32/bin /usr/bin) 19 | set (QT_INCLUDE_DIRS_NO_SYSTEM ON) 20 | 21 | # set the resource compiler (RHBZ #652435) 22 | set (CMAKE_RC_COMPILER x86_64-w64-mingw32-windres) 23 | set (CMAKE_MC_COMPILER x86_64-w64-mingw32-windmc) 24 | 25 | # override boost thread component suffix as mingw-w64-boost is compiled with threadapi=win32 26 | set (Boost_THREADAPI win32) 27 | 28 | # These are needed for compiling lapack (RHBZ #753906) 29 | set (CMAKE_Fortran_COMPILER x86_64-w64-mingw32-gfortran) 30 | set (CMAKE_AR:FILEPATH x86_64-w64-mingw32-ar) 31 | set (CMAKE_RANLIB:FILEPATH x86_64-w64-mingw32-ranlib) 32 | 33 | -------------------------------------------------------------------------------- /doc/README: -------------------------------------------------------------------------------- 1 | This is the documentation tree for NLopt. 2 | 3 | Markdown (.md) files for NLopt documentation live in $(top_src_dir)/doc/docs. 4 | 5 | To build and visualize the HTML documentation locally using the 6 | mkdocs package (useful for verifying on your local machine before 7 | committing): 8 | 9 | 1. Install the `mkdocs` package, together with the requirements in 10 | requirements.txt: 11 | 12 | % pip install mkdocs 13 | % pip install -r requirements.txt 14 | 15 | 2. Run the following command from the top-level NLopt repository tree: 16 | 17 | % mkdocs serve 18 | 19 | and then open the following URL in a browser window: 20 | 21 | http://127.0.0.1:8000/NLopt 22 | 23 | This launches a little web server on your local machine 24 | plus a filesystem hook for rebuilding the documentation 25 | tree automatically whenever any .md file is modified, so 26 | you can see what the actual HTML documentation looks 27 | like in real time as you edit the source. 28 | -------------------------------------------------------------------------------- /doc/docs/Citing_NLopt.md: -------------------------------------------------------------------------------- 1 | --- 2 | # Citing NLopt 3 | --- 4 | 5 | If you use NLopt in work that leads to a publication, we would appreciate it if you would kindly cite NLopt in your manuscript. Please cite *both* the NLopt library *and* the authors of the specific algorithm(s) that you employed in your work. Cite NLopt as something like: 6 | 7 | - Steven G. Johnson, The NLopt nonlinear-optimization package, 8 | 9 | The authors and appropriate citations for the specific optimization algorithms in NLopt are listed in the [NLopt Algorithms](NLopt_Algorithms.md) page. 10 | 11 | See also the [NLopt `CITATION.bib` file](https://github.com/stevengj/nlopt/blob/master/CITATION.bib) for citations in BibTeX format. 12 | -------------------------------------------------------------------------------- /doc/docs/NLopt_FAQ.md: -------------------------------------------------------------------------------- 1 | --- 2 | # NLopt FAQ 3 | --- 4 | 5 | General 6 | ------- 7 | 8 | ### What is NLopt? 9 | 10 | NLopt is a free/open-source library for nonlinear optimization. 11 | 12 | ### Why use NLopt, when some of the same algorithms are available elsewhere? 13 | 14 | Several of the algorithms provided by NLopt are based on free/open-source packages by other authors, which we have put together under a common interface in NLopt. If you are using one of these packages and are happy with it, great! 15 | 16 | However, our experience is that, for nonlinear optimization, the best algorithm is highly problem-dependent; the best approach is to try several different techniques and see which one works best for *you*. NLopt makes this easy by providing a single package and a common interface for many different algorithms, callable from many different languages. 17 | 18 | Installation 19 | ------------ 20 | 21 | ### Where can I install NLopt? 22 | 23 | NLopt should be straightforward to install on any Unix-like system (GNU/Linux is fine) with a C compiler (gcc is fine). 24 | 25 | It should be possible to compile under Windows, too, but to simplify life we plan to provide precompiled DLL files soon for Windows, cross-compiled using [MinGW](https://en.wikipedia.org/wiki/MinGW). 26 | 27 | Usage 28 | ----- 29 | 30 | ### I included your header file, but the compiler still complains 31 | 32 | You need to link to the NLopt library in addition to doing `#include `. On Unix, this means adding `-lnlopt` `-lm` at the *end* of your link command. 33 | 34 | ### It's not converging 35 | 36 | The most common cause of convergence problems is if you use a gradient-based algorithm and return an incorrect gradient. It seems surprisingly easy to have bugs in code to compute derivatives—you should test it by comparing to finite-difference derivatives for a few random points and directions. 37 | 38 | You could also try switching algorithms to see if it is a problem in a particular algorithm (but check the gradient first!). 39 | 40 | 41 | -------------------------------------------------------------------------------- /doc/docs/NLopt_License_and_Copyright.md: -------------------------------------------------------------------------------- 1 | --- 2 | # NLopt License and Copyright 3 | --- 4 | 5 | The NLopt library is under the [GNU Lesser General Public License](https://en.wikipedia.org/wiki/GNU_Lesser_General_Public_License) (LGPL), and the copyrights are owned by a variety of authors. 6 | 7 | In general, the different code in NLopt comes from different sources, and have a variety of licenses. For example, all of the *new* code we wrote for NLopt is under the [MIT License](https://en.wikipedia.org/wiki/MIT_License) (which imposes essentially no practical restrictions, and is hence compatible with the LGPL). The *combination* of all of this software is under the conjunction of the license terms, and in particular they are limited by the most restrictive of the licenses for the portions, which in this case is the LGPL. (The other portions of NLopt are under LGPL-compatible, less-restrictive licenses like the MIT license. So, if you remove the LGPL portions, which are currently code [by Ladislav Luksan](http://www.cs.cas.cz/~luksan/subroutines.html), the remainder reverts to a looser license.) 8 | 9 | New code written for NLopt, including tutorial code at [NLopt Tutorial](NLopt_Tutorial.md), is Copyright © 2007-2020 Massachusetts Institute of Technology and other authors, and comes with **no warranty**: 10 | 11 | **THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.** 12 | 13 | Please [cite NLopt](Citing_NLopt.md) and the authors of the respective algorithms in any publication for which you find it useful. (This is not a legal requirement, just a polite request.) 14 | 15 | 16 | -------------------------------------------------------------------------------- /doc/docs/NLopt_R_Reference.md: -------------------------------------------------------------------------------- 1 | --- 2 | # NLopt R Reference 3 | --- 4 | 5 | An NLopt interface for [GNU R](https://en.wikipedia.org/wiki/GNU_R) was developed by [Jelmer Ypma](https://www.linkedin.com/in/jelmerypma/) when he was at [University College London](https://en.wikipedia.org/wiki/University_College_London) (UCL), and is currently available as a separate download (with documentation) from: 6 | 7 | - Stable: [https://CRAN.R-project.org/package=nloptr](https://CRAN.R-project.org/package=nloptr) 8 | - Development: [https://github.com/jyypma/nloptr](https://github.com/jyypma/nloptr) 9 | 10 | -------------------------------------------------------------------------------- /doc/docs/NLopt_manual.md: -------------------------------------------------------------------------------- 1 | --- 2 | # NLopt manual 3 | --- 4 | 5 | Welcome to the manual for [NLopt](index.md), our nonlinear optimization library. The manual is divided into a the following sections: 6 | 7 | - [NLopt Introduction](NLopt_Introduction.md) — overview of the library and the problems that it solves 8 | - [NLopt Installation](NLopt_Installation.md) — installation instructions 9 | - [NLopt Tutorial](NLopt_Tutorial.md) — some simple examples in C, Fortran, and Octave/Matlab 10 | - [NLopt Reference](NLopt_Reference.md) — reference manual, listing the NLopt API functions 11 | - [NLopt Algorithms](NLopt_Algorithms.md) — the optimization algorithms available in NLopt (including literature citations and links to original source code, where available) 12 | - [NLopt License and Copyright](NLopt_License_and_Copyright.md) — legal information 13 | 14 | 15 | -------------------------------------------------------------------------------- /doc/docs/NLopt_on_Windows.md: -------------------------------------------------------------------------------- 1 | --- 2 | # NLopt on Windows 3 | --- 4 | 5 | NLopt on Windows 6 | ---------------- 7 | 8 | NLopt works fine on Microsoft Windows computers, and you can compile it directly using the included [CMake](https://en.wikipedia.org/wiki/CMake) build scripts. 9 | 10 | To simplify installation, there are also precompiled 32-bit and 64-bit Windows [DLLs](https://en.wikipedia.org/wiki/Dynamic-link_library) (along with binaries for many other systems) at [NLoptBuilder/releases](https://github.com/stevengj/NLoptBuilder/releases). In particular, the Windows builds are 11 | 12 | - [NLopt.v2.6.2.i686-w64-mingw32.tar.gz](https://github.com/stevengj/NLoptBuilder/releases/download/v2.6.2/NLopt.v2.6.2.i686-w64-mingw32.tar.gz) (32-bit) 13 | - [NLopt.v2.6.2.x86_64-w64-mingw32.tar.gz](https://github.com/stevengj/NLoptBuilder/releases/download/v2.6.2/NLopt.v2.6.2.x86_64-w64-mingw32.tar.gz) (64-bit) 14 | 15 | These `.tar.gz` files unpack (with a variety of Windows software, e.g. 7-zip) into a folder with a `bin` subdirectory that contains `libnlopt.dll`. To link with this in your compiler, you will typically also want the [import library](https://stackoverflow.com/questions/3573475/how-does-the-import-library-work-details) for the DLL, which can be found in the `lib` subdirectory and is called `libnlopt.dll.a` (this can be used similarly to the `.lib` files you may be used to). See, in particular, [these instructions for nlopt](https://www.mathworks.com/matlabcentral/answers/380072-mex-error-undefined-reference#answer_356517). 16 | 17 | If you need a step-by-step guide on how to install NLopt and run the [tutorial](https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial/) you might be interested in [this blog post](https://medium.com/@gabrielr.domingos/installing-nlopt-on-windows-and-using-it-with-c-3e84d91c8960). 18 | 19 | Unofficial Python binaries for Windows are available from Christoph Gohike: 20 | 21 | - [Python binaries for 64-bit Windows](http://www.lfd.uci.edu/~gohlke/pythonlibs/#nlopt) 22 | 23 | ### NLopt with MinGW 24 | 25 | If you want to compile NLopt on Windows with [MinGW](https://www.mingw-w64.org/), be sure to install the MinGW version of `cmake` (e.g. with `pacman -S mingw-w64-x86_64-cmake`) and then build via `cmake -G"MSYS Makefiles" . && make` in order to ensure that `cmake` produces the correct type of makefile. 26 | 27 | ### Octave plugin 28 | 29 | To build the NLopt plugin for [GNU Octave](https://en.wikipedia.org/wiki/GNU_Octave) (a free Matlab clone, which uses the [same NLopt interface as in Matlab](NLopt_Matlab_Reference.md)), you will need the following additional steps. (See [Octave for Windows](https://wiki.octave.org/Octave_for_Microsoft_Windows) on the Octave web page to download Octave.) 30 | 31 | 1. First, download the `.dll` and import library (`.dll.a`) from above. 32 | 2. Download [`nlopt_optimize-oct.cc`](https://github.com/stevengj/nlopt/raw/master/src/octave/nlopt_optimize-oct.cc) and put it in the same directory as the `.dll` and `.dll.a` files. 33 | 3. Compile the Octave plugin (`.oct` file) with `mkoctfile -lnlopt --output nlopt_optimize.oct nlopt_optimize-oct.cc` (`mkoctfile` is a program included with Octave). 34 | 4. Finally, move `libnlopt.dll` to the *octave*``bin` directory (the location of `octave.exe`) so that Octave can find it. 35 | -------------------------------------------------------------------------------- /doc/docs/images/NLopt-example-constraints.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevengj/nlopt/019f61ac7253a537760d9cdd9febd927ec97320c/doc/docs/images/NLopt-example-constraints.png -------------------------------------------------------------------------------- /doc/docs/images/Nlopt-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevengj/nlopt/019f61ac7253a537760d9cdd9febd927ec97320c/doc/docs/images/Nlopt-logo.png -------------------------------------------------------------------------------- /doc/requirements.txt: -------------------------------------------------------------------------------- 1 | python-markdown-math 2 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: NLopt Documentation 2 | site_author: S. G. Johnson et al. 3 | repo_url: https://github.com/stevengj/nlopt/ 4 | edit_uri: edit/master/doc/docs 5 | 6 | docs_dir: 'doc/docs' 7 | site_dir: 'doc/site' 8 | 9 | theme: readthedocs 10 | 11 | markdown_extensions: 12 | - wikilinks 13 | - toc: 14 | title: Table of Contents 15 | - attr_list 16 | - fenced_code 17 | - mdx_math: 18 | enable_dollar_delimiter: True 19 | 20 | extra_javascript: ['https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML','mathjaxhelper.js'] 21 | 22 | nav: 23 | - NLopt: 24 | - Overview: index.md 25 | - FAQ: NLopt_FAQ.md 26 | - NLopt manual: 27 | - NLopt manual: NLopt_manual.md 28 | - Introduction: NLopt_Introduction.md 29 | - Installation: NLopt_Installation.md 30 | - Tutorial: NLopt_Tutorial.md 31 | - NLopt reference: 32 | - General reference: NLopt_Reference.md 33 | - C++ reference: NLopt_C-plus-plus_Reference.md 34 | - Fortran reference: NLopt_Fortran_Reference.md 35 | - Matlab reference: NLopt_Matlab_Reference.md 36 | - Python reference: NLopt_Python_Reference.md 37 | - Guile reference: NLopt_Guile_Reference.md 38 | - R reference: NLopt_R_Reference.md 39 | - Java reference: NLopt_Java_Reference.md 40 | - Deprecated API reference: NLopt_Deprecated_API_Reference.md 41 | - NLopt algorithms: 42 | - NLopt algorithms: NLopt_Algorithms.md 43 | - NLopt on Windows: NLopt_on_Windows.md 44 | - License and Copyright: NLopt_License_and_Copyright.md 45 | - Citing NLopt: Citing_NLopt.md 46 | -------------------------------------------------------------------------------- /nlopt.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=${prefix} 3 | libdir=${exec_prefix}/@NLOPT_INSTALL_LIBDIR@ 4 | includedir=${prefix}/include 5 | 6 | Name: NLopt 7 | Description: nonlinear optimization library 8 | Version: @NLOPT_VERSION_STRING@ 9 | Libs: -L${libdir} -lnlopt 10 | Libs.private: @LIBS_PRIVATE@ 11 | Cflags: -I${includedir} 12 | 13 | -------------------------------------------------------------------------------- /src/algs/ags/COPYRIGHT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Sovrasov V. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/algs/ags/README: -------------------------------------------------------------------------------- 1 | An implementation of the algorithm AGS to solve constrained nonlinear 2 | programming problems with Lipschitzian functions. AGS was introduced 3 | by prof. R.G. Strongin (see R. G. Strongin, D. L. Markin, 4 | ,Minimization of multiextremal functions under nonconvex constraints, 5 | Cybernetics 22(4), 486-493. Translated from Russian. Consultant 6 | Bureau. New York, 1986.). The method exploits Peano-type curve to 7 | reduce dimension of the source bounded multidimensional constrained 8 | NLP problem and then solves a univariate one. 9 | 10 | AGS is proven to converge to a global optima if all objectives and 11 | constraints satisfy Lipschitz condition in a given hyperrectangle, the 12 | reliability parameter r is large enough and accuracy parameter eps is 13 | zero. 14 | 15 | Contributed to NLopt by Vladislav Sovrasov from 16 | 17 | https://github.com/sovrasov/glob_search_nlp_solver -------------------------------------------------------------------------------- /src/algs/ags/ags.h: -------------------------------------------------------------------------------- 1 | /* A C-callable front-end to the AGS global-optimization library. 2 | -- Vladislav Sovrasov */ 3 | 4 | #ifndef AGS_H 5 | #define AGS_H 6 | 7 | #include "nlopt-util.h" 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | /* The algorithm supports 3 types of stop criterions: stop by execution time, stop by value and stop by exceeding limit of iterations. */ 14 | 15 | int ags_minimize(unsigned n, nlopt_func func, void *data, unsigned m, nlopt_constraint *fc, 16 | double *x, double *minf, const double *l, const double *u, nlopt_stopping *stop); 17 | 18 | extern double ags_eps; /* method tolerance in Holder metric on 1d interval. Less value -- better search precision, less probability of early stop. */ 19 | extern double ags_r; /* reliability parameter. Higher value of r -- slower convergence, higher chance to cache the global minima. */ 20 | extern double eps_res; /* parameter which prevents method from paying too much attention to constraints. Greater values of this parameter speed up convergence, 21 | but global minima can be lost. */ 22 | extern unsigned evolvent_density; /* density of evolvent. By default density is 2^-12 on hybercube [0,1]^N, 23 | which means that maximum search accuracyis 2^-12. If search hypercube is large the density 24 | can be increased accordingly to achieve better accuracy. */ 25 | extern int ags_refine_loc; /* refine the final optimum using built-in local optimizer */ 26 | extern int ags_verbose; /* print additional info */ 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/algs/ags/data_types.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2018 Sovrasov V. - All Rights Reserved 3 | * You may use, distribute and modify this code under the 4 | * terms of the MIT license. 5 | * You should have received a copy of the MIT license with 6 | * this file. If not visit https://opensource.org/licenses/MIT 7 | */ 8 | #pragma once 9 | 10 | #include 11 | #include 12 | 13 | #define NLP_SOLVER_ERROR(msg) throw std::runtime_error(std::string(msg)) 14 | #define NLP_SOLVER_ASSERT(expr, msg) if(!(expr)) NLP_SOLVER_ERROR(msg) 15 | 16 | namespace ags 17 | { 18 | 19 | const unsigned solverMaxDim = 10; 20 | const unsigned solverMaxConstraints = 10; 21 | 22 | template 23 | class IGOProblem 24 | { 25 | public: 26 | ~IGOProblem() {} 27 | 28 | virtual fptype Calculate(const fptype* y, int fNumber) const = 0; 29 | virtual int GetConstraintsNumber() const = 0; 30 | virtual int GetDimension() const = 0; 31 | virtual void GetBounds(fptype* left, fptype* right) const = 0; 32 | virtual int GetOptimumPoint(fptype* y) const = 0; 33 | virtual fptype GetOptimumValue() const = 0 ; 34 | }; 35 | 36 | struct Trial 37 | { 38 | double x; 39 | double y[solverMaxDim]; 40 | double g[solverMaxConstraints + 1]; 41 | int idx; 42 | Trial() {} 43 | Trial(double _x) : x(_x) {} 44 | }; 45 | 46 | struct Interval 47 | { 48 | Trial pl; 49 | Trial pr; 50 | double R; 51 | double delta; 52 | Interval() {} 53 | Interval(const Trial& _pl, const Trial& _pr) : pl(_pl), pr(_pr) {} 54 | }; 55 | 56 | struct CompareIntervals 57 | { 58 | bool operator() (const Interval* i1, const Interval* i2) const 59 | { 60 | return i1->pl.x < i2->pl.x; 61 | } 62 | }; 63 | 64 | class CompareByR 65 | { 66 | public: 67 | bool operator() (const Interval* i1, const Interval* i2) const 68 | { 69 | return i1->R < i2->R; 70 | } 71 | }; 72 | 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/algs/ags/evolvent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2018 Sovrasov V. - All Rights Reserved 3 | * You may use, distribute and modify this code under the 4 | * terms of the MIT license. 5 | * You should have received a copy of the MIT license with 6 | * this file. If not visit https://opensource.org/licenses/MIT 7 | */ 8 | #pragma once 9 | 10 | #include 11 | 12 | namespace ags 13 | { 14 | 15 | class Evolvent 16 | { 17 | protected: 18 | int mDimension; 19 | int mTightness; 20 | 21 | std::vector mRho; 22 | std::vector mShiftScalars; 23 | 24 | void TransformToStandardCube(const double *y, double *z); 25 | void TransformToSearchDomain(const double *y, double *z); 26 | 27 | bool mIsInitialized; 28 | 29 | public: 30 | Evolvent(); 31 | Evolvent(int dimension, int tightness, const double* lb, const double* ub); 32 | ~Evolvent(); 33 | 34 | virtual void GetImage(double x, double y[]); 35 | }; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/algs/ags/local_optimizer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2018 Sovrasov V. - All Rights Reserved 3 | * You may use, distribute and modify this code under the 4 | * terms of the MIT license. 5 | * You should have received a copy of the MIT license with 6 | * this file. If not visit https://opensource.org/licenses/MIT 7 | */ 8 | #pragma once 9 | 10 | #include "data_types.hpp" 11 | 12 | #include 13 | #include 14 | 15 | namespace ags 16 | { 17 | 18 | class HookeJeevesOptimizer 19 | { 20 | private: 21 | double mEps; 22 | double mStep; 23 | double mStepMultiplier; 24 | 25 | mutable std::vector mTrialsCounters; 26 | 27 | std::shared_ptr> mProblem; 28 | 29 | Trial mCurrentPoint; 30 | Trial mStartPoint; 31 | Trial mCurrentResearchDirection; 32 | Trial mPreviousResearchDirection; 33 | 34 | void DoStep(); 35 | double ComputeObjective(const double* x) const; 36 | double MakeResearch(double*); 37 | 38 | public: 39 | void SetParameters(double eps, double step, double stepMult); 40 | Trial Optimize(std::shared_ptr> problem, 41 | const Trial& startPoint, std::vector& trialsCounters); 42 | }; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/algs/ags/solver.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2018 Sovrasov V. - All Rights Reserved 3 | * You may use, distribute and modify this code under the 4 | * terms of the MIT license. 5 | * You should have received a copy of the MIT license with 6 | * this file. If not visit https://opensource.org/licenses/MIT 7 | */ 8 | #pragma once 9 | 10 | #include "data_types.hpp" 11 | #include "evolvent.hpp" 12 | #include "local_optimizer.hpp" 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace ags 22 | { 23 | 24 | struct SolverParameters 25 | { 26 | double eps = 0.01; //method tolerance in Holder metric on 1d interval. Less value -- better search precision, less probability of early stop. 27 | double stopVal = std::numeric_limits::lowest(); //method stops after objective becomes less than this value 28 | double r = 3; //reliability parameter. Higher value of r -- slower convergence, higher chance to cache the global minima. 29 | unsigned numPoints = 1; //number of new points per iteration. > 1 is useless in current implementation. 30 | unsigned itersLimit = 20000; // max number of iterations. 31 | unsigned evolventDensity = 12; // density of evolvent. By default density is 2^-12 on hybercube [0,1]^N, 32 | // which means that maximum search accuracyis 2^-12. If search hypercube is large the density can be increased accordingly to achieve better accuracy. 33 | double epsR = 0.001; // parameter which prevents method from paying too much attention to constraints. Greater values of this parameter speed up convergence, 34 | // but global minima can be lost. 35 | bool refineSolution = false; //if true, the fibal solution will be refined with the HookJeves method. 36 | 37 | SolverParameters() {} 38 | SolverParameters(double _eps, double _r, 39 | double epsR_, unsigned _trialsLimit) : 40 | eps(_eps), r(_r), itersLimit(_trialsLimit), epsR(epsR_) 41 | {} 42 | }; 43 | 44 | class NLPSolver 45 | { 46 | protected: 47 | using PriorityQueue = 48 | std::priority_queue, CompareByR>; 49 | 50 | HookeJeevesOptimizer mLocalOptimizer; 51 | 52 | SolverParameters mParameters; 53 | std::shared_ptr> mProblem; 54 | Evolvent mEvolvent; 55 | 56 | std::vector mHEstimations; 57 | std::vector mZEstimations; 58 | std::vector mNextPoints; 59 | PriorityQueue mQueue; 60 | std::set mSearchInformation; 61 | std::vector mNextIntervals; 62 | Trial mOptimumEstimation; 63 | 64 | std::vector mCalculationsCounters; 65 | unsigned mIterationsCounter; 66 | bool mNeedRefillQueue; 67 | bool mNeedStop; 68 | double mMinDelta; 69 | int mMaxIdx; 70 | 71 | void InitLocalOptimizer(); 72 | void FirstIteration(); 73 | void MakeTrials(); 74 | void InsertIntervals(); 75 | void CalculateNextPoints(); 76 | void RefillQueue(); 77 | void EstimateOptimum(); 78 | 79 | void InitDataStructures(); 80 | void ClearDataStructures(); 81 | 82 | void UpdateAllH(std::set::iterator); 83 | void UpdateH(double newValue, int index); 84 | double CalculateR(Interval*) const; 85 | double GetNextPointCoordinate(Interval*) const; 86 | 87 | public: 88 | using FuncPtr = std::function; 89 | NLPSolver(); 90 | 91 | void SetParameters(const SolverParameters& params); 92 | void SetProblem(std::shared_ptr> problem); 93 | void SetProblem(const std::vector& functions, 94 | const std::vector& leftBound, const std::vector& rightBound); 95 | 96 | Trial Solve(std::function externalStopFunc); 97 | Trial Solve(); 98 | std::vector GetCalculationsStatistics() const; 99 | std::vector GetHolderConstantsEstimations() const; 100 | }; 101 | 102 | namespace solver_utils 103 | { 104 | bool checkVectorsDiff(const double* y1, const double* y2, size_t dim, double eps); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/algs/ags/tst.cc: -------------------------------------------------------------------------------- 1 | #define _USE_MATH_DEFINES 2 | #include 3 | #include 4 | #include 5 | 6 | #include "nlopt.hpp" 7 | 8 | double f_obj(const std::vector& x, std::vector&, void*) 9 | { 10 | return -1.5*pow(x[0], 2) * exp(1 - pow(x[0], 2) 11 | - 20.25*pow(x[0] - x[1], 2)) - pow(0.5 * (x[1] - 1)*(x[0]- 1), 4) 12 | * exp(2 - pow(0.5 * (x[0] - 1), 4) - pow(x[1] - 1, 4)); 13 | } 14 | 15 | double f_c0(const std::vector& x, std::vector&, void*) 16 | { 17 | return 0.01*(pow(x[0] - 2.2, 2) + pow(x[1] - 1.2, 2) - 2.25); 18 | } 19 | double f_c1(const std::vector& x, std::vector&, void*) 20 | { 21 | return 100 * (1 - pow(x[0] - 2, 2) / 1.44 - pow(0.5*x[1], 2)); 22 | } 23 | double f_c2(const std::vector& x, std::vector&, void*) 24 | { 25 | return 10 * (x[1] - 1.5 - 1.5*sin(2*M_PI*(x[0] - 1.75))); 26 | } 27 | 28 | int ags_verbose = 1; 29 | double ags_eps = 0.001; 30 | double eps_res = 0.1; 31 | 32 | int main(int argc, char **argv) 33 | { 34 | nlopt::opt opt(nlopt::GN_AGS, 2); 35 | 36 | opt.set_lower_bounds({0, -1}); 37 | opt.set_upper_bounds({4, 3}); 38 | 39 | opt.add_inequality_constraint(f_c0, NULL, 0); 40 | opt.add_inequality_constraint(f_c1, NULL, 0); 41 | opt.add_inequality_constraint(f_c2, NULL, 0); 42 | opt.set_min_objective(f_obj, NULL); 43 | opt.set_maxeval(2000); 44 | 45 | double minf; 46 | std::vector x(2); 47 | 48 | try { 49 | nlopt::result result = opt.optimize(x, minf); 50 | std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = " 51 | << std::setprecision(10) << minf << std::endl; 52 | } 53 | catch(std::exception &e) { 54 | std::cout << "nlopt failed: " << e.what() << std::endl; 55 | } 56 | 57 | return EXIT_SUCCESS; 58 | } 59 | -------------------------------------------------------------------------------- /src/algs/auglag/README: -------------------------------------------------------------------------------- 1 | This directory contains my implementation of the "augmented Lagrangian" 2 | method to express constrained optimization problems (including equality 3 | constraints) in terms of unconstrained optimization problems (or just 4 | inequality constraints, or just box constraints). 5 | 6 | I used the algorithm description (but no source code) from the outline 7 | in the paper: 8 | 9 | E. G. Birgin and J. M. Martinez, "Improving ultimate convergence 10 | of an augmented Lagrangian method," Optimization Methods and 11 | Software vol. 23, no. 2, p. 177-195 (2008). 12 | 13 | http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.72.6121 14 | 15 | (The authors of this paper have their own free-software implementation 16 | of this idea and its variations, in the TANGO project: 17 | http://www.ime.usp.br/~egbirgin/tango/ ....I did *not* use any code 18 | from TANGO here, or even look at the TANGO code. TANGO is GPLed, and 19 | I'm trying to keep NLopt at most LGPLed.) 20 | 21 | The code in this directory is under the same MIT license as the rest 22 | of my code in NLopt (see ../COPYRIGHT). 23 | 24 | Steven G. Johnson 25 | November 2008 26 | -------------------------------------------------------------------------------- /src/algs/auglag/auglag.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef AUGLAG_H 24 | #define AUGLAG_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | extern int auglag_verbose; 35 | 36 | nlopt_result auglag_minimize(int n, nlopt_func f, void *f_data, 37 | int m, nlopt_constraint *fc, 38 | int p, nlopt_constraint *h, 39 | const double *lb, const double *ub, /* bounds */ 40 | double *x, /* in: initial guess, out: minimizer */ 41 | double *minf, 42 | nlopt_stopping *stop, 43 | nlopt_opt sub_opt, int sub_has_fc); 44 | 45 | #ifdef __cplusplus 46 | } /* extern "C" */ 47 | #endif /* __cplusplus */ 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /src/algs/bobyqa/COPYRIGHT: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 M. J. D. Powell (mjdp@cam.ac.uk) 2 | * Modifications Copyright (c) 2010 Massachusetts Institute of Technology 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | 24 | -------------------------------------------------------------------------------- /src/algs/bobyqa/README: -------------------------------------------------------------------------------- 1 | This is the BOBYQA software by M. J. D. Powell, which performs 2 | derivative-free unconstrained optimization using an iteratively 3 | constructed quadratic approximation for the objective function. See: 4 | 5 | M. J. D. Powell, "The BOBYQA algorithm for bound constrained 6 | optimization without derivatives," Department of Applied 7 | Mathematics and Theoretical Physics, Cambridge England, 8 | technical report NA2009/06 (2009). 9 | 10 | http://www.damtp.cam.ac.uk/user/na/NA_papers/NA2009_06.pdf 11 | http://plato.asu.edu/ftp/other_software/bobyqa.zip 12 | 13 | The C translation by S. G. Johnson (2009) includes a few minor 14 | modifications, mainly to use the NLopt stopping criteria (and to 15 | take the objective function as an argument rather than a global). 16 | 17 | The original Fortran code was released by Powell with "no restrictions 18 | or charges", and the C translation by S. G. Johnson is released in a 19 | similar spirit under the MIT License (see the COPYRIGHT file in this 20 | directory). 21 | -------------------------------------------------------------------------------- /src/algs/bobyqa/README.orig: -------------------------------------------------------------------------------- 1 | =========================================================================== 2 | On 8/13/09 added the paper on BOBYQA 3 | =========================================================================== 4 | 5 | For simplicity, the Makefi;e has been replaced by a one-line compile 6 | script "comp" which needs to be adjusted if the compiler name is not 7 | f77. All Fortran files are in bobyla.f. Compiling and running bobyqa 8 | should produce results similar to those in RESULTS. 9 | Hans Mittelmann, Jan 2009 10 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 11 | 12 | The Fortran version of BOBYQA is attached. Its purpose is to seek 13 | the least value of a function F of several variables, when derivatives 14 | are not available, where F is specified by the user through a subroutine 15 | called CALFUN. The name BOBYQA denotes Bound Approximation BY Quadratic 16 | Approximation, the constraints being lower and upper bounds on every 17 | variable, which can be set to huge values for unconstrained variables. 18 | The algorithm is intended to change the variables to values that are close 19 | to a local minimum of F. The user, however, should assume responsibility for 20 | finding out if the calculations are satisfactory, by considering carefully 21 | the values of F that occur. The BOBYQA software has been developed from the 22 | method of the paper "The NEWUOA software for unconstrained minimization 23 | without derivatives", in Large-Scale Nonlinear Optimization, editors G. Di 24 | Pillo and M. Roma, Springer (2006), pages 255-297. A report that describes 25 | the details of the development is going to be written soon. 26 | 27 | The attachments in sequence are a suitable Makefile, followed by a main 28 | program and a CALFUN routine for the "Invdist2" problem, in order to provide 29 | an example for testing. Then BOBYQA and its six auxiliary routines, namely 30 | BOBYQB, ALTMOV, PRELIM, RESCUE, TRSBOX and UPDATE, are given. Finally, the 31 | computed output that the author obtained for the Invdist2 problems is listed. 32 | 33 | In addition to providing CALFUN, an initial vector of variables and 34 | the lower and upper bounds, the user has to set the values of the parameters 35 | RHOBEG, RHOEND and NPT. After scaling the individual variables if necessary, 36 | so that the magnitudes of their expected changes are similar, RHOBEG is the 37 | initial steplength for changes to the variables, a reasonable choice being 38 | the mesh size of a coarse grid search. Further, RHOEND should be suitable for 39 | a search on a very fine grid. Typically, the software calculates a vector 40 | of variables that is within distance 10*RHOEND of a local minimum. Another 41 | consideration is that every trial vector of variables is forced to satisfy 42 | the lower and upper bounds, but there has to be room to make a search in all 43 | directions. Therefore an error return occurs if the difference between the 44 | bounds on any variable is less than 2*RHOBEG. The parameter NPT specifies 45 | the number of interpolation conditions on each quadratic model, the value 46 | NPT=2*N+1 being recommended for a start, where N is the number of variables. 47 | It is often worthwhile to try other choices too, but much larger values tend 48 | to be inefficient, because the amount of routine work of each iteration is 49 | of magnitude NPT**2, and because the achievement of adequate accuracy in some 50 | matrix calculations becomes more difficult. Some excellent numerical results 51 | have been found in the case NPT=N+6 even with more than 100 variables. 52 | 53 | The way of calling BOBYQA should be clear from the Invdist2 examples 54 | and from the comments near the beginning of SUBROUTINE BOBYQA. There are no 55 | restrictions on or charges for the use of the software. I hope that the time 56 | and effort I have spent on developing the package will be helpful to much 57 | research and to many applications. 58 | 59 | January 5th, 2009 M.J.D. Powell (mjdp@cam.ac.uk) 60 | 61 | -------------------------------------------------------------------------------- /src/algs/bobyqa/bobyqa.h: -------------------------------------------------------------------------------- 1 | #ifndef BOBYQA_H 2 | #define BOBYQA_H 1 3 | 4 | #include "nlopt-util.h" 5 | #include "nlopt.h" 6 | 7 | extern nlopt_result bobyqa(int n, int npt, double *x, 8 | const double *lb, const double *ub, 9 | const double *dx, 10 | nlopt_stopping *stop, double *minf, 11 | nlopt_func f, void *f_data); 12 | 13 | #endif /* BOBYQA_H */ 14 | -------------------------------------------------------------------------------- /src/algs/cdirect/README: -------------------------------------------------------------------------------- 1 | From-scratch re-implementation of the DIRECT and DIRECT-L algorithms 2 | described in: 3 | 4 | D. R. Jones, C. D. Perttunen, and B. E. Stuckmann, 5 | "Lipschitzian optimization without the lipschitz constant," 6 | J. Optimization Theory and Applications, vol. 79, p. 157 (1993). 7 | 8 | J. M. Gablonsky and C. T. Kelley, "A locally-biased form 9 | of the DIRECT algorithm," J. Global Optimization 21 (1), 10 | p. 27-37 (2001). 11 | 12 | I re-implemented the algorithms for a couple of reasons. First, 13 | because I was interested in the algorithms and wanted to play with 14 | them by trying some variations (originally, because I wanted to 15 | experiment with a hybrid approach combining DIRECT with local search 16 | algorithms, see hybrid.c). Second, I wanted to remove some arbitrary 17 | restrictions in the original Fortran code, e.g. a fixed upper bound on 18 | the number of function evaluations. Third, because it was fun to 19 | code. As far as I can tell, my version converges in about the same 20 | number of iterations as Gablonsky's code (with occasional slight 21 | differences due to minor differences in how I break ties, etc.). 22 | 23 | The code in this directory is under the same MIT license as the rest 24 | of my code in NLopt (see ../COPYRIGHT). 25 | 26 | Steven G. Johnson 27 | -------------------------------------------------------------------------------- /src/algs/cdirect/cdirect.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef CDIRECT_H 24 | #define CDIRECT_H 25 | 26 | #include "nlopt-util.h" 27 | #include "nlopt.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | extern nlopt_result cdirect_unscaled(int n, nlopt_func f, void *f_data, 35 | const double *lb, const double *ub, 36 | double *x, 37 | double *minf, 38 | nlopt_stopping *stop, 39 | double magic_eps, int which_alg); 40 | 41 | extern nlopt_result cdirect(int n, nlopt_func f, void *f_data, 42 | const double *lb, const double *ub, 43 | double *x, 44 | double *minf, 45 | nlopt_stopping *stop, 46 | double magic_eps, int which_alg); 47 | 48 | extern nlopt_result cdirect_hybrid(int n, nlopt_func f, void *f_data, 49 | const double *lb, const double *ub, 50 | double *x, 51 | double *minf, 52 | nlopt_stopping *stop, 53 | nlopt_algorithm local_alg, 54 | int local_maxeval, 55 | int randomized_div); 56 | 57 | extern nlopt_result cdirect_hybrid_unscaled(int n, nlopt_func f, void *f_data, 58 | const double *lb, const double *ub, 59 | double *x, 60 | double *minf, 61 | nlopt_stopping *stop, 62 | nlopt_algorithm local_alg, 63 | int local_maxeval, 64 | int randomized_div); 65 | 66 | /* internal routines and data structures: */ 67 | extern int cdirect_hyperrect_compare(double *a, double *b); 68 | typedef struct { 69 | nlopt_func f; 70 | void *f_data; 71 | double *x; 72 | const double *lb, *ub; 73 | } cdirect_uf_data; 74 | extern double cdirect_uf(unsigned n, const double *xu, double *grad, void *d_); 75 | 76 | #ifdef __cplusplus 77 | } /* extern "C" */ 78 | #endif /* __cplusplus */ 79 | 80 | #endif /* DIRECT_H */ 81 | -------------------------------------------------------------------------------- /src/algs/cobyla/COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright (c) 1992, Michael J. D. Powell (M.J.D.Powell@damtp.cam.ac.uk) 2 | Copyright (c) 2004, Jean-Sebastien Roy (js@jeannot.org) 3 | Copyright (c) 2008, Steven G. Johnson (stevenj@alum.mit.edu) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a 6 | copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included 14 | in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /src/algs/cobyla/README: -------------------------------------------------------------------------------- 1 | This code implements COBYLA (Constrained Optimization BY Linear 2 | Approximations) algorithm derivative free optimization with nonlinear 3 | inequality constraints by M. J. D. Powell, described by: 4 | 5 | M. J. D. Powell, "A direct search optimization method that 6 | models the objective and constraint functions by linear 7 | interpolation," in Advances in Optimization and Numerical 8 | Analysis, eds. S. Gomez and J.-P. Hennart (Kluwer Academic: 9 | Dordrecht, 1994), p. 51-67. 10 | 11 | and reviewed in: 12 | 13 | M. J. D. Powell, "Direct search algorithms for optimization 14 | calculations," Acta Numerica 7, 287-336 (1998). 15 | 16 | It constructs successive linear approximations of the objective 17 | function and constraints via a simplex of n+1 points (in n 18 | dimensions), and optimizes these approximations in a trust region at 19 | each step. 20 | 21 | The original code itself was written in Fortran by Powell, and 22 | apparently released without restrictions (like several of his other 23 | programs), and was converted to C in 2004 by Jean-Sebastien Roy 24 | (js@jeannot.org) for the SciPy project. The C version was released 25 | under the attached license (basically the MIT license) at: 26 | http://www.jeannot.org/~js/code/index.en.html#COBYLA 27 | 28 | It was incorporated into NLopt in 2008 by S. G. Johnson, and kept under 29 | the same MIT license. In incorporating it into NLopt, SGJ adapted it 30 | to include the NLopt stopping conditions (the original code provided 31 | an x tolerance and a maximum number of function evaluations only). 32 | 33 | The original COBYLA did not have explicit support for bound 34 | constraints; these are included as linear constraints along with any 35 | other nonlinear constraints. This is mostly fine---linear constraints 36 | are handled exactly by COBYLA's linear approximations. However, 37 | occasionally COBYLA takes a "simplex" step, either to create the 38 | initial simplex or to fix a degenerate simplex, and these steps could 39 | violate the bound constraints. SGJ modified COBYLA to explicitly 40 | honor the bound constraints in these cases, so that the 41 | objective/constraints are never evaluated outside of the bound 42 | constraints, without slowing convergence. 43 | -------------------------------------------------------------------------------- /src/algs/cobyla/README.orig: -------------------------------------------------------------------------------- 1 | # COBYLA : constrained optimization by linear approximation 2 | # Version 1.1 3 | # Copyright (c) 1992, Michael J. D. Powell (M.J.D.Powell@damtp.cam.ac.uk) 4 | # Copyright (c) 2004, J.S. Roy (js@jeannot.org) 5 | # See the LICENSE file for copyright information. 6 | # $Jeannot: README,v 1.7 2004/04/18 14:04:20 js Exp $ 7 | 8 | This software is a C version of COBYLA2, a contrained optimization by linear 9 | approximation package developed by Michael J. D. Powell in Fortran. 10 | 11 | The original source code can be found at : 12 | http://plato.la.asu.edu/topics/problems/nlores.html 13 | 14 | Reference article for the method: Powell, J.M.D. (1992), "A Direct Search 15 | Optimization Method that Models the Objective and Constraint Functions by Linear 16 | Interpolation", DAMTP/NA5, Cambridge, England. 17 | 18 | This package was initially built by J.S. Roy to ease integration into SciPy. 19 | See: http://www.scipy.org/ 20 | Many thanks to Michael J. D. Powell for allowing this to happen ! 21 | 22 | This software, a derivative free non-linear optimizer, aims at minimizing the 23 | value of a nonlinear function subject to nonlinear constraints. It requires to 24 | be able to evaluate the function and the value of the constraints. 25 | 26 | COBYLA will try to make all the values of the constraints positive. 27 | So if you want to input a constraint j such as variable x[i] <= MAX, set: 28 | constraint[j] = MAX - x[i] 29 | 30 | See the comments in cobyla.c for more details. 31 | 32 | This software has been converted from the Fortran into C and provides the 33 | following modifications : 34 | - reentrancy, no global variables or functions ; 35 | - ability to pass a pointer to the function to be optimized (to provide 36 | access to constants) ; 37 | - ability to end the minimization at any time ; 38 | And other small changes. 39 | 40 | The last version (and other software) is avalaible at the URL : 41 | http://www.jeannot.org/~js/code/index.en.html 42 | 43 | A Python interface module is also provided. 44 | 45 | Contents : 46 | - cobyla.c : Source 47 | - cobyla.h : Header, and API documentation 48 | - LICENSE : License and copyright information 49 | - HISTORY : Release history 50 | - README : This file 51 | - example.c : A simple example 52 | - Makefile : Make file used to build the examples 53 | - moduleCobyla.c : the source of the python module 54 | - cobyla.py : the python module wrapper 55 | - example.py : an example for the python module 56 | - setup.py : the python installer 57 | 58 | Use is described in cobyla.h. For more information, see the example. 59 | The example can be built and executed by doing : 60 | make test 61 | 62 | You may need to adjust the Makefile before building cobyla. 63 | 64 | To install the module in the current directory, use: 65 | python setup.py build_ext --inplace 66 | To test it, execute: 67 | python cobyla.py 68 | To install it globaly, use: 69 | python setup.py install 70 | 71 | If you make use of this software, or if you make modifications to it (for a 72 | specific platform for example), you are encouraged to contact the author of 73 | this Fortran to C conversion at the following email : js@jeannot.org 74 | Thanks ! 75 | -------------------------------------------------------------------------------- /src/algs/cobyla/cobyla.h: -------------------------------------------------------------------------------- 1 | /* cobyla : contrained optimization by linear approximation */ 2 | 3 | /* 4 | * Copyright (c) 1992, Michael J. D. Powell (M.J.D.Powell@damtp.cam.ac.uk) 5 | * Copyright (c) 2004, Jean-Sebastien Roy (js@jeannot.org) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a 8 | * copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included 16 | * in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | 27 | /* 28 | * This software is a C version of COBYLA2, a contrained optimization by linear 29 | * approximation package developed by Michael J. D. Powell in Fortran. 30 | * 31 | * The original source code can be found at : 32 | * http://plato.la.asu.edu/topics/problems/nlores.html 33 | */ 34 | 35 | /* $Jeannot: cobyla.h,v 1.10 2004/04/18 09:51:37 js Exp $ */ 36 | 37 | #ifndef _COBYLA_ 38 | #define _COBYLA_ 39 | 40 | #include "nlopt.h" 41 | #include "nlopt-util.h" 42 | 43 | #ifdef __cplusplus 44 | extern "C" 45 | { 46 | #endif /* __cplusplus */ 47 | 48 | /* NLopt-style interface function */ 49 | nlopt_result cobyla_minimize(unsigned n, nlopt_func f, void *f_data, 50 | unsigned m, nlopt_constraint *fc, 51 | unsigned p, nlopt_constraint *h, 52 | const double *lb, const double *ub, /* bounds */ 53 | double *x, /* in: initial guess, out: minimizer */ 54 | double *minf, 55 | nlopt_stopping *stop, 56 | const double *dx); 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | #endif /* _COBYLA_ */ 63 | -------------------------------------------------------------------------------- /src/algs/cquad/README: -------------------------------------------------------------------------------- 1 | An experimental method for nonlinearly constrained optimization 2 | without derivatives, by SGJ, using conservative quadratic 3 | approximations. It is based on a combination of two ideas: 4 | 5 | 1) first, quadratic approximations for the function & constraints 6 | are constructed based on the techniques suggested by M. J. D. 7 | Powell for his unconstrained NEWUOA software (2004) [*]. 8 | 9 | 2) second, the quadratic approximation is successively solved 10 | and refined using conservative-approximation inner/outer 11 | iterations based on those of the MMA algorithm of Svanberg (2002). 12 | 13 | It doesn't really work very well yet (it converges extremely slowly), 14 | unfortunately, so I'm keeping it out of NLopt until/unless I have a 15 | chance to think about it yet. 16 | 17 | [*] Actually, we use a greatly simplified version of Powell's 18 | technique. Powell goes to great lengths to ensure that his quadratic 19 | approximation is constructed iteratively with only O(n^2) work at each 20 | step, where n is the number of design variables. Instead, I just use 21 | an O(n^3) method, based on the assumptions that (a) the objective 22 | function is relatively costly and (b) n is not too big (in the 23 | hundreds, not in the thousands) -- if you have thousands of unknowns, 24 | you really need to be using a gradient-based method, I think. 25 | -------------------------------------------------------------------------------- /src/algs/cquad/cquad.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef CQUAD_H 24 | #define CQUAD_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | extern int cquad_verbose; 35 | 36 | nlopt_result cquad_minimize(int n, nlopt_func f, void *f_data, 37 | int m, nlopt_func fc, 38 | void *fc_data_, ptrdiff_t fc_datum_size, 39 | const double *lb, const double *ub, /* bounds */ 40 | double *x, /* in: initial guess, out: minimizer */ 41 | double *minf, 42 | nlopt_stopping *stop, 43 | nlopt_algorithm model_alg, 44 | double dual_tolrel, int dual_maxeval); 45 | 46 | #ifdef __cplusplus 47 | } /* extern "C" */ 48 | #endif /* __cplusplus */ 49 | 50 | #endif 51 | 52 | -------------------------------------------------------------------------------- /src/algs/crs/README: -------------------------------------------------------------------------------- 1 | This is my implementation of the "controlled random search" (CRS2) algorithm 2 | with the "local mutation" modification, as defined by: 3 | 4 | P. Kaelo and M. M. Ali, "Some variants of the controlled random 5 | search algorithm for global optimization," J. Optim. Theory Appl. 6 | 130 (2), 253-264 (2006). 7 | 8 | The original CRS2 algorithm was described by: 9 | 10 | W. L. Price, "A controlled random search procedure for global 11 | optimization," in Towards Global Optimization 2, p. 71-84 12 | edited by L. C. W. Dixon and G. P. Szego (North-Holland Press, 13 | Amsterdam, 1978). 14 | 15 | It is under the same MIT license as the rest of my code in NLopt (see 16 | ../COPYRIGHT). 17 | 18 | Steven G. Johnson 19 | September 2007 20 | -------------------------------------------------------------------------------- /src/algs/crs/crs.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef CRS_H 24 | #define CRS_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | nlopt_result crs_minimize(int n, nlopt_func f, void *f_data, 35 | const double *lb, const double *ub, /* bounds */ 36 | double *x, /* in: initial guess, out: minimizer */ 37 | double *minf, 38 | nlopt_stopping *stop, 39 | int population, /* initial population (0=default) */ 40 | int random); /* random or low-discrepancy seq. */ 41 | 42 | #ifdef __cplusplus 43 | } /* extern "C" */ 44 | #endif /* __cplusplus */ 45 | 46 | #endif 47 | 48 | -------------------------------------------------------------------------------- /src/algs/direct/AUTHORS: -------------------------------------------------------------------------------- 1 | C conversion: Steven G. Johnson (stevenj@alum.mit.edu) 2 | Original Fortran code: Joerg.M.Gablonsky (jmgablon@mailandnews.com) 3 | -------------------------------------------------------------------------------- /src/algs/direct/COPYING: -------------------------------------------------------------------------------- 1 | This code is based on the DIRECT 2.0.4 Fortran code by Gablonsky et al. at 2 | http://www4.ncsu.edu/~ctk/SOFTWARE/DIRECTv204.tar.gz 3 | The C version was initially converted via f2c and then cleaned up and 4 | reorganized by Steven G. Johnson (stevenj@alum.mit.edu), August 2007. 5 | 6 | ******** Copyright and license for the original Fortran DIRECT code ******** 7 | Copyright (c) 1999, 2000, 2001 North Carolina State University 8 | 9 | This program is distributed under the MIT License (see 10 | http://www.opensource.org/licenses/mit-license.php): 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy of 13 | this software and associated documentation files (the "Software"), to deal in 14 | the Software without restriction, including without limitation the rights to 15 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 16 | of the Software, and to permit persons to whom the Software is furnished to do 17 | so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | -------------------------------------------------------------------------------- /src/algs/direct/README: -------------------------------------------------------------------------------- 1 | The DIRECT algorithm (DIviding RECTangles) is a derivative-free global 2 | optimization algorithm invented by Jones et al.: 3 | 4 | D. R. Jones, C. D. Perttunen, and B. E. Stuckmann, 5 | "Lipschitzian optimization without the lipschitz constant," 6 | J. Optimization Theory and Applications, vol. 79, p. 157 (1993). 7 | 8 | This is a deterministic-search algorithm based on systematic division 9 | of the search domain into smaller and smaller hyperrectangles. 10 | 11 | The implementation is based on the 1998-2001 Fortran version by 12 | J. M. Gablonsky at North Carolina State University, converted to C by 13 | Steven G. Johnson. The Fortran source was downloaded from: 14 | 15 | http://www4.ncsu.edu/~ctk/SOFTWARE/DIRECTv204.tar.gz 16 | 17 | Gablonsky et al implemented a modified version of the original DIRECT 18 | algorithm, as described in: 19 | 20 | J. M. Gablonsky and C. T. Kelley, "A locally-biased form 21 | of the DIRECT algorithm," J. Global Optimization 21 (1), 22 | p. 27-37 (2001). 23 | 24 | Both the original Jones algorithm (NLOPT_GN_DIRECT) and the 25 | Gablonsky modified version (NLOPT_GN_DIRECT_L) are implemented 26 | and available from the NLopt interface. The Gablonsky version 27 | makes the algorithm "more biased towards local search" so that it 28 | is more efficient for functions without too many local minima. 29 | 30 | Also, Gablonsky et al. extended the algorithm to handle "hidden 31 | constraints", i.e. arbitrary nonlinear constraints. In NLopt, a 32 | hidden constraint is represented by returning NaN (or Inf, or 33 | HUGE_VAL) from the objective function at any points violating the 34 | constraint. 35 | 36 | Further information on the DIRECT algorithm and Gablonsky's 37 | implementation can be found in the included userguide.pdf file. 38 | -------------------------------------------------------------------------------- /src/algs/direct/direct.h: -------------------------------------------------------------------------------- 1 | #ifndef DIRECT_H 2 | #define DIRECT_H 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif /* __cplusplus */ 11 | 12 | typedef double (*direct_objective_func)(int n, const double *x, 13 | int *undefined_flag, 14 | void *data); 15 | 16 | typedef enum { 17 | DIRECT_ORIGINAL, DIRECT_GABLONSKY 18 | } direct_algorithm; 19 | 20 | typedef enum { 21 | DIRECT_INVALID_BOUNDS = -1, 22 | DIRECT_MAXFEVAL_TOOBIG = -2, 23 | DIRECT_INIT_FAILED = -3, 24 | DIRECT_SAMPLEPOINTS_FAILED = -4, 25 | DIRECT_SAMPLE_FAILED = -5, 26 | DIRECT_MAXFEVAL_EXCEEDED = 1, 27 | DIRECT_MAXITER_EXCEEDED = 2, 28 | DIRECT_GLOBAL_FOUND = 3, 29 | DIRECT_VOLTOL = 4, 30 | DIRECT_SIGMATOL = 5, 31 | DIRECT_MAXTIME_EXCEEDED = 6, 32 | 33 | DIRECT_OUT_OF_MEMORY = -100, 34 | DIRECT_INVALID_ARGS = -101, 35 | DIRECT_FORCED_STOP = -102 36 | } direct_return_code; 37 | 38 | #define DIRECT_UNKNOWN_FGLOBAL (-HUGE_VAL) 39 | #define DIRECT_UNKNOWN_FGLOBAL_RELTOL (0.0) 40 | 41 | extern direct_return_code direct_optimize( 42 | direct_objective_func f, void *f_data, 43 | int dimension, 44 | const double *lower_bounds, const double *upper_bounds, 45 | 46 | double *x, double *minf, 47 | 48 | int max_feval, int max_iter, 49 | double start, double maxtime, 50 | double magic_eps, double magic_eps_abs, 51 | double volume_reltol, double sigma_reltol, 52 | int *force_stop, 53 | 54 | double fglobal, 55 | double fglobal_reltol, 56 | 57 | FILE *logfile, 58 | direct_algorithm algorithm); 59 | 60 | #ifdef __cplusplus 61 | } /* extern "C" */ 62 | #endif /* __cplusplus */ 63 | 64 | #endif /* DIRECT_H */ 65 | -------------------------------------------------------------------------------- /src/algs/direct/direct_wrap.c: -------------------------------------------------------------------------------- 1 | /* C-style API for DIRECT functions. SGJ (August 2007). */ 2 | 3 | #include "direct-internal.h" 4 | 5 | /* Perform global minimization using (Gablonsky implementation of) DIRECT 6 | algorithm. Arguments: 7 | 8 | f, f_data: the objective function and any user data 9 | -- the objective function f(n, x, undefined_flag, data) takes 4 args: 10 | int n: the dimension, same as dimension arg. to direct_optimize 11 | const double *x: array x[n] of point to evaluate 12 | int *undefined_flag: set to 1 on return if x violates constraints 13 | or don't touch otherwise 14 | void *data: same as f_data passed to direct_optimize 15 | return value = value of f(x) 16 | 17 | dimension: the number of minimization variable dimensions 18 | lower_bounds, upper_bounds: arrays of length dimension of variable bounds 19 | 20 | x: an array of length dimension, set to optimum variables upon return 21 | minf: on return, set to minimum f value 22 | 23 | magic_eps, magic_eps_abs: Jones' "magic" epsilon parameter, and 24 | also an absolute version of the same 25 | (not multipled by minf). Jones suggests 26 | setting this to 1e-4, but 0 also works... 27 | 28 | max_feval, max_iter: maximum number of function evaluations & DIRECT iters 29 | volume_reltol: relative tolerance on hypercube volume (0 if none) 30 | sigma_reltol: relative tolerance on hypercube "measure" (??) (0 if none) 31 | 32 | fglobal: the global minimum of f, if known ahead of time 33 | -- this is mainly for benchmarking, in most cases it 34 | is not known and you should pass DIRECT_UNKNOWN_FGLOBAL 35 | fglobal_reltol: relative tolerance on how close we should find fglobal 36 | -- ignored if fglobal is DIRECT_UNKNOWN_FGLOBAL 37 | 38 | logfile: an output file to write diagnostic info to (NULL for no I/O) 39 | 40 | algorithm: whether to use the original DIRECT algorithm (DIRECT_ORIGINAL) 41 | or Gablonsky's "improved" version (DIRECT_GABLONSKY) 42 | */ 43 | direct_return_code direct_optimize( 44 | direct_objective_func f, void *f_data, 45 | int dimension, 46 | const double *lower_bounds, const double *upper_bounds, 47 | 48 | double *x, double *minf, 49 | 50 | int max_feval, int max_iter, 51 | double start, double maxtime, 52 | double magic_eps, double magic_eps_abs, 53 | double volume_reltol, double sigma_reltol, 54 | int *force_stop, 55 | 56 | double fglobal, 57 | double fglobal_reltol, 58 | 59 | FILE *logfile, 60 | direct_algorithm algorithm) 61 | { 62 | integer algmethod = algorithm == DIRECT_GABLONSKY; 63 | integer ierror; 64 | doublereal *l, *u; 65 | int i; 66 | 67 | /* convert to percentages: */ 68 | volume_reltol *= 100; 69 | sigma_reltol *= 100; 70 | fglobal_reltol *= 100; 71 | 72 | /* make sure these are ignored if <= 0 */ 73 | if (volume_reltol <= 0) volume_reltol = -1; 74 | if (sigma_reltol <= 0) sigma_reltol = -1; 75 | 76 | if (fglobal == DIRECT_UNKNOWN_FGLOBAL) 77 | fglobal_reltol = DIRECT_UNKNOWN_FGLOBAL_RELTOL; 78 | 79 | if (dimension < 1) return DIRECT_INVALID_ARGS; 80 | 81 | l = (doublereal *) malloc(sizeof(doublereal) * dimension * 2); 82 | if (!l) return DIRECT_OUT_OF_MEMORY; 83 | u = l + dimension; 84 | for (i = 0; i < dimension; ++i) { 85 | l[i] = lower_bounds[i]; 86 | u[i] = upper_bounds[i]; 87 | } 88 | 89 | direct_direct_(f, x, &dimension, &magic_eps, magic_eps_abs, 90 | &max_feval, &max_iter, 91 | start, maxtime, force_stop, 92 | minf, 93 | l, u, 94 | &algmethod, 95 | &ierror, 96 | logfile, 97 | &fglobal, &fglobal_reltol, 98 | &volume_reltol, &sigma_reltol, 99 | f_data); 100 | 101 | free(l); 102 | 103 | return (direct_return_code) ierror; 104 | } 105 | -------------------------------------------------------------------------------- /src/algs/direct/tstc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "direct.h" 5 | 6 | /* has two global minima at (0.09,-0.71) and (-0.09,0.71), plus 7 | 4 additional local minima */ 8 | static int cnt=0; 9 | double tst_obj(int n, const double *xy, int *undefined_flag, void *unused) 10 | { 11 | double x, y, f; 12 | x = xy[0]; 13 | y = xy[1]; 14 | f = ((x*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3) + x*y + (y*y)*(-4+4*(y*y))); 15 | printf("feval:, %d, %g, %g, %g\n", ++cnt, x,y, f); 16 | return f; 17 | } 18 | 19 | int main(int argc, char **argv) 20 | { 21 | int n = 2; 22 | double x[2], l[2], u[2]; 23 | long int maxits = 0; 24 | int info; 25 | double minf; 26 | int force_stop = 0; 27 | 28 | maxits = argc < 2 ? 100 : atoi(argv[1]); 29 | 30 | l[0] = -3; l[1] = -3; 31 | u[0] = 3; u[1] = 3; 32 | 33 | info = direct_optimize(tst_obj, NULL, n, l, u, x, &minf, 34 | maxits, 500, 35 | 0, 0, 0, 0, 36 | 0.0, -1.0, 37 | &force_stop, 38 | DIRECT_UNKNOWN_FGLOBAL, 0, 39 | stdout, DIRECT_GABLONSKY); 40 | 41 | printf("min f = %g at (%g,%g) after %d evals, return value %d\n", 42 | minf, x[0], x[1], cnt, info); 43 | 44 | return EXIT_SUCCESS; 45 | } 46 | -------------------------------------------------------------------------------- /src/algs/direct/userguide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevengj/nlopt/019f61ac7253a537760d9cdd9febd927ec97320c/src/algs/direct/userguide.pdf -------------------------------------------------------------------------------- /src/algs/esch/COPYRIGHT: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2008-2013 Carlos Henrique da Silva Santos 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | -------------------------------------------------------------------------------- /src/algs/esch/README: -------------------------------------------------------------------------------- 1 | This is Carlos Henrique da Silva Santos's implementation of 2 | a modified version of the Evolutionary Algorithm described in 3 | the following paper and Ph.D. thesis: 4 | 5 | C. H. da Silva Santos, M. S. Gonçalves, and H. E. Hernandez-Figueroa, 6 | "Designing Novel Photonic Devices by Bio-Inspired Computing," 7 | IEEE Photonics Technology Letters 22(15), pp. 1177-1179 (2010). 8 | 9 | C. H. da Silva Santos, "Parallel and Bio-Inspired Computing 10 | Applied to Analyze Microwave and Photonic Metamaterial Strucutures," 11 | University of Campinas, (2010) 12 | http://www.bibliotecadigital.unicamp.br/document/?code=000767537&opt=4&lg=en_US 13 | 14 | The algorithms are adapted from ideas described in: 15 | 16 | H.-G. Beyer and H.-P. Schwefel. Evolution Strategies: A Comprehensive Introduction. Journal Natural Computing, 1(1):3–52, 2002. 17 | 18 | Ingo Rechenberg (1971): Evolutionsstrategie – Optimierung technischer Systeme nach Prinzipien der biologischen Evolution (PhD thesis). Reprinted by Fromman-Holzboog (1973). 19 | 20 | It is distributed under the "MIT license" given in the attached 21 | COPYRIGHT file (similar to the rest of NLopt), and was 22 | supportedfinancially by the São Paulo Science Foundation (FAPESP - 23 | Fundação de Amparo à Pesquisa do Estado de São Paulo) under the grant 24 | 2012/14553-9. 25 | 26 | Carlos Henrique da Silva Santos 27 | January 2013 28 | -------------------------------------------------------------------------------- /src/algs/esch/esch.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2008-2013 Carlos Henrique da Silva Santos 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef ES_POP_H 24 | #define ES_POP_H 1 25 | 26 | #include "nlopt-util.h" 27 | 28 | #ifdef __cplusplus 29 | extern "C" 30 | { 31 | #endif /* __cplusplus */ 32 | 33 | nlopt_result chevolutionarystrategy( 34 | unsigned, /* Number of input parameters */ 35 | nlopt_func, /* Recursive Objective Function Call */ 36 | void *, /* Data to Objective Function */ 37 | const double*, /* Lower bound values */ 38 | const double*, /* Upper bound values */ 39 | double*, /* in: initial guess, out: minimizer */ 40 | double*, 41 | nlopt_stopping*, /* nlopt stop condition */ 42 | unsigned, /* Number of Parents */ 43 | unsigned); /* Number of Offsprings */ 44 | 45 | #ifdef __cplusplus 46 | } /* extern "C" */ 47 | #endif /* __cplusplus */ 48 | 49 | #endif /* ES_POP_H */ 50 | -------------------------------------------------------------------------------- /src/algs/isres/README: -------------------------------------------------------------------------------- 1 | This is my implementation of the "Improved Stochastic Ranking 2 | Evolution Strategy" (ISRES) algorithm for nonlinearly-constrained 3 | global optimization, based on the method described in: 4 | 5 | Thomas Philip Runarsson and Xin Yao, "Search biases in constrained 6 | evolutionary optimization," IEEE Trans. on Systems, Man, and Cybernetics 7 | Part C: Applications and Reviews, vol. 35 (no. 2), pp. 233-243 (2005). 8 | 9 | It is a refinement of an earlier method described in: 10 | 11 | Thomas P. Runarsson and Xin Yao, "Stochastic ranking for constrained 12 | evolutionary optimization," IEEE Trans. Evolutionary Computation, 13 | vol. 4 (no. 3), pp. 284-294 (2000). 14 | 15 | This is an independent implementation by S. G. Johnson (2009) based 16 | on the papers above. Runarsson also has his own Matlab implementation 17 | available from his web page: http://www3.hi.is/~tpr 18 | 19 | It is under the same MIT license as the rest of my code in NLopt (see 20 | ../COPYRIGHT). 21 | 22 | Steven G. Johnson 23 | November 2009 24 | -------------------------------------------------------------------------------- /src/algs/isres/isres.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef ISRES_H 24 | #define ISRES_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | nlopt_result isres_minimize(int n, nlopt_func f, void *f_data, 35 | int m, nlopt_constraint *fc, /* fc <= 0 */ 36 | int p, nlopt_constraint *h, /* h == 0 */ 37 | const double *lb, const double *ub, /* bounds */ 38 | double *x, /* in: initial guess, out: minimizer */ 39 | double *minf, 40 | nlopt_stopping *stop, 41 | int population); /* init. population */ 42 | 43 | #ifdef __cplusplus 44 | } /* extern "C" */ 45 | #endif /* __cplusplus */ 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /src/algs/luksan/COPYRIGHT: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------- 2 | 3 | Copyright: 4 | 5 | Subroutines PBUN, PNEW, PVAR, PSEN, Copyright ACM, 2001. The original 6 | versions were published in Transactions on Mathematical Software, 7 | Vol.27, 2001, pp.193-213. Here are the author's modifications. They 8 | are posted here by permission of ACM for your personal use. Not for 9 | redistribution. Subroutines PLIP, PSEN, Copyright Jan Vlcek, 2007. 10 | The remaining subroutines, Copyright Ladislav Luksan, 2007. Many of 11 | sparse matrix modules were prepared by Miroslav Tuma. 12 | 13 | License: 14 | 15 | This library (with exception of PBUN, PNEW, PVAR, PSEN) is a free 16 | software; you can redistribute it and/or modify it under the terms 17 | of the GNU Lesser General Public License as published by the Free 18 | Software Foundation; either version 2.1 of the License, or (at your 19 | option) any later version (see http://www.gnu.org/copyleft/gpl.html). 20 | 21 | This library is distributed in the hope that it will be useful, 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | Lesser General Public License for more details. 25 | 26 | Permission is hereby granted to use or copy this program under the 27 | terms of the GNU LGPL, provided that the Copyright, this License, 28 | and the Availability of the original version is retained on all copies. 29 | User documentation of any code that uses this code or any modified 30 | version of this code must cite the Copyright, this License, the 31 | Availability note, and "Used by permission." Permission to modify 32 | the code and to distribute modified code is granted, provided the 33 | Copyright, this License, and the Availability note are retained, 34 | and a notice that the code was modified is included. 35 | 36 | Availability: 37 | 38 | http://www.cs.cas.cz/~luksan/subroutines.html 39 | 40 | Acknowledgements: 41 | 42 | This work was supported by the Grant Agency of the Czech Academy of 43 | Sciences, under grant IAA1030405. 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/algs/luksan/README: -------------------------------------------------------------------------------- 1 | These routines for nonlinear optimization are based on the Fortran 77 2 | code placed online by Professor Ladislav Luksan at his web site: 3 | 4 | http://www.uivt.cas.cz/~luksan/subroutines.html 5 | 6 | and graciously licensed under the GNU Lesser General Public License 7 | (LGPL). See also the COPYRIGHT file for details. 8 | 9 | The C conversions were done via f2c by S. G. Johnson, with the f2c 10 | output manually cleaned up somewhat, and then converted to use the 11 | NLopt termination conditions, C dynamic allocation, etc. 12 | 13 | In particular, we converted the subroutines PLIS, PLIP, and PNET (as 14 | well as various auxiliary routines required by this code), all of 15 | which solve general nonlinear unconstrained or box-constrained 16 | optimization problems. Prof. Luksan also provides a large number of 17 | more specialized routines for cases where one has additional knowledge 18 | of the objective function, but these cases seem outside the scope of 19 | NLopt. 20 | 21 | [We also did not convert the PMIN, PBUN, PNEW, or PVAR subroutines 22 | since these were published in ACM Trans. on Math. Software (TOMS) and 23 | are subject to the non-free ACM licensing conditions.] 24 | -------------------------------------------------------------------------------- /src/algs/luksan/v999-07.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevengj/nlopt/019f61ac7253a537760d9cdd9febd927ec97320c/src/algs/luksan/v999-07.pdf -------------------------------------------------------------------------------- /src/algs/mlsl/README: -------------------------------------------------------------------------------- 1 | This is my implementation of the "Multi-Level Single-Linkage" (MLSL) 2 | algorithm for global optimization by random local optimizations (a 3 | multistart algorithm with "clustering" to avoid repeated detection of 4 | the same local minimum), modified to optionally use a Sobol' 5 | low-discrepancy sequence (LDS) instead of pseudorandom numbers. See: 6 | 7 | A. H. G. Rinnooy Kan and G. T. Timmer, "Stochastic global optimization 8 | methods," Mathematical Programming, vol. 39, p. 27-78 (1987). 9 | [ actually 2 papers -- part I: clustering methods (p. 27), then 10 | part II: multilevel methods (p. 57) ] 11 | 12 | and also: 13 | 14 | Sergei Kucherenko and Yury Sytsko, "Application of deterministic 15 | low-discrepancy sequences in global optimization," Computational 16 | Optimization and Applications, vol. 30, p. 297-318 (2005). 17 | 18 | It is under the same MIT license as the rest of my code in NLopt (see 19 | ../COPYRIGHT). 20 | 21 | Steven G. Johnson 22 | September 2007 23 | -------------------------------------------------------------------------------- /src/algs/mlsl/mlsl.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef MLSL_H 24 | #define MLSL_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data, 35 | const double *lb, const double *ub, /* bounds */ 36 | double *x, /* in: initial guess, out: minimizer */ 37 | double *minf, 38 | nlopt_stopping *stop, 39 | nlopt_opt local_opt, 40 | int Nsamples, /* #samples/iteration (0=default) */ 41 | int lds); 42 | 43 | #ifdef __cplusplus 44 | } /* extern "C" */ 45 | #endif /* __cplusplus */ 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /src/algs/mma/README: -------------------------------------------------------------------------------- 1 | Implementation of the globally-convergent method-of-moving-asymptotes (MMA) 2 | algorithm for gradient-based local optimization, as described in: 3 | 4 | Krister Svanberg, "A class of globally convergent optimization 5 | methods based on conservative convex separable approximations," 6 | SIAM J. Optim. 12 (2), p. 555-573 (2002). 7 | 8 | In fact, this algorithm is much more general than most of the other 9 | algorithms in NLopt, in that it handles an arbitrary set of nonlinear 10 | (differentiable) constraints as well, in a very efficient manner. 11 | I've implemented the full nonlinear-constrained MMA algorithm, and it 12 | is exported under the nlopt_minimize_constrained API. 13 | 14 | I also implemented another CCSA algorithm from the same paper: instead of 15 | constructing local MMA approximations, it constructs simple quadratic 16 | approximations (or rather, affine approximations plus a quadratic penalty 17 | term to stay conservative). This is the ccsa_quadratic code. It seems 18 | to have similar convergence rates to MMA for most problems, which is not 19 | surprising as they are both essentially similar. However, for the quadratic 20 | variant I implemented the possibility of preconditioning: including a 21 | user-supplied Hessian approximation in the local model. It is easy to 22 | incorporate this into the proof in Svanberg's paper, and to show that 23 | global convergence is still guaranteed as long as the user's "Hessian" 24 | is positive semidefinite, and it practice it can greatly improve convergence 25 | if the preconditioner is a good approximation for (at least for the 26 | largest eigenvectors) the real Hessian. 27 | 28 | It is under the same MIT license as the rest of my code in NLopt (see 29 | ../COPYRIGHT). 30 | 31 | Steven G. Johnson 32 | July 2008 - July 2012 33 | -------------------------------------------------------------------------------- /src/algs/mma/mma.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef MMA_H 24 | #define MMA_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | extern unsigned mma_verbose; 35 | extern unsigned ccsa_verbose; 36 | 37 | nlopt_result mma_minimize(unsigned n, nlopt_func f, void *f_data, 38 | unsigned m, nlopt_constraint *fc, 39 | const double *lb, const double *ub, /* bounds */ 40 | double *x, /* in: initial guess, out: minimizer */ 41 | double *minf, 42 | nlopt_stopping *stop, 43 | nlopt_opt dual_opt, int inner_maxeval, unsigned verbose, double rho_init, 44 | const double *sigma_init); 45 | 46 | nlopt_result ccsa_quadratic_minimize( 47 | unsigned n, nlopt_func f, void *f_data, 48 | unsigned m, nlopt_constraint *fc, 49 | 50 | nlopt_precond pre, 51 | 52 | const double *lb, const double *ub, /* bounds */ 53 | double *x, /* in: initial guess, out: minimizer */ 54 | double *minf, 55 | nlopt_stopping *stop, 56 | nlopt_opt dual_opt, int inner_maxeval, unsigned verbose, double rho_init, 57 | const double *sigma_init); 58 | 59 | #ifdef __cplusplus 60 | } /* extern "C" */ 61 | #endif /* __cplusplus */ 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/algs/neldermead/neldermead.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef NELDERMEAD_H 24 | #define NELDERMEAD_H 25 | 26 | #include "nlopt.h" 27 | #include "nlopt-util.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" 31 | { 32 | #endif /* __cplusplus */ 33 | 34 | nlopt_result nldrmd_minimize(int n, nlopt_func f, void *f_data, 35 | const double *lb, const double *ub, /* bounds */ 36 | double *x, /* in: initial guess, out: minimizer */ 37 | double *minf, 38 | const double *xstep, /* initial step sizes */ 39 | nlopt_stopping *stop); 40 | 41 | nlopt_result nldrmd_minimize_(int n, nlopt_func f, void *f_data, 42 | const double *lb, const double *ub, /* bounds */ 43 | double *x,/* in: initial guess, out: minimizer */ 44 | double *minf, 45 | const double *xstep, /* initial step sizes */ 46 | nlopt_stopping *stop, 47 | double psi, double *scratch, double *fdiff); 48 | 49 | nlopt_result sbplx_minimize(int n, nlopt_func f, void *f_data, 50 | const double *lb, const double *ub, /* bounds */ 51 | double *x, /* in: initial guess, out: minimizer */ 52 | double *minf, 53 | const double *xstep0, /* initial step sizes */ 54 | nlopt_stopping *stop); 55 | 56 | #ifdef __cplusplus 57 | } /* extern "C" */ 58 | #endif /* __cplusplus */ 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /src/algs/newuoa/COPYRIGHT: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2004 M. J. D. Powell (mjdp@cam.ac.uk) 2 | * Copyright (c) 2007-2011 Massachusetts Institute of Technology 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | 24 | -------------------------------------------------------------------------------- /src/algs/newuoa/README: -------------------------------------------------------------------------------- 1 | This is the NEWUOA software by M. J. D. Powell, which performs 2 | derivative-free unconstrained optimization using an iteratively 3 | constructed quadratic approximation for the objective function. See: 4 | 5 | M. J. D. Powell, "The NEWUOA software for unconstrained 6 | optimization without derivatives," Proc. 40th Workshop 7 | on Large Scale Nonlinear Optimization (Erice, Italy, 2004). 8 | 9 | The C translation by S. G. Johnson (2008) includes a few minor 10 | modifications, mainly to use the NLopt stopping criteria (and to 11 | take the objective function as an argument rather than a global). 12 | 13 | The C translation also includes a variant (NEWUOA_BOUND, when the lb 14 | and ub parameters to newuoa are non-NULL) that is substantially 15 | modified in order to support bound constraints on the input variables. 16 | In the original NEWUOA algorithm, Powell solved the quadratic 17 | subproblems (in routines TRSAPP and BIGLAG) in a spherical trust 18 | region via a truncated conjugate-gradient algorithm. In the new 19 | variant, we use the MMA algorithm for these subproblems to solve them 20 | with both bound constraints and a spherical trust region. In principle, 21 | we should also change the BIGDEN subroutine in a similar way (since 22 | BIGDEN also approximately solves a trust-region subproblem), but instead 23 | I just truncated its result to the bounds (which probably gives suboptimal 24 | convergence, but BIGDEN is called only very rarely in practice). 25 | 26 | The original Fortran code was released by Powell with "no restrictions 27 | or charges", and the C translation by S. G. Johnson is released in a 28 | similar spirit under the MIT License (see the COPYRIGHT file in this 29 | directory). 30 | -------------------------------------------------------------------------------- /src/algs/newuoa/README.orig: -------------------------------------------------------------------------------- 1 | This is the Fortran version of NEWUOA. Its purpose is to seek 2 | the least value of a function F of several variables, when derivatives 3 | are not available, where F is specified by the user through a subroutine 4 | called CALFUN. The algorithm is intended to change the variables to values 5 | that are close to a local minimum of F. The user, however, should assume 6 | responsibility for finding out if the calculations are satisfactory, by 7 | considering carefully the values of F that occur. The method is described 8 | in the report "The NEWUOA software for unconstrained optimization without 9 | derivatives", which is available on the web at www.damtp.cam.ac.uk, where 10 | you have to click on Numerical Analysis and then on Reports, the number 11 | of the report being NA2004/08. Let N be the number of variables. The main 12 | new feature of the method is that quadratic models are updated using only 13 | about NPT=2N+1 interpolation conditions, the remaining freedom being taken 14 | up by minimizing the Frobenius norm of the change to the second derivative 15 | matrix of the model. 16 | 17 | The new software was developed from UOBYQA, which also forms quadratic 18 | models from interpolation conditions. That method requires NPT=(N+1)(N+2)/2 19 | conditions, however, because they have to define all the parameters of the 20 | model. The least Frobenius norm updating procedure with NPT=2N+1 is usually 21 | much more efficient when N is large, because the work of each iteration is 22 | much less than before, and in some experiments the number of calculations 23 | of the objective function seems to be only of magnitude N. 24 | 25 | The attachments in sequence are a suitable Makefile, followed by a main 26 | program and a CALFUN routine for the Chebyquad problems, in order to provide 27 | an example for testing. Then NEWUOA and its five auxiliary routines, namely 28 | NEWUOB, BIGDEN, BIGLAG, TRSAPP and UPDATE, are given. Finally, the computed 29 | output that the author obtained for the Chebyquad problems is listed. 30 | 31 | The way of calling NEWUOA should be clear from the Chebyquad example 32 | and from the comments of that subroutine. It is hoped that the software will 33 | be helpful to much future research and to many applications. There are no 34 | restrictions on or charges for its use. If you wish to refer to it, please 35 | cite the DAMTP report that is mentioned above, which has been submitted for 36 | publication in the proceedings of the 40th Workshop on Large Scale Nonlinear 37 | Optimization (Erice, Italy, 2004). 38 | 39 | December 16th, 2004 M.J.D. Powell (mjdp@cam.ac.uk) 40 | 41 | -------------------------------------------------------------------------------- /src/algs/newuoa/newuoa.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWUOA_H 2 | #define NEWUOA_H 1 3 | 4 | #include "nlopt-util.h" 5 | #include "nlopt.h" 6 | 7 | typedef double (*newuoa_func)(int n, const double *x, void *func_data); 8 | 9 | extern nlopt_result newuoa(int n, int npt, double *x, 10 | const double *lb, const double *ub, 11 | double rhobeg, nlopt_stopping *stop, double *minf, 12 | newuoa_func calfun, void *calfun_data); 13 | 14 | #endif /* NEWUOA_H */ 15 | -------------------------------------------------------------------------------- /src/algs/praxis/README: -------------------------------------------------------------------------------- 1 | praxis gradient-free local optimization via the "principal-axis method", 2 | downloaded from Netlib: 3 | 4 | http://netlib.org/opt/praxis 5 | 6 | The original Fortran code was written by Richard Brent and made 7 | available by the Stanford Linear Accelerator Center, dated 3/1/73. 8 | Since this code contains no copyright statements and is dated prior to 9 | 1977, under US copyright law it is in the public domain (not copyrighted). 10 | 11 | Converted to C via f2c and cleaned up by Steven G. Johnson 12 | (stevenj@alum.mit.edu). C version is licensed under the MIT license 13 | (which is in the same spirit as public domain, but disclaims warranty 14 | and is clearer legally). 15 | 16 | -------------------------------------------------------------------------------- /src/algs/praxis/praxis.h: -------------------------------------------------------------------------------- 1 | #ifndef PRAXIS_H 2 | #define PRAXIS_H 3 | 4 | #include "nlopt-util.h" 5 | #include "nlopt.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif /* __cplusplus */ 11 | 12 | typedef double (*praxis_func)(int n, const double *x, void *f_data); 13 | 14 | nlopt_result praxis_(double t0, double machep, double h0, 15 | int n, double *x, praxis_func f, void *f_data, 16 | nlopt_stopping *stop, double *minf); 17 | 18 | #ifdef __cplusplus 19 | } /* extern "C" */ 20 | #endif /* __cplusplus */ 21 | 22 | #endif /* PRAXIS_H */ 23 | -------------------------------------------------------------------------------- /src/algs/slsqp/COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright (c) 1988 Dieter Kraft 2 | 3 | Copyright (c) 1994 Association for Computing Machinery 4 | 5 | Copyright (c) 2001, 2002 Enthought, Inc. 6 | All rights reserved. 7 | 8 | Copyright (c) 2003-2009 SciPy Developers. 9 | All rights reserved. 10 | 11 | Copyright (c) 2010 Massachusetts Institute of Technology 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | a. Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | b. Redistributions in binary form must reproduce the above copyright 20 | notice, this list of conditions and the following disclaimer in the 21 | documentation and/or other materials provided with the distribution. 22 | c. Neither the name of the Enthought nor the names of its contributors 23 | may be used to endorse or promote products derived from this software 24 | without specific prior written permission. 25 | 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR 30 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 32 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 33 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 36 | DAMAGE. 37 | 38 | C http://permalink.gmane.org/gmane.comp.python.scientific.devel/6725 39 | C ------ 40 | C From: Deborah Cotton 41 | C Date: Fri, 14 Sep 2007 12:35:55 -0500 42 | C Subject: RE: Algorithm License requested 43 | C To: Alan Isaac 44 | C 45 | C Prof. Issac, 46 | C 47 | C In that case, then because the author consents to [the ACM] releasing 48 | C the code currently archived at http://www.netlib.org/toms/733 under the 49 | C BSD license, the ACM hereby releases this code under the BSD license. 50 | C 51 | C Regards, 52 | C 53 | C Deborah Cotton, Copyright & Permissions 54 | C ACM Publications 55 | C 2 Penn Plaza, Suite 701** 56 | C New York, NY 10121-0701 57 | C permissions@acm.org 58 | C 212.869.7440 ext. 652 59 | C Fax. 212.869.0481 60 | C ------ 61 | -------------------------------------------------------------------------------- /src/algs/slsqp/README: -------------------------------------------------------------------------------- 1 | This code implements a sequential quadratic programming (SQP) 2 | algorithm for nonlinearly constrained gradient-based optimization, and 3 | was originally written by Dieter Kraft and described in: 4 | 5 | Dieter Kraft, "A software package for sequential quadratic 6 | programming", Technical Report DFVLR-FB 88-28, Institut für 7 | Dynamik der Flugsysteme, Oberpfaffenhofen, July 1988. 8 | 9 | Dieter Kraft, "Algorithm 733: TOMP–Fortran modules for optimal 10 | control calculations," ACM Transactions on Mathematical Software, 11 | vol. 20, no. 3, pp. 262-281 (1994). 12 | 13 | (I believe that SLSQP stands for something like Sequential 14 | Least-Squares Quadratic Programming, because the problem is treated as 15 | a sequence of constrained least-squared problems, but such a 16 | least-squares problem is equivalent to a QP.) 17 | 18 | The actual Fortran file was obtained from the SciPy project, who are 19 | responsible for obtaining permission to distribute it under a 20 | free-software (3-clause BSD) license (see the permission email from 21 | ACM at the top of slsqp.c, and also projects.scipy.org/scipy/ticket/1056). 22 | 23 | The code was modified for inclusion in NLopt by S. G. Johnson in 2010, 24 | with the following changes. The code was converted to C and manually 25 | cleaned up. It was modified to be re-entrant, preserving the 26 | reverse-communication interface but explicitly saving the state in a 27 | data structure. The reverse-communication interface was wrapped with 28 | an NLopt-style interface, with NLopt stopping conditions. The inexact 29 | line search was modified to evaluate the functions including gradients 30 | for the first step, since this removes the need to evaluate the 31 | function+gradient a second time for the same point in the common case 32 | when the inexact line search concludes after a single step, since 33 | NLopt's interface combines the function and gradient computations. 34 | Since roundoff errors sometimes pushed SLSQP's parameters slightly 35 | outside the bound constraints (not allowed by NLopt), we added checks 36 | to force the parameters within the bounds. Fixed a bug in LSEI (use 37 | of uninitialized variables) for the case where the number of equality 38 | constraints equals the dimension of the problem. The LSQ subroutine 39 | was modified to handle infinite lower/upper bounds (in which case 40 | those constraints are omitted). 41 | 42 | The exact line-search option is currently disabled; if we want to 43 | re-enable this (although exact line-search is usually overkill in 44 | these kinds of algorithms), we plan to do so using a recursive call to 45 | NLopt. (This will allow a user-specified line-search algorithm to be 46 | used, and will allow the gradient to be exploited in the exact line 47 | search, in contrast to the routine provided with SLSQP.) 48 | -------------------------------------------------------------------------------- /src/algs/slsqp/slsqp.h: -------------------------------------------------------------------------------- 1 | #ifndef SLSQP_H 2 | #define SLSQP_H 3 | 4 | #include "nlopt.h" 5 | #include "nlopt-util.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif /* __cplusplus */ 11 | 12 | nlopt_result nlopt_slsqp(unsigned n, nlopt_func f, void *f_data, 13 | unsigned m, nlopt_constraint *fc, 14 | unsigned p, nlopt_constraint *h, 15 | const double *lb, const double *ub, 16 | double *x, double *minf, 17 | nlopt_stopping *stop); 18 | #ifdef __cplusplus 19 | } /* extern "C" */ 20 | #endif /* __cplusplus */ 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/algs/stogo/COPYRIGHT: -------------------------------------------------------------------------------- 1 | The original StoGO code is: 2 | 3 | Copyright (c) 1998 by S. Zertchaninov and K. Madsen. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | ---------------------------------------------------------------------- 25 | 26 | The StoGO source code on the authors' web site has no copyright or 27 | license information, but I (Steven G. Johnson) contacted the author 28 | K. Madsen for clarification and he gave me permission to distribute 29 | it under the MIT license above. See the correspondence below: 30 | 31 | ---------------------------------------------------------------------- 32 | Date: Fri, 24 Aug 2007 10:46:09 +0200 33 | From: Kaj Madsen 34 | To: stevenj@math.mit.edu 35 | Subject: RE: open source license for StoGO software? 36 | 37 | The MIT licence is ok with me, and so is your copyright suggestion. 38 | 39 | Kaj Madsen. 40 | 41 | -----Original Message----- 42 | From: Steven G. Johnson [mailto:stevenj@fftw.org] 43 | Sent: 23. august 2007 22:23 44 | To: Kaj Madsen 45 | Subject: RE: open source license for StoGO software? 46 | 47 | On Thu, 23 Aug 2007, Kaj Madsen wrote: 48 | > Many thanks for your mail. You can definitely consider the software as 49 | > open source, please use it as you like, however please refer to us if 50 | > you publish results based on the software. 51 | > 52 | > I am now in a heavy administrative position as a Head of Department 53 | > (has been since 1998, therefore I never really followed up on this). 54 | > The paper was never published, however I am attaching two fdf-files, 55 | > one with the paper, one with some descriptions of the code. I hope 56 | > this makes sense, otherwise please don't hesitate to contact me again. 57 | 58 | Thanks so much for your response! 59 | 60 | To be open source it needs some specific open-source license to specify 61 | the permissions in legal terms. The simplest open-source license is 62 | probably the MIT license, is this okay? 63 | 64 | http://opensource.org/licenses/mit-license.php 65 | 66 | Also, I need to know who the authors are for the copyright statement. 67 | Should I list it as: 68 | Copyright (c) 1998 by S. Zertchaninov and K. Madsen ? 69 | 70 | Thanks again! I will definitely cite it if I publish any results based 71 | on StoGO! (It's one of the few public global-optimization programs that 72 | is able to exploit gradient information.) 73 | 74 | Regards, 75 | Steven G. Johnson 76 | -------------------------------------------------------------------------------- /src/algs/stogo/README: -------------------------------------------------------------------------------- 1 | This code is modified from the original StoGO Global Optimization library 2 | by Madsen et al., downloaded from: 3 | 4 | http://www2.imm.dtu.dk/~km/GlobOpt/opt.html 5 | 6 | It was modified to allow C-callable wrappers and some other niceties by 7 | Steven G. Johnson (stevenj@alum.mit.edu) in 2007. 8 | 9 | StoGO uses a gradient-based direct-search branch-and-bound algorithm, 10 | described in: 11 | 12 | S. Gudmundsson, "Parallel Global Optimization," M.Sc. Thesis, IMM, 13 | Technical University of Denmark, 1998. 14 | 15 | K. Madsen, S. Zertchaninov, and A. Zilinskas, "Global Optimization 16 | using Branch-and-Bound," Submitted to the Journal of Global 17 | Optimization, 1998. 18 | [ never published, but preprint is included as paper.pdf ] 19 | 20 | S. Zertchaninov and K. Madsen, "A C++ Programme for Global Optimization," 21 | IMM-REP-1998-04, Department of Mathematical Modelling, 22 | Technical University of Denmark, DK-2800 Lyngby, Denmark, 1998. 23 | [ included as techreport.pdf ] 24 | -------------------------------------------------------------------------------- /src/algs/stogo/global.h: -------------------------------------------------------------------------------- 1 | #ifndef GLOBAL_H 2 | #define GLOBAL_H 3 | 4 | #include "nlopt-util.h" 5 | 6 | #include 7 | //#include "function.h" 8 | #include "tools.h" 9 | using namespace std; 10 | 11 | extern "C" int stogo_verbose; 12 | 13 | typedef void dom(RTBox) ; 14 | typedef dom* Pdom ; 15 | 16 | typedef double obj(RCRVector) ; 17 | typedef obj* Pobj ; 18 | 19 | typedef void grad(RCRVector,RVector&) ; 20 | typedef grad* Pgrad ; 21 | 22 | typedef enum { OBJECTIVE_ONLY, GRADIENT_ONLY, OBJECTIVE_AND_GRADIENT } whichO; 23 | 24 | typedef double objgrad(RCRVector,RCRVector,whichO) ; 25 | typedef objgrad* Pobjgrad ; 26 | 27 | class GlobalParams { 28 | public: 29 | nlopt_stopping *stop; 30 | double eps_cl, mu, rshift; 31 | int det_pnts, rnd_pnts; 32 | }; 33 | 34 | class Global: public GlobalParams { 35 | public: 36 | // Problem specification 37 | int dim ; 38 | Pobj Objective ; 39 | Pgrad Gradient ; 40 | long int numeval; 41 | 42 | virtual double ObjectiveGradient(RCRVector xy, RVector&gradient, whichO which){ 43 | ++numeval; 44 | switch (which) { 45 | case OBJECTIVE_AND_GRADIENT: 46 | Gradient(xy, gradient); 47 | return Objective(xy); 48 | case OBJECTIVE_ONLY: 49 | return Objective(xy); 50 | case GRADIENT_ONLY: 51 | Gradient(xy, gradient); 52 | } 53 | return 0.0; 54 | } 55 | 56 | Global(RTBox, Pobj, Pgrad, GlobalParams); 57 | 58 | virtual ~Global(){}; 59 | 60 | // Global& operator=(const Global &); 61 | 62 | void Search(int, RCRVector); 63 | void DispMinimizers(); 64 | virtual double OneMinimizer(RCRVector); 65 | virtual bool NoMinimizers(); 66 | void SetDomain(RTBox); 67 | void GetDomain(RTBox); 68 | double GetMinValue(); 69 | void SetMinValue(double); 70 | void ClearSolSet(); 71 | void AddPoint(RCRVector, double); 72 | 73 | double GetTime(); 74 | bool InTime(); 75 | 76 | protected: 77 | list SolSet; 78 | list::const_iterator titr; 79 | priority_queue CandSet; 80 | priority_queue Garbage; 81 | 82 | double fbound; 83 | TBox Domain; 84 | 85 | void FillRegular(RTBox, RTBox); 86 | void FillRandom(RTBox, RTBox); 87 | double NewtonTest(RTBox, int, RCRVector, int*); 88 | void ReduceOrSubdivide(RTBox, int, RCRVector); 89 | }; 90 | #endif 91 | 92 | -------------------------------------------------------------------------------- /src/algs/stogo/linalg.h: -------------------------------------------------------------------------------- 1 | /* 2 | Temporary implementation of vector and matrix classes 3 | This is more or less borrowed from Serguei's program 4 | */ 5 | 6 | #ifndef LINALG_H 7 | #define LINALG_H 8 | 9 | #include 10 | using namespace std; 11 | #include // for sqrt() 12 | #include 13 | 14 | typedef const class RVector CRVector; 15 | typedef CRVector& RCRVector; 16 | typedef const class RMatrix CRMatrix ; 17 | typedef CRMatrix& RCRMatrix; 18 | 19 | double eps() ; 20 | 21 | /********************* Class RVector *********************/ 22 | 23 | class RVector{ 24 | protected: 25 | 26 | public: 27 | int len = 0; // size of array 28 | double* elements = nullptr; // array of values 29 | 30 | RVector() ; 31 | RVector(int); // Constructor 32 | RVector(RCRVector); // copy constructor 33 | ~RVector() { delete[] elements; elements=0 ; len=0; } 34 | 35 | RCRVector operator=(double) ; 36 | RCRVector operator=(RCRVector); 37 | 38 | double & operator () (int i) const {return elements[i] ; } 39 | double nrm2() ; // Euclidian norm 40 | 41 | double *raw_data() { return elements; } 42 | const double *raw_data_const() const { return elements; } 43 | 44 | friend ostream & operator << (ostream &, const RVector &); 45 | 46 | friend double norm2(RCRVector) ; 47 | friend double normInf(RCRVector) ; 48 | friend double dot(RCRVector, RCRVector) ; 49 | friend void scal(double, RCRVector) ; 50 | friend void copy(RCRVector, RCRVector) ; 51 | friend void axpy(double, RCRVector, RCRVector) ; 52 | friend void gemv(char,double, RCRMatrix, RCRVector, double, RCRVector); 53 | friend void ger(double alpha, RCRVector, RCRVector, RCRMatrix); 54 | 55 | int GetLength() const { return len; }; // get vector size 56 | }; 57 | 58 | /******************* Class RMatrix *************************/ 59 | 60 | class RMatrix 61 | { 62 | protected: 63 | double* Vals; // array of values 64 | int Dim; // dimension 65 | 66 | public: 67 | RMatrix() ; 68 | RMatrix(int); // dimension 69 | ~RMatrix() { delete[] Vals; Vals=0 ; Dim=0; } 70 | 71 | RMatrix(RCRMatrix); // copy constructor 72 | RCRMatrix operator=(double num) ; 73 | RCRMatrix operator=(RCRMatrix) ; // (needed for template stuff) 74 | 75 | double& operator()(int vidx,int hidx) ; 76 | friend ostream & operator << (ostream &, const RMatrix &); 77 | 78 | friend void gemv(char,double, RCRMatrix, RCRVector, double, RCRVector); 79 | friend void ger(double alpha,RCRVector,RCRVector,RCRMatrix); 80 | 81 | int GetDim() { return Dim; }; // get dimension 82 | }; 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /src/algs/stogo/local.h: -------------------------------------------------------------------------------- 1 | /* 2 | Definitions of various variables used in the local search 3 | */ 4 | 5 | #ifndef LOCAL_H 6 | #define LOCAL_H 7 | 8 | #include "tools.h" 9 | #include "global.h" 10 | 11 | extern int FC, GC ; 12 | 13 | // Results of local search 14 | enum {LS_Unstable, LS_MaxIter, LS_Old, LS_New,LS_Out, LS_MaxEvalTime} ; 15 | 16 | const double delta_coef = 1.0/2.0; // Initialization of trust region 17 | const double epsilon = 1.0E-4 ; // Stopping criterion, var 1e-4 18 | const int max_outside_steps=1 ; // Maximum number of steps outside the box 19 | const int max_iter=50 ; // Max iterations = max_iter*dim. of problem 20 | 21 | extern double MacEpsilon ; // min {x >= 0 : 1 + x > 1} 22 | 23 | int local(Trial &, TBox &, TBox &, double, double*, Global&, int, RCRVector, nlopt_stopping *stop); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/algs/stogo/paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevengj/nlopt/019f61ac7253a537760d9cdd9febd927ec97320c/src/algs/stogo/paper.pdf -------------------------------------------------------------------------------- /src/algs/stogo/rosen.h: -------------------------------------------------------------------------------- 1 | #include "linalg.h" 2 | #include "tools.h" 3 | #include "stogo_config.h" 4 | 5 | void Domain_Rosenbrock(RTBox box) { 6 | box.lb=-10.0 ; box.ub=10.0; 7 | } 8 | 9 | double Objective_Rosenbrock(RCRVector x) { 10 | double a=x(1)-x(0)*x(0); 11 | double b=1-x(0); 12 | return 100*a*a + b*b; 13 | } 14 | 15 | void Gradient_Rosenbrock(RCRVector x, RCRVector grad) { 16 | grad(0)=200*(x(1)-x(0)*x(0))*(-2*x(0))-2*(1-x(0)); 17 | grad(1)=200*(x(1)-x(0)*x(0)); 18 | } 19 | -------------------------------------------------------------------------------- /src/algs/stogo/stogo.cc: -------------------------------------------------------------------------------- 1 | // A C-callable front-end to the StoGO global-optimization library. 2 | // -- Steven G. Johnson 3 | 4 | #include "stogo.h" 5 | #include "global.h" 6 | 7 | namespace { 8 | class MyGlobal : public Global { 9 | protected: 10 | objective_func my_func; 11 | void *my_data; 12 | 13 | // store optimum as the local algorithm does not consider all evaluations as candidates 14 | double minf = DBL_MAX; 15 | RVector bestx; 16 | 17 | public: 18 | 19 | MyGlobal(RTBox D, GlobalParams P, objective_func func, void *data) : Global(D, 0, 0, P), my_func(func), my_data(data), bestx(D.GetDim()) {} 20 | 21 | double ObjectiveGradient(RCRVector xy, RVector &grad, whichO which) override { 22 | ++ numeval; 23 | double val = 0.0; 24 | switch (which) { 25 | case GRADIENT_ONLY: 26 | // fallthrough 27 | case OBJECTIVE_AND_GRADIENT: 28 | val = my_func(static_cast(xy.GetLength()), xy.raw_data_const(), grad.raw_data(), my_data); 29 | break; 30 | case OBJECTIVE_ONLY: 31 | val = my_func(static_cast(xy.GetLength()), xy.raw_data_const(), NULL, my_data); 32 | break; 33 | } 34 | 35 | if (Domain.InsideBox(xy) && (val < minf)) 36 | { 37 | minf = val; 38 | copy(xy, bestx); 39 | } 40 | return val; 41 | } 42 | 43 | bool NoMinimizers() override { 44 | return minf >= DBL_MAX; 45 | } 46 | 47 | double OneMinimizer(RCRVector x) override { 48 | copy(bestx, x); 49 | return minf; 50 | } 51 | 52 | }; 53 | } // namespace 54 | 55 | int stogo_minimize(int n, 56 | objective_func fgrad, void *data, 57 | double *x, double *minf, 58 | const double *l, const double *u, 59 | #ifdef NLOPT_UTIL_H 60 | nlopt_stopping *stop, 61 | #else 62 | long int maxeval, double maxtime, 63 | #endif 64 | int nrandom) 65 | { 66 | GlobalParams params; 67 | 68 | // FIXME: WTF do these parameters mean? 69 | params.rnd_pnts=nrandom; 70 | params.det_pnts=2*n+1 - nrandom; 71 | params.eps_cl=0.1; params.rshift=0.3; 72 | params.mu=1.0E-4; 73 | params.stop = stop; 74 | 75 | TBox D(n); 76 | for (int i = 0; i < n; ++i) { 77 | D.lb(i) = l[i]; 78 | D.ub(i) = u[i]; 79 | } 80 | 81 | MyGlobal Problem(D, params, fgrad, data); 82 | RVector dummyvec(n); 83 | Problem.Search(-1, dummyvec); 84 | 85 | if (Problem.NoMinimizers()) 86 | return 0; 87 | 88 | *minf = Problem.OneMinimizer(dummyvec); 89 | for (int i = 0; i < n; ++i) x[i] = dummyvec(i); 90 | return 1; 91 | } 92 | -------------------------------------------------------------------------------- /src/algs/stogo/stogo.h: -------------------------------------------------------------------------------- 1 | /* A C-callable front-end to the StoGO global-optimization library. 2 | -- Steven G. Johnson */ 3 | 4 | #ifndef STOGO_H 5 | #define STOGO_H 6 | 7 | #include "nlopt-util.h" 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | typedef double (*objective_func)(unsigned n, const double *x, double *grad, 14 | void *data); 15 | 16 | /* search for the global minimum of the function fgrad(n, x, grad, data) 17 | inside a simple n-dimensional hyperrectangle. 18 | 19 | Input: 20 | 21 | n: dimension of search space (number of decision variables) 22 | 23 | fgrad: the objective function of the form fgrad(n, x, grad, data), 24 | returning the objective function at x, where 25 | n: dimension of search space 26 | x: pointer to array of length n, point to evaluate 27 | grad: if non-NULL, an array of length n which 28 | should on return be the gradient d(fgrad)/dx 29 | [ if NULL, grad should be ignored ] 30 | data: arbitrary data pointer, whose value is the 31 | data argument of stogo_minimize 32 | 33 | data: arbitrary pointer to any auxiliary data needed by fgrad 34 | 35 | l, u: arrays of length n giving the lower and upper bounds of the 36 | search space 37 | 38 | maxeval: if nonzero, a maximum number of fgrad evaluations 39 | maxtime: if nonzero, a maximum time (in seconds) 40 | -- REPLACED in NLopt by nlopt_stopping *stop 41 | 42 | nrandom: number of randomized search points to use per box, 43 | in addition to 2*n+1 deterministic search points 44 | (0 for a deterministic algorithm). 45 | 46 | Output: 47 | 48 | minf: the minimum value of the objective function found 49 | 50 | x: pointer to array of length n, giving the location of the minimum 51 | 52 | Return value: 0 if no minimum found, 1 otherwise. 53 | 54 | */ 55 | 56 | int stogo_minimize(int n, 57 | objective_func fgrad, void *data, 58 | double *x, double *minf, 59 | const double *l, const double *u, 60 | #ifdef NLOPT_UTIL_H 61 | nlopt_stopping *stop, 62 | #else 63 | long int maxeval, double maxtime, 64 | #endif 65 | int nrandom); 66 | 67 | extern int stogo_verbose; /* set to nonzero for verbose output */ 68 | 69 | #ifdef __cplusplus 70 | } 71 | #endif 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/algs/stogo/stogo_config.h: -------------------------------------------------------------------------------- 1 | #ifndef STOGO_CONFIG_H 2 | #define STOGO_CONFIG_H 3 | 4 | /* Compiler flags to enable/disable various options in the code */ 5 | 6 | /* To obtain extra information on the global search process: */ 7 | /* #define GS_DEBUG */ 8 | 9 | /* To obtain info on the local search process: */ 10 | /* #define LS_DEBUG */ 11 | 12 | /* Initialization strategy in the local search (INI1,INI2 or INI3) */ 13 | #define INI2 14 | 15 | /* Use the more pessimistic strategy for lower bound estimation */ 16 | /* #define LB2 */ 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/algs/stogo/techreport.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevengj/nlopt/019f61ac7253a537760d9cdd9febd927ec97320c/src/algs/stogo/techreport.pdf -------------------------------------------------------------------------------- /src/algs/stogo/testros.cc: -------------------------------------------------------------------------------- 1 | #include "global.h" 2 | #include "tools.h" 3 | #include "rosen.h" 4 | 5 | int main() { 6 | int dim=2; 7 | 8 | Pdom Dom=Domain_Rosenbrock; 9 | Pobj Obj=Objective_Rosenbrock; 10 | Pgrad Grad=Gradient_Rosenbrock; 11 | 12 | GlobalParams params; 13 | params.det_pnts=2*dim+1; params.rnd_pnts=0; 14 | params.eps_cl=0.1; params.rshift=0.3; 15 | params.mu=1.0E-4; params.maxtime=5; 16 | 17 | TBox D(dim); 18 | Dom(D); 19 | Global Problem(D,Obj, Grad, params); 20 | RVector dummyvec(dim); 21 | Problem.Search(-1, dummyvec); 22 | 23 | cout << "Minimizers found\n"; 24 | Problem.DispMinimizers(); 25 | } 26 | -------------------------------------------------------------------------------- /src/algs/stogo/tools.h: -------------------------------------------------------------------------------- 1 | 2 | // Various datastructures and functions used by the global optimizer 3 | 4 | #ifndef TOOLS_H 5 | #define TOOLS_H 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "linalg.h" 15 | 16 | typedef const class Trial CTrial; 17 | typedef CTrial& RCTrial; 18 | typedef CTrial* PCTrial; 19 | 20 | class Trial{ 21 | public: 22 | RVector xvals; 23 | double objval; 24 | 25 | Trial(); 26 | Trial(int); 27 | Trial(RCTrial); // Copy constructor 28 | RCTrial operator=(RCTrial) ; // assignment operator 29 | friend ostream & operator << (ostream &, RCTrial) ; 30 | }; 31 | 32 | class TrialGT 33 | // Predicate for Trial (needed for remove_if) 34 | { 35 | public: 36 | explicit TrialGT(double val) : _val(val) {} 37 | bool operator()(Trial& foo) { 38 | return foo.objval > _val; 39 | } 40 | private: 41 | double _val; 42 | }; 43 | 44 | typedef class VBox& RVBox; 45 | typedef const class VBox CVBox; 46 | typedef CVBox* PCVBox; 47 | typedef CVBox& RCVBox; 48 | 49 | class VBox{ 50 | public: 51 | RVector lb, ub; 52 | 53 | VBox(); // Construct a box 54 | VBox(int); 55 | VBox(RCVBox); // Copy constructor 56 | RCVBox operator=(RCVBox); // assignment operator 57 | 58 | int GetDim(); // Returns the dimension of the box 59 | double Width(int) ; // Returns the width of the i-th interval 60 | void Midpoint(RCRVector); // Returns the midpoint 61 | 62 | friend ostream & operator << (ostream &, const VBox &); 63 | }; 64 | 65 | typedef class TBox& RTBox; 66 | typedef const class TBox CTBox; 67 | typedef CTBox* PCTBox; 68 | typedef CTBox& RCTBox; 69 | 70 | class TBox: public VBox { 71 | public: 72 | double minf; // Smallest function value found so far 73 | list TList; // List of trials 74 | 75 | TBox(); // Construct a box 76 | TBox(int); 77 | TBox(RCTBox); // Copy constructor 78 | 79 | RCTBox operator=(RCTBox); // assignment operator 80 | 81 | double GetMin(); // Returns 'minf' 82 | bool EmptyBox(); // Returns TRUE if Box contains no trials 83 | void AddTrial(RCTrial); // Add a trial to the (back of) box 84 | void RemoveTrial(Trial &); // Remove a trial from the (back of) box 85 | void GetLastTrial(Trial &); // Return a trial from the back of the box 86 | 87 | list::const_iterator FirstTrial(); 88 | list::const_iterator LastTrial(); 89 | 90 | void GetTrial(list::const_iterator, Trial&); 91 | void ClearBox(); 92 | bool CloseToMin(RVector&, double*, double); 93 | 94 | int NStationary(); // Returns the number of stationary points 95 | 96 | void split(RTBox, RTBox); // Split a box 97 | void dispTrials(); 98 | 99 | bool InsideBox(RCRVector); 100 | int OutsideBox(RCRVector, RCTBox); 101 | double ShortestSide(int*); // Returns the shortest side 102 | double LongestSide(int*); // Returns the longest side 103 | double ClosestSide(RCRVector x); 104 | double FarthestSide(RCRVector x); 105 | bool Intersection(RCRVector, RCRVector, RCRVector); 106 | double LowerBound(double); 107 | 108 | bool operator<(const TBox & x) const {return (minf>x.minf);} 109 | friend ostream & operator << (ostream &, const TBox &); 110 | }; 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /src/algs/stogo/tst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "global.h" 4 | #include "tools.h" 5 | 6 | #include "linalg.h" 7 | #include "tools.h" 8 | #include "stogo_config.h" 9 | 10 | void Domain_Mine(RTBox box) { 11 | box.lb=-3.0 ; box.ub=3.0; 12 | } 13 | 14 | static int cnt = 0; 15 | double Objective_Mine(RCRVector xy) { 16 | double x = xy(0); 17 | double y = xy(1); 18 | double f = ((x*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3) + x*y + (y*y)*(-4+4*(y*y))); 19 | printf("feval:, %d, %g, %g, %g\n", ++cnt, x,y, f); 20 | return f; 21 | } 22 | 23 | void Gradient_Mine(RCRVector xy, RVector &grad) { 24 | double x = xy(0); 25 | double y = xy(1); 26 | grad(0) = /* df/dx */ 27 | ((2*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3) 28 | + (x*x)*(-4.2*x+4*(x*(x*x))/3) 29 | + y); 30 | grad(1) = /* df/dy */ 31 | (x + (2*y)*(-4+4*(y*y)) + (y*y)*(8*(y))); 32 | } 33 | 34 | 35 | int main(int argc, char **argv) { 36 | int dim=2; 37 | 38 | Pdom Dom=Domain_Mine; 39 | Pobj Obj=Objective_Mine; 40 | Pgrad Grad=Gradient_Mine; 41 | 42 | GlobalParams params; 43 | params.det_pnts=2*dim+1; params.rnd_pnts=0; 44 | params.eps_cl=0.1; params.rshift=0.3; 45 | params.mu=1.0E-4; params.maxtime=0; 46 | 47 | params.maxeval = argc < 2 ? 100 : atoi(argv[1]); 48 | 49 | TBox D(dim); 50 | Dom(D); 51 | Global Problem(D,Obj, Grad, params); 52 | RVector dummyvec(dim); 53 | Problem.Search(-1, dummyvec); 54 | 55 | cout << "Minimizers found\n"; 56 | Problem.DispMinimizers(); 57 | 58 | cout << "dummyvec: " << dummyvec << "\n"; 59 | 60 | double val = Problem.OneMinimizer(dummyvec); 61 | cout << "one minimizer: " << val << ": " << dummyvec << "\n"; 62 | } 63 | -------------------------------------------------------------------------------- /src/algs/stogo/tstc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "stogo.h" 5 | 6 | /* has two global minima at (0.09,-0.71) and (-0.09,0.71), plus 7 | 4 additional local minima */ 8 | static int cnt=0; 9 | double tst_obj(int n, const double *xy, double *g, void *unused) 10 | { 11 | double x, y, f; 12 | x = xy[0]; 13 | y = xy[1]; 14 | f = ((x*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3) + x*y + (y*y)*(-4+4*(y*y))); 15 | printf("feval:, %d, %g, %g, %g\n", ++cnt, x,y, f); 16 | if (g) { 17 | g[0] = /* df/dx */ 18 | ((2*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3) 19 | + (x*x)*(-4.2*x+4*(x*(x*x))/3) 20 | + y); 21 | g[1] = /* df/dy */ 22 | (x + (2*y)*(-4+4*(y*y)) + (y*y)*(8*(y))); 23 | } 24 | return f; 25 | } 26 | 27 | int main(int argc, char **argv) 28 | { 29 | int n = 2; 30 | double x[2], l[2], u[2]; 31 | long int maxits = 0, maxtim = 0; 32 | int info; 33 | double minf; 34 | 35 | maxits = argc < 2 ? 100 : atoi(argv[1]); 36 | 37 | l[0] = -3; l[1] = -3; 38 | u[0] = 3; u[1] = 3; 39 | 40 | info = stogo_minimize(n, tst_obj, NULL, x, &minf, l, u, maxits, maxtim); 41 | 42 | printf("min f = %g at (%g,%g) after %d evals, return value %d\n", 43 | minf, x[0], x[1], cnt, info); 44 | 45 | return EXIT_SUCCESS; 46 | } 47 | -------------------------------------------------------------------------------- /src/algs/subplex/README: -------------------------------------------------------------------------------- 1 | "Subplex" gradient-free minimization code, based on a variant of 2 | Nelder-Mead simplex by Tom Rowan. 3 | 4 | http://www.netlib.org/opt/subplex.tgz 5 | 6 | T. Rowan, "Functional Stability Analysis of Numerical Algorithms", 7 | Ph.D. thesis, Department of Computer Sciences, University of Texas 8 | at Austin, 1990. 9 | 10 | This code was downloaded from Netlib.org, converted via f2c, and then cleaned 11 | up a bit by Steven G. Johnson (stevenj@alum.mit.edu) for use with libctl. 12 | 13 | Unfortunately, neither Netlib nor the Subplex source code contain any 14 | explicit indication of the copyright/license status of the code, and I 15 | was unable to contact the author. However, it is listed as "public 16 | domain" by the Starlink astronomical software review 17 | (http://star-www.rl.ac.uk/star/docs/sun194.htx/node151.html). 18 | 19 | The original README file describes the algorithm as: 20 | 21 | Subplex is a subspace-searching simplex method for the 22 | unconstrained optimization of general multivariate functions. 23 | Like the Nelder-Mead simplex method it generalizes, the subplex 24 | method is well suited for optimizing noisy objective functions. 25 | The number of function evaluations required for convergence 26 | typically increases only linearly with the problem size, so for 27 | most applications the subplex method is much more efficient than 28 | the simplex method. 29 | -------------------------------------------------------------------------------- /src/algs/subplex/subplex.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBPLEX_H 2 | #define SUBPLEX_H 3 | 4 | #include "nlopt-util.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif /* __cplusplus */ 10 | 11 | typedef double (*subplex_func)(int n, const double *x, void *func_data); 12 | 13 | extern int nlopt_subplex(subplex_func f, double *minf, double *x, int n, void *fdata, 14 | nlopt_stopping *stop, 15 | const double *scale); 16 | 17 | #ifdef __cplusplus 18 | } /* extern "C" */ 19 | #endif /* __cplusplus */ 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /src/octave/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set (MFILES NLOPT_GN_DIRECT.m NLOPT_GN_DIRECT_L.m NLOPT_GN_DIRECT_L_RAND.m NLOPT_GN_DIRECT_NOSCAL.m NLOPT_GN_DIRECT_L_NOSCAL.m NLOPT_GN_DIRECT_L_RAND_NOSCAL.m NLOPT_GN_ORIG_DIRECT.m NLOPT_GN_ORIG_DIRECT_L.m NLOPT_GD_STOGO.m NLOPT_GD_STOGO_RAND.m NLOPT_LD_LBFGS.m NLOPT_LN_PRAXIS.m NLOPT_LD_VAR1.m NLOPT_LD_VAR2.m NLOPT_LD_TNEWTON.m NLOPT_LD_TNEWTON_RESTART.m NLOPT_LD_TNEWTON_PRECOND.m NLOPT_LD_TNEWTON_PRECOND_RESTART.m NLOPT_GN_CRS2_LM.m NLOPT_GN_MLSL.m NLOPT_GD_MLSL.m NLOPT_GN_MLSL_LDS.m NLOPT_GD_MLSL_LDS.m NLOPT_LD_MMA.m NLOPT_LN_COBYLA.m NLOPT_LN_NEWUOA.m NLOPT_LN_NEWUOA_BOUND.m NLOPT_LN_NELDERMEAD.m NLOPT_LN_SBPLX.m NLOPT_LN_AUGLAG.m NLOPT_LD_AUGLAG.m NLOPT_LN_AUGLAG_EQ.m NLOPT_LD_AUGLAG_EQ.m NLOPT_LN_BOBYQA.m NLOPT_GN_ISRES.m NLOPT_AUGLAG.m NLOPT_AUGLAG_EQ.m NLOPT_G_MLSL.m NLOPT_G_MLSL_LDS.m NLOPT_LD_SLSQP.m NLOPT_LD_CCSAQ.m NLOPT_GN_ESCH.m NLOPT_GN_AGS.m) 2 | set (M_DATA ${MFILES} nlopt_minimize.m nlopt_minimize_constrained.m) 3 | 4 | 5 | if (Matlab_FOUND AND Matlab_MX_LIBRARY) 6 | matlab_add_mex (NAME nlopt_optimize-mex SRC nlopt_optimize-mex.c OUTPUT_NAME nlopt_optimize LINK_TO ${nlopt_lib}) 7 | target_include_directories (nlopt_optimize-mex PRIVATE ${PROJECT_SOURCE_DIR}/src/api) 8 | 9 | if (NLOPT_CXX) 10 | set_target_properties (nlopt_optimize-mex PROPERTIES LINKER_LANGUAGE CXX) 11 | endif () 12 | 13 | set (NLOPT_INSTALL_MEXDIR ${NLOPT_INSTALL_LIBDIR}/matlab CACHE STRING "mex dir") 14 | install (TARGETS nlopt_optimize-mex DESTINATION ${NLOPT_INSTALL_MEXDIR}) 15 | 16 | set (NLOPT_INSTALL_MDIR ${NLOPT_INSTALL_LIBDIR}/matlab CACHE STRING "m dir") 17 | install (FILES ${M_DATA} DESTINATION ${NLOPT_INSTALL_MDIR}) 18 | endif () 19 | 20 | 21 | 22 | if (OCTAVE_FOUND) 23 | file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt_optimize_usage.h "#define NLOPT_OPTIMIZE_USAGE \\\n") 24 | file (STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/nlopt_optimize.m INPUT_LINES) 25 | foreach (INPUT_LINE ${INPUT_LINES}) 26 | string (REGEX REPLACE "^% " "" INPUT_LINE ${INPUT_LINE}) 27 | string (REGEX REPLACE "^%" "" INPUT_LINE ${INPUT_LINE}) 28 | foreach (repl_expr "tolerance" "help NLOPT_LN_SBPLX" "population") 29 | string (REGEX REPLACE "\"${repl_expr}\"" "${repl_expr}" INPUT_LINE "${INPUT_LINE}") 30 | endforeach () 31 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt_optimize_usage.h "\"${INPUT_LINE}\\n\" \\\n") 32 | endforeach () 33 | file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt_optimize_usage.h "\n") 34 | 35 | octave_add_oct (nlopt_optimize SOURCES nlopt_optimize-oct.cc ${CMAKE_CURRENT_BINARY_DIR}/nlopt_optimize_usage.h LINK_LIBRARIES ${nlopt_lib}) 36 | target_include_directories (nlopt_optimize PRIVATE ${OCTAVE_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/src/api) 37 | 38 | if (NOT DEFINED INSTALL_OCT_DIR) 39 | file (RELATIVE_PATH INSTALL_OCT_DIR ${OCTAVE_ROOT_DIR} ${OCTAVE_OCT_SITE_DIR}) 40 | endif () 41 | install (TARGETS nlopt_optimize DESTINATION ${INSTALL_OCT_DIR}) 42 | 43 | if (NOT DEFINED INSTALL_M_DIR) 44 | file (RELATIVE_PATH INSTALL_M_DIR ${OCTAVE_ROOT_DIR} ${OCTAVE_M_SITE_DIR}) 45 | endif () 46 | 47 | install (FILES ${M_DATA} DESTINATION ${INSTALL_M_DIR}) 48 | endif () 49 | -------------------------------------------------------------------------------- /src/octave/NLOPT_AUGLAG.m: -------------------------------------------------------------------------------- 1 | % NLOPT_AUGLAG: Augmented Lagrangian method (needs sub-algorithm) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_AUGLAG 5 | val = 36; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_AUGLAG_EQ.m: -------------------------------------------------------------------------------- 1 | % NLOPT_AUGLAG_EQ: Augmented Lagrangian method for equality constraints (needs sub-algorithm) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_AUGLAG_EQ 5 | val = 37; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GD_MLSL.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GD_MLSL: Multi-level single-linkage (MLSL), random (global, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GD_MLSL 5 | val = 21; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GD_MLSL_LDS.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GD_MLSL_LDS: Multi-level single-linkage (MLSL), quasi-random (global, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GD_MLSL_LDS 5 | val = 23; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GD_STOGO.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GD_STOGO: StoGO (global, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GD_STOGO 5 | val = 8; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GD_STOGO_RAND.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GD_STOGO_RAND: StoGO with randomized search (global, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GD_STOGO_RAND 5 | val = 9; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_AGS.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_AGS: global derivative-free optimization 2 | % with nonlinear inequality constraints. 3 | % 4 | % See nlopt_minimize for more information. 5 | function val = NLOPT_GN_AGS 6 | val = 43; 7 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_CRS2_LM.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_CRS2_LM: Controlled random search (CRS2) with local mutation (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_CRS2_LM 5 | val = 19; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_DIRECT.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_DIRECT: DIRECT (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_DIRECT 5 | val = 0; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_DIRECT_L.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_DIRECT_L: DIRECT-L (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_DIRECT_L 5 | val = 1; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_DIRECT_L_NOSCAL.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_DIRECT_L_NOSCAL: Unscaled DIRECT-L (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_DIRECT_L_NOSCAL 5 | val = 4; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_DIRECT_L_RAND.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_DIRECT_L_RAND: Randomized DIRECT-L (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_DIRECT_L_RAND 5 | val = 2; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_DIRECT_L_RAND_NOSCAL.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_DIRECT_L_RAND_NOSCAL: Unscaled Randomized DIRECT-L (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_DIRECT_L_RAND_NOSCAL 5 | val = 5; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_DIRECT_NOSCAL.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_DIRECT_NOSCAL: Unscaled DIRECT (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_DIRECT_NOSCAL 5 | val = 3; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_ESCH.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_ESCH: ESCH evolutionary strategy 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_ESCH 5 | val = 42; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_ISRES.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_ISRES: ISRES evolutionary constrained optimization (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_ISRES 5 | val = 35; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_MLSL.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_MLSL: Multi-level single-linkage (MLSL), random (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_MLSL 5 | val = 20; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_MLSL_LDS.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_MLSL_LDS: Multi-level single-linkage (MLSL), quasi-random (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_MLSL_LDS 5 | val = 22; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_ORIG_DIRECT.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_ORIG_DIRECT: Original DIRECT version (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_ORIG_DIRECT 5 | val = 6; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_GN_ORIG_DIRECT_L.m: -------------------------------------------------------------------------------- 1 | % NLOPT_GN_ORIG_DIRECT_L: Original DIRECT-L version (global, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_GN_ORIG_DIRECT_L 5 | val = 7; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_G_MLSL.m: -------------------------------------------------------------------------------- 1 | % NLOPT_G_MLSL: Multi-level single-linkage (MLSL), random (global, needs sub-algorithm) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_G_MLSL 5 | val = 38; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_G_MLSL_LDS.m: -------------------------------------------------------------------------------- 1 | % NLOPT_G_MLSL_LDS: Multi-level single-linkage (MLSL), quasi-random (global, needs sub-algorithm) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_G_MLSL_LDS 5 | val = 39; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_AUGLAG.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_AUGLAG: Augmented Lagrangian method (local, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_AUGLAG 5 | val = 31; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_AUGLAG_EQ.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_AUGLAG_EQ: Augmented Lagrangian method for equality constraints (local, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_AUGLAG_EQ 5 | val = 33; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_CCSAQ.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_CCSAQ: CCSA (Conservative Convex Separable Approximations) with simple quadratic approximations (local, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_CCSAQ 5 | val = 41; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_LBFGS.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_LBFGS: Limited-memory BFGS (L-BFGS) (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_LBFGS 5 | val = 11; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_MMA.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_MMA: Method of Moving Asymptotes (MMA) (local, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_MMA 5 | val = 24; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_SLSQP.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_SLSQP: Sequential Quadratic Programming (SQP) (local, derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_SLSQP 5 | val = 40; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_TNEWTON.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_TNEWTON: Truncated Newton (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_TNEWTON 5 | val = 15; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_TNEWTON_PRECOND.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_TNEWTON_PRECOND: Preconditioned truncated Newton (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_TNEWTON_PRECOND 5 | val = 17; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_TNEWTON_PRECOND_RESTART.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_TNEWTON_PRECOND_RESTART: Preconditioned truncated Newton with restarting (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_TNEWTON_PRECOND_RESTART 5 | val = 18; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_TNEWTON_RESTART.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_TNEWTON_RESTART: Truncated Newton with restarting (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_TNEWTON_RESTART 5 | val = 16; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_VAR1.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_VAR1: Limited-memory variable-metric, rank 1 (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_VAR1 5 | val = 13; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LD_VAR2.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LD_VAR2: Limited-memory variable-metric, rank 2 (local, derivative-based) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LD_VAR2 5 | val = 14; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_AUGLAG.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_AUGLAG: Augmented Lagrangian method (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_AUGLAG 5 | val = 30; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_AUGLAG_EQ.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_AUGLAG_EQ: Augmented Lagrangian method for equality constraints (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_AUGLAG_EQ 5 | val = 32; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_BOBYQA.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_BOBYQA: BOBYQA bound-constrained optimization via quadratic models (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_BOBYQA 5 | val = 34; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_COBYLA.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_COBYLA: COBYLA (Constrained Optimization BY Linear Approximations) (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_COBYLA 5 | val = 25; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_NELDERMEAD.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_NELDERMEAD: Nelder-Mead simplex algorithm (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_NELDERMEAD 5 | val = 28; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_NEWUOA.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_NEWUOA: NEWUOA unconstrained optimization via quadratic models (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_NEWUOA 5 | val = 26; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_NEWUOA_BOUND.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_NEWUOA_BOUND: Bound-constrained optimization via NEWUOA-based quadratic models (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_NEWUOA_BOUND 5 | val = 27; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_PRAXIS.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_PRAXIS: Principal-axis, praxis (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_PRAXIS 5 | val = 12; 6 | -------------------------------------------------------------------------------- /src/octave/NLOPT_LN_SBPLX.m: -------------------------------------------------------------------------------- 1 | % NLOPT_LN_SBPLX: Sbplx variant of Nelder-Mead (re-implementation of Rowan's Subplex) (local, no-derivative) 2 | % 3 | % See nlopt_minimize for more information. 4 | function val = NLOPT_LN_SBPLX 5 | val = 29; 6 | -------------------------------------------------------------------------------- /src/octave/dummy.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) { printf("Hello world.\n"); return 0; } 3 | -------------------------------------------------------------------------------- /src/octave/mkconstants.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | names=`egrep 'NLOPT_[LG][ND]|NLOPT_AUGLAG|NLOPT_G_MLSL' ../api/nlopt.h |sed 's/ //g' |tr = , |cut -d, -f1` 4 | i=0 5 | 6 | gcc -I../util -I.. -E ../api/general.c | perl -pe 's/^ *\n//' > foo.c 7 | desc_start=`grep -n nlopt_algorithm_names foo.c |cut -d: -f1 |head -1` 8 | 9 | for n in $names; do 10 | if test -r $n.m; then 11 | perl -pi -e "s/val = [0-9]+;/val = $i;/" $n.m 12 | else 13 | descline=`expr $i + $desc_start + 1` 14 | desc=`tail -n +$descline foo.c |head -1 |cut -d\" -f2` 15 | cat > $n.m <= 2 6 | # define gh_symbol2scm scm_from_latin1_symbol 7 | # undef scm_listify 8 | # define scm_listify scm_list_n 9 | #else 10 | # define gh_symbol2scm scm_str2symbol 11 | #endif 12 | %} 13 | 14 | %typemap(throws) std::runtime_error %{ 15 | scm_throw(gh_symbol2scm("runtime-error"), 16 | scm_list_1(scm_from_locale_string(($1).what()))); 17 | %} 18 | 19 | %typemap(throws) std::bad_alloc %{ 20 | scm_throw(gh_symbol2scm("bad-alloc"), 21 | scm_list_1(scm_from_locale_string(($1).what()))); 22 | %} 23 | 24 | %typemap(throws) std::invalid_argument %{ 25 | scm_throw(gh_symbol2scm("invalid-argument"), 26 | scm_list_1(scm_from_locale_string(($1).what()))); 27 | %} 28 | 29 | %typemap(throws) nlopt::forced_stop %{ 30 | scm_throw(gh_symbol2scm("forced-stop"), SCM_EOL); 31 | %} 32 | 33 | %typemap(throws) nlopt::roundoff_limited %{ 34 | scm_throw(gh_symbol2scm("roundoff-limited"), SCM_EOL); 35 | %} 36 | 37 | %{ 38 | // because our f_data pointer to the Scheme function is stored on the 39 | // heap, rather than the stack, it may be missed by the Guile garbage 40 | // collection and be accidentally freed. Hence, use NLopts munge 41 | // feature to prevent this, by incrementing Guile's reference count. 42 | static void *free_guilefunc(void *p) { 43 | scm_gc_unprotect_object((SCM) p); return p; } 44 | static void *dup_guilefunc(void *p) { 45 | scm_gc_protect_object((SCM) p); return p; } 46 | 47 | // func wrapper around Guile function val = f(x, grad) 48 | static double func_guile(unsigned n, const double *x, double *grad, void *f) { 49 | SCM xscm = scm_c_make_vector(n, SCM_UNSPECIFIED); 50 | for (unsigned i = 0; i < n; ++i) 51 | SCM_SIMPLE_VECTOR_SET(xscm, i, scm_from_double(x[i])); 52 | SCM grad_scm = grad ? scm_c_make_vector(n, SCM_UNSPECIFIED) : SCM_BOOL_F; 53 | SCM ret = scm_call_2((SCM) f, xscm, grad_scm); 54 | if (!scm_is_real(ret)) 55 | throw std::invalid_argument("invalid result passed to nlopt"); 56 | if (grad) { 57 | for (unsigned i = 0; i < n; ++i) { 58 | if (!scm_is_real(ret)) 59 | throw std::invalid_argument("invalid gradient passed to nlopt"); 60 | grad[i] = scm_to_double(SCM_SIMPLE_VECTOR_REF(grad_scm, i)); 61 | } 62 | } 63 | return scm_to_double(ret); 64 | } 65 | %} 66 | 67 | %typemap(in)(nlopt::func f, void *f_data, nlopt_munge md, nlopt_munge mc) { 68 | $1 = func_guile; 69 | $2 = dup_guilefunc((void*) $input); // input = SCM pointer to Scheme function 70 | $3 = free_guilefunc; 71 | $4 = dup_guilefunc; 72 | } 73 | %typecheck(SWIG_TYPECHECK_POINTER)(nlopt::func f, void *f_data, nlopt_munge md, nlopt_munge mc) { 74 | $1 = scm_is_true(scm_procedure_p($input)); 75 | } 76 | 77 | // export constants as variables, rather than as functions returning the value 78 | %feature("constasvar", "1"); 79 | 80 | %scheme %{ 81 | (load-extension "nlopt_guile" "SWIG_init") 82 | %} 83 | -------------------------------------------------------------------------------- /src/swig/nlopt.i: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | // kate: hl c++ 3 | 4 | %define DOCSTRING 5 | "NLopt is a multi-language library for nonlinear optimization (local or 6 | global, with or without derivatives, and supporting nonlinear 7 | constraints). Complete documentation, including a Python tutorial, 8 | can be found at the NLopt web page: http://ab-initio.mit.edu/nlopt" 9 | %enddef 10 | 11 | #ifdef SWIGJAVA 12 | %module(docstring=DOCSTRING) NLopt 13 | #else 14 | %module(docstring=DOCSTRING) nlopt 15 | #endif 16 | %{ 17 | #include "nlopt.hpp" 18 | %} 19 | 20 | #define SWIG_STD_EXCEPTIONS_AS_CLASSES 21 | 22 | %include "std_string.i" 23 | %include "std_except.i" 24 | %include "std_vector.i" 25 | namespace std { 26 | #ifdef SWIGJAVA 27 | %template(DoubleVector) vector; 28 | #else 29 | %template(nlopt_doublevector) vector; 30 | #endif 31 | }; 32 | 33 | %ignore nlopt::opt::myfunc_data; 34 | %ignore nlopt::opt::operator=; 35 | 36 | // dont use the in-place version of get_initial_step 37 | %ignore nlopt_get_initial_step; 38 | %ignore nlopt::opt::get_initial_step; 39 | #ifdef SWIGJAVA 40 | %rename(getInitialStep) nlopt::opt::get_initial_step_; 41 | #else 42 | %rename(get_initial_step) nlopt::opt::get_initial_step_; 43 | #endif 44 | 45 | // prepend "nlopt_" in Guile to substitute for namespace 46 | #if defined(SWIGGUILE) 47 | %rename(nlopt_opt) nlopt::opt; 48 | %rename(nlopt_roundoff_limited) nlopt::roundoff_limited; 49 | %rename(nlopt_forced_stop) nlopt::forced_stop; 50 | %rename(nlopt_srand) nlopt::srand; 51 | %rename(nlopt_srand_time) nlopt::srand_time; 52 | %rename(nlopt_version) nlopt::version; 53 | %rename(nlopt_version_major) nlopt::version_major; 54 | %rename(nlopt_version_minor) nlopt::version_minor; 55 | %rename(nlopt_version_bugfix) nlopt::version_bugfix; 56 | %rename(nlopt_algorithm_name) nlopt::algorithm_name; 57 | %include "nlopt-enum-renames.i" 58 | #endif 59 | 60 | %include "nlopt-exceptions.i" 61 | 62 | #ifdef SWIGGUILE 63 | %include "nlopt-guile.i" 64 | #endif 65 | 66 | #ifdef SWIGPYTHON 67 | %include "nlopt-python.i" 68 | #endif 69 | 70 | #ifdef SWIGJAVA 71 | %include "nlopt-java.i" 72 | #endif 73 | 74 | %include "nlopt.hpp" 75 | -------------------------------------------------------------------------------- /src/util/mt19937ar_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "nlopt-util.h" 3 | 4 | int main(void) 5 | { 6 | int i; 7 | /* 8 | uint32_t init[4]={0x123, 0x234, 0x345, 0x456}, length=4; 9 | nlopt_init_by_array(init, length); 10 | */ 11 | nlopt_init_genrand(5489UL); 12 | /* nlopt_genrand_int32 */ 13 | printf("1000 outputs of nlopt_iurand()\n"); 14 | for (i=0; i<1000; i++) { 15 | printf("%10d ", nlopt_iurand(0x7fff)); 16 | if (i%5==4) printf("\n"); 17 | } 18 | /* genrand_real2 */ 19 | printf("\n1000 outputs of nlopt_urand()\n"); 20 | for (i=0; i<1000; i++) { 21 | printf("%10.8f ", nlopt_urand(0,1)); 22 | if (i%5==4) printf("\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/util/nlopt-getopt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "nlopt-getopt.h" 4 | 5 | int opterr = 1, /* if error message should be printed */ 6 | optind = 1, /* index into parent argv vector */ 7 | optopt, /* character checked for validity */ 8 | optreset; /* reset getopt */ 9 | char *optarg; /* argument associated with option */ 10 | 11 | #define BADCH (int)'?' 12 | #define BADARG (int)':' 13 | #define EMSG "" 14 | 15 | /* 16 | * getopt -- 17 | * Parse argc/argv argument vector. 18 | */ 19 | int getopt(int nargc, char *const nargv[], const char *ostr) 20 | { 21 | static char *place = EMSG; /* option letter processing */ 22 | const char *oli; /* option letter list index */ 23 | if (optreset || !*place) { /* update scanning pointer */ 24 | optreset = 0; 25 | if (optind >= nargc || *(place = nargv[optind]) != '-') { 26 | place = EMSG; 27 | return (-1); 28 | } 29 | if (place[1] && *++place == '-') { /* found "--" */ 30 | ++optind; 31 | place = EMSG; 32 | return (-1); 33 | } 34 | } /* option letter okay? */ 35 | if ((optopt = (int) *place++) == (int) ':' || !(oli = strchr(ostr, optopt))) { 36 | /* if the user didn't specify '-' as an option, assume it means -1. */ 37 | if (optopt == (int) '-') 38 | return (-1); 39 | if (!*place) 40 | ++optind; 41 | if (opterr && *ostr != ':') 42 | (void) printf("illegal option -- %c\n", optopt); 43 | return (BADCH); 44 | } 45 | if (*++oli != ':') { /* don't need argument */ 46 | optarg = NULL; 47 | if (!*place) 48 | ++optind; 49 | } else { /* need an argument */ 50 | if (*place) /* no white space */ 51 | optarg = place; 52 | else if (nargc <= ++optind) { /* no arg */ 53 | place = EMSG; 54 | if (*ostr == ':') 55 | return (BADARG); 56 | if (opterr) 57 | (void) printf("option requires an argument -- %c\n", optopt); 58 | return (BADCH); 59 | } else /* white space */ 60 | optarg = nargv[optind]; 61 | place = EMSG; 62 | ++optind; 63 | } 64 | return (optopt); /* dump back option letter */ 65 | } 66 | -------------------------------------------------------------------------------- /src/util/nlopt-getopt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1987, 1993, 1994 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3. All advertising materials mentioning features or use of this software 14 | * must display the following acknowledgement: 15 | * This product includes software developed by the University of 16 | * California, Berkeley and its contributors. 17 | * 4. Neither the name of the University nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef NLOPT_GETOPT_H 35 | #define NLOPT_GETOPT_H 36 | 37 | /* avoid linker conflicts if we use nlopt-getopt.c with a libc that has getopt */ 38 | #define opterr nlopt_opterr 39 | #define optind nlopt_optind 40 | #define optopt nlopt_optopt 41 | #define optreset nlopt_optreset 42 | #define optarg nlopt_optarg 43 | #define getopt nlopt_getopt 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | extern int opterr, optind, optopt, optreset; 50 | extern char *optarg; 51 | 52 | extern int getopt(int nargc, char *const nargv[], const char *ostr); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | #endif 58 | -------------------------------------------------------------------------------- /src/util/redblack.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef REDBLACK_H 24 | #define REDBLACK_H 25 | 26 | #include /* for ptrdiff_t */ 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif /* __cplusplus */ 31 | 32 | typedef double *rb_key; /* key type ... double* is convenient for us, 33 | but of course this could be cast to anything 34 | desired (although void* would look more generic) */ 35 | 36 | typedef enum { RED, BLACK } rb_color; 37 | typedef struct rb_node_s { 38 | struct rb_node_s *p, *r, *l; /* parent, right, left */ 39 | rb_key k; /* key (and data) */ 40 | rb_color c; 41 | } rb_node; 42 | 43 | typedef int (*rb_compare) (rb_key k1, rb_key k2); 44 | 45 | typedef struct { 46 | rb_compare compare; 47 | rb_node *root; 48 | int N; /* number of nodes */ 49 | } rb_tree; 50 | 51 | extern void nlopt_rb_tree_init(rb_tree * t, rb_compare compare); 52 | extern void nlopt_rb_tree_destroy(rb_tree * t); 53 | extern void nlopt_rb_tree_destroy_with_keys(rb_tree * t); 54 | extern rb_node *nlopt_rb_tree_insert(rb_tree * t, rb_key k); 55 | extern int nlopt_rb_tree_check(rb_tree * t); 56 | extern rb_node *nlopt_rb_tree_find(rb_tree * t, rb_key k); 57 | extern rb_node *nlopt_rb_tree_find_le(rb_tree * t, rb_key k); 58 | extern rb_node *nlopt_rb_tree_find_lt(rb_tree * t, rb_key k); 59 | extern rb_node *nlopt_rb_tree_find_gt(rb_tree * t, rb_key k); 60 | extern rb_node *nlopt_rb_tree_resort(rb_tree * t, rb_node * n); 61 | extern rb_node *nlopt_rb_tree_min(rb_tree * t); 62 | extern rb_node *nlopt_rb_tree_max(rb_tree * t); 63 | extern rb_node *nlopt_rb_tree_succ(rb_node * n); 64 | extern rb_node *nlopt_rb_tree_pred(rb_node * n); 65 | extern void nlopt_rb_tree_shift_keys(rb_tree * t, ptrdiff_t kshift); 66 | 67 | /* To change a key, use nlopt_rb_tree_find+resort. Removing a node 68 | currently wastes memory unless you change the allocation scheme 69 | in redblack.c */ 70 | extern rb_node *nlopt_rb_tree_remove(rb_tree * t, rb_node * n); 71 | 72 | #ifdef __cplusplus 73 | } /* extern "C" */ 74 | #endif /* __cplusplus */ 75 | #endif 76 | -------------------------------------------------------------------------------- /src/util/rescale.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include 24 | #include "nlopt-util.h" 25 | 26 | /* Return a new array of length n (> 0) that gives a rescaling factor 27 | for each dimension, or NULL if out of memory, with dx being the 28 | array of nonzero initial steps in each dimension. */ 29 | double *nlopt_compute_rescaling(unsigned n, const double *dx) 30 | { 31 | double *s = (double *) malloc(sizeof(double) * n); 32 | unsigned i; 33 | 34 | if (!s) 35 | return NULL; 36 | for (i = 0; i < n; ++i) 37 | s[i] = 1.0; /* default: no rescaling */ 38 | if (n == 1) 39 | return s; 40 | 41 | for (i = 1; i < n && dx[i] == dx[i - 1]; ++i); 42 | if (i < n) { /* unequal initial steps, rescale to make equal to dx[0] */ 43 | for (i = 1; i < n; ++i) 44 | s[i] = dx[i] / dx[0]; 45 | } 46 | return s; 47 | } 48 | 49 | void nlopt_rescale(unsigned n, const double *s, const double *x, double *xs) 50 | { 51 | unsigned i; 52 | if (!s) { 53 | for (i = 0; i < n; ++i) 54 | xs[i] = x[i]; 55 | } else { 56 | for (i = 0; i < n; ++i) 57 | xs[i] = x[i] / s[i]; 58 | } 59 | } 60 | 61 | void nlopt_unscale(unsigned n, const double *s, const double *x, double *xs) 62 | { 63 | unsigned i; 64 | if (!s) { 65 | for (i = 0; i < n; ++i) 66 | xs[i] = x[i]; 67 | } else { 68 | for (i = 0; i < n; ++i) 69 | xs[i] = x[i] * s[i]; 70 | } 71 | } 72 | 73 | /* return a new array of length n equal to the original array 74 | x divided by the scale factors s, or NULL on a memory error */ 75 | double *nlopt_new_rescaled(unsigned n, const double *s, const double *x) 76 | { 77 | double *xs = (double *) malloc(sizeof(double) * n); 78 | if (!xs) 79 | return NULL; 80 | nlopt_rescale(n, s, x, xs); 81 | return xs; 82 | } 83 | 84 | /* since rescaling can flip the signs of the x components and the bounds, 85 | we may have to re-order the bounds in order to ensure that they 86 | remain in the correct order */ 87 | void nlopt_reorder_bounds(unsigned n, double *lb, double *ub) 88 | { 89 | unsigned i; 90 | for (i = 0; i < n; ++i) 91 | if (lb[i] > ub[i]) { 92 | double t = lb[i]; 93 | lb[i] = ub[i]; 94 | ub[i] = t; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/util/sobolseq_test.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include "nlopt-util.h" 28 | 29 | #define MAXDIM 1111 30 | 31 | /* test integrand from Joe and Kuo paper ... integrates to 1 */ 32 | static double testfunc(unsigned n, const double *x) 33 | { 34 | double f = 1; 35 | unsigned j; 36 | for (j = 1; j <= n; ++j) { 37 | double cj = pow((double) j, 0.3333333333333333333); 38 | f *= (fabs(4 * x[j - 1] - 2) + cj) / (1 + cj); 39 | } 40 | return f; 41 | } 42 | 43 | int main(int argc, char **argv) 44 | { 45 | unsigned n, j, i, sdim; 46 | static double x[MAXDIM]; 47 | double testint_sobol = 0, testint_rand = 0; 48 | nlopt_sobol s; 49 | if (argc < 3) { 50 | fprintf(stderr, "Usage: %s \n", argv[0]); 51 | return 1; 52 | } 53 | nlopt_init_genrand(time(NULL)); 54 | sdim = atoi(argv[1]); 55 | s = nlopt_sobol_create(sdim); 56 | n = atoi(argv[2]); 57 | nlopt_sobol_skip(s, n, x); 58 | for (j = 1; j <= n; ++j) { 59 | nlopt_sobol_next01(s, x); 60 | testint_sobol += testfunc(sdim, x); 61 | if (j < 100) { 62 | printf("x[%u]: %g", j, x[0]); 63 | for (i = 1; i < sdim; ++i) 64 | printf(", %g", x[i]); 65 | printf("\n"); 66 | } 67 | for (i = 0; i < sdim; ++i) 68 | x[i] = nlopt_urand(0., 1.); 69 | testint_rand += testfunc(sdim, x); 70 | } 71 | nlopt_sobol_destroy(s); 72 | printf("Test integral = %g using Sobol, %g using pseudorandom.\n", testint_sobol / n, testint_rand / n); 73 | printf(" error = %g using Sobol, %g using pseudorandom.\n", testint_sobol / n - 1, testint_rand / n - 1); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /src/util/timer.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007-2014 Massachusetts Institute of Technology 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining 4 | * a copy of this software and associated documentation files (the 5 | * "Software"), to deal in the Software without restriction, including 6 | * without limitation the rights to use, copy, modify, merge, publish, 7 | * distribute, sublicense, and/or sell copies of the Software, and to 8 | * permit persons to whom the Software is furnished to do so, subject to 9 | * the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be 12 | * included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "nlopt-util.h" 24 | 25 | #ifdef TIME_WITH_SYS_TIME 26 | # include 27 | # include 28 | #else 29 | # ifdef HAVE_SYS_TIME_H 30 | # include 31 | # else 32 | # include 33 | # endif 34 | #endif 35 | 36 | #if defined(_WIN32) || defined(__WIN32__) 37 | # include 38 | #endif 39 | 40 | /* return time in seconds since some arbitrary point in the past */ 41 | double nlopt_seconds(void) 42 | { 43 | static THREADLOCAL int start_inited = 0; /* whether start time has been initialized */ 44 | #if defined(HAVE_GETTIMEOFDAY) 45 | static THREADLOCAL struct timeval start; 46 | struct timeval tv; 47 | if (!start_inited) { 48 | start_inited = 1; 49 | gettimeofday(&start, NULL); 50 | } 51 | gettimeofday(&tv, NULL); 52 | return (tv.tv_sec - start.tv_sec) + 1.e-6 * (tv.tv_usec - start.tv_usec); 53 | #elif defined(HAVE_TIME) 54 | return (double)time(NULL); 55 | #elif defined(_WIN32) || defined(__WIN32__) 56 | static THREADLOCAL ULONGLONG start; 57 | FILETIME ft; 58 | if (!start_inited) { 59 | start_inited = 1; 60 | GetSystemTimeAsFileTime(&ft); 61 | start = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime; 62 | } 63 | GetSystemTimeAsFileTime(&ft); 64 | return 100e-9 * (((((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime) - start); 65 | #else 66 | /* use clock() as a fallback... this is somewhat annoying 67 | because clock() may wrap around with a fairly short period */ 68 | static THREADLOCAL clock_t start; 69 | if (!start_inited) { 70 | start_inited = 1; 71 | start = clock(); 72 | } 73 | return (clock() - start) * 1.0 / CLOCKS_PER_SEC; 74 | #endif 75 | } 76 | 77 | /* number based on time for use as random seed */ 78 | unsigned long nlopt_time_seed(void) 79 | { 80 | #if defined(HAVE_GETTIMEOFDAY) 81 | struct timeval tv; 82 | gettimeofday(&tv, NULL); 83 | return (tv.tv_sec ^ tv.tv_usec); 84 | #elif defined(HAVE_TIME) 85 | return (unsigned long)time(NULL); 86 | #elif defined(_WIN32) || defined(__WIN32__) 87 | FILETIME ft; 88 | GetSystemTimeAsFileTime(&ft); 89 | return ft.dwHighDateTime ^ ft.dwLowDateTime; 90 | #else 91 | return clock(); 92 | #endif 93 | } 94 | -------------------------------------------------------------------------------- /test/lorentzfit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | typedef struct { 8 | int N; 9 | double *x, *y; /* length N; */ 10 | } lorentzdata; 11 | 12 | static double sqr(double x) 13 | { 14 | return x * x; 15 | } 16 | 17 | static int count = 0; 18 | 19 | static double lorentzerr(int n, const double *p, double *grad, void *data) 20 | { 21 | lorentzdata *d = (lorentzdata *) data; 22 | int N = d->N; 23 | const double *xs = d->x; 24 | const double *ys = d->y; 25 | double val = 0; 26 | int i, j; 27 | 28 | for (i = 0; i < N; ++i) { 29 | double x = xs[i], y = ys[i]; 30 | double lorsum = 0; 31 | 32 | for (j = 0; j < n; j += 3) { 33 | double A = p[j + 0]; 34 | double w = p[j + 1]; 35 | double G = p[j + 2]; 36 | double lor = A / (sqr(x - w) + G * G); 37 | 38 | lorsum += lor; 39 | } 40 | 41 | val += sqr(y - lorsum); 42 | 43 | if (grad) 44 | for (j = 0; j < n; j += 3) { 45 | double A = p[j + 0]; 46 | double w = p[j + 1]; 47 | double G = p[j + 2]; 48 | double deninv = 1.0 / (sqr(x - w) + G * G); 49 | 50 | grad[j + 0] += -2 * (y - lorsum) * deninv; 51 | grad[j + 1] += 4 * A * (w - x) * (y - lorsum) * sqr(deninv); 52 | grad[j + 2] += 4 * A * G * (y - lorsum) * sqr(deninv); 53 | } 54 | } 55 | ++count; 56 | // printf("%d: f(%g,%g,%g) = %g\n", count, p[0],p[1],p[2], val); 57 | return val; 58 | } 59 | 60 | extern double nlopt_urand(double a, double b); 61 | 62 | int main(void) 63 | { 64 | lorentzdata d; 65 | int i; 66 | double A = 1, w = 0, G = 1, noise = 0.01; 67 | double lb[3] = { -HUGE_VAL, -HUGE_VAL, 0 }; 68 | double ub[3] = { HUGE_VAL, HUGE_VAL, HUGE_VAL }; 69 | double p[3] = { 0, 1, 2 }, minf; 70 | 71 | nlopt_srand_time(); 72 | 73 | d.N = 200; 74 | d.x = (double *) malloc(sizeof(double) * d.N * 2); 75 | d.y = d.x + d.N; 76 | for (i = 0; i < d.N; ++i) { 77 | d.x[i] = nlopt_urand(-0.5, 0.5) * 8 * G + w; 78 | d.y[i] = 2 * noise * nlopt_urand(-0.5, 0.5) + A / (sqr(d.x[i] - w) + G * G); 79 | } 80 | 81 | nlopt_minimize(NLOPT_LN_NEWUOA_BOUND, 3, lorentzerr, &d, lb, ub, p, &minf, -HUGE_VAL, 0, 0, 1e-6, NULL, 0, 0); 82 | 83 | printf("%d minf=%g at A=%g, w=%g, G=%g\n", count, minf, p[0], p[1], p[2]); 84 | 85 | count = 0; 86 | nlopt_minimize(NLOPT_LN_COBYLA, 3, lorentzerr, &d, lb, ub, p, &minf, -HUGE_VAL, 0, 0, 1e-6, NULL, 0, 0); 87 | 88 | printf("%d minf=%g at A=%g, w=%g, G=%g\n", count, minf, p[0], p[1], p[2]); 89 | 90 | count = 0; 91 | nlopt_minimize(NLOPT_LN_NELDERMEAD, 3, lorentzerr, &d, lb, ub, p, &minf, -HUGE_VAL, 0, 0, 1e-6, NULL, 0, 0); 92 | 93 | printf("%d minf=%g at A=%g, w=%g, G=%g\n", count, minf, p[0], p[1], p[2]); 94 | 95 | count = 0; 96 | nlopt_minimize(NLOPT_LN_SBPLX, 3, lorentzerr, &d, lb, ub, p, &minf, -HUGE_VAL, 0, 0, 1e-6, NULL, 0, 0); 97 | 98 | printf("%d minf=%g at A=%g, w=%g, G=%g\n", count, minf, p[0], p[1], p[2]); 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /test/myconstraint.m: -------------------------------------------------------------------------------- 1 | function [val, gradient] = myconstraint(x,a,b) 2 | val = (a*x(1) + b)^3 - x(2); 3 | if (nargout > 1) 4 | gradient = [3*a*(a*x(1) + b)^2, -1]; 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /test/myfunc.m: -------------------------------------------------------------------------------- 1 | function [val, gradient] = myfunc(x) 2 | val = sqrt(x(2)); 3 | if (nargout > 1) 4 | gradient = [0, 0.5 / val]; 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /test/t_bounded.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | double myvfunc(const std::vector &x, std::vector &grad, void *data) 10 | { 11 | (void)data; 12 | if (!grad.empty()) { 13 | grad[0] = 2.0 * x[0]; 14 | grad[1] = 2.0 * x[1]; 15 | } 16 | return x[0]*x[0]+x[1]*x[1]; 17 | } 18 | 19 | int main(int argc, char *argv[]) { 20 | nlopt::opt opt(argc < 2 ? nlopt::GD_STOGO : (nlopt::algorithm)atoi(argv[1]), 2); 21 | opt.set_lower_bounds(0.0); 22 | opt.set_upper_bounds(1.0); 23 | opt.set_max_objective(myvfunc, nullptr); 24 | opt.set_maxeval(100000); 25 | std::vector x = {0.5, 0.5}; 26 | double minf = 0.0; 27 | std::cout << "algo: " << opt.get_algorithm_name() << std::endl; 28 | try{ 29 | opt.optimize(x, minf); 30 | std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = " 31 | << std::setprecision(10) << minf < 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * Test exception handling. Contributed by Kevin Kofler . 10 | * This tests a test case known to fail (t_bounded.cxx on LD_SLSQP) with 11 | * set_exceptions_enabled set to true if the argument is 1, false if the 12 | * argument is 0, and verifies that the exception is thrown resp. not thrown, 13 | * as expected. 14 | */ 15 | 16 | double myvfunc(const std::vector &x, std::vector &grad, void *data) 17 | { 18 | (void)data; 19 | if (!grad.empty()) { 20 | grad[0] = 2.0 * x[0]; 21 | grad[1] = 2.0 * x[1]; 22 | } 23 | return x[0]*x[0]+x[1]*x[1]; 24 | } 25 | 26 | int main(int argc, char *argv[]) { 27 | nlopt::opt opt(nlopt::LD_SLSQP, 2); 28 | if (argc >= 2) { 29 | bool enable = (bool) atoi(argv[1]); 30 | opt.set_exceptions_enabled(enable); 31 | if (opt.get_exceptions_enabled() != enable) { 32 | std::cerr << "set_exceptions_enabled(" << (enable ? "true" : "false") 33 | << ") failed" << std::endl; 34 | return EXIT_FAILURE; 35 | } 36 | } 37 | opt.set_lower_bounds(0.0); 38 | opt.set_upper_bounds(1.0); 39 | opt.set_max_objective(myvfunc, nullptr); 40 | opt.set_maxeval(100000); 41 | std::vector x = {0.5, 0.5}; 42 | double minf = 0.0; 43 | std::cout << "exceptions enabled: " 44 | << (opt.get_exceptions_enabled() ? "true" : "false") 45 | << std::endl; 46 | try{ 47 | opt.optimize(x, minf); 48 | std::cout << "no exception was thrown" << std::endl; 49 | return opt.get_exceptions_enabled() ? EXIT_FAILURE : EXIT_SUCCESS; 50 | } 51 | catch(std::exception &e) { 52 | std::cerr << "exception: " << e.what() << std::endl; 53 | return opt.get_exceptions_enabled() ? EXIT_SUCCESS : EXIT_FAILURE; 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /test/t_fbound.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | double myvfunc(const std::vector &x, std::vector &grad, void *data) 9 | { 10 | (void)data; 11 | if (!grad.empty()) { 12 | grad[0] = 2*x[0]; 13 | grad[1] = 2*x[1]; 14 | } 15 | if (std::isnan(x[0])) 16 | throw std::invalid_argument("nan"); 17 | return -x[0]*x[0]-x[1]*x[1]; 18 | } 19 | 20 | 21 | int main() { 22 | nlopt::srand(0); 23 | for (int repeat = 0; repeat < 100; ++repeat) 24 | { 25 | std::cout << "repeat="< lb = {-1.0, -10.0}; 28 | std::vector ub = {10.0, 1.0}; 29 | opt.set_lower_bounds(lb); 30 | opt.set_upper_bounds(ub); 31 | opt.set_min_objective(myvfunc, NULL); 32 | opt.set_ftol_rel(1e-6); 33 | std::vector x = {0.5, 0.5}; 34 | double minf; 35 | try{ 36 | opt.optimize(x, minf); 37 | std::cout << "found optimum at f(" << x[0] << "," << x[1] << ") = " 38 | << std::setprecision(10) << minf < 0: 30 | # raise ValueError('Cannot suppply gradient values') 31 | grad[0] = 2.0 * (10000. * x1 * x2 - 1.) * 10000. * x2 + 2.0 * (m.exp(-x1) + m.exp(-x2) - 1.0001) * -1.0 32 | grad[1] = 2.0 * (10000. * x1 * x2 - 1.) * 10000. * x1 + 2.0 * (m.exp(-x1) + m.exp(-x2) - 1.0001) * -1.0 33 | 34 | # print("myfunc: x:", x, ", val:", retval) 35 | 36 | if retval < minf_ext[0]: 37 | minf_ext[0] = retval 38 | 39 | return retval 40 | 41 | 42 | for algo in range(nlopt.NUM_ALGORITHMS): 43 | if algo in [nlopt.LD_LBFGS, nlopt.LD_VAR1, nlopt.LD_VAR2]: 44 | continue 45 | opt = nlopt.opt(algo, 2) 46 | 47 | print('-'*40) 48 | print("Algo:", opt.get_algorithm_name(), algo) 49 | numFuncEval[0] = 0 50 | minf_ext = [1e300] 51 | 52 | opt.set_min_objective(myfunc) 53 | opt.set_lower_bounds(lb) 54 | opt.set_upper_bounds(ub) 55 | opt.set_maxeval(int(1e4)) 56 | opt.set_xtol_rel(1e-4) 57 | local_opt = nlopt.opt(nlopt.LD_MMA, 2) 58 | opt.set_local_optimizer(local_opt) 59 | x0 = [0.0, 0.0] 60 | print("x0:", x0) 61 | try: 62 | x = opt.optimize(x0) 63 | minf = opt.last_optimum_value() 64 | print("optimum at ", x[0], x[1]) 65 | print("minimum value = ", minf) 66 | print("result code = ", opt.last_optimize_result()) 67 | print("num function evaluations:", numFuncEval[0]) 68 | if minf_ext[0] < minf: 69 | raise ValueError(f"minimum value {minf} is not the true minimum {minf_ext[0]}") 70 | except nlopt.invalid_argument: 71 | # stogo/ags might not be enabled 72 | pass 73 | -------------------------------------------------------------------------------- /test/t_octave.m: -------------------------------------------------------------------------------- 1 | 2 | arg_list = argv (); 3 | for i = 1:nargin 4 | loadpath = arg_list{i}; 5 | printf ('-- adding path: %s\n', loadpath); 6 | addpath (loadpath); 7 | endfor 8 | 9 | function [val, gradient] = myfunc(x) 10 | val = sqrt(x(2)); 11 | if (nargout > 1) 12 | gradient = [0, 0.5 / val]; 13 | end 14 | endfunction 15 | 16 | function [val, gradient] = myconstraint(x,a,b) 17 | val = (a*x(1) + b)^3 - x(2); 18 | if (nargout > 1) 19 | gradient = [3*a*(a*x(1) + b)^2, -1]; 20 | end 21 | endfunction 22 | 23 | 24 | opt.algorithm = NLOPT_LD_MMA; 25 | % opt.algorithm = NLOPT_LN_COBYLA 26 | opt.lower_bounds = [-inf, 0]; 27 | opt.min_objective = @myfunc; 28 | opt.fc = { (@(x) myconstraint(x,2,0)), (@(x) myconstraint(x,-1,1)) }; 29 | opt.fc_tol = [1e-8, 1e-8]; 30 | opt.xtol_rel = 1e-4; 31 | [xopt, fmin, retcode] = nlopt_optimize(opt, [1.234 5.678]) 32 | 33 | assert (retcode, 4); 34 | assert (fmin, 0.5443, 1e-4); 35 | assert (xopt, [0.3333, 0.2963], 1e-4); 36 | -------------------------------------------------------------------------------- /test/t_python.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import nlopt 4 | import math as m 5 | import sys 6 | 7 | 8 | def myfunc(x, grad): 9 | if grad.size > 0: 10 | grad[0] = 0.0 11 | grad[1] = 0.5 / m.sqrt(x[1]) 12 | return m.sqrt(x[1]) 13 | 14 | 15 | def myconstraint(x, grad, a, b): 16 | if grad.size > 0: 17 | grad[0] = 3 * a * (a*x[0] + b)**2 18 | grad[1] = -1.0 19 | return (a*x[0] + b)**3 - x[1] 20 | 21 | 22 | algo = nlopt.LD_MMA if len(sys.argv) < 2 else int(sys.argv[1]) 23 | opt = nlopt.opt(algo, 2) 24 | print(f"algo: {opt.get_algorithm_name()}") 25 | opt.set_lower_bounds([-float('inf'), 1e-6]) 26 | opt.set_min_objective(myfunc) 27 | opt.add_inequality_constraint(lambda x, grad: myconstraint(x, grad, 2, 0), 1e-8) 28 | opt.add_inequality_constraint(lambda x, grad: myconstraint(x, grad, -1, 1), 1e-8) 29 | opt.set_xtol_rel(1e-4) 30 | x0 = [1.234, 5.678] 31 | x = opt.optimize(x0) 32 | minf = opt.last_optimum_value() 33 | print(f"optimum at {x}") 34 | print(f"minimum value: {minf:.7g}") 35 | print(f"result code: {opt.last_optimize_result()}") 36 | print(f"nevals: {opt.get_numevals()}") 37 | print(f"initial step: {opt.get_initial_step(x0)}") 38 | assert m.fabs(minf - 0.544331) < 1e-3, "wrong optimum" 39 | -------------------------------------------------------------------------------- /test/t_tutorial.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | double myvfunc(const std::vector &x, std::vector &grad, void *data) 9 | { 10 | (void)data; 11 | if (!grad.empty()) { 12 | grad[0] = 0.0; 13 | grad[1] = 0.5 / sqrt(x[1]); 14 | } 15 | return sqrt(x[1]); 16 | } 17 | 18 | typedef struct { 19 | double a, b; 20 | } my_constraint_data; 21 | 22 | double myvconstraint(const std::vector &x, std::vector &grad, void *data) 23 | { 24 | my_constraint_data *d = reinterpret_cast(data); 25 | double a = d->a, b = d->b; 26 | if (!grad.empty()) { 27 | grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b); 28 | grad[1] = -1.0; 29 | } 30 | return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]); 31 | } 32 | 33 | 34 | int main(int argc, char *argv[]) { 35 | nlopt::opt opt(argc < 2 ? nlopt::LD_MMA : (nlopt::algorithm)atoi(argv[1]), 2); 36 | const std::vector lb = {-HUGE_VAL, 1e-6}; 37 | opt.set_lower_bounds(lb); 38 | opt.set_min_objective(myvfunc, NULL); 39 | my_constraint_data data[2] = { {2,0}, {-1,1} }; 40 | opt.add_inequality_constraint(myvconstraint, &data[0], 1e-8); 41 | opt.add_inequality_constraint(myvconstraint, &data[1], 1e-8); 42 | opt.set_xtol_rel(1e-4); 43 | 44 | // try setting an algorithm parameter: */ 45 | opt.set_param("inner_maxeval", 123); 46 | if (opt.get_param("inner_maxeval", 1234) != 123 || opt.get_param("not a param", 1234) != 1234 || 47 | opt.num_params() != 1 || std::string(opt.nth_param(0)) != "inner_maxeval") { 48 | std::cerr << "failed to retrieve nlopt parameter" << std::endl; 49 | return EXIT_FAILURE; 50 | } 51 | 52 | // set a couple of other parameters 53 | opt.set_param("rho_init", 0.5); 54 | opt.set_initial_step(0.1); 55 | 56 | std::vector x = {1.234, 5.678}; 57 | double minf = 0.0; 58 | 59 | try{ 60 | opt.optimize(x, minf); 61 | std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = " 62 | << std::setprecision(10) << minf <