Debian Codegraph @

src/dr/inference/model/JointParameter.java

raw symbols SCIP
1 /*
2 * JointParameter.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
30 /**
31 * A parameter which controls the values of a set of other parameters
32 *
33 * @author Andrew Rambaut
34 * @version $Id$
35 */
36 public class JointParameter extends Parameter.Abstract implements VariableListener {
37
38 public JointParameter(String name, Parameter[] params) {
39 this(name);
40
41 for (Parameter parameter : params) {
42 addParameter(parameter);
43 }
44 }
45
46 public JointParameter(String name) {
47 this.name = name;
48 dimension = 0;
49 }
50
51 public void addParameter(Parameter param) {
52
53 if (dimension == 0) {
54 dimension = param.getDimension();
55 } else {
56 for (int dim = 0; dim < dimension; dim++) {
57 param.setParameterValue(dim, parameters.get(0).getParameterValue(dim));
58 }
59 if (param.getDimension() != dimension) {
60 throw new RuntimeException("subsequent parameters do not match the dimensionality of the first");
61 }
62 }
63
64 // AR - I think we ignore the messages from the containing parameters. Possibly would be a good
65 // idea to check they don't change independently of this one.
66 // param.addParameterListener(this);
67
68 parameters.add(param);
69 }
70
71 /**
72 * @return name if the parameter has been given a specific name, else it returns getId()
73 */
74 public final String getParameterName() {
75 if (name != null) return name;
76 return getId();
77 }
78
79 public int getDimension() {
80 return dimension;
81 }
82
83 public void setDimension(int dim) {
84 throw new RuntimeException();
85 }
86
87 public void addBounds(Bounds<Double> bounds) {
88 throw new RuntimeException("Can't add bounds to a joint parameter, only its components");
89 }
90
91 public Bounds<Double> getBounds() {
92 return parameters.get(0).getBounds();
93 }
94
95 public void addDimension(int index, double value) {
96 throw new RuntimeException();
97 }
98
99 public double removeDimension(int index) {
100 throw new RuntimeException();
101 }
102
103
104 public double getParameterValue(int dim) {
105 return parameters.get(0).getParameterValue(dim);
106 }
107
108 public void setParameterValue(int dim, double value) {
109 for (Parameter parameter : parameters) {
110 parameter.setParameterValue(dim, value);
111 }
112 }
113
114 public void setParameterValueQuietly(int dim, double value) {
115 for (Parameter parameter : parameters) {
116 parameter.setParameterValueQuietly(dim, value);
117 }
118 }
119
120 public void setParameterValueNotifyChangedAll(int dim, double value){
121 for (Parameter parameter : parameters) {
122 parameter.setParameterValueNotifyChangedAll(dim, value);
123 }
124 }
125
126 protected void storeValues() {
127 for (Parameter parameter : parameters) {
128 parameter.storeParameterValues();
129 }
130 }
131
132 protected void restoreValues() {
133 for (Parameter parameter : parameters) {
134 parameter.restoreParameterValues();
135 }
136 }
137
138 protected final void acceptValues() {
139 for (Parameter parameter : parameters) {
140 parameter.acceptParameterValues();
141 }
142 }
143
144 protected final void adoptValues(Parameter source) {
145 // the parameters that make up a compound parameter will have
146 // this function called on them individually so we don't need
147 // to do anything here.
148 }
149
150 public String toString() {
151 StringBuffer buffer = new StringBuffer(String.valueOf(getParameterValue(0)));
152 final Bounds bounds = getBounds();
153 buffer.append("[").append(String.valueOf(bounds.getLowerLimit(0)));
154 buffer.append(",").append(String.valueOf(bounds.getUpperLimit(0))).append("]");
155
156 for (int i = 1; i < getDimension(); i++) {
157 buffer.append(", ").append(String.valueOf(getParameterValue(i)));
158 buffer.append("[").append(String.valueOf(bounds.getLowerLimit(i)));
159 buffer.append(",").append(String.valueOf(bounds.getUpperLimit(i))).append("]");
160 }
161 return buffer.toString();
162 }
163
164 // ****************************************************************
165 // Parameter listener interface
166 // ****************************************************************
167
168 public void variableChangedEvent(Variable variable, int index, ChangeType type) {
169
170 if (variable.getSize() > 1) {
171 fireParameterChangedEvent(index, type);
172 } else {
173 fireParameterChangedEvent();
174 }
175 }
176
177 private final ArrayList<Parameter> parameters = new ArrayList<Parameter>();
178
179 private int dimension;
180 private String name;
181
182 }