Debian Codegraph @

src/dr/math/LineFunction.java

raw symbols SCIP
1 /*
2 * LineFunction.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math;
27
28 /**
29 * converts a multivariate function into a univariate function
30 *
31 * @author Korbinian Strimmer
32 */
33 public class LineFunction implements UnivariateFunction
34 {
35 /**
36 * construct univariate function from multivariate function
37 *
38 * @param func multivariate function
39 * @param start start point
40 * @param dir direction vector
41 */
42 public LineFunction(MultivariateFunction func)
43 {
44 f = func;
45
46 dim = f.getNumArguments();
47
48 x = new double[dim];
49 }
50
51 /**
52 * update start point and direction
53 * (bounds and search direction are NOT checked)
54 *
55 * @param start new start point
56 * @param dir new direction vector
57 */
58 public void update(double[] start, double[] dir)
59 {
60 s = start;
61 d = dir;
62
63 computeBounds();
64 }
65
66
67 /**
68 * get point associated with the one-dimensional parameter
69 * (bounds of of multivariate function are NOT checked)
70 *
71 * @param lambda argument
72 * @param p array for coordinates of corresponding point
73 */
74 public void getPoint(double lambda, double[] p)
75 {
76 for (int i = 0; i < dim; i++)
77 {
78 p[i] = s[i] + lambda*d[i];
79 }
80 }
81
82
83 // implementation of UnivariateFunction
84
85 /**
86 * evaluate f(start+lambda*dir)
87 */
88 public double evaluate(double lambda)
89 {
90 getPoint(lambda, x);
91
92 return f.evaluate(x);
93 }
94
95 public double getLowerBound()
96 {
97 return lowerBound;
98 }
99
100 public double getUpperBound()
101 {
102 return upperBound;
103 }
104
105 /**
106 * find parameter lambda within the given bounds
107 * that minimizes the univariate function
108 * (due to numerical inaccuaries it may happen
109 * that getPoint for the returned lambda produces
110 * a point that lies
111 * slightly out of bounds)
112 *
113 * @return lambda that achieves minimum
114 */
115 public double findMinimum()
116 {
117 if (um == null)
118 {
119 um = new UnivariateMinimum();
120 }
121 return um.findMinimum(this);
122 }
123
124
125 /**
126 * get parameter that limits the upper bound
127 *
128 * @return parameter number
129 */
130 public int getUpperBoundParameter()
131 {
132 return upperBoundParam;
133 }
134
135 /**
136 * get parameter that limits the lower bound
137 *
138 * @return parameter number
139 */
140 public int getLowerBoundParameter()
141 {
142 return lowerBoundParam;
143 }
144
145
146 /**
147 * check (and modify, if necessary) whether a point lies properly
148 * within the predefined bounds
149 *
150 * @param p coordinates of point
151 *
152 * @return true if p was modified, false otherwise
153 */
154 public boolean checkPoint(double[] p)
155 {
156 boolean modified = false;
157 for (int i = 0; i < dim; i++)
158 {
159 if (p[i] < f.getLowerBound(i))
160 {
161 p[i] = f.getLowerBound(i);
162 modified = true;
163 }
164 if (p[i] > f.getUpperBound(i))
165 {
166 p[i] = f.getUpperBound(i);
167 modified = true;
168 }
169 }
170
171 return modified;
172 }
173
174 /**
175 * determine active variables at a point p and corresponding
176 * gradient grad (if a component of p lies on a border and
177 * the corresponding component of the gradient points
178 * out of the border the variable is considered inactive)
179 *
180 * @param p coordinates of point
181 * @param grad gradient at that point
182 * @param list of active variables (on return)
183 *
184 * @return number of active variables
185 */
186 public int checkVariables(double[] p, double[] grad, boolean[] active)
187 {
188 // this seems to be a reasonable small value
189 double EPS = MachineAccuracy.SQRT_EPSILON;
190
191 int numActive = 0;
192 for (int i = 0; i < dim; i++)
193 {
194 active[i] = true;
195 if (p[i] <= f.getLowerBound(i)+EPS)
196 {
197 // no search towards lower boundary
198 if (grad[i] > 0)
199 {
200 active[i] = false;
201 }
202 }
203 else if (p[i] >= f.getUpperBound(i)-EPS)
204 {
205 // no search towards upper boundary
206 if (grad[i] < 0)
207 {
208 active[i] = false;
209 }
210 }
211 else
212 {
213 numActive++;
214 }
215 }
216
217 return numActive;
218 }
219
220 /**
221 * check direction vector. If it points out of the defined
222 * area at a point at the boundary the corresponding component
223 * of the direction vector is set to zero.
224 *
225 * @param p coordinates of point
226 * @param dir direction vector at that point
227 *
228 * @return number of changed components in direction vector
229 */
230 public int checkDirection(double[] p, double[] dir)
231 {
232 // this seems to be a reasonable small value
233 double EPS = MachineAccuracy.SQRT_EPSILON;
234
235 int numChanged = 0;
236 for (int i = 0; i < dim; i++)
237 {
238 if (p[i] <= f.getLowerBound(i)+EPS)
239 {
240 // no search towards lower boundary
241 if (dir[i] < 0)
242 {
243 dir[i] = 0;
244 numChanged++;
245 }
246 }
247 else if (p[i] >= f.getUpperBound(i)-EPS)
248 {
249 // no search towards upper boundary
250 if (dir[i] > 0)
251 {
252 dir[i] = 0;
253 numChanged++;
254 }
255 }
256 }
257
258 return numChanged;
259 }
260
261
262 //
263 // Private stuff
264 //
265
266 private MultivariateFunction f;
267 private int lowerBoundParam, upperBoundParam;
268 private int dim;
269 private double lowerBound, upperBound;
270 private double[] s, d, x;
271 private UnivariateMinimum um = null;
272
273 private void computeBounds()
274 {
275 boolean firstVisit = true;
276 for (int i = 0; i < dim; i++)
277 {
278 if (d[i] != 0)
279 {
280 double upper = (f.getUpperBound(i) - s[i])/d[i];
281 double lower = (f.getLowerBound(i) - s[i])/d[i];
282 if (lower > upper)
283 {
284 double tmp = upper;
285 upper = lower;
286 lower = tmp;
287 }
288
289 if (firstVisit)
290 {
291 lowerBound = lower;
292 lowerBoundParam = i;
293 upperBound = upper;
294 upperBoundParam = i;
295 firstVisit = false;
296 }
297 else
298 {
299 if (lower > lowerBound)
300 {
301 lowerBound = lower;
302 lowerBoundParam = i;
303 }
304 if (upper < upperBound)
305 {
306 upperBound = upper;
307 upperBoundParam = i;
308 }
309 }
310 }
311 }
312 }
313 }