0.1.1 • Published 3 years ago

nlopt-js v0.1.1

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

npm version Website shields.io Made with emscripten GitHub license

NLopt-js

NLopt-js is a port of the nlopt C++ optimization library

It uses a WebAssembly compiled subset of the nlopt library, and implements a garbage collection mechanism to manage memory

HomeDocumentation

Usage

NLopt-js can be installed via npm or yarn

npm install nlopt-js
yarn add nlopt-js

In a node application or in the browser (using webpack)

const nlopt = require('nlopt-js')
// ES6: import nlopt from 'nlopt-js'
(async () => {
  await nlopt.ready
  const opt = new nlopt.Optimize(nlopt.Algorithm.LD_SLSQP, 2);
  opt.setMinObjective((x, grad) => {
    if (grad) {
      grad[0] = 0;
      grad[1] = 0.5 / Math.sqrt(x[1]);
    }
    return Math.sqrt(x[1]);
  }, 1e-4);
  const res = opt.optimize([1, 6]);
  // Flush the GC
  nlopt.GC.flush();
})();

Documentation

The documentation is available at NLopt-js

Build

Make sure Emscripten is intalled & activated in your terminal session

source path/to/emsdk/emsdk_env.sh
./emcc -v

Dowload the latest version of the nlopt library and extract it under

lib/nlopt

Now build a bytecode shared library

mkdir build; cd build
emcmake cmake ..
emmake make

It should generate a shared library bytecode file named libnlopt.bc

Now to compile the wasm binary, run the following command

mkdir build
emcc -I lib/nlopt/build -I lib/nlopt/src/api  -Isrc lib/nlopt/build/libnlopt.a -s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=0 -O3 -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 --bind -o build/nlopt_gen.js src/cpp/embind.cc 

Generate the documentation

The documentation is generated from classes descriptions using documentation.js

documentation build src/classes/ -f json -o docs/doc.json