Debian Codegraph @

src/dr/inference/operators/SwapOperator.java

raw symbols SCIP
1 /*
2 * SwapOperator.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 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 * A generic operator swapping a number of pairs in a multi-dimensional parameter.
38 *
39 * @author Alexei Drummond
40 * @author Andrew Rambaut
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 * @return the parameter this operator acts on.
62 */
63 public Parameter getParameter() {
64 return parameter;
65 }
66
67 /**
68 * swap the values in two random parameter slots.
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 //PRIVATE STUFF
96
97 private Parameter parameter = null;
98 private List<Integer> masterList = null;
99 }