Debian Codegraph @

src/dr/inference/model/DuplicatedParameter.java

raw symbols SCIP
1 /*
2 * DuplicatedParameter.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 /**
29 * @author Marc Suchard
30 */
31 public class DuplicatedParameter extends Parameter.Abstract implements VariableListener {
32
33 public DuplicatedParameter(Parameter parameter) {
34 this.parameter = parameter;
35 parameter.addVariableListener(this);
36 copies = 1;
37 originalBounds = parameter.getBounds();
38 bounds = originalBounds;
39 }
40
41 public void addDuplicationParameter(Parameter dupParameter) {
42 this.dupParameter = dupParameter;
43 dupParameter.addParameterListener(this);
44 updateDuplication();
45 }
46
47 private void updateDuplication() {
48 copies = (int) dupParameter.getParameterValue(0);
49 final int originalLength = originalBounds.getBoundsDimension();
50 double[] lowers = new double[getDimension()];
51 double[] uppers = new double[getDimension()];
52 for(int i=0; i<originalLength; i++) {
53 lowers[i] = originalBounds.getLowerLimit(i);
54 uppers[i] = originalBounds.getUpperLimit(i);
55 }
56 for(int i=1; i<copies; i++) {
57 System.arraycopy(lowers,0,lowers,i*originalLength,originalLength);
58 System.arraycopy(uppers,0,uppers,i*originalLength,originalLength);
59 }
60 bounds = new DefaultBounds(uppers,lowers);
61 }
62
63 public int getDimension() {
64 return parameter.getDimension() * copies;
65 }
66
67 protected void storeValues() {
68 parameter.storeParameterValues();
69 }
70
71 protected void restoreValues() {
72 parameter.restoreParameterValues();
73 }
74
75 protected void acceptValues() {
76 parameter.acceptParameterValues();
77 }
78
79 protected void adoptValues(Parameter source) {
80 parameter.adoptParameterValues(source);
81 }
82
83 public double getParameterValue(int dim) {
84 return parameter.getParameterValue(dim % parameter.getDimension());
85 }
86
87 public void setParameterValue(int dim, double value) {
88 parameter.setParameterValue(dim % parameter.getDimension(), value);
89 fireParameterChangedEvent(dim, Parameter.ChangeType.VALUE_CHANGED);
90 }
91
92 public void setParameterValueQuietly(int dim, double value) {
93 parameter.setParameterValueQuietly(dim % parameter.getDimension(), value);
94 }
95
96 public void setParameterValueNotifyChangedAll(int dim, double value){
97 parameter.setParameterValueNotifyChangedAll(dim % parameter.getDimension(), value);
98 }
99
100 public String getParameterName() {
101 if (getId() == null)
102 return "duplicated" + parameter.getParameterName();
103 return getId();
104 }
105
106 public void addBounds(Bounds bounds) {
107 throw new RuntimeException("Not yet implemented.");
108 }
109
110 public Bounds<Double> getBounds() {
111 return bounds;
112 }
113
114 public void addDimension(int index, double value) {
115 throw new RuntimeException("Not yet implemented.");
116 }
117
118 public double removeDimension(int index) {
119 throw new RuntimeException("Not yet implemented.");
120 }
121
122 public void variableChangedEvent(Variable variable, int index, ChangeType type) {
123 if (variable == dupParameter) {
124 updateDuplication();
125 }
126 // Values have changed, so notify listeners
127 for (int i = 0; i < copies; ++i) {
128 // fire once for each duplication
129 fireParameterChangedEvent(index + i * parameter.getDimension(), Parameter.ChangeType.VALUE_CHANGED);
130 }
131 }
132
133 private final Parameter parameter;
134 private Parameter dupParameter;
135 private int copies;
136 private Bounds<Double> bounds;
137 private final Bounds<Double> originalBounds;
138
139 }