Debian Codegraph @

src/dr/inference/model/EqualityConstrainedParameter.java

raw symbols SCIP
1 /*
2 * EqualityConstrainedParameter.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 dr.util.Author;
29 import dr.util.Citable;
30 import dr.util.Citation;
31 import dr.xml.*;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * @author Marc A. Suchard
38 */
39 public class EqualityConstrainedParameter extends Parameter.Abstract implements VariableListener {
40 public static final String EQUALITY_CONSTRAINED_PARAMETER = "constrainedEqualParameter";
41
42 public EqualityConstrainedParameter(String name, List<Parameter> params) {
43 super(name);
44 uniqueParameters = params;
45 dimension = uniqueParameters.get(0).getDimension(); // TODO Assumes that all parameters have the same dimension
46 bounds = makeIntersectionBounds();
47
48 for (Parameter parameter : params) {
49 parameter.addParameterListener(this);
50 // Convert all unique parameters' bounds into intersection bounds on all parameters
51 for (Parameter otherParameter : params) {
52 if (parameter != otherParameter) {
53 parameter.addBounds(otherParameter.getBounds());
54 }
55 }
56 }
57 StringBuilder sb = new StringBuilder("Constraining multiple parameters to be equal: ");
58 sb.append(getId()).append("\n");
59 java.util.logging.Logger.getLogger("dr.inference.model").info(sb.toString());
60 }
61
62 public final String getParameterName() {
63 return getId();
64 }
65
66 public int getDimension() {
67 return dimension;
68 }
69
70 public void setDimension(int dim) {
71 throw new UnsupportedOperationException();
72 }
73
74 public void addBounds(Bounds<Double> boundary) {
75 throw new UnsupportedOperationException();
76 }
77
78 public Bounds<Double> getBounds() {
79 return bounds;
80 }
81
82 private Bounds<Double> makeIntersectionBounds() {
83 Bounds<Double> bounds = new IntersectionBounds(dimension);
84 for (Parameter p : uniqueParameters) {
85 ((IntersectionBounds) bounds).addBounds(p.getBounds());
86 }
87 return bounds;
88 }
89
90 public void addDimension(int index, double value) {
91 throw new UnsupportedOperationException();
92 }
93
94 public double removeDimension(int index) {
95 throw new UnsupportedOperationException();
96 }
97
98 public double getParameterValue(int dim) {
99 return uniqueParameters.get(0).getParameterValue(dim);
100 }
101
102 public void setParameterValue(int dim, double value) {
103 isLocked = true;
104 for (Parameter p : uniqueParameters) {
105 p.setParameterValue(dim, value);
106 }
107 isLocked = false;
108 }
109
110 public void setParameterValueQuietly(int dim, double value) {
111 isLocked = true;
112 for (Parameter p : uniqueParameters) {
113 p.setParameterValueQuietly(dim, value);
114 }
115 isLocked = false;
116 }
117
118 public void setParameterValueNotifyChangedAll(int dim, double value) {
119 isLocked = true;
120 for (Parameter p : uniqueParameters) {
121 p.setParameterValueNotifyChangedAll(dim, value);
122 }
123 isLocked = false;
124 }
125
126 protected void storeValues() {
127 for (Parameter parameter : uniqueParameters) {
128 parameter.storeParameterValues();
129 }
130 }
131
132 protected void restoreValues() {
133 for (Parameter parameter : uniqueParameters) {
134 parameter.restoreParameterValues();
135 }
136 }
137
138 protected final void acceptValues() {
139 for (Parameter parameter : uniqueParameters) {
140 parameter.acceptParameterValues();
141 }
142 }
143
144 protected final void adoptValues(Parameter source) {
145 // Do nothing
146 }
147
148 public void variableChangedEvent(Variable variable, int index, ChangeType type) {
149
150 if (!isLocked) {
151 Parameter changedParameter = (Parameter) variable;
152
153 if (type == ChangeType.ALL_VALUES_CHANGED) {
154 double[] newValues = changedParameter.getParameterValues();
155 for (Parameter p : uniqueParameters) {
156 if (p != changedParameter) {
157 for (int i = 1; i < newValues.length; ++i) {
158 isLocked = true;
159 p.setParameterValueQuietly(i, newValues[i]);
160 isLocked = false;
161 }
162 isLocked = true;
163 p.setParameterValueNotifyChangedAll(0, newValues[0]);
164 isLocked = false;
165 }
166 }
167 } else if (type == ChangeType.VALUE_CHANGED) {
168 double newValue = changedParameter.getParameterValue(index);
169 for (Parameter p : uniqueParameters) {
170 if (p != changedParameter) {
171 isLocked = true;
172 p.setParameterValue(index, newValue);
173 isLocked = false;
174 }
175 }
176 } else {
177 throw new UnsupportedOperationException();
178 }
179 }
180 }
181
182 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
183
184 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
185
186 int dim = -1;
187 double[] firstValues = null;
188 List<Parameter> parameterList = new ArrayList<Parameter>();
189
190 for (int i = 0; i < xo.getChildCount(); ++i) {
191 Parameter param = (Parameter) xo.getChild(i);
192 if (i == 0) {
193 dim = param.getDimension();
194 firstValues = param.getParameterValues();
195 } else {
196 if (param.getDimension() != dim) {
197 throw new XMLParseException("All parameters must have the same dimension.");
198 }
199 for (int j = 0; j < dim; ++j) {
200 param.setParameterValue(j, firstValues[j]);
201 }
202 }
203 parameterList.add(param);
204 }
205 return new EqualityConstrainedParameter(xo.getId(), parameterList);
206 }
207
208 public XMLSyntaxRule[] getSyntaxRules() {
209 return rules;
210 }
211
212 private final XMLSyntaxRule[] rules = {
213 new ElementRule(Parameter.class, 2, Integer.MAX_VALUE),
214 };
215
216 public String getParserDescription() {
217 return "Forces a set of parameters to have equal values";
218 }
219
220 public Class getReturnType() {
221 return EqualityConstrainedParameter.class;
222 }
223
224 public String getParserName() {
225 return EQUALITY_CONSTRAINED_PARAMETER;
226 }
227 };
228
229 private final List<Parameter> uniqueParameters;
230
231 private final Bounds bounds;
232 private int dimension;
233 private boolean isLocked = false;
234 }