-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
201 lines (157 loc) · 4.38 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
var minimize1d = require('minimize-golden-section-1d');
module.exports = powellsMethod;
function powellsMethod (f, x0, options, status) {
var i, j, iter, ui, tmin, pj, fi, un, u, p0, sum, dx, err, perr, du, tlimit;
options = options || {};
var maxIter = options.maxIter === undefined ? 20 : options.maxIter;
var tol = options.tolerance === undefined ? 1e-8 : options.tolerance;
var tol1d = options.lineTolerance === undefined ? tol : options.lineTolerance;
var bounds = options.bounds === undefined ? [] : options.bounds;
var verbose = options.verbose === undefined ? false : options.verbose;
if (status) status.points = [];
// Dimensionality:
var n = x0.length;
// Solution vector:
var p = x0.slice(0);
// Search directions:
u = [];
un = [];
for (i = 0; i < n; i++) {
u[i] = [];
for (j = 0; j < n; j++) {
u[i][j] = i === j ? 1 : 0;
}
}
// Bound the input:
function constrain (x) {
for (var i = 0; i < bounds.length; i++) {
var ibounds = bounds[i];
if (!ibounds) continue;
if (isFinite(ibounds[0])) {
x[i] = Math.max(ibounds[0], x[i]);
}
if (isFinite(ibounds[1])) {
x[i] = Math.min(ibounds[1], x[i]);
}
}
}
constrain(p);
if (status) status.points.push(p.slice());
var bound = options.bounds
? function (p, ui) {
var upper = Infinity;
var lower = -Infinity;
for (var j = 0; j < n; j++) {
var jbounds = bounds[j];
if (!jbounds) continue;
if (ui[j] !== 0) {
if (jbounds[0] !== undefined && isFinite(jbounds[0])) {
lower = (ui[j] > 0 ? Math.max : Math.min)(lower, (jbounds[0] - p[j]) / ui[j]);
}
if (jbounds[1] !== undefined && isFinite(jbounds[1])) {
upper = (ui[j] > 0 ? Math.min : Math.max)(upper, (jbounds[1] - p[j]) / ui[j]);
}
}
}
return [lower, upper];
}
: function () {
return [-Infinity, Infinity];
};
// A function to evaluate:
pj = [];
fi = function (t) {
for (var i = 0; i < n; i++) {
pj[i] = p[i] + ui[i] * t;
}
return f(pj);
};
iter = 0;
perr = 0;
while (++iter < maxIter) {
// Reinitialize the search vectors:
if (iter % (n) === 0) {
for (i = 0; i < n; i++) {
u[i] = [];
for (j = 0; j < n; j++) {
u[i][j] = i === j ? 1 : 0;
}
}
}
// Store the starting point p0:
for (j = 0, p0 = []; j < n; j++) {
p0[j] = p[j];
}
// Minimize over each search direction u[i]:
for (i = 0; i < n; i++) {
ui = u[i];
// Compute bounds based on starting point p in the
// direction ui:
tlimit = bound(p, ui);
// Minimize using golden section method:
dx = 0.1;
tmin = minimize1d(fi, {
lowerBound: tlimit[0],
upperBound: tlimit[1],
initialIncrement: dx,
tolerance: dx * tol1d
});
if (tmin === 0) {
return p;
}
// Update the solution vector:
for (j = 0; j < n; j++) {
p[j] += tmin * ui[j];
}
constrain(p);
if (status) status.points.push(p.slice());
}
// Throw out the first search direction:
u.shift();
// Construct a new search direction:
for (j = 0, un = [], sum = 0; j < n; j++) {
un[j] = p[j] - p0[j];
sum += un[j] * un[j];
}
// Normalize:
sum = Math.sqrt(sum);
if (sum > 0) {
for (j = 0; j < n; j++) {
un[j] /= sum;
}
} else {
// Exactly nothing moved, so it it appears we've converged. In particular,
// it's possible the solution is up against a boundary and simply can't
// move farther.
return p;
}
u.push(un);
// One more minimization, this time along the new direction:
ui = un;
tlimit = bound(p, ui);
dx = 0.1;
tmin = minimize1d(fi, {
lowerBound: tlimit[0],
upperBound: tlimit[1],
initialIncrement: dx,
tolerance: dx * tol1d
});
if (tmin === 0) {
return p;
}
err = 0;
for (j = 0; j < n; j++) {
du = tmin * ui[j];
err += du * du;
p[j] += du;
}
constrain(p);
if (status) status.points.push(p.slice());
err = Math.sqrt(err);
if (verbose) console.log('Iteration ' + iter + ': ' + (err / perr) + ' f(' + p + ') = ' + f(p));
if (err / perr < tol) return p;
perr = err;
}
return p;
}