Debian Codegraph @

src/dr/inference/operators/SwapParameterOperator.java

raw symbols SCIP
1 /*
2 * SwapParameterOperator.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.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 * A generic operator swapping all values (dimensions) between two or more parameters
37 *
38 * @author Marc A. Suchard
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 * swap all values in two random parameters.
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 // Check bk outside of aBounds or ak outside of bBounds
84 if (isOutside(aBounds, bk, k) || isOutside(bBounds, ak, k)) {
85 // throw new OperatorFailedException("proposed value outside boundaries");
86 return Double.NEGATIVE_INFINITY;
87 }
88
89 // Swap
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 }