//  InvertedGenerationalDistance.java
//
//  Author:
//       Antonio J. Nebro <antonio@lcc.uma.es>
//       Juan J. Durillo <durillo@lcc.uma.es>
//
//  Copyright (c) 2011 Antonio J. Nebro, Juan J. Durillo
//
//  This program 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 3 of the License, or
//  (at your option) any later version.
//
//  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

package jmetal.qualityIndicator;

/**
 * This class implements the inverted generational distance metric. 
 * It can be used also as a command line by typing: 
 * "java jmetal.qualityIndicator.InvertedGenerationalDistance <solutionFrontFile> <trueFrontFile> 
 * <getNumberOfObjectives>"
 * Reference: Van Veldhuizen, D.A., Lamont, G.B.: Multiobjective Evolutionary 
 *            Algorithm Research: A History and Analysis. 
 *            Technical Report TR-98-03, Dept. Elec. Comput. Eng., Air Force 
 *            Inst. Technol. (1998)
 */
public class InvertedGenerationalDistance {
  public jmetal.qualityIndicator.util.MetricsUtil utils_;  //utils_ is used to access to the
                                           //MetricsUtil funcionalities
  
  static final double pow_ = 2.0;          //pow. This is the pow used for the
                                           //distances
  
  /**
   * Constructor.
   * Creates a new instance of the generational distance metric. 
   */
  public InvertedGenerationalDistance() {
    utils_ = new jmetal.qualityIndicator.util.MetricsUtil();
  } // GenerationalDistance
  
  /**
   * Returns the inverted generational distance value for a given front
   * @param front The front 
   * @param trueParetoFront The true pareto front
   */
  public double invertedGenerationalDistance(double [][] front,
                                     double [][] trueParetoFront, 
                                     int numberOfObjectives) {
	  int N = trueParetoFront.length;
	  double sum = 0.0;
	  for (int i=0;i<N ;i++ )
	  {
		  sum += minDisToTurePF(trueParetoFront[i],front);
	  }

	  return sum/(double)N;
        
 
    
    // STEP 2. Get the normalized front and true Pareto fronts
    normalizedFront       = utils_.getNormalizedFront(front, 
    		                                          maximumValue, 
    		                                          minimumValue);
    normalizedParetoFront = utils_.getNormalizedFront(trueParetoFront, 
    		                                          maximumValue,
    		                                          minimumValue);
    
    // STEP 3. Sum the distances between each point of the true Pareto front and
    // the nearest point in the true Pareto front
    double sum = 0.0;
    for (double[] aNormalizedParetoFront : normalizedParetoFront)
      sum += Math.pow(utils_.distanceToClosedPoint(aNormalizedParetoFront,
              normalizedFront),
              pow_);
   
    
    // STEP 4. Obtain the sqrt of the sum
    sum = Math.pow(sum,1.0/pow_);
    
    // STEP 5. Divide the sum by the maximum number of points of the front
    double generationalDistance = sum / normalizedParetoFront.length;
    
    return generationalDistance;
  } // generationalDistance
  
  /**
   * This class can be invoqued from the command line. Two params are required:
   * 1) the name of the file containing the front, and 2) the name of the file 
   * containig the true Pareto front
   **/  

	private double norm(double[] z) {
		double sum = 0;

		for (int i = 0; i < z.length; i++)
			sum += z[i] * z[i];

		return Math.sqrt(sum);
	}

	private double[] minus(double[] vec1,double[] vec2){
		int n = vec1.length;
		double[] value = new double[n];
		for (int i=0;i<n ;i++ )
		{
			value[i] = vec1[i] - vec2[i];
		}
		return value;

	}

  private double minDisToTurePF(double[] p, double[][]PF){
	double minDis = 1000000000000.00;
	double temp = 0.0;
	int n = PF.length;
	for (int i=0;i <n; i++ )
	{
		temp = norm(minus(p,PF[i]));
		if (temp<minDis)
		{
			minDis = temp;
		}
	}
	return minDis;
  
  }
  
} // InvertedGenerationalDistance
