| 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.xml.*; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.List; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class EqualityConstraintModel extends AbstractModel { |
| 38 |
|
| 39 |
public static final String EQUALITY_CONSTRAINT = "equalityConstraint"; |
| 40 |
|
| 41 |
public EqualityConstraintModel(String name, List<Variable> parameterList) { |
| 42 |
super(name); |
| 43 |
this.parameterList = parameterList; |
| 44 |
for (Variable p : parameterList) { |
| 45 |
addVariable(p); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) { |
| 54 |
if (noReentry) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
noReentry = true; |
| 59 |
if (type == Variable.ChangeType.ALL_VALUES_CHANGED) { |
| 60 |
Object[] newValues = variable.getValues(); |
| 61 |
for (Variable p : parameterList) { |
| 62 |
if (p != variable) { |
| 63 |
for (int i = 0; i < newValues.length; ++i) { |
| 64 |
p.setValue(i, newValues[i]); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} else if (type == Variable.ChangeType.VALUE_CHANGED) { |
| 69 |
Object newValue = variable.getValue(index); |
| 70 |
for (Variable p : parameterList) { |
| 71 |
if (p != variable) { |
| 72 |
p.setValue(index, newValue); |
| 73 |
} |
| 74 |
} |
| 75 |
} else { |
| 76 |
throw new IllegalArgumentException("Variable dimensional updates are not yet implemented."); |
| 77 |
} |
| 78 |
noReentry = false; |
| 79 |
} |
| 80 |
|
| 81 |
protected void storeState() { |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
protected void restoreState() { |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
protected void acceptState() { |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
private static void setEqual(List<Variable> parameterList) { |
| 94 |
|
| 95 |
final int dim = parameterList.get(0).getSize(); |
| 96 |
|
| 97 |
|
| 98 |
if (parameterList.get(0) instanceof Parameter) { |
| 99 |
|
| 100 |
double[] total = new double[dim]; |
| 101 |
|
| 102 |
for (Variable v : parameterList) { |
| 103 |
Parameter p = (Parameter) v; |
| 104 |
for (int j = 0; j < dim; ++j) { |
| 105 |
total[j] += p.getParameterValue(j); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
for (int j = 0; j < dim; ++j) { |
| 110 |
total[j] /= parameterList.size(); |
| 111 |
} |
| 112 |
|
| 113 |
for (Variable v : parameterList) { |
| 114 |
Parameter p = (Parameter) v; |
| 115 |
for (int j = 0; j < dim; ++j) { |
| 116 |
p.setParameterValue(j, total[j]); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private static int checkDimensions(List<Parameter> parameterList) { |
| 123 |
int dim = parameterList.get(0).getDimension(); |
| 124 |
for (Parameter p : parameterList) { |
| 125 |
if (p.getDimension() != dim) { |
| 126 |
return -1; |
| 127 |
} |
| 128 |
} |
| 129 |
return dim; |
| 130 |
} |
| 131 |
|
| 132 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 133 |
|
| 134 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 135 |
|
| 136 |
int dim = -1; |
| 137 |
double[] firstValues = null; |
| 138 |
List<Variable> parameterList = new ArrayList<Variable>(); |
| 139 |
|
| 140 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 141 |
Parameter param = (Parameter) xo.getChild(i); |
| 142 |
if (i == 0) { |
| 143 |
dim = param.getDimension(); |
| 144 |
firstValues = param.getParameterValues(); |
| 145 |
} else { |
| 146 |
if (param.getDimension() != dim) { |
| 147 |
throw new XMLParseException("All parameters must have the same dimension."); |
| 148 |
} |
| 149 |
for (int j = 0; j < dim; ++j) { |
| 150 |
param.setParameterValue(j, firstValues[j]); |
| 151 |
} |
| 152 |
} |
| 153 |
parameterList.add(param); |
| 154 |
} |
| 155 |
return new EqualityConstraintModel("equalityConstraint", parameterList); |
| 156 |
} |
| 157 |
|
| 158 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 159 |
return rules; |
| 160 |
} |
| 161 |
|
| 162 |
private final XMLSyntaxRule[] rules = { |
| 163 |
new ElementRule(Parameter.class, 2, Integer.MAX_VALUE), |
| 164 |
}; |
| 165 |
|
| 166 |
public String getParserDescription() { |
| 167 |
return "Forces a set of variables to have equal values"; |
| 168 |
} |
| 169 |
|
| 170 |
public Class getReturnType() { |
| 171 |
return EqualityConstraintModel.class; |
| 172 |
} |
| 173 |
|
| 174 |
public String getParserName() { |
| 175 |
return EQUALITY_CONSTRAINT; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
final private List<Variable> parameterList; |
| 180 |
private boolean noReentry = false; |
| 181 |
} |