| 1 | /* |
| 2 | * SetOperator.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.math.MathUtils; |
| 30 | import org.w3c.dom.Document; |
| 31 | import org.w3c.dom.Element; |
| 32 | |
| 33 | /** |
| 34 | * A generic operator for selecting uniformly from a discrete set of values. |
| 35 | * |
| 36 | * @author Alexei Drummond |
| 37 | * @author Andrew Rambaut |
| 38 | * @version $Id: SetOperator.java,v 1.12 2005/05/24 20:26:00 rambaut Exp $ |
| 39 | */ |
| 40 | public class SetOperator extends SimpleMCMCOperator { |
| 41 | |
| 42 | public SetOperator(Parameter parameter, double[] values) { |
| 43 | this.parameter = parameter; |
| 44 | this.values = values; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return the parameter this operator acts on. |
| 49 | */ |
| 50 | public Parameter getParameter() { |
| 51 | return parameter; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * change the parameter and return the hastings ratio. |
| 56 | */ |
| 57 | public final double doOperation() { |
| 58 | |
| 59 | int index = MathUtils.nextInt(values.length); |
| 60 | double newValue = values[index]; |
| 61 | |
| 62 | if (newValue < parameter.getBounds().getLowerLimit(index) || newValue > parameter.getBounds().getUpperLimit(index)) { |
| 63 | // throw new OperatorFailedException("proposed value outside boundaries"); |
| 64 | return Double.NEGATIVE_INFINITY; |
| 65 | } |
| 66 | |
| 67 | parameter.setParameterValue(index, newValue); |
| 68 | |
| 69 | return 0.0; |
| 70 | } |
| 71 | |
| 72 | public Element createOperatorElement(Document document) { |
| 73 | throw new RuntimeException("Not implememented!"); |
| 74 | } |
| 75 | |
| 76 | public String getOperatorName() { |
| 77 | return "setOperator(" + parameter.getParameterName() + ")"; |
| 78 | } |
| 79 | |
| 80 | public String getPerformanceSuggestion() { |
| 81 | return "No suggestions"; |
| 82 | } |
| 83 | |
| 84 | //PRIVATE STUFF |
| 85 | |
| 86 | private Parameter parameter = null; |
| 87 | private double[] values; |
| 88 | |
| 89 | } |