Debian Codegraph @

src/dr/inference/model/EqualityConstraintModel.java

raw symbols SCIP
1 /*
2 * EqualityConstraintModel.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.xml.*;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 /**
34 * @author Marc A. Suchard
35 * @author Andrew Rambaut
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 // Do nothing
51 }
52
53 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
54 if (noReentry) {
55 return; // Already processing a member variable
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 // Do nothing
83 }
84
85 protected void restoreState() {
86 // Do nothing
87 }
88
89 protected void acceptState() {
90 // Do nothing
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 // Can take an average
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 }