/**********************************************************************
# Copyright (c) EWA Systems Inc. 1998 - 2005 All Rights Reserved
#               No part of this program may be photocopied, reproduced,
#               or translated to another programming language without
#               the prior written consent of EWA Systems.
**********************************************************************/

package com.ewasystems.opt.ga.test;

import com.ewasystems.opt.ga.GeneticAlleleSet;
import com.ewasystems.opt.ga.GeneticConfig;
import com.ewasystems.opt.ga.GeneticEngine;
import com.ewasystems.opt.ga.GeneticGene;
import com.ewasystems.opt.ga.GeneticProblem;
import com.ewasystems.opt.ga.GeneticRunTask;

/**
 * <p>Genetic Algorithm Test Case: Single Variable Minimization</p>
 * <p>Description: Minimize f(x) = x^4 - 12*x^3 + 15*x^2 + 56*x - 60 where X = [-3, 3] by .05</p>
 * <p>Copyright: EWASystems Copyright (c) 1998 - 2005</p>
 * <p>Company: EWASystems</p>
 * @author Lincoln Evans-Beauchamp
 * @version 1.0
 */
public class SingleVarMin extends GeneticProblem
{
  /**
   * Problem Constructor
   */
  public SingleVarMin()
  {
    super("Single Variable Minimize", "Minimize f(x) = x^4 - 12*x^3 + 15*x^2 + 56*x - 60 where X = [-3, 3] by .05");
  }

  /**
   * The Objective Function
   * @param gene The Genetic Gene to Test
   * @return double: The Genetic Gene's Fitness
   */
  public double f(GeneticGene gene)
  {
    double x = ((Double)gene.getGeneValue(0)).doubleValue();
    double value = Math.pow(x,4d)-12d*Math.pow(x,3d)+15d*Math.pow(x,2d)+56d*x-60d;
    return value;
  }

  /**
   * Gets the Allele Set for the Problem
   * @return GeneticAlleleSet: The Allele Set
   */
  public GeneticAlleleSet getAlleleSet()
  {
    GeneticAlleleSet gas = new GeneticAlleleSet();
    Double[] values = new Double[] {new Double(-3d), new Double(.05d), new Double(3d)};
    gas.addContinuousAllele("Double [-3, 3] by .05", values, 1);
    return gas;
  }

  /**
   * Gets the Objective Type
   * @return int: The Objective Type
   */
  public int getObjectiveType()
    {return OBJECTIVE_TYPE_MIN;}

  /**
   * Main for Running the Problem
   * @param args Not Used
   */
  public static final void main(String[] args)
  {
    GeneticEngine ge = new GeneticEngine();
    GeneticConfig c = ge.getDefaultConfig();
    c.setShowFinalReport(true);
    c.setShowGenerationResults(true);
    c.setNumPopulations(5);
    c.setNumThreads(2);
    GeneticRunTask grt = ge.createGeneticTask(new SingleVarMin(), c);
    grt.run();
    System.out.println("Analytic Min at: -0.870173");
  }
}