{"id":17069616,"url":"https://github.com/freethenation/node-nlopt","last_synced_at":"2025-04-12T18:31:03.327Z","repository":{"id":9305429,"uuid":"11144433","full_name":"freethenation/node-nlopt","owner":"freethenation","description":"A numerical nonlinear optimization library for node. A wrapper around nlopt.","archived":false,"fork":false,"pushed_at":"2022-12-11T07:09:32.000Z","size":2313,"stargazers_count":15,"open_issues_count":3,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T16:07:15.164Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/freethenation.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-03T07:20:43.000Z","updated_at":"2025-03-31T22:45:19.000Z","dependencies_parsed_at":"2023-01-11T20:11:55.078Z","dependency_job_id":null,"html_url":"https://github.com/freethenation/node-nlopt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freethenation%2Fnode-nlopt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freethenation%2Fnode-nlopt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freethenation%2Fnode-nlopt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freethenation%2Fnode-nlopt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/freethenation","download_url":"https://codeload.github.com/freethenation/node-nlopt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248613241,"owners_count":21133474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T11:27:22.954Z","updated_at":"2025-04-12T18:31:03.307Z","avatar_url":"https://github.com/freethenation.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About #\r\nNLopt is a free/open-source library for nonlinear optimization, providing a common interface for a number of different free optimization routines available online as well as original implementations of various other algorithms. Node-nlopt is a JS wrapper around nlopt. For reference about the different algorithms available and the parameters they accept you should consult [NLopt's website](http://ab-initio.mit.edu/wiki/index.php/NLopt).\r\n\r\n# Installation #\r\nRun the command\r\n\r\n`npm install nlopt`\r\n\r\nRunning this command builds nlopt which is a c library. I have tested that it build on Windows 64bit and Linux 64bit. I have not tested other platforms\r\n\r\n# Simple Example #\r\nThe library defines a single method. A simple example of how to use the library can be found below. This is the same example used in the [tutorial at NLopt's website](http://ab-initio.mit.edu/wiki/index.php/NLopt_Tutorial).\r\n\r\n```javascript\r\nvar nlopt = require('nlopt');\r\nvar myfunc = function(n, x, grad){\r\n  if(grad){\r\n    grad[0] = 0.0;\r\n    grad[1] = 0.5 / Math.sqrt(x[1]);\r\n  }\r\n  return Math.sqrt(x[1]);\r\n}\r\nvar createMyConstraint = function(cd){\r\n  return {\r\n    callback:function(n, x, grad){\r\n      if(grad){\r\n        grad[0] = 3.0 * cd[0] * (cd[0]*x[0] + cd[1]) * (cd[0]*x[0] + cd[1])\r\n        grad[1] = -1.0\r\n      }\r\n      tmp = cd[0]*x[0] + cd[1]\r\n      return tmp * tmp * tmp - x[1]\r\n    },\r\n    tolerance:1e-8\r\n  }\r\n}\r\noptions = {\r\n  algorithm: \"LD_MMA\",\r\n  numberOfParameters:2,\r\n  minObjectiveFunction: myfunc,\r\n  inequalityConstraints:[createMyConstraint([2.0, 0.0]), createMyConstraint([-1.0, 1.0])],\r\n  xToleranceRelative:1e-4,\r\n  initalGuess:[1.234, 5.678],\r\n  lowerBounds:[Number.MIN_VALUE, 0]\r\n}\r\nconsole.log(nlopt(options).parameterValues);\r\n```\r\nThe code above should write \"[ 0.33333333465873644, 0.2962962893886998 ]\" to the console.\r\n\r\n# API #\r\n\r\nThe library defines a single function that takes a JavaScript object as a parameter. The format for the JavaScript is:\r\n```javascript\r\n{\r\n\t//The algorithm to run. Look at the nlopt site for a complete list of options\r\n\talgorithm: \"LD_MMA\",\r\n\t//The number of parameters that the function to be optimized takes\r\n    numberOfParameters:2,\r\n    //The function to be minified.\r\n    minObjectiveFunction: function(numberOfParameters, parameterValues, gradient){},\r\n    //The function to be maximized. If minObjectiveFunction is specified this option should not be.\r\n    maxObjectiveFunction: function(numberOfParameters, parameterValues, gradient){},\r\n    //An inital guess of the values that maximize or minimize the objective function\r\n    initalGuess:[1.234, 5.678],\r\n    //Parameter values must be above the provided numbers\r\n    lowerBounds:[Number.MIN_VALUE, 0],\r\n    //Parameter values must be below the provided numbers\r\n    upperBounds:[Number.MIN_VALUE, 0],\r\n    //Inequality constraints on the function to be optimized.\r\n    inequalityConstraints:[function(numberOfParameters, parameterValues, gradient), function(){}],\r\n    //Equalit constraints on the function to be optimized.\r\n    equalityConstraints:[function(numberOfParameters, parameterValues, gradient), function(){}],\r\n    //Consult http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Stopping_criteria for more info\r\n    //on the next couple of options\r\n  \tstopValue: 1e-4,\r\n  \tfToleranceRelative: 1e-4,\r\n  \tfToleranceAbsolute: 1e-4,\r\n  \txToleranceRelative: 1e-4,\r\n  \txToleranceAbsolute: 1e-4,\r\n  \tmaxEval: 1e-4,\r\n  \tmaxTime: 1e-4,\r\n}\r\n```\r\nThe return value has the format\r\n```javascript\r\n{\r\n\t//The parameter values that produce the min or max value for the object function\r\n\tparameterValues: [ 0.33333333465873644, 0.2962962893886998 ],\r\n\t//The min or max function for the objective function.\r\n   \toutputValue: 0.5443310476067847 ,\r\n   \t//A string indicating if optimization was successful. If optimization was successful the string will\r\n   \t//start with \"Success\"\r\n   \tstatus: 'Success: Optimization stopped because xToleranceRelative or xToleranceAbsolute was reached',\r\n   \t//A string will also be outputed for each setting/option set. This string will also start with \"Success\"\r\n   \t//if the operation was successful. Examples can be found below.\r\n   \tmaxObjectiveFunction: 'Success',\r\n    lowerBounds: 'Success'\r\n}\r\n```\r\nSome of the descriptions above are incomplete. Consult [NLopt's website](http://ab-initio.mit.edu/wiki/index.php/NLopt) for more info on the various options.\r\n\r\n# Limitations #\r\nThe biggest limitation at the moment is there is currently no support for making the call to nlopt asynchronously. Numerical optimization is inherently CPU bound so asynchronously calling nlopt is not incredibly useful and I did not need it for my use case.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreethenation%2Fnode-nlopt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffreethenation%2Fnode-nlopt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreethenation%2Fnode-nlopt/lists"}