Debian Codegraph @

src/dr/inference/model/ValuesPool.java

raw symbols SCIP
1 /*
2 * ValuesPool.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 Joseph Heled
30 * Date: 4/09/2009
31 */
32 public class ValuesPool extends DefaultModel {
33 private final Variable<Double> pool;
34 private final Variable<Double> selector;
35 private final double defaultValue;
36
37 ValuesPool(Variable<Double> pool, Variable<Double> selector, Double defaultValue) {
38 this.pool = pool;
39 this.selector = selector;
40 this.defaultValue = defaultValue;
41
42 assert pool.getSize() == selector.getSize();
43
44 //pool.addVariableListener(this);
45 // selector.addVariableListener(this);
46
47 addVariable(pool);
48 addVariable(selector);
49
50 addStatistic(numberOfParams);
51 }
52
53 public Variable<Double> getPool() {
54 return pool;
55 }
56
57 public Variable<Double> getSelector() {
58 return selector;
59 }
60
61 public int length() {
62 return pool.getSize();
63 }
64
65 private int get(int index) {
66 final double s = selector.getValue(index);
67 return s < 0 ? -1 : (int)(s+0.5);
68 }
69
70 public Double getValue(int index) {
71 final int s = get(index);
72 return s < 0 ? defaultValue : pool.getValue(s);
73 }
74
75 public Bounds<Double> getBounds() {
76 return bounds;
77 }
78
79 private final Bounds<Double> bounds = new Bounds<Double>() {
80 public Double getUpperLimit(int dimension) {
81 final int s = get(dimension);
82 return s < 0 ? defaultValue : pool.getBounds().getUpperLimit(s);
83 }
84
85 public Double getLowerLimit(int dimension) {
86 final int s = get(dimension);
87 return s < 0 ? defaultValue : pool.getBounds().getLowerLimit(s);
88 }
89
90 public int getBoundsDimension() {
91 return pool.getSize();
92 }
93 };
94
95 @SuppressWarnings({"FieldCanBeLocal"})
96 private final Statistic numberOfParams = new Statistic.Abstract() {
97
98 public String getStatisticName() {
99 return "numberOfParams";
100 }
101
102 public int getDimension() {
103 return 1;
104 }
105
106 public double getStatisticValue(int dim) {
107 double m = -1;
108 for(int k = 0; k < selector.getSize(); ++k) {
109 final Double sk = selector.getValue(k);
110 if( sk > m ) {
111 m = sk;
112 }
113 }
114 return m+1;
115 }
116 };
117
118 public boolean hasChanged(int i, Object object, int index) {
119 if( object == pool ) {
120 if( get(i) == index ) {
121 return true;
122 }
123 } else if ( object == selector ) {
124 if( i == index ) {
125 return true;
126 }
127 }
128 return false;
129 }
130 }