Debian Codegraph @

src/dr/math/BoundsCheckedFunction.java

raw symbols SCIP
1 /*
2 * BoundsCheckedFunction.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 /**
30 * returns a very large number instead of the function value
31 * if arguments are out of bound (useful for minimization with
32 * minimizers that don't check argument boundaries)
33 *
34 * @author Korbinian Strimmer
35 */
36 public class BoundsCheckedFunction implements MultivariateFunction {
37 /**
38 * construct bound-checked multivariate function
39 * (a large number will be returned on function evaluation if argument
40 * is out of bounds; default is 1000000)
41 *
42 * @param func unconstrained multivariate function
43 */
44 public BoundsCheckedFunction(MultivariateFunction func) {
45 this(func, 1000000);
46 }
47
48 /**
49 * construct constrained multivariate function
50 *
51 * @param func unconstrained multivariate function
52 * @param largeNumber value returned on function evaluation
53 * if argument is out of bounds
54 */
55 public BoundsCheckedFunction(MultivariateFunction func, double largeNumber) {
56 f = func;
57 veryLarge = largeNumber;
58 }
59
60 /**
61 * computes function value, taking into account the constraints on the
62 * argument
63 *
64 * @param x function argument
65 * @return function value (if argument is not in the predefined constrained area
66 * a very large number is returned instead of the true function value)
67 */
68 public double evaluate(double[] x) {
69 int len = f.getNumArguments();
70
71 for (int i = 0; i < len; i++) {
72 if (x[i] < f.getLowerBound(i) ||
73 x[i] > f.getUpperBound(i)) {
74 return veryLarge;
75 }
76 }
77
78 return f.evaluate(x);
79 }
80
81 public int getNumArguments() {
82 return f.getNumArguments();
83 }
84
85 public double getLowerBound(int n) {
86 return f.getLowerBound(n);
87 }
88
89 public double getUpperBound(int n) {
90 return f.getUpperBound(n);
91 }
92
93 //
94 // Private stuff
95 //
96
97 private MultivariateFunction f;
98 private double veryLarge;
99 }