Debian Codegraph @

src/dr/inference/operators/Scalable.java

raw symbols SCIP
1 /*
2 * Scalable.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.inference.operators;
27
28 import dr.inference.model.Bounds;
29 import dr.inference.model.Parameter;
30
31 /**
32 * A generic interface for objects capable of scaling.
33 * <p/>
34 * A default impelementation for any parameter.
35 *
36 * @author Alexei Drummond
37 * @author Andrew Rambaut
38 * @version $Id: UpDownOperator.java,v 1.25 2005/06/14 10:40:34 rambaut Exp $
39 */
40
41 public interface Scalable {
42 /**
43 * @param factor scaling factor
44 * @param nDims
45 * @return Number of dimentions.
46 */
47 int scale(double factor, int nDims, boolean testBounds);
48
49 /**
50 * Checks that all elements are within their bounds and returns
51 * false if any are not.
52 * @return
53 */
54 boolean testBounds();
55
56 /**
57 * @return Name for display purposes.
58 */
59 String getName();
60
61 public class Default implements Scalable {
62 private final Parameter parameter;
63
64 public Default(Parameter p) {
65 this.parameter = p;
66 }
67
68 public int scale(double factor, int nDims, boolean testBounds) {
69 assert nDims <= 0;
70 final int dimension = parameter.getDimension();
71
72
73 for (int i = 0; i < dimension; ++i) {
74 // scale offset by the lower bound
75 parameter.setParameterValue(i, (parameter.getParameterValue(i)) * factor);
76 }
77
78 if (testBounds) {
79 final Bounds<Double> bounds = parameter.getBounds();
80
81 for (int i = 0; i < dimension; i++) {
82 final double value = parameter.getParameterValue(i);
83
84 if (value < bounds.getLowerLimit(i) || value > bounds.getUpperLimit(i)) {
85 throw new RuntimeException("proposed value outside bounds");
86 }
87 }
88 }
89
90 return dimension;
91 }
92
93 public boolean testBounds() {
94 final int dimension = parameter.getDimension();
95
96
97 final Bounds<Double> bounds = parameter.getBounds();
98
99 for (int i = 0; i < dimension; i++) {
100 final double value = parameter.getParameterValue(i);
101
102 if (value < bounds.getLowerLimit(i) || value > bounds.getUpperLimit(i)) {
103 return false;
104 }
105 }
106
107 return true;
108 }
109
110 public String getName() {
111 return parameter.getParameterName();
112 }
113
114
115 /**
116 *
117 * This method scales values of all dimesions quitely except the last one where is also signals that
118 * all values in all dimensions have been scaled. This prevents repeated firing of Events when in fact
119 * only ONE parameter is changed.
120 *
121 */
122 public int scaleAllAndNotify(double factor, int nDims, boolean testBounds) {
123
124 assert nDims <= 0;
125 final int dimension = parameter.getDimension();
126 final int dimMinusOne = dimension-1;
127
128 for(int i = 0; i < dimMinusOne; ++i) {
129 // scale offset by the lower bound
130 parameter.setParameterValueQuietly(i, parameter.getParameterValue(i) * factor);
131 }
132
133 parameter.setParameterValueNotifyChangedAll(dimMinusOne, parameter.getParameterValue(dimMinusOne) * factor);
134
135 if (testBounds) {
136 final Bounds<Double> bounds = parameter.getBounds();
137
138 for (int i = 0; i < dimension; i++) {
139 final double value = parameter.getParameterValue(i);
140 if (value < bounds.getLowerLimit(i) || value > bounds.getUpperLimit(i)) {
141 throw new RuntimeException("proposed value outside bounds");
142 }
143 }
144 }
145 return dimension;
146 }
147 }
148 }