Debian Codegraph @

src/dr/inference/model/CompoundParameter.java

raw symbols SCIP
1 /*
2 * CompoundParameter.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.model;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32 * A multidimensional parameter constructed from its component parameters.
33 *
34 * @author Alexei Drummond
35 * @author Andrew Rambaut
36 * @version $Id: CompoundParameter.java,v 1.13 2005/06/14 10:40:34 rambaut Exp $
37 */
38 public class CompoundParameter extends Parameter.Abstract implements VariableListener {
39
40 public CompoundParameter(String name, Parameter[] params) {
41 this(name);
42 for (Parameter parameter : params) {
43 dimension += parameter.getDimension();
44 parameter.addParameterListener(this);
45 }
46
47 for (Parameter parameter : params) {
48 for (int j = 0; j < parameter.getDimension(); j++) {
49 parameters.add(parameter);
50 pindex.add(j);
51 }
52 uniqueParameters.add(parameter);
53 labelParameter(parameter);
54 }
55 }
56
57 public CompoundParameter(String name) {
58 this.name = name;
59 dimension = 0;
60 }
61
62 private void labelParameter(Parameter parameter) {
63 if (parameter.getParameterName() == null) {
64 String parameterName = name + uniqueParameters.size();
65 parameter.setId(parameterName);
66 }
67 }
68
69 public void addParameter(Parameter param) {
70
71 uniqueParameters.add(param);
72 for (int j = 0; j < param.getDimension(); j++) {
73 parameters.add(param);
74 pindex.add(j);
75 }
76 dimension += param.getDimension();
77 if (dimension != parameters.size()) {
78 throw new RuntimeException(
79 "dimension=" + dimension + " parameters.size()=" + parameters.size()
80 );
81 }
82 param.addParameterListener(this);
83 labelParameter(param);
84 }
85
86 public void removeParameter(Parameter param) {
87
88 int dim = 0;
89 for (Parameter parameter : uniqueParameters) {
90 if (parameter == param) {
91 break;
92 }
93 dim += parameter.getDimension();
94 }
95
96 for (int i = 0; i < param.getDimension(); i++) {
97 parameters.remove(dim);
98 pindex.remove(dim);
99 }
100
101 if (parameters.contains(param)) throw new RuntimeException();
102
103 uniqueParameters.remove(param);
104
105 dimension -= param.getDimension();
106 if (dimension != parameters.size()) throw new RuntimeException();
107 param.removeParameterListener(this);
108 }
109
110 /**
111 * @return name if the parameter has been given a specific name, else it returns getId()
112 */
113 public final String getParameterName() {
114 if (name != null) return name;
115 return getId();
116 }
117
118 public Parameter getParameter(int index) {
119 return uniqueParameters.get(index);
120 }
121
122 public int getParameterCount() {
123 return uniqueParameters.size();
124 }
125
126 public String getDimensionName(int dim) {
127 return parameters.get(dim).getDimensionName(pindex.get(dim));
128 }
129
130 public int getDimension() {
131 return dimension;
132 }
133
134 public void setDimension(int dim) {
135 throw new UnsupportedOperationException();
136 }
137
138 public void addBounds(Bounds<Double> boundary) {
139
140 if (bounds == null) {
141 bounds = new CompoundBounds();
142 // return;
143 } //else {
144 IntersectionBounds newBounds = new IntersectionBounds(getDimension());
145 newBounds.addBounds(bounds);
146
147 // }
148 ((IntersectionBounds) bounds).addBounds(boundary);
149 }
150
151 public Bounds<Double> getBounds() {
152
153 if (bounds == null) {
154 bounds = new CompoundBounds();
155 }
156 return bounds;
157 }
158
159 public void addDimension(int index, double value) {
160 Parameter p = parameters.get(index);
161 int pi = pindex.get(index);
162
163 parameters.add(index, p);
164 pindex.add(index, pi);
165
166 p.addDimension(pi, value);
167 for (int i = pi; i < p.getDimension(); i++) {
168 pindex.set(index, i);
169 index += 1;
170 }
171 }
172
173 public double removeDimension(int index) {
174 Parameter p = parameters.get(index);
175 int pi = pindex.get(index);
176
177 parameters.remove(index);
178 pindex.remove(index);
179
180 double v = p.removeDimension(pi);
181 for (int i = pi; i < p.getDimension(); i++) {
182 pindex.set(index, i);
183 index += 1;
184 }
185 return v;
186 }
187
188 public void fireParameterChangedEvent() {
189 doNotPropogateChangeUp = true;
190 for (Parameter p : parameters) {
191 p.fireParameterChangedEvent();
192 }
193 doNotPropogateChangeUp = false;
194 fireParameterChangedEvent(-1, ChangeType.ALL_VALUES_CHANGED);
195 }
196
197 public double getParameterValue(int dim) {
198 return parameters.get(dim).getParameterValue(pindex.get(dim));
199 }
200
201 public double[] inspectParametersValues() {
202 return getParameterValues();
203 }
204
205 public void setParameterValue(int dim, double value) {
206 parameters.get(dim).setParameterValue(pindex.get(dim), value);
207 }
208
209 // public void setParameterValue(int row, int column, double a)
210 // {
211 // getParameter(column).setParameterValue(row, a);
212 // }
213
214 public void setParameterValueQuietly(int dim, double value) {
215 parameters.get(dim).setParameterValueQuietly(pindex.get(dim), value);
216 }
217
218 // public void setParameterValueQuietly(int row, int column, double a){
219 // getParameter(column).setParameterValueQuietly(row, a);
220 // }
221
222 public void setParameterValueNotifyChangedAll(int dim, double value) {
223 parameters.get(dim).setParameterValueNotifyChangedAll(pindex.get(dim), value);
224 }
225
226 // public void setParameterValueNotifyChangedAll(int row, int column, double val){
227 // getParameter(column).setParameterValueNotifyChangedAll(row, val);
228 // }
229
230 protected void storeValues() {
231 for (Parameter parameter : uniqueParameters) {
232 parameter.storeParameterValues();
233 }
234 }
235
236 protected void restoreValues() {
237 for (Parameter parameter : uniqueParameters) {
238 parameter.restoreParameterValues();
239 }
240 }
241
242 protected void acceptValues() {
243 for (Parameter parameter : uniqueParameters) {
244 parameter.acceptParameterValues();
245 }
246 }
247
248 protected final void adoptValues(Parameter source) {
249 // the parameters that make up a compound parameter will have
250 // this function called on them individually so we don't need
251 // to do anything here.
252 }
253
254 public String toString() {
255 StringBuffer buffer = new StringBuffer(String.valueOf(getParameterValue(0)));
256 final Bounds bounds = getBounds();
257 buffer.append("[").append(String.valueOf(bounds.getLowerLimit(0)));
258 buffer.append(",").append(String.valueOf(bounds.getUpperLimit(0))).append("]");
259
260 for (int i = 1; i < getDimension(); i++) {
261 buffer.append(", ").append(String.valueOf(getParameterValue(i)));
262 buffer.append("[").append(String.valueOf(bounds.getLowerLimit(i)));
263 buffer.append(",").append(String.valueOf(bounds.getUpperLimit(i))).append("]");
264 }
265 return buffer.toString();
266 }
267
268 // ****************************************************************
269 // Parameter listener interface
270 // ****************************************************************
271
272 public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
273
274 int dim = 0;
275 for (Parameter parameter1 : uniqueParameters) {
276 if (variable == parameter1) {
277 if (!doNotPropogateChangeUp) {
278 fireParameterChangedEvent(dim + index, type);
279 }
280 break;
281 }
282 dim += parameter1.getDimension();
283 }
284 }
285
286 // ****************************************************************
287 // Private and protected stuff
288 // ****************************************************************
289
290 private class CompoundBounds implements Bounds<Double> {
291
292 public Double getUpperLimit(int dim) {
293 return parameters.get(dim).getBounds().getUpperLimit(pindex.get(dim));
294 }
295
296 public Double getLowerLimit(int dim) {
297 return parameters.get(dim).getBounds().getLowerLimit(pindex.get(dim));
298 }
299
300 public int getBoundsDimension() {
301 return getDimension();
302 }
303 }
304
305 protected ArrayList<Parameter> getParameters(){
306 return parameters;
307 }
308
309 private final List<Parameter> uniqueParameters = new ArrayList<Parameter>();
310
311 private final ArrayList<Parameter> parameters = new ArrayList<Parameter>();
312 private final ArrayList<Integer> pindex = new ArrayList<Integer>();
313 private Bounds bounds = null;
314 private int dimension;
315 private String name;
316
317 private boolean doNotPropogateChangeUp = false;
318
319 public static void main(String[] args) {
320
321 Parameter param1 = new Parameter.Default(2);
322 Parameter param2 = new Parameter.Default(2);
323 Parameter param3 = new Parameter.Default(2);
324
325 System.out.println(param1.getDimension());
326
327 CompoundParameter parameter = new CompoundParameter("parameter", new Parameter[]{param1, param2});
328 parameter.addParameter(param3);
329 parameter.removeParameter(param2);
330 }
331 }