-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnlopt.js
72 lines (67 loc) · 3.59 KB
/
nlopt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function() {
var _, algorithms, optimize;
_ = require("lodash");
algorithms = ["GN_DIRECT", "GN_DIRECT_L", "GN_DIRECT_L_RAND", "GN_DIRECT_NOSCAL", "GN_DIRECT_L_NOSCAL", "GN_DIRECT_L_RAND_NOSCAL", "GN_ORIG_DIRECT", "GN_ORIG_DIRECT_L", "GD_STOGO", "GD_STOGO_RAND", "LD_LBFGS_NOCEDAL", "LD_LBFGS", "LN_PRAXIS", "LD_VAR1", "LD_VAR2", "LD_TNEWTON", "LD_TNEWTON_RESTART", "LD_TNEWTON_PRECOND", "LD_TNEWTON_PRECOND_RESTART", "GN_CRS2_LM", "GN_MLSL", "GD_MLSL", "GN_MLSL_LDS", "GD_MLSL_LDS", "LD_MMA", "LN_COBYLA", "LN_NEWUOA", "LN_NEWUOA_BOUND", "LN_NELDERMEAD", "LN_SBPLX", "LN_AUGLAG", "LD_AUGLAG", "LN_AUGLAG_EQ", "LD_AUGLAG_EQ", "LN_BOBYQA", "GN_ISRES", "AUGLAG", "AUGLAG_EQ", "G_MLSL", "G_MLSL_LDS", "LD_SLSQP", "LD_CCSAQ"];
optimize = require('./build/Release/nlopt').optimize;
module.exports = function(options) {
var i, isArrayOfCallbackTolObjects, isArrayOfDoubles, len, parm, ref;
options = _.cloneDeep(options);
if (!options.algorithm) {
throw "'algorithm' must be specified";
}
options.algorithm = _.indexOf(algorithms, options.algorithm.replace("NLOPT_", ""));
if (options.algorithm === -1) {
throw "unknown or invalid 'algorithm'";
}
if (!options.skipValidation) {
isArrayOfDoubles = function(arr) {
return _.isArray(arr) && _.reduce(arr, (function(acc, val) {
return acc && _.isNumber(val);
}), true);
};
isArrayOfCallbackTolObjects = function(arr) {
return _.isArray(arr) && _.reduce(arr, (function(acc, val) {
return acc && _.isObject(val) && _.isFunction(val.callback) && _.isNumber(val.tolerance);
}), true);
};
if (!options.numberOfParameters) {
throw "'numberOfParameters' must be specified";
}
if (!_.isNumber(options.numberOfParameters)) {
throw "'numberOfParameters' must be a number";
}
if (!options.minObjectiveFunction && !options.maxObjectiveFunction) {
throw "'minObjectiveFunction' or 'maxObjectiveFunction' must be specifed";
}
if (options.minObjectiveFunction && options.maxObjectiveFunction) {
throw "'minObjectiveFunction' and 'maxObjectiveFunction' should not both be specifed";
}
if (!_.isFunction(options.minObjectiveFunction || options.maxObjectiveFunction)) {
throw "'minObjectiveFunction' and 'maxObjectiveFunction' must be functions";
}
if (options.lowerBounds && !isArrayOfDoubles(options.lowerBounds)) {
throw "'lowerBounds' should be an array of doubles";
}
if (options.upperBounds && !isArrayOfDoubles(options.upperBounds)) {
throw "'upperBounds' should be an array of doubles";
}
if (options.inequalityConstraints && !isArrayOfCallbackTolObjects(options.inequalityConstraints)) {
throw "'inequalityConstraints' should be an array of {callback:function(){}, tolerance:0} objects";
}
if (options.equalityConstraints && !isArrayOfCallbackTolObjects(options.equalityConstraints)) {
throw "'equalityConstraints' should be an array of {callback:function(){}, tolerance:0} objects";
}
if (options.initalGuess && !isArrayOfDoubles(options.initalGuess)) {
throw "'initalGuess' should be an array of doubles";
}
ref = ["stopValue", "fToleranceRelative", "fToleranceAbsolute", "xToleranceRelative", "xToleranceAbsolute", "maxEval", "maxTime"];
for (i = 0, len = ref.length; i < len; i++) {
parm = ref[i];
if (options[parm] && !_.isNumber(options[parm])) {
throw "'" + parm + "' must be a double";
}
}
}
return optimize(options);
};
}).call(this);