//  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 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;
  } // generationalDistance
  

	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
