| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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(); |
| 46 |
bounds = makeIntersectionBounds(); |
| 47 |
|
| 48 |
for (Parameter parameter : params) { |
| 49 |
parameter.addParameterListener(this); |
| 50 |
|
| 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 |
|
| 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 |
} |