### Install NLopt with sudo make install Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Install the NLopt libraries and header files after building. By default installs libnlopt.so in /usr/local/lib and nlopt.h in /usr/local/include, plus manual pages and other files. ```bash sudo make install ``` -------------------------------- ### Install NLopt with cmake Source: https://nlopt.readthedocs.io/en/latest Standard build and installation procedure for NLopt on Unix-like systems with a C compiler. Run from the NLopt source directory. ```bash cmake . && make && sudo make install ``` -------------------------------- ### Set and get initial step in Guile: nlopt-opt-set-initial-step and nlopt-opt-get-initial-step Source: https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference Sets or gets the initial step sizes for derivative-free optimization algorithms. dx can be a vector or list of nonzero initial steps per dimension, or a single number for all dimensions. nlopt-opt-get-initial-step returns the initial step vector used for a starting guess of x in (nlopt-opt-optimize opt x). ```guile (nlopt-opt-set-initial-step opt dx) (nlopt-opt-get-initial-step opt x) ``` -------------------------------- ### Install NLopt to custom directory with CMAKE_INSTALL_PREFIX Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Install NLopt into a custom directory using the CMAKE_INSTALL_PREFIX variable. Creates directories like $HOME/install/lib and installs NLopt there. Also changes where Python plugins are installed, so you may need to set PYTHONPATH. ```bash cmake -DCMAKE_INSTALL_PREFIX=$HOME/install .. make make install ``` -------------------------------- ### NLopt Optimization Example in Python Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial A complete example demonstrating how to define an objective function, add inequality constraints, and run an optimization using the NLopt Python interface. ```python import nlopt from numpy import * def myfunc(x, grad):     if grad.size > 0:         grad[0] = 0.0         grad[1] = 0.5 / sqrt(x[1])     return sqrt(x[1]) def myconstraint(x, grad, a, b):     if grad.size > 0:         grad[0] = 3 * a * (a*x[0] + b)**2         grad[1] = -1.0     return (a*x[0] + b)**3 - x[1] opt = nlopt.opt(nlopt.LD_MMA, 2) opt.set_lower_bounds([-float('inf'), 0]) opt.set_min_objective(myfunc) opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8) opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,-1,1), 1e-8) opt.set_xtol_rel(1e-4) x = opt.optimize([1.234, 5.678]) minf = opt.last_optimum_value() print("optimum at  ", x[0], x[1]) print("minimum value =  ", minf) print("result code =  ", opt.last_optimize_result()) ``` -------------------------------- ### Compile program with custom NLopt install Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Compile a program against a custom NLopt installation. Use -I to point to the header files and -L to point to the libraries. ```bash cc -I$HOME/install/include myprogram.c -L$HOME/install/lib -lnlopt -lm -o myprogram ``` -------------------------------- ### NLopt Optimization Example in Java Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial A complete Java example demonstrating how to define an objective function, inequality constraints, and perform optimization using the NLopt library. This implementation requires Java 1.8 or later to support lambda expressions. ```java import nlopt.*; public class t_java { private static double myfunc(double[] x, double[] grad) { if (grad != null) { grad[0] = 0.0; grad[1] = 0.5 / Math.sqrt(x[1]); } return Math.sqrt(x[1]); } private static double myconstraint(double[] x, double[] grad, double a, double b) { if (grad != null) { grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b); grad[1] = -1.0; } return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]); } public static void main(String[] args) { System.loadLibrary("nloptjni"); Opt opt = new Opt(Algorithm.LD_MMA, 2); opt.setLowerBounds(new DoubleVector(Double.NEGATIVE_INFINITY, 0.)); opt.setMinObjective(t_java::myfunc); opt.addInequalityConstraint((x, grad) -> myconstraint(x, grad, 2, 0), 1e-8); opt.addInequalityConstraint((x, grad) -> myconstraint(x, grad, -1, 1), 1e-8); opt.setXtolRel(1e-4); DoubleVector x = opt.optimize(new DoubleVector(1.234, 5.678)); double minf = opt.lastOptimumValue(); System.out.println("optimum at " + x); System.out.println("minimum value: " + minf); System.out.println("result code: " + opt.lastOptimizeResult()); } } ``` -------------------------------- ### Install MinGW cmake and build NLopt Source: https://nlopt.readthedocs.io/en/latest/NLopt_on_Windows Install the MinGW version of cmake using pacman, then build NLopt with the MSYS Makefiles generator to ensure cmake produces the correct makefile type. ```bash pacman -S mingw-w64-x86_64-cmake ``` ```bash cmake -G"MSYS Makefiles" . && make ``` -------------------------------- ### Set and get initial step size Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Sets or gets initial step sizes for derivative-free optimization. dx can be an array (NumPy or list) or a single number for all dimensions. get_initial_step returns the step used for a starting guess x in optimize(x). ```python opt.set_initial_step(dx) dx = opt.get_initial_step(x) ``` -------------------------------- ### Install NLopt via vcpkg Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Build and install NLopt using the vcpkg dependency manager. The NLopt port is maintained by Microsoft team members and community contributors; if the version is out of date, create an issue or pull request on the vcpkg repository. ```shell git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ./vcpkg install nlopt ``` -------------------------------- ### GNU Guile (Scheme) NLopt optimization example Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Complete example using NLopt in GNU Guile. Defines objective and constraint functions that take x and grad arguments, sets lower bounds, adds two inequality constraints with tolerances, and runs the optimizer with NLOPT-LD-MMA. Note the side-effect style and the mapping of nlopt:: C++ functions to nlopt- Guile functions. ```Scheme (use-modules (nlopt)) (define (myfunc x grad) (if grad (begin (vector-set! grad 0 0.0) (vector-set! grad 1 (/ 0.5 (sqrt (vector-ref x 1)))))) (sqrt (vector-ref x 1))) (define (myconstraint x grad a b) (let ((x0 (vector-ref x 0)) (x1 (vector-ref x 1))) (if grad (begin (vector-set! grad 0 (* 3 a (expt (+ (* a x0) b) 2))) (vector-set! grad 1 -1.0))) (- (expt (+ (* a x0) b) 3) x1))) (define opt (new-nlopt-opt NLOPT-LD-MMA 2)) (nlopt-opt-set-lower-bounds opt (vector (- (inf)) 0)) (nlopt-opt-set-min-objective opt myfunc) (nlopt-opt-add-inequality-constraint opt (lambda (x grad) (myconstraint x grad 2 0)) 1e-8) (nlopt-opt-add-inequality-constraint opt (lambda (x grad) (myconstraint x grad -1 1)) 1e-8) (nlopt-opt-set-xtol-rel opt 1e-4) (define x (nlopt-opt-optimize opt (vector 1.234 5.678))) (define minf (nlopt-opt-last-optimum-value opt)) (define result (nlopt-opt-last-optimize-result opt)) ``` -------------------------------- ### Get algorithm name Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Returns a descriptive string for a given algorithm. ```c const char *nlopt_algorithm_name(nlopt_algorithm algorithm); ``` -------------------------------- ### Expected Output for NLopt Optimization Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial The expected console output after running the optimization example. ```text optimum at  0.333333331366 0.296296292697 minimum value =  0.544331050646 result =  4 ``` -------------------------------- ### Set and get vector storage for limited-memory quasi-Newton algorithms Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference Sets or gets the number M of stored vectors for limited-memory quasi-Newton algorithms. The default is M=0, in which case NLopt uses a heuristic nonzero value. ```java opt.setVectorStorage(M); opt.getVectorStorage(); ``` -------------------------------- ### Perform Optimization Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Execute the optimization process starting from an initial guess and handle the result. ```c double x[2] = { 1.234, 5.678 }; /* `*`some initial guess`*` */ double minf; /* `*`the minimum objective value, upon return`*` */ if (nlopt_optimize(opt, x, &minf) < 0) { printf("nlopt failed!\n"); } else { printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1], minf); } ``` -------------------------------- ### opt.optimize(x) Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Performs the optimization starting from an initial guess and returns the optimized parameters. ```APIDOC ## opt.optimize(x) ### Description Performs the optimization starting from an initial guess and returns the optimized parameters. ### Method optimize ### Parameters - **x** (array) - Required - An array (NumPy array or Python list) of length n (the dimension of the problem) giving an initial guess for the optimization parameters. ### Request Example ```python xopt = opt.optimize(x) ``` ### Response Returns a NumPy array containing the optimized values of the optimization parameters. ``` -------------------------------- ### Get algorithm name string Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Returns a string description of the algorithm used by the nlopt.opt object. ```python opt.get_algorithm_name() ``` -------------------------------- ### Get algorithm name string Source: https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference Returns a string description of the algorithm used by the given nlopt-opt object. ```scheme (nlopt-opt-get-algorithm-name opt) ``` -------------------------------- ### Initialize and Run NLopt Optimization Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Example of setting up an optimization problem using the nlopt::opt object and handling results with exceptions. ```cpp nlopt::opt opt(nlopt::LD_MMA, 2); std::vector lb(2); lb[0] = -HUGE_VAL; lb[1] = 0; opt.set_lower_bounds(lb); opt.set_min_objective(myfunc, NULL); my_constraint_data data[2] = { {2,0}, {-1,1} }; opt.add_inequality_constraint(myconstraint, &data[0], 1e-8); opt.add_inequality_constraint(myconstraint, &data[1], 1e-8); opt.set_xtol_rel(1e-4); std::vector x(2); x[0] = 1.234; x[1] = 5.678; double minf; try{ nlopt::result result = opt.optimize(x, minf); std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = " << std::setprecision(10) << minf << std::endl; } catch(std::exception &e) { std::cout << "nlopt failed: " << e.what() << std::endl; } ``` -------------------------------- ### Set and get initial step size Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference Sets and gets the initial step sizes for derivative-free optimization. The setter accepts a DoubleVector of nonzero steps per dimension or a single number for uniform steps. The getter returns the step that will be used for a given starting guess x in opt.optimize(x). ```java opt.setInitialStep(dx); DoubleVector dx = opt.getInitialStep(x); ``` -------------------------------- ### Get NLopt version components Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Returns the major, minor, and bugfix components of the NLopt version number at runtime. For example, version 3.1.4 would return major=3, minor=1, and bugfix=4. ```python nlopt.version_major() ``` ```python nlopt.version_minor() ``` ```python nlopt.version_bugfix() ``` -------------------------------- ### Initialize Optimization Object Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Set up the lower bounds and the optimization object with the chosen algorithm. ```c double lb[2] = { -HUGE_VAL, 0 }; /* lower bounds */ nlopt_opt opt; ``` ```c opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */ nlopt_set_lower_bounds(opt, lb); nlopt_set_min_objective(opt, myfunc, NULL); ``` -------------------------------- ### Set and get forced-stop integer values Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Allows setting a custom integer value to provide information about a forced stop, which can be retrieved later. The value is reset to zero at the start of each optimization. ```c nlopt_result nlopt_set_force_stop(nlopt_opt opt, int val) ``` ```c int nlopt_get_force_stop(nlopt_opt opt) ``` -------------------------------- ### Compiling and linking an NLopt program Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Compile and link the tutorial.c file on Unix systems using the -lnlopt and -lm flags. ```bash cc tutorial.c -o tutorial -lnlopt -lm ``` -------------------------------- ### Get NLopt version numbers with nlopt-version-major, nlopt-version-minor, and nlopt-version-bugfix Source: https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference Returns the major, minor, and bugfix components of the NLopt version at runtime. For example, version 3.1.4 would return major=3, minor=1, and bugfix=4. ```scheme (nlopt-version-major) ``` ```scheme (nlopt-version-minor) ``` ```scheme (nlopt-version-bugfix) ``` -------------------------------- ### Initial Step Size Methods Source: https://nlopt.readthedocs.io/en/latest/NLopt_C-plus-plus_Reference Methods to get and set the initial step sizes for derivative-free optimization algorithms. ```APIDOC ## set_initial_step (vector) ### Description Sets the initial step sizes for derivative-free optimization algorithms using a vector of step sizes. ### Method set_initial_step ### Signature void nlopt::opt::set_initial_step(const std::vector &dx); ### Parameters - **dx** (const std::vector&) - Required - Vector of initial step sizes for each dimension. ## set_initial_step (scalar) ### Description Sets the initial step size for derivative-free optimization algorithms using a single scalar value applied to all dimensions. ### Method set_initial_step ### Signature void nlopt::opt::set_initial_step(double dx); ### Parameters - **dx** (double) - Required - Initial step size applied to all dimensions. ## get_initial_step ### Description Gets the initial step sizes for derivative-free optimization algorithms. ### Method get_initial_step ### Signature void nlopt::opt::get_initial_step(const std::vector &x, std::vector &dx) const; ### Parameters - **x** (const std::vector&) - Required - The point at which to evaluate the initial step. - **dx** (std::vector&) - Output - The resulting initial step sizes. ``` -------------------------------- ### Get NLopt version number at runtime Source: https://nlopt.readthedocs.io/en/latest/NLopt_Fortran_Reference Returns the NLopt version number at runtime. The three integer arguments receive the major, minor, and bugfix components; for example, version 3.1.4 yields major=3, minor=1, and bugfix=4. ```fortran call nloptv(major, minor, bugfix) ``` -------------------------------- ### Set and get initial step sizes Source: https://nlopt.readthedocs.io/en/latest/NLopt_C-plus-plus_Reference Methods to define or retrieve initial step sizes for derivative-free optimization algorithms in C++. ```cpp void nlopt::opt::set_initial_step(const std::vector &dx); void nlopt::opt::set_initial_step(double dx); void nlopt::opt::get_initial_step(const std::vector &x, std::vector &dx) const; ``` -------------------------------- ### Main Optimization Program in Fortran Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial A complete Fortran program demonstrating how to set up and run an optimization using the NLopt interface. ```Fortran program main external myfunc, myconstraint double precision lb(2) integer*8 opt double precision d1(2), d2(2) double precision x(2), minf integer ires include 'nlopt.f' call nlo_create(opt, NLOPT_LD_MMA, 2) call nlo_get_lower_bounds(ires, opt, lb) lb(2) = 0.0 call nlo_set_lower_bounds(ires, opt, lb) call nlo_set_min_objective(ires, opt, myfunc, 0) d1(1) = 2. d1(2) = 0. call nlo_add_inequality_constraint(ires, opt, $ myconstraint, d1, 1.D-8) d2(1) = -1. d2(2) = 1. call nlo_add_inequality_constraint(ires, opt, $ myconstraint, d2, 1.D-8) call nlo_set_xtol_rel(ires, opt, 1.D-4) x(1) = 1.234 x(2) = 5.678 call nlo_optimize(ires, opt, x, minf) if (ires.lt.0) then write(*,*) 'nlopt failed!' else write(*,*) 'found min at ', x(1), x(2) write(*,*) 'min val = ', minf endif call nlo_destroy(opt) end ``` -------------------------------- ### Set and get stochastic population Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Sets or gets the initial population for stochastic algorithms. A pop of zero uses the heuristic default. ```python opt.set_population(pop) opt.get_population() ``` -------------------------------- ### Set and get stochastic population Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference Sets and gets the initial population for stochastic optimization algorithms. A population of zero means the heuristic default will be used. ```java opt.setPopulation(pop); opt.getPopulation(); ``` -------------------------------- ### Get initial step sizes with nlopt_get_initial_step Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Retrieves the initial step sizes that NLopt will use. Pass the same initial guess x that will be given to nlopt_optimize, because the heuristic step size may depend on x. On successful return, dx (an array of length n) contains the initial step sizes. ```c nlopt_result nlopt_get_initial_step(const nlopt_opt opt, const double *x, double *dx); ``` -------------------------------- ### Include NLopt Headers Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Include the necessary NLopt and math headers for the optimization implementation. ```c #include #include ``` -------------------------------- ### Set and get stochastic population in Guile: nlopt-opt-set-population and nlopt-opt-get-population Source: https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference Sets or gets the initial population for stochastic optimization algorithms. A pop of zero implies the heuristic default will be used. ```guile (nlopt-opt-set-population opt pop) (nlopt-opt-get-population opt) ``` -------------------------------- ### Set and get stochastic population size Source: https://nlopt.readthedocs.io/en/latest/NLopt_Deprecated_API_Reference Sets and gets the global population parameter for stochastic algorithms. The default is zero, which is interpreted as letting the algorithm choose an algorithm-specific default value. ```c void nlopt_set_stochastic_population(int pop); int nlopt_get_stochastic_population(void); ``` -------------------------------- ### Set initial step sizes with nlopt_set_initial_step Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Sets the initial step size for derivative-free local optimization. dx is an array of length n (the problem dimension) containing nonzero initial step sizes for each component of x. Passing NULL for dx makes NLopt use its heuristics to determine the step size. ```c nlopt_result nlopt_set_initial_step(nlopt_opt opt, const double* dx); ``` -------------------------------- ### Set and get vector storage with nlopt-opt-set-vector-storage and nlopt-opt-get-vector-storage Source: https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference Sets or gets the number M of stored vectors for limited-memory quasi-Newton algorithms. The default is M=0, in which case NLopt uses a heuristic nonzero value. ```scheme (nlopt-opt-set-vector-storage opt M) ``` ```scheme (nlopt-opt-get-vector-storage opt) ``` -------------------------------- ### Link NLopt library on Unix Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Example command for linking the NLopt library and the math library during compilation on Unix systems. The -lnlopt and -lm flags must follow the source or object files. ```bash compiler ...source/object files... -lnlopt -lm -o myprogram ``` -------------------------------- ### Set Octave plugin install directories with -DINSTALL_OCT_DIR and -DINSTALL_M_DIR Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Override the default installation directories for Octave plugins (.oct files) and .m script files. The defaults are typically /usr/local/lib/octave/2.1.73/site/oct/i486-pc-linux-gnu and /usr/local/share/octave/2.1.73/site/m/ respectively. ```cmake cmake -DINSTALL_OCT_DIR=octdir -DINSTALL_M_DIR=mdir .. ``` -------------------------------- ### Set Matlab plugin install directory with -DINSTALL_MEX_DIR Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Override the default installation directory for Matlab plugins (INSTALL_MEX_DIR). When using a custom directory, you must run Matlab from that directory or add it to the Matlab path. ```cmake cmake -DINSTALL_MEX_DIR=dir .. ``` -------------------------------- ### Build NLopt with CMake Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Build NLopt from source using CMake. Run these commands in the nlopt directory. Requires a C compiler and optionally Octave, Matlab, Guile, or Python for plugins. ```bash mkdir build cd build cmake .. make ``` -------------------------------- ### Set and get initial step sizes in Fortran (nlo_set_initial_step, nlo_set_initial_step1, nlo_get_initial_step) Source: https://nlopt.readthedocs.io/en/latest/NLopt_Fortran_Reference Sets or retrieves the initial step sizes for derivative-free optimization. `dx` is an array of nonzero initial steps per dimension; `dx1` is a single value applied to all dimensions. `ires` is an integer return value (positive on success). ```Fortran double precision x(n) dx(n), dx1 call nlo_set_initial_step(ires, opt, dx) call nlo_set_initial_step1(ires, opt, dx1) call nlo_get_initial_step(ires, opt, x, dx) ``` -------------------------------- ### Set and get vector storage for limited-memory quasi-Newton algorithms Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Sets or gets the number M of stored vectors for limited-memory quasi-Newton algorithms. The default is M=0, in which case NLopt uses a heuristic nonzero value. ```python opt.set_vector_storage(M) ``` ```python opt.get_vector_storage() ``` -------------------------------- ### Set uniform initial step with nlopt_set_initial_step1 Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Convenience variant of nlopt_set_initial_step that sets the same step size dx in every direction. Use when all components of the initial step should be equal. ```c nlopt_result nlopt_set_initial_step1(nlopt_opt opt, double dx); ``` -------------------------------- ### Configure Optimization Structure Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Setting up the opt structure with algorithm, bounds, objective, and constraints. ```matlab opt.algorithm = NLOPT_LD_MMA opt.lower_bounds = [-inf, 0] opt.min_objective = @myfunc opt.fc = { (@(x) myconstraint(x,2,0)), (@(x) myconstraint(x,-1,1)) } opt.fc_tol = [1e-8, 1e-8]; opt.xtol_rel = 1e-4 ``` -------------------------------- ### Get number of evaluations Source: https://nlopt.readthedocs.io/en/latest/NLopt_C-plus-plus_Reference Request the number of evaluations. ```cpp int nlopt::opt::get_numevals() const; ``` -------------------------------- ### setInitialStep and getInitialStep Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference These methods set and retrieve the initial step sizes for derivative-free optimization algorithms. The setter accepts a DoubleVector of nonzero initial steps per dimension or a single number for uniform steps, while the getter returns the initial step that will be used for a given starting guess. ```APIDOC ## setInitialStep ### Description Sets the initial step sizes for derivative-free optimization algorithms. ### Method `void setInitialStep(DoubleVector dx)` ### Parameters - **dx** (DoubleVector) - Required - A DoubleVector of nonzero initial steps for each dimension, or a single number to use the same initial step for all dimensions. ### Example ```java opt.setInitialStep(dx); ``` ## getInitialStep ### Description Returns the initial step that will be used for a given starting guess x in opt.optimize(x). ### Method `DoubleVector getInitialStep(DoubleVector x)` ### Parameters - **x** (DoubleVector) - Required - The starting guess for which the initial step is to be retrieved. ### Returns - **DoubleVector** - The initial step sizes that will be used for the given starting guess. ### Example ```java DoubleVector dx = opt.getInitialStep(x); ``` ``` -------------------------------- ### Get numevals Source: https://nlopt.readthedocs.io/en/latest/NLopt_Fortran_Reference Requests the number of evaluations (nevals). ```fortran call nlo_get_numevals(nevals, opt) ``` -------------------------------- ### Run optimization with opt.optimize(x) Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Call this to perform the optimization. On input, x is an array (NumPy array or Python list) of length n (the dimension from the nlopt.opt constructor) giving an initial guess. The return value xopt is a NumPy array containing the optimized values. ```python xopt = opt.optimize(x) ``` -------------------------------- ### setXtolRel / getXtolRel Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference Set or get the relative tolerance on the optimization parameters. ```APIDOC ## setXtolRel / getXtolRel ### Description Set or get the relative tolerance on the optimization parameters. ### Methods - `opt.setXtolRel(tol)` - Set the relative tolerance. - `opt.getXtolRel()` - Get the current relative tolerance. ### Parameters - `tol` (double) - Required - The relative tolerance value. ### Example ```java opt.setXtolRel(tol); double currentXtolRel = opt.getXtolRel(); ``` ``` -------------------------------- ### setFtolAbs / getFtolAbs Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference Set or get the absolute tolerance on the function value. ```APIDOC ## setFtolAbs / getFtolAbs ### Description Set or get the absolute tolerance on the function value. ### Methods - `opt.setFtolAbs(tol)` - Set the absolute tolerance. - `opt.getFtolAbs()` - Get the current absolute tolerance. ### Parameters - `tol` (double) - Required - The absolute tolerance value. ### Example ```java opt.setFtolAbs(tol); double currentFtolAbs = opt.getFtolAbs(); ``` ``` -------------------------------- ### Execute optimization with nlopt_optimize Source: https://nlopt.readthedocs.io/en/latest/NLopt_Reference Performs the optimization process. On input, x is the initial guess; on success, x is updated with optimized values and opt_f contains the objective function result. ```c nlopt_result nlopt_optimize(nlopt_opt opt, double *x, double *opt_f); ``` -------------------------------- ### setFtolRel / getFtolRel Source: https://nlopt.readthedocs.io/en/latest/NLopt_Java_Reference Set or get the relative tolerance on the function value. ```APIDOC ## setFtolRel / getFtolRel ### Description Set or get the relative tolerance on the function value. ### Methods - `opt.setFtolRel(tol)` - Set the relative tolerance. - `opt.getFtolRel()` - Get the current relative tolerance. ### Parameters - `tol` (double) - Required - The relative tolerance value. ### Example ```java opt.setFtolRel(tol); double currentFtolRel = opt.getFtolRel(); ``` ``` -------------------------------- ### Build NLopt as a static library with -DBUILD_SHARED_LIBS=OFF Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Compile NLopt as a static library instead of the default shared library. Add this flag to the cmake command, then run make and make install as usual. ```cmake cmake -DBUILD_SHARED_LIBS=OFF .. ``` -------------------------------- ### MMA algorithm verbose output Source: https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial Example output showing objective function values at each intermediate step for the MMA algorithm. ```text nlopt_minimize_constrained eval #1: 2.38286 nlopt_minimize_constrained eval #2: 2.35613 nlopt_minimize_constrained eval #3: 2.24586 nlopt_minimize_constrained eval #4: 2.0191 nlopt_minimize_constrained eval #5: 1.74093 nlopt_minimize_constrained eval #6: 1.40421 nlopt_minimize_constrained eval #7: 1.0223 nlopt_minimize_constrained eval #8: 0.685203 nlopt_minimize_constrained eval #9: 0.552985 nlopt_minimize_constrained eval #10: 0.544354 nlopt_minimize_constrained eval #11: 0.544331 ``` -------------------------------- ### Get number of evaluations (nevals) in Guile Source: https://nlopt.readthedocs.io/en/latest/NLopt_Guile_Reference Request the number of evaluations. ```scheme (nlopt-opt-get-nevals opt) ``` -------------------------------- ### Get number of evaluations Source: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference Requests the number of evaluations performed so far. ```python opt.get_numevals() ``` -------------------------------- ### Enable Matlab plugin with -DNLOPT_MATLAB=ON and set Matlab_ROOT_DIR Source: https://nlopt.readthedocs.io/en/latest/NLopt_Installation Enable the Matlab plugin build and specify the Matlab installation directory. Some Matlab versions also require compiling NLopt as a shared library. Use -DNLOPT_MATLAB=OFF to disable the plugin. ```cmake cmake -DNLOPT_MATLAB=ON -DMatlab_ROOT_DIR=/opt/matlab/RYYYYx/ .. ```