Debian Codegraph @

src/dr/inference/model/Variable.java

raw symbols SCIP
1 /*
2 * Variable.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.inference.loggers.LogColumn;
29 import dr.inference.loggers.Loggable;
30 import dr.inference.loggers.NumberColumn;
31 import dr.util.Identifiable;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * A generic random variable.
38 *
39 * @author Alexei Drummond
40 */
41 public interface Variable<V> extends Identifiable {
42
43 public enum ChangeType {
44 VALUE_CHANGED,
45 REMOVED,
46 ADDED,
47 ALL_VALUES_CHANGED
48 }
49
50 /**
51 * @return the name of this variable.
52 */
53 public String getVariableName();
54
55 public V getValue(int index);
56
57 public void setValue(int index, V value);
58
59 public V[] getValues();
60
61 /**
62 * @return the size of this variable - i.e. the length of the vector
63 */
64 public int getSize();
65
66 /**
67 * adds a parameter listener that is notified when this parameter changes.
68 *
69 * @param listener the listener
70 */
71 void addVariableListener(VariableListener listener);
72
73 /**
74 * removes a parameter listener.
75 *
76 * @param listener the listener
77 */
78 void removeVariableListener(VariableListener listener);
79
80 /**
81 * stores the state of this parameter for subsequent restore
82 */
83 void storeVariableValues();
84
85 /**
86 * restores the stored state of this parameter
87 */
88 void restoreVariableValues();
89
90 /**
91 * accepts the stored state of this parameter
92 */
93 void acceptVariableValues();
94
95 /**
96 * @return the bounds on this parameter
97 */
98 Bounds<V> getBounds();
99
100 void addBounds(Bounds<V> bounds);
101
102 public abstract class Base<V> implements Variable<V>, Loggable {
103 Base(String id) {
104 this.id = id;
105 }
106
107 protected void fireVariableChanged(int index) {
108 for (VariableListener listener : listeners) {
109 listener.variableChangedEvent(this, index, ChangeType.VALUE_CHANGED);
110 }
111 }
112
113 public void addVariableListener(VariableListener listener) {
114 listeners.add(listener);
115 }
116
117 public void removeVariableListener(VariableListener listener) {
118 listeners.remove(listener);
119 }
120
121 public String getId() {
122 return id;
123 }
124
125 public void setId(String id) {
126 this.id = id;
127 }
128
129 public String getVariableName() {
130 return id;
131 }
132
133 protected List<VariableListener> listeners = new ArrayList<VariableListener>();
134
135 protected String id;
136 }
137
138
139 public abstract class BaseNumerical<V extends Number> extends Base<V> {
140 BaseNumerical(String id) {
141 super(id);
142 }
143
144 class StatisticColumn extends NumberColumn {
145 private final int dim;
146
147 public StatisticColumn(String label, int dim) {
148 super(label);
149 this.dim = dim;
150 }
151
152 public double getDoubleValue() {
153 return getValue(dim).doubleValue();
154 }
155 }
156
157 /**
158 * @return the log columns.
159 */
160 public LogColumn[] getColumns() {
161 LogColumn[] columns = new LogColumn[getSize()];
162 if (getSize() == 1) {
163 columns[0] = new StatisticColumn(this.getVariableName(), 0);
164 } else {
165 for (int i = 0; i < getSize(); i++) {
166 columns[i] = new StatisticColumn(this.getVariableName() + "[" + i + "]", i);
167 }
168 }
169 return columns;
170 }
171 }
172
173 public class D implements Variable<Double>, Loggable {
174
175 public D(double value, int size) {
176 values = new double[size];
177 storedValues = new double[values.length];
178
179 for (int i = 0; i < size; i++) {
180 values[i] = value;
181 }
182 }
183
184 public D(double[] v) {
185 values = new double[v.length];
186 System.arraycopy(v, 0, values, 0, v.length);
187 storedValues = new double[values.length];
188 }
189
190 public D(String name, double[] v) {
191 this(v);
192 setId(name);
193 }
194
195 public D(String name, double value) {
196 this(name, new double[]{value});
197 }
198
199 public String getId() {
200 return id;
201 }
202
203 public void setId(String id) {
204 this.id = id;
205 }
206
207
208 public String getVariableName() {
209 return id;
210 }
211
212 public Double getValue(int index) {
213 return values[index];
214 }
215
216 public Double[] getValues() {
217 Double[] copyOfValues = new Double[values.length];
218 for (int i = 0; i < values.length; i++) {
219 copyOfValues[i] = values[i];
220 }
221 return copyOfValues;
222 }
223
224 public void setValue(int index, Double value) {
225 values[index] = value;
226 fireVariableChanged(index);
227 }
228
229 private void fireVariableChanged(int index) {
230 for (VariableListener listener : listeners) {
231 listener.variableChangedEvent(this, index, ChangeType.VALUE_CHANGED);
232 }
233 }
234
235 public int getSize() {
236 return values.length;
237 }
238
239 public void addVariableListener(VariableListener listener) {
240 listeners.add(listener);
241 }
242
243 public void removeVariableListener(VariableListener listener) {
244 listeners.remove(listener);
245 }
246
247 public void storeVariableValues() {
248 System.arraycopy(values, 0, storedValues, 0, storedValues.length);
249 }
250
251 public void restoreVariableValues() {
252 double[] temp = storedValues;
253 storedValues = values;
254 values = temp;
255 }
256
257 public void acceptVariableValues() {
258 }
259
260 public Bounds<Double> getBounds() {
261 if (bounds == null) {
262 return new Bounds<Double>() {
263 public Double getUpperLimit(int dimension) {
264 return Double.MAX_VALUE;
265 }
266
267 public Double getLowerLimit(int dimension) {
268 return -Double.MAX_VALUE;
269 }
270
271 public int getBoundsDimension() {
272 return getSize();
273 }
274 };
275 } else return bounds;
276 }
277
278 // **************************************************************
279 // Loggable IMPLEMENTATION
280 // **************************************************************
281
282 /**
283 * @return the log columns.
284 */
285 public LogColumn[] getColumns() {
286 LogColumn[] columns = new LogColumn[getSize()];
287 if (getSize() == 1) {
288 columns[0] = new StatisticColumn(getVariableName(), 0);
289 } else {
290 for (int i = 0; i < getSize(); i++) {
291 columns[i] = new StatisticColumn(getVariableName() + "[" + i + "]", i);
292 }
293 }
294 return columns;
295 }
296
297 /**
298 * Careful use! Do not write to the array provided!!
299 *
300 * @return the underlying array of doubles
301 */
302 public double[] peekValues() {
303 return values;
304 }
305
306 public void addBounds(Bounds<Double> b) {
307 if (bounds == null) {
308 bounds = new IntersectionBounds(getSize());
309 }
310 bounds.addBounds(b);
311 }
312
313 private class StatisticColumn extends NumberColumn {
314 private final int dim;
315
316 public StatisticColumn(String label, int dim) {
317 super(label);
318 this.dim = dim;
319 }
320
321 public double getDoubleValue() {
322 return getValue(dim);
323 }
324 }
325
326 String id;
327 double[] values;
328 double[] storedValues;
329 List<VariableListener> listeners = new ArrayList<VariableListener>();
330 private IntersectionBounds bounds = null;
331 }
332
333 public class DM implements Variable<double[]>, Loggable {
334
335 public DM(double[][] v) {
336
337 lower = new double[v.length];
338 upper = new double[v.length];
339
340 values = new double[v.length][v[0].length];
341 for (int i = 0; i < v.length; i++) {
342 System.arraycopy(v[i], 0, values[i], 0, v[i].length);
343 lower[i] = -Double.MAX_VALUE;
344 upper[i] = Double.MAX_VALUE;
345 }
346 storedValues = new double[values.length][values[0].length];
347 }
348
349 public String getId() {
350 return id;
351 }
352
353 public void setId(String id) {
354 this.id = id;
355 }
356
357
358 public String getVariableName() {
359 return id;
360 }
361
362 public double[] getValue(int index) {
363 return values[index];
364 }
365
366 public double[][] getValues() {
367 double[][] copyOfValues = new double[values.length][values[0].length];
368 for (int i = 0; i < values.length; i++) {
369 System.arraycopy(values[i], 0, copyOfValues[i], 0, values[i].length);
370 }
371 return copyOfValues;
372 }
373
374 public void setValue(int index, double[] value) {
375 System.arraycopy(value, 0, values[index], 0, values[index].length);
376
377 fireVariableChanged(index);
378 }
379
380 private void fireVariableChanged(int index) {
381 for (VariableListener listener : listeners) {
382 listener.variableChangedEvent(this, index, ChangeType.VALUE_CHANGED);
383 }
384 }
385
386 public int getSize() {
387 return values.length;
388 }
389
390 public void addVariableListener(VariableListener listener) {
391 listeners.add(listener);
392 }
393
394 public void removeVariableListener(VariableListener listener) {
395 listeners.remove(listener);
396 }
397
398 public void storeVariableValues() {
399 for (int i = 0; i < values.length; i++) {
400 System.arraycopy(values[i], 0, storedValues[i], 0, storedValues[i].length);
401 }
402 }
403
404 public void restoreVariableValues() {
405 double[][] temp = storedValues;
406 storedValues = values;
407 values = temp;
408 }
409
410 public void acceptVariableValues() {
411 }
412
413 public Bounds<double[]> getBounds() {
414 return new Bounds<double[]>() {
415 public double[] getUpperLimit(int dimension) {
416 return upper;
417 }
418
419 public double[] getLowerLimit(int dimension) {
420 return lower;
421 }
422
423 public int getBoundsDimension() {
424 return getSize();
425 }
426 };
427 }
428
429 public void addBounds(Bounds<double[]> bounds) {
430
431 }
432
433
434 // **************************************************************
435 // Loggable IMPLEMENTATION
436 // **************************************************************
437
438 /**
439 * @return the log columns.
440 */
441 public LogColumn[] getColumns() {
442 LogColumn[] columns = new LogColumn[getSize()];
443 for (int i = 0; i < getSize(); i++) {
444 double[] values = getValue(i);
445 for (int j = 0; j < values.length; j++) {
446 if (getSize() == 1) {
447 columns[i] = new StatisticColumn(getVariableName() + "[" + j + "]", i, j);
448 } else {
449 columns[i] = new StatisticColumn(getVariableName() + "[" + i + "," + j + "]", i, j);
450 }
451 }
452 }
453 return columns;
454 }
455
456 private class StatisticColumn extends NumberColumn {
457 private final int i;
458 private final int j;
459
460 public StatisticColumn(String label, int i, int j) {
461 super(label);
462 this.i = i;
463 this.j = j;
464 }
465
466 public double getDoubleValue() {
467 return getValue(i)[j];
468 }
469 }
470
471 String id;
472 double[][] values;
473 double[][] storedValues;
474 List<VariableListener> listeners = new ArrayList<VariableListener>();
475 double[] lower;
476 double[] upper;
477 }
478
479 public class I implements Variable<Integer>, Loggable {
480
481 public I(int value, int size) {
482 values = new int[size];
483 storedValues = new int[values.length];
484
485 for (int i = 0; i < size; i++) {
486 values[i] = value;
487 }
488 }
489
490 public I(int[] v) {
491 values = new int[v.length];
492 System.arraycopy(v, 0, values, 0, v.length);
493 storedValues = new int[values.length];
494 }
495
496 public I(String name, int[] v) {
497 this(v);
498 setId(name);
499 }
500
501 public I(String name, int value) {
502 this(name, new int[]{value});
503 }
504
505 public String getId() {
506 return id;
507 }
508
509 public void setId(String id) {
510 this.id = id;
511 }
512
513
514 public String getVariableName() {
515 return id;
516 }
517
518 public Integer getValue(int index) {
519 return values[index];
520 }
521
522 public Integer[] getValues() {
523 Integer[] copyOfValues = new Integer[values.length];
524 for (int i = 0; i < values.length; i++) {
525 copyOfValues[i] = values[i];
526 }
527 return copyOfValues;
528 }
529
530 public void setValue(int index, Integer value) {
531 values[index] = value;
532 fireVariableChanged(index);
533 }
534
535 private void fireVariableChanged(int index) {
536 for (VariableListener listener : listeners) {
537 listener.variableChangedEvent(this, index, ChangeType.VALUE_CHANGED);
538 }
539 }
540
541 public int getSize() {
542 return values.length;
543 }
544
545 public void addVariableListener(VariableListener listener) {
546 listeners.add(listener);
547 }
548
549 public void removeVariableListener(VariableListener listener) {
550 listeners.remove(listener);
551 }
552
553 public void storeVariableValues() {
554 System.arraycopy(values, 0, storedValues, 0, storedValues.length);
555 }
556
557 public void restoreVariableValues() {
558 int[] temp = storedValues;
559 storedValues = values;
560 values = temp;
561 }
562
563 public void acceptVariableValues() {
564 }
565
566 public Bounds<Integer> getBounds() {
567 if (bounds == null) {
568 return new Bounds<Integer>() {
569 public Integer getUpperLimit(int dimension) {
570 return Integer.MAX_VALUE;
571 }
572
573 public Integer getLowerLimit(int dimension) {
574 return -Integer.MAX_VALUE;
575 }
576
577 public int getBoundsDimension() {
578 return getSize();
579 }
580 };
581 } else return bounds;
582 }
583
584 // **************************************************************
585 // Loggable IMPLEMENTATION
586 // **************************************************************
587
588 /**
589 * @return the log columns.
590 */
591 public LogColumn[] getColumns() {
592 LogColumn[] columns = new LogColumn[getSize()];
593 if (getSize() == 1) {
594 columns[0] = new StatisticColumn(getVariableName(), 0);
595 } else {
596 for (int i = 0; i < getSize(); i++) {
597 columns[i] = new StatisticColumn(getVariableName() + "[" + i + "]", i);
598 }
599 }
600 return columns;
601 }
602
603 /**
604 * Careful use! Do not write to the array provided!!
605 *
606 * @return the underlying array of doubles
607 */
608 public int[] peekValues() {
609 return values;
610 }
611
612 public void addBounds(Bounds<Integer> b) {
613 if (bounds == null) {
614 bounds = b;
615 } else {
616 if (!(bounds instanceof Bounds.Staircase)) {
617 Bounds.Staircase newBounds = new Bounds.Staircase(getSize());
618 newBounds.addBounds(bounds);
619 bounds = newBounds;
620 }
621
622 ((Bounds.Staircase) bounds).addBounds(b);
623 }
624
625 }
626
627 private class StatisticColumn extends NumberColumn {
628 private final int dim;
629
630 public StatisticColumn(String label, int dim) {
631 super(label);
632 this.dim = dim;
633 }
634
635 public double getDoubleValue() {
636 return getValue(dim);
637 }
638 }
639
640 String id;
641 int[] values;
642 int[] storedValues;
643 List<VariableListener> listeners = new ArrayList<VariableListener>();
644 private Bounds<Integer> bounds = null;
645 }
646 }