| 1 | /* |
| 2 | * WilsonBalding.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.evomodel.operators; |
| 27 | |
| 28 | import dr.evolution.tree.NodeRef; |
| 29 | import dr.evomodel.tree.TreeModel; |
| 30 | import dr.evomodelxml.operators.WilsonBaldingParser; |
| 31 | import dr.math.MathUtils; |
| 32 | |
| 33 | /** |
| 34 | * Implements the unweighted wilson-balding branch swapping move. |
| 35 | * |
| 36 | * @author Alexei Drummond |
| 37 | * @version $Id: WilsonBalding.java,v 1.38 2005/06/14 10:40:34 rambaut Exp $ |
| 38 | */ |
| 39 | public class WilsonBalding extends AbstractTreeOperator { |
| 40 | |
| 41 | private TreeModel tree = null; |
| 42 | private final int tipCount; |
| 43 | |
| 44 | |
| 45 | public WilsonBalding(TreeModel tree, double weight) { |
| 46 | this.tree = tree; |
| 47 | tipCount = tree.getExternalNodeCount(); |
| 48 | setWeight(weight); |
| 49 | } |
| 50 | |
| 51 | public double doOperation() { |
| 52 | |
| 53 | double logq = proposeTree(); |
| 54 | |
| 55 | if (tree.getExternalNodeCount() != tipCount) { |
| 56 | int newCount = tree.getExternalNodeCount(); |
| 57 | throw new RuntimeException("Lost some tips in modified SPR! (" + |
| 58 | tipCount + "-> " + newCount + ")"); |
| 59 | } |
| 60 | //System.out.println("last accepted deviation: " + getDeviation()); |
| 61 | //System.out.println("logq=" + logq); |
| 62 | return logq; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * WARNING: Assumes strictly bifurcating tree. |
| 67 | */ |
| 68 | public double proposeTree() { |
| 69 | |
| 70 | NodeRef i; |
| 71 | double oldMinAge, newMinAge, newRange, oldRange, newAge, q; |
| 72 | |
| 73 | //Bchoose |
| 74 | |
| 75 | //for (int n =0; n < tree.getNodeCount(); n++) { |
| 76 | // System.out.println(n + " " + ( (tree.getNode(n) == null) ? "null" : tree.getNode(n).getId())); |
| 77 | //} |
| 78 | |
| 79 | // choose a random node avoiding root |
| 80 | final int nodeCount = tree.getNodeCount(); |
| 81 | do { |
| 82 | i = tree.getNode(MathUtils.nextInt(nodeCount)); |
| 83 | } while (tree.getRoot() == i); |
| 84 | final NodeRef iP = tree.getParent(i); |
| 85 | |
| 86 | // choose another random node to insert i above |
| 87 | NodeRef j = tree.getNode(MathUtils.nextInt(nodeCount)); |
| 88 | NodeRef k = tree.getParent(j); |
| 89 | |
| 90 | // make sure that the target branch <k, j> is above the subtree being moved |
| 91 | while ((k != null && tree.getNodeHeight(k) <= tree.getNodeHeight(i)) || (i == j)) { |
| 92 | j = tree.getNode(MathUtils.nextInt(nodeCount)); |
| 93 | k = tree.getParent(j); |
| 94 | } |
| 95 | |
| 96 | // disallow moves that change the root. |
| 97 | if (j == tree.getRoot() || iP == tree.getRoot()) { |
| 98 | return Double.NEGATIVE_INFINITY; |
| 99 | } |
| 100 | |
| 101 | if (k == iP || j == iP || k == i) { |
| 102 | return Double.NEGATIVE_INFINITY; |
| 103 | } |
| 104 | |
| 105 | final NodeRef CiP = getOtherChild(tree, iP, i); |
| 106 | NodeRef PiP = tree.getParent(iP); |
| 107 | |
| 108 | // ConstantPopulation demoFunc = null; |
| 109 | // if (demoModel != null && demoModel.getDemographicFunction() instanceof ConstantPopulation) { |
| 110 | // demoFunc = (ConstantPopulation)demoModel.getDemographicFunction(); |
| 111 | // } |
| 112 | |
| 113 | // if (j == tree.getRoot()) { |
| 114 | // if (demoModel != null) { |
| 115 | // delta = -demoFunc.getN0() * Math.log(MathUtils.nextDouble()); |
| 116 | // } else { |
| 117 | // delta = tree.getNodeHeight(j) * MathUtils.nextDouble(); |
| 118 | // } |
| 119 | // newAge = tree.getNodeHeight(j) + delta; |
| 120 | // |
| 121 | // PiP = tree.getParent(iP); |
| 122 | // oldMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(CiP)); |
| 123 | // oldRange = tree.getNodeHeight(PiP) - oldMinAge; |
| 124 | // |
| 125 | // if (demoFunc == null) { |
| 126 | // q = tree.getNodeHeight(j) / oldRange; |
| 127 | // } else { |
| 128 | // q = Math.exp(delta/demoFunc.getN0())*demoFunc.getN0()/oldRange; |
| 129 | // } |
| 130 | // } else if (iP == tree.getRoot()) { |
| 131 | // |
| 132 | // newMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(j)); |
| 133 | // newRange = tree.getNodeHeight(k) - newMinAge; |
| 134 | // newAge = newMinAge + (MathUtils.nextDouble()*newRange); |
| 135 | // |
| 136 | // if (demoFunc == null) { |
| 137 | // if (tree.getNodeHeight(iP) > (tree.getNodeHeight(CiP) * 2)) throw new OperatorFailedException("too big"); |
| 138 | // q = newRange / tree.getNodeHeight(CiP); |
| 139 | // } else { |
| 140 | // q = (tree.getNodeHeight(CiP)-tree.getNodeHeight(iP))/demoFunc.getN0() + Math.log(newRange/demoFunc.getN0()); |
| 141 | // } |
| 142 | // } else { |
| 143 | newMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(j)); |
| 144 | newRange = tree.getNodeHeight(k) - newMinAge; |
| 145 | newAge = newMinAge + (MathUtils.nextDouble() * newRange); |
| 146 | oldMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(CiP)); |
| 147 | oldRange = tree.getNodeHeight(PiP) - oldMinAge; |
| 148 | q = newRange / Math.abs(oldRange); |
| 149 | //System.out.println(newRange + "/" + oldRange + "=" + q); |
| 150 | // } |
| 151 | |
| 152 | //Bupdate |
| 153 | |
| 154 | tree.beginTreeEdit(); |
| 155 | |
| 156 | if (j == tree.getRoot()) { |
| 157 | |
| 158 | // 1. remove edges <iP, CiP> |
| 159 | tree.removeChild(iP, CiP); |
| 160 | tree.removeChild(PiP, iP); |
| 161 | |
| 162 | // 2. add edges <k, iP>, <iP, j>, <PiP, CiP> |
| 163 | tree.addChild(iP, j); |
| 164 | tree.addChild(PiP, CiP); |
| 165 | |
| 166 | // iP is the new root |
| 167 | tree.setRoot(iP); |
| 168 | |
| 169 | } else if (iP == tree.getRoot()) { |
| 170 | |
| 171 | // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP> |
| 172 | tree.removeChild(k, j); |
| 173 | tree.removeChild(iP, CiP); |
| 174 | |
| 175 | // 2. add edges <k, iP>, <iP, j>, <PiP, CiP> |
| 176 | tree.addChild(iP, j); |
| 177 | tree.addChild(k, iP); |
| 178 | |
| 179 | //CiP is the new root |
| 180 | tree.setRoot(CiP); |
| 181 | |
| 182 | } else { |
| 183 | // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP> |
| 184 | tree.removeChild(k, j); |
| 185 | tree.removeChild(iP, CiP); |
| 186 | tree.removeChild(PiP, iP); |
| 187 | |
| 188 | // 2. add edges <k, iP>, <iP, j>, <PiP, CiP> |
| 189 | tree.addChild(iP, j); |
| 190 | tree.addChild(k, iP); |
| 191 | tree.addChild(PiP, CiP); |
| 192 | } |
| 193 | |
| 194 | tree.setNodeHeight(iP, newAge); |
| 195 | |
| 196 | tree.endTreeEdit(); |
| 197 | |
| 198 | // AR - I don't believe this check is needed and in tests it never fails... |
| 199 | // try { |
| 200 | // tree.checkTreeIsValid(); |
| 201 | // } catch( MutableTree.InvalidTreeException ite ) { |
| 202 | // throw new RuntimeException(ite.toString()); |
| 203 | //// throw new OperatorFailedException(ite.toString()); |
| 204 | // } |
| 205 | |
| 206 | |
| 207 | return Math.log(q); |
| 208 | } |
| 209 | |
| 210 | public double getMinimumAcceptanceLevel() { |
| 211 | return 0.01; |
| 212 | } |
| 213 | |
| 214 | public String getPerformanceSuggestion() { |
| 215 | // seems like equvivalent code to me :) |
| 216 | return ""; |
| 217 | |
| 218 | // if (MCMCOperator.Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) { |
| 219 | // return ""; |
| 220 | // } else if (MCMCOperator.Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) { |
| 221 | // return ""; |
| 222 | // } else { |
| 223 | // return ""; |
| 224 | // } |
| 225 | } |
| 226 | |
| 227 | public String getOperatorName() { |
| 228 | return WilsonBaldingParser.WILSON_BALDING + "(" + tree.getId() + ")"; |
| 229 | } |
| 230 | } |