| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 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 |
|
| 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 |
} |