-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrustregion.cpp
executable file
·179 lines (161 loc) · 6 KB
/
trustregion.cpp
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
/*
Copyright ©2013 The Regents of the University of California
(Regents). All Rights Reserved. Permission to use, copy, modify, and
distribute this software and its documentation for educational,
research, and not-for-profit purposes, without fee and without a
signed licensing agreement, is hereby granted, provided that the
above copyright notice, this paragraph and the following two
paragraphs appear in all copies, modifications, and
distributions. Contact The Office of Technology Licensing, UC
Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620,
(510) 643-7201, for commercial licensing opportunities.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "optimization.hpp"
#include "sparse_solver.hpp"
#include "util.hpp"
using namespace std;
// v = x + y
static void add (vector<double> &v, const vector<double> &x,
const vector<double> &y);
// v = a x + b y
static void add (vector<double> &v, double a, const vector<double> &x,
double b, const vector<double> &y);
// v = a x
static void scalar_mult (vector<double> &v, double a, const vector<double> &x);
static double dot (const vector<double> &x, const vector<double> &y);
static double dot (const vector<double> &x, const SpMat<double> &A,
const vector<double> &y);
static double norm (const vector<double> &x);
bool minimize_in_ball (const vector<double> &g, const SpMat<double> &H,
double radius, vector<double> &p);
void trust_region_method (const NLOpt &problem, OptOptions opt, bool verbose) {
// see Nocedal and Wright 2006, algorithm 4.1
int n = problem.nvar;
double radius = 1e-3;
double eta = 0.125;
vector<double> x(n), p(n), x_new(n);
problem.initialize(&x[0]);
problem.precompute(&x[0]);
double f = problem.objective(&x[0]);
vector<double> g(n);
SpMat<double> H(n,n);
assert(problem.hessian(&x[0], H));
for (int iter = 0; iter < opt.max_iter(); iter++) {
problem.gradient(&x[0], &g[0]);
if (norm(g) < opt.eps_g())
break;
problem.hessian(&x[0], H);
bool boundary = minimize_in_ball(g, H, radius, p);
add(x_new, x, p);
problem.precompute(&x_new[0]);
double f_new = problem.objective(&x_new[0]);
double delta_f = f_new - f;
double delta_m = dot(g, p) + dot(p, H, p)/2;
double ratio = delta_f/delta_m;
radius = (ratio < 0.25) ? radius/4
: (ratio > 0.75 && boundary) ? 2*radius
: radius;
if (ratio > eta) {
x.swap(x_new);
if (radius < opt.eps_x() || f - f_new < opt.eps_f())
break;
f = f_new;
problem.precompute(&x[0]);
}
}
problem.finalize(&x[0]);
}
// finds t such that ||x1 + t (x2 - x1)|| = r
// where n1 = ||x1||, n2 = ||x2||, d = x1 dot x2
double line_circle_intersection (double n1, double n2, double d, double r);
bool minimize_in_ball (const vector<double> &g, const SpMat<double> &H,
double radius, vector<double> &p) {
// int n = g.size();
static vector<double> p1, p2;
scalar_mult(p1, -dot(g, g)/dot(g, H, g), g);
p2 = eigen_linear_solve(H, g);
scalar_mult(p2, -1, p2);
double n1 = norm(p1), n2 = norm(p2);
if (n2 < radius) {
p.swap(p2);
return false;
} else if (n1 < radius) {
double d12 = dot(p1, p2);
double t = line_circle_intersection(n1, n2, d12, radius);
add(p, 1-t, p1, t, p2);
return true;
} else {
scalar_mult(p, radius/n1, p1);
return true;
}
}
double line_circle_intersection (double n1, double n2, double d, double r) {
double a = sq(n1) - 2*d + sq(n2),
b = -2*sq(n1) + 2*d,
c = sq(n1) - sq(r);
double x[2];
int nsol = solve_quadratic(a, b, c, x);
return (nsol < 2) ? 0 : (x[0] >= 0) ? x[0] : x[1];
}
static void add (vector<double> &v, const vector<double> &x,
const vector<double> &y) {
int n = x.size();
v.resize(n);
#pragma omp parallel for
for (int i = 0; i < n; i++)
v[i] = x[i] + y[i];
}
static void add (vector<double> &v, double a, const vector<double> &x,
double b, const vector<double> &y) {
int n = x.size();
v.resize(n);
#pragma omp parallel for
for (int i = 0; i < n; i++)
v[i] = a*x[i] + b*y[i];
}
static void scalar_mult (vector<double> &v, double a, const vector<double> &x) {
int n = x.size();
v.resize(n);
#pragma omp parallel for
for (int i = 0; i < n; i++)
v[i] = a*x[i];
}
static double dot (const vector<double> &x, const vector<double> &y) {
int n = x.size();
double d = 0;
#pragma omp parallel for reduction(+:d)
for (int i = 0; i < n; i++)
d += x[i]*y[i];
return d;
}
static double dot (const vector<double> &x, const SpMat<double> &A,
const vector<double> &y) {
int n = x.size();
double d = 0;
#pragma omp parallel for reduction(+:d)
for (int i = 0; i < n; i++) {
double Ayi = 0;
const SpVec<double> &Ai = A.rows[i];
for (int jj = 0; jj < Ai.indices.size(); jj++) {
int j = Ai.indices[jj];
double Aij = Ai.entries[jj];
Ayi += Aij*y[j];
}
d += x[i]*Ayi;
}
return d;
}
static double norm (const vector<double> &x) {
return sqrt(dot(x,x));
}