| 1 | /* |
| 2 | * AbstractTreeOperator.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.evomodel.tree.TreeModel; |
| 29 | import dr.evolution.tree.*; |
| 30 | import dr.inference.operators.SimpleMCMCOperator; |
| 31 | |
| 32 | /** |
| 33 | * @author Andrew Rambaut |
| 34 | * @version $Id$ |
| 35 | */ |
| 36 | public abstract class AbstractTreeOperator extends SimpleMCMCOperator { |
| 37 | |
| 38 | private long transitions = 0; |
| 39 | |
| 40 | /** |
| 41 | * @return the number of transitions since last call to reset(). |
| 42 | */ |
| 43 | public long getTransitions() { |
| 44 | return transitions; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Set the number of transitions since last call to reset(). This is used |
| 49 | * to restore the state of the operator |
| 50 | * |
| 51 | * @param transitions number of transition |
| 52 | */ |
| 53 | public void setTransitions(long transitions) { |
| 54 | this.transitions = transitions; |
| 55 | } |
| 56 | |
| 57 | public double getTransistionProbability() { |
| 58 | final long accepted = getAcceptCount(); |
| 59 | final long rejected = getRejectCount(); |
| 60 | final long transition = getTransitions(); |
| 61 | return (double) transition / (double) (accepted + rejected); |
| 62 | } |
| 63 | |
| 64 | /* exchange sub-trees whose root are i and j */ |
| 65 | protected void exchangeNodes(TreeModel tree, NodeRef i, NodeRef j, |
| 66 | NodeRef iP, NodeRef jP) { |
| 67 | |
| 68 | tree.beginTreeEdit(); |
| 69 | tree.removeChild(iP, i); |
| 70 | tree.removeChild(jP, j); |
| 71 | tree.addChild(jP, i); |
| 72 | tree.addChild(iP, j); |
| 73 | |
| 74 | tree.endTreeEdit(); |
| 75 | } |
| 76 | |
| 77 | public void reset() { |
| 78 | super.reset(); |
| 79 | transitions = 0; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param tree the tree |
| 84 | * @param parent the parent |
| 85 | * @param child the child that you want the sister of |
| 86 | * @return the other child of the given parent. |
| 87 | */ |
| 88 | protected NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) { |
| 89 | if( tree.getChild(parent, 0) == child ) { |
| 90 | return tree.getChild(parent, 1); |
| 91 | } else { |
| 92 | return tree.getChild(parent, 0); |
| 93 | } |
| 94 | } |
| 95 | } |