Debian Codegraph @

src/dr/inference/operators/AlternatingOperatorSchedule.java

raw symbols SCIP
1 /*
2 * ComplexOperatorSchedule.java
3 *
4 * Copyright (c) 2002-2016 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.operators;
27
28 import dr.math.MathUtils;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Vector;
33
34 /**
35 * File: AlternatingOperatorSchedule
36 * @author Andrew Rambaut
37 */
38 public class AlternatingOperatorSchedule implements OperatorSchedule {
39
40 private final List<OperatorSchedule> operatorSchedules = new ArrayList<OperatorSchedule>();
41 private final List<Long> operatorCounts = new ArrayList<Long>();
42
43 private int currentSchedule;
44 private long scheduleCount;
45
46 public AlternatingOperatorSchedule() {
47 currentSchedule = 0;
48 scheduleCount = 0;
49 }
50
51 public void addOperatorSchedule(OperatorSchedule os, long count) {
52 operatorSchedules.add(os);
53 operatorCounts.add(count);
54 }
55
56 public int getNextOperatorIndex() {
57
58 if (scheduleCount >= operatorCounts.get(currentSchedule)) {
59 currentSchedule += 1;
60 if (currentSchedule >= operatorSchedules.size()) {
61 currentSchedule = 0;
62 }
63 scheduleCount = 0;
64 }
65 scheduleCount += 1;
66
67 int offset = 0;
68 for (int i = 0; i < currentSchedule; ++i) {
69 offset += operatorSchedules.get(i).getOperatorCount();
70 }
71
72 return offset + operatorSchedules.get(currentSchedule).getNextOperatorIndex();
73 }
74
75 public void reset() {
76 for (OperatorSchedule os : operatorSchedules) {
77 for (int i = 0; i < os.getOperatorCount(); ++i) {
78 os.getOperator(i).reset();
79 }
80 }
81 }
82
83 public int getOperatorCount() {
84 int operatorCount = 0;
85 for (OperatorSchedule os : operatorSchedules) {
86 operatorCount += os.getOperatorCount();
87 }
88 return operatorCount;
89 }
90
91 public MCMCOperator getOperator(int index) {
92 for (OperatorSchedule os : operatorSchedules) {
93 int opCount = os.getOperatorCount();
94 if (index < opCount) {
95 return os.getOperator(index);
96 } else {
97 index -= opCount;
98 }
99 }
100 // if we reach here the index must be out of bounds return null
101 return null;
102 }
103
104 public void addOperator(MCMCOperator op) {
105 System.err.println("ERROR: addOperator() should not be called on AlternatingOperatorSchedule, " +
106 "use individual operator schedules to add operators. This call will have no affect!");
107 }
108
109 public void addOperators(List<MCMCOperator> v) {
110 System.err.println("ERROR: addOperators() should not be called on AlternatingOperatorSchedule, " +
111 "use individual operator schedules to add operators. This call will have no affect!");
112 }
113
114 public void operatorsHasBeenUpdated() {
115 for (OperatorSchedule os : operatorSchedules) {
116 os.operatorsHasBeenUpdated();
117 }
118 }
119
120 public double getOptimizationTransform(double d) {
121 if (operatorSchedules.size() > 0)
122 return operatorSchedules.get(0).getOptimizationTransform(d);
123 else
124 return 0;
125 }
126
127 public long getMinimumAcceptAndRejectCount() {
128 long minCount = Integer.MAX_VALUE;
129 for (OperatorSchedule os : operatorSchedules) {
130 if (os.getMinimumAcceptAndRejectCount() < minCount) {
131 minCount = os.getMinimumAcceptAndRejectCount();
132 }
133 }
134 return minCount;
135 }
136
137
138 }