Debian Codegraph @

src/dr/inference/operators/SimpleOperator.java

raw symbols SCIP
1 /*
2 * SimpleOperator.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 /**
27 *
28 */
29 package dr.inference.operators;
30
31 /**
32 * @author Sebastian Hoehna
33 *
34 */
35 public abstract class SimpleOperator implements MCMCOperator {
36
37 /**
38 *
39 */
40 public SimpleOperator() {
41 // TODO Auto-generated constructor stub
42 }
43
44 public abstract String getOperatorName();
45
46 /**
47 * @return the weight of this operator.
48 */
49 public final double getWeight() {
50 return weight;
51 }
52
53 /**
54 * Sets the weight of this operator.
55 */
56 public final void setWeight(double w) {
57 if (w > 0) {
58 weight = w;
59 } else throw new IllegalArgumentException("Weight must be a positive real. (called with " + w + ")");
60 }
61
62 public void accept(double deviation) {
63 lastDeviation = deviation;
64
65 if (!operateAllowed) {
66 operateAllowed = true;
67 accepted += 1;
68 sumDeviation += deviation;
69 } else throw new RuntimeException("Accept/reject methods called twice without operate called in between!");
70 }
71
72 public void reject() {
73 if (!operateAllowed) {
74 operateAllowed = true;
75 rejected += 1;
76 } else throw new RuntimeException("Accept/reject methods called twice without operate called in between!");
77 }
78
79 public final void reset() {
80 operateAllowed = true;
81 accepted = 0;
82 rejected = 0;
83 lastDeviation = 0.0;
84 sumDeviation = 0.0;
85 }
86
87 public final long getCount() {
88 return accepted + rejected;
89 }
90
91 public final long getAcceptCount() {
92 return accepted;
93 }
94
95 public final void setAcceptCount(long accepted) {
96 this.accepted = accepted;
97 }
98
99 public final long getRejectCount() {
100 return rejected;
101 }
102
103 public final void setRejectCount(long rejected) {
104 this.rejected = rejected;
105 }
106
107 public final double getMeanDeviation() {
108 return sumDeviation / accepted;
109 }
110
111 public final double getDeviation() {
112 return lastDeviation;
113 }
114
115 public final double getSumDeviation() {
116 return sumDeviation;
117 }
118
119 public final void setSumDeviation(double sumDeviation) {
120 this.sumDeviation = sumDeviation;
121 }
122
123 public final double getAcceptanceProbability() {
124 return (double) accepted / (double) (accepted + rejected);
125 }
126
127 public double getSpan(boolean reset) {
128 double span = 0;
129 if( spanDeviation[1] > spanDeviation[0] && spanCount > 20 ) {
130 span = spanDeviation[1] - spanDeviation[0];
131
132 if( reset ) {
133 spanDeviation[0] = Double.MAX_VALUE;
134 spanDeviation[1] = -Double.MAX_VALUE;
135 spanCount = 0;
136 }
137 }
138 return span;
139 }
140
141 public double getMeanEvaluationTime() {
142 return (double) sumEvaluationTime / (double) (accepted + rejected);
143 }
144
145 public long getTotalEvaluationTime() {
146 return sumEvaluationTime;
147 }
148
149 public void addEvaluationTime(long time) {
150 sumEvaluationTime += time;
151 }
152
153 private long sumEvaluationTime = 0;
154
155 private double[] spanDeviation = {Double.MAX_VALUE,-Double.MAX_VALUE};
156 private int spanCount = 0;
157
158 private double weight = 1.0;
159 private long accepted = 0;
160 private long rejected = 0;
161 private double sumDeviation = 0.0;
162 private double lastDeviation = 0.0;
163 protected boolean operateAllowed = true;
164
165 }