//  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;
import  jmetal.qualityIndicator.GenerationalDistance;

/**
 * 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 Delta_p {
  public jmetal.qualityIndicator.util.MetricsUtil utils_;  //utils_ is used to access to the
                                           //MetricsUtil funcionalities
  
  /**
   * Constructor.
   * Creates a new instance of the generational distance metric. 
   */
  public Delta_p() {
    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 delta_p(double [][] front,
                        double [][] trueParetoFront, 
                        int numberOfObjectives) {
	  return Math.max( (new GenerationalDistance().generationalDistance(front, trueParetoFront, numberOfObjectives))*front.length,
				(new GenerationalDistance().generationalDistance(trueParetoFront, front, numberOfObjectives))*trueParetoFront.length)/
		     Math.max(front.length, trueParetoFront.length);
  } // 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
   **/
  public static void main(String args[]) {
    if (args.length < 2) {
      System.err.println("Delta_p::Main: Usage: java " +
      		             "Delta_p <FrontFile> " +
      		             "<TrueFrontFile>  <getNumberOfObjectives>");
      System.exit(1);
    } // if
    
    // STEP 1. Create an instance of Generational Distance
    Delta_p qualityIndicator = new Delta_p();
    
    // STEP 2. Read the fronts from the files
    double [][] solutionFront = qualityIndicator.utils_.readFront(args[0]);
    double [][] trueFront     = qualityIndicator.utils_.readFront(args[1]);
    
    // STEP 3. Obtain the metric value
    double value = qualityIndicator.delta_p(
    		                                 solutionFront,
    		                                 trueFront,
            new Integer(args[2]));
    
    System.out.println(value);  
  } // main  
  
} // Delta_p
