| 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.inference.model.Bounds; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inferencexml.operators.SwapOperatorParser; |
| 31 |
import dr.math.MathUtils; |
| 32 |
|
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class SwapParameterOperator extends SimpleMCMCOperator { |
| 41 |
|
| 42 |
public SwapParameterOperator(List<Parameter> parameterList, double weight) { |
| 43 |
this.parameterList = parameterList; |
| 44 |
|
| 45 |
if (parameterList.size() < 2) { |
| 46 |
throw new IllegalArgumentException("More than 1 parameter is needed"); |
| 47 |
} |
| 48 |
|
| 49 |
int size = parameterList.get(0).getDimension(); |
| 50 |
for (int i = 1; i < parameterList.size(); ++i) { |
| 51 |
if (size != parameterList.get(i).getDimension()) { |
| 52 |
throw new IllegalArgumentException("All parameters most be the same size"); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
setWeight(weight); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public final double doOperation() { |
| 64 |
|
| 65 |
int i = MathUtils.nextInt(parameterList.size()); |
| 66 |
int j = i; |
| 67 |
|
| 68 |
while (j == i) { |
| 69 |
j = MathUtils.nextInt(parameterList.size()); |
| 70 |
} |
| 71 |
|
| 72 |
final Parameter a = parameterList.get(i); |
| 73 |
final Parameter b = parameterList.get(j); |
| 74 |
|
| 75 |
Bounds<Double> aBounds = a.getBounds(); |
| 76 |
Bounds<Double> bBounds = b.getBounds(); |
| 77 |
|
| 78 |
|
| 79 |
for (int k = 0; k < a.getDimension(); ++k) { |
| 80 |
final double ak = a.getParameterValue(k); |
| 81 |
final double bk = b.getParameterValue(k); |
| 82 |
|
| 83 |
|
| 84 |
if (isOutside(aBounds, bk, k) || isOutside(bBounds, ak, k)) { |
| 85 |
|
| 86 |
return Double.NEGATIVE_INFINITY; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
a.setParameterValueQuietly(k, bk); |
| 91 |
b.setParameterValueQuietly(k, ak); |
| 92 |
} |
| 93 |
|
| 94 |
a.fireParameterChangedEvent(); |
| 95 |
b.fireParameterChangedEvent(); |
| 96 |
|
| 97 |
return 0.0; |
| 98 |
} |
| 99 |
|
| 100 |
private boolean isOutside(final Bounds<Double> bounds, final double x, final int index) { |
| 101 |
return (x < bounds.getLowerLimit(index) || x > bounds.getUpperLimit(index)); |
| 102 |
} |
| 103 |
|
| 104 |
private String getParameterNames() { |
| 105 |
if (parameterNames == null) { |
| 106 |
StringBuilder sb = new StringBuilder(); |
| 107 |
for (Parameter p : parameterList) { |
| 108 |
sb.append(p.getParameterName()).append("."); |
| 109 |
} |
| 110 |
parameterNames = sb.toString(); |
| 111 |
} |
| 112 |
return parameterNames; |
| 113 |
} |
| 114 |
|
| 115 |
public String getOperatorName() { |
| 116 |
return SwapOperatorParser.SWAP_OPERATOR + "(" + getParameterNames() + "swap)"; |
| 117 |
} |
| 118 |
|
| 119 |
public String getPerformanceSuggestion() { |
| 120 |
return "No suggestions"; |
| 121 |
} |
| 122 |
|
| 123 |
private final List<Parameter> parameterList; |
| 124 |
private String parameterNames = null; |
| 125 |
} |