| 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.Parameter; |
| 29 |
import dr.inferencexml.operators.SwapOperatorParser; |
| 30 |
import dr.math.MathUtils; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Collections; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class SwapOperator extends SimpleMCMCOperator { |
| 43 |
private int size = 1; |
| 44 |
|
| 45 |
public SwapOperator(Parameter parameter, int size) { |
| 46 |
this.parameter = parameter; |
| 47 |
this.size = size; |
| 48 |
if (parameter.getDimension() < 2 * size) { |
| 49 |
throw new IllegalArgumentException(); |
| 50 |
} |
| 51 |
|
| 52 |
int dimension = parameter.getDimension(); |
| 53 |
List<Integer> list = new ArrayList<Integer>(); |
| 54 |
for (int i = 0; i < dimension; i++) { |
| 55 |
list.add(i); |
| 56 |
} |
| 57 |
masterList = Collections.unmodifiableList(list); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public Parameter getParameter() { |
| 64 |
return parameter; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public final double doOperation() { |
| 71 |
|
| 72 |
List<Integer> allIndices = new ArrayList<Integer>(masterList); |
| 73 |
int left, right; |
| 74 |
|
| 75 |
for (int i = 0; i < size; i++) { |
| 76 |
left = allIndices.remove(MathUtils.nextInt(allIndices.size())); |
| 77 |
right = allIndices.remove(MathUtils.nextInt(allIndices.size())); |
| 78 |
double value1 = parameter.getParameterValue(left); |
| 79 |
double value2 = parameter.getParameterValue(right); |
| 80 |
parameter.setParameterValue(left, value2); |
| 81 |
parameter.setParameterValue(right, value1); |
| 82 |
} |
| 83 |
|
| 84 |
return 0.0; |
| 85 |
} |
| 86 |
|
| 87 |
public String getOperatorName() { |
| 88 |
return SwapOperatorParser.SWAP_OPERATOR + "(" + parameter.getParameterName() + ")"; |
| 89 |
} |
| 90 |
|
| 91 |
public String getPerformanceSuggestion() { |
| 92 |
return "No suggestions"; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private Parameter parameter = null; |
| 98 |
private List<Integer> masterList = null; |
| 99 |
} |