package dr.inference.operators;

import dr.inference.model.Parameter;
import dr.math.MathUtils;
import dr.xml.*;

/**
 * A generic uniform sampler/operator for use with a multi-dimensional parameter.
 *
 * @author Alexei Drummond
 * @author Andrew Rambaut
 * @version $Id: UniformOperator.java,v 1.16 2005/06/14 10:40:34 rambaut Exp $
 */
public class UniformIntegerOperator extends SimpleMCMCOperator {

    public UniformIntegerOperator(Parameter parameter, int lower, int upper, double weight) {
        this.parameter = parameter;
        this.lower = lower;
        this.upper = upper;
        setWeight(weight);
    }

    /**
     * @return the parameter this operator acts on.
     */
    public Parameter getParameter() {
        return parameter;
    }

    /**
     * change the parameter and return the hastings ratio.
     */
    public final double doOperation() {

        int index = MathUtils.nextInt(parameter.getDimension());
        int newValue = MathUtils.nextInt(upper - lower) + lower;

        parameter.setParameterValue(index, newValue);

        return 0.0;
    }

    //MCMCOperator INTERFACE
    public final String getOperatorName() {
        return "uniformInteger(" + parameter.getParameterName() + ")";
    }

    public final void optimize(double targetProb) {

        throw new RuntimeException("This operator cannot be optimized!");
    }

    public boolean isOptimizing() {
        return false;
    }

    public void setOptimizing(boolean opt) {
        throw new RuntimeException("This operator cannot be optimized!");
    }

    public double getMinimumAcceptanceLevel() {
        return 0.1;
    }

    public double getMaximumAcceptanceLevel() {
        return 0.4;
    }

    public double getMinimumGoodAcceptanceLevel() {
        return 0.20;
    }

    public double getMaximumGoodAcceptanceLevel() {
        return 0.30;
    }

    public String getPerformanceSuggestion() {
        if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
            return "";
        } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
            return "";
        } else {
            return "";
        }
    }

    public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {

        public String getParserName() {
            return "uniformIntegerOperator";
        }

        public Object parseXMLObject(XMLObject xo) throws XMLParseException {

            double weight = xo.getDoubleAttribute(WEIGHT);
            int lower = xo.getIntegerAttribute("lower");
            int upper = xo.getIntegerAttribute("upper");
            Parameter parameter = (Parameter) xo.getChild(Parameter.class);

            return new UniformIntegerOperator(parameter, lower, upper, weight);
        }

        //************************************************************************
        // AbstractXMLObjectParser implementation
        //************************************************************************

        public String getParserDescription() {
            return "An operator that picks new parameter values uniformly at random.";
        }

        public Class getReturnType() {
            return UniformIntegerOperator.class;
        }


        public XMLSyntaxRule[] getSyntaxRules() {
            return rules;
        }

        private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
                AttributeRule.newDoubleRule(WEIGHT),
                AttributeRule.newDoubleRule("upper"),
                AttributeRule.newDoubleRule("lower"),
                new ElementRule(Parameter.class)
        };
    };

    public String toString() {
        return "uniformIntegerOperator(" + parameter.getParameterName() + ")";
    }

    //PRIVATE STUFF

    private Parameter parameter = null;
    private int upper;
    private int lower;
}

@@ -2517,16 +2517,209 @@
 EIGHT);%0A
+%0A            Parameter parameter = (Parameter) xo.getChild(Parameter.class);%0A%0A            int lower = (int) parameter.getBounds().getLowerLimit(0);%0A            if (xo.hasAttribute(%22lower%22))%0A   
         
@@ -2714,35 +2714,32 @@
 %0A               
-int
  lower = xo.getI
@@ -2760,24 +2760,25 @@
 e(%22lower%22);%0A
+%0A
             
@@ -2784,24 +2784,136 @@
  int upper =
+ (int) parameter.getBounds().getUpperLimit(0);%0A            if (xo.hasAttribute(%22upper%22))%0A                lower =
  xo.getInteg
@@ -2930,24 +2930,26 @@
 e(%22upper%22);%0A
+%0A 
             
@@ -2943,34 +2943,224 @@
             
- Parameter
+if (upper == lower %7C%7C lower == (int) Double.NEGATIVE_INFINITY %7C%7C upper == (int) Double.POSITIVE_INFINITY) %7B%0A                throw new XMLParseException(this.getParserName() + %22 boundaries not found in
  parameter =
@@ -3154,36 +3154,37 @@
 n parameter 
-= (P
+%22 + p
 arameter
 ) xo.getChil
@@ -3175,39 +3175,79 @@
 eter
-) xo.getChild(Parameter.class);
+.getParameterName() + %22 Use operator lower and upper !%22);%0A            %7D
 %0A%0A  
@@ -4049,16 +4049,22 @@
 (%22upper%22
+, true
 ),%0A     
@@ -4109,16 +4109,22 @@
 (%22lower%22
+, true
 ),%0A     

Allow optional upper and lower. If optional then the boundaries are obtained directly from the parameter.