/*
 * InvariantOperator.java
 *
 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.inference.operators;

import dr.inference.model.Likelihood;
import dr.inference.model.Parameter;
import dr.inferencexml.operators.DirtyLikelihoodOperatorParser;

/**
 * @author Marc Suchard
 */
public abstract class InvariantOperator extends SimpleMCMCOperator implements GibbsOperator {

    private InvariantOperator(Parameter parameter, Likelihood likelihood, double weight,
                             boolean checkLikelihood) {
        this.parameter = parameter;
        this.likelihood = likelihood;
        this.checkLikelihood = checkLikelihood || DEBUG;

        setWeight(weight);
    }

    public String getPerformanceSuggestion() {
        return null;
    }

    public String getOperatorName() {
        return DirtyLikelihoodOperatorParser.TOUCH_OPERATOR;
    }

    @Override
    public double doOperation() {

        double logLikelihood = 0;
        if (checkLikelihood) {
            if (likelihood != null) {
                logLikelihood = likelihood.getLogLikelihood();
            }
        }

        transform(parameter);

        if (checkLikelihood) {
            if (likelihood != null) {
                double newLogLikelihood = likelihood.getLogLikelihood();

                if (Math.abs(logLikelihood - newLogLikelihood) >  tolerance) {
                    System.err.println("Likelihood is not invariant to transformation:");
                    System.err.println("Before: " + logLikelihood);
                    System.err.println("After : " + newLogLikelihood);
                    System.exit(-1);
                }
            }
        }

        return 0;
    }

    protected abstract void transform(Parameter parameter);

    public int getStepCount() {
        return 1;
    }

    private final Parameter parameter;
    private final Likelihood likelihood;
    private final boolean checkLikelihood;

    private final static boolean DEBUG = false;
    private final static double tolerance = 1E-1;

    public static class Rotation extends InvariantOperator {

        private final boolean translationInvariant;
        private final boolean rotationInvariant;
        private final int dim;

        public Rotation(Parameter parameter, int dim,
                        double weight, Likelihood likelihood,
                        boolean translate, boolean rotate,
                        boolean checkLikelihood) {
            super(parameter, likelihood, weight, checkLikelihood);

            this.dim = dim;
            this.translationInvariant = translate;
            this.rotationInvariant = rotate;
        }

        @Override
        protected void transform(Parameter parameter) {

            double[] x = parameter.getParameterValues();

            EllipticalSliceOperator.transformPoint(x, translationInvariant, rotationInvariant, dim);

            final int len = x.length;
            for (int i = 0; i < len; ++i) {
                parameter.setParameterValueQuietly(i, x[i]);
            }
            parameter.fireParameterChangedEvent();
        }
    }
}

@@ -2295,34 +2295,27 @@
        S
-ystem.err.println(
+tring sb = 
 %22Likelih
@@ -2357,12 +2357,19 @@
 ion:
-%22);%0A
+%5Cn%22 +%0A     
     
@@ -2384,35 +2384,19 @@
         
-System.err.println(
+   
 %22Before:
@@ -2405,34 +2405,41 @@
  + logLikelihood
-);
+ + %22%5Cn%22 +
 %0A               
@@ -2447,27 +2447,16 @@
     
-System.err.println(
+        
 %22Aft
@@ -2480,17 +2480,23 @@
 kelihood
-)
+ + %22%5Cn%22
 ;%0A      
@@ -2513,22 +2513,37 @@
     
-System.exit(-1
+throw new RuntimeException(sb
 );%0A 
@@ -2605,24 +2605,147 @@
 n 0;%0A    %7D%0A%0A
+    @Override%0A    public void setPathParameter(double beta) %7B%0A        // Do nothing  // TODO - need a better option%0A    %7D%0A%0A
     protecte

allow invariant operator in path sampling