Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.regression.base.AggregationFitnessFunction.java

Here you can find all the information about the file org.dgpf.gp.regression.base.AggregationFitnessFunction.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-07-26 16:29:26
 * Original Filename: org.dgpf.gp.regression.AggregationFitnessFunction.java
 * Version          : 1.0.0
 * Last modification: 2006-07-26
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" 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.
 */

 
package org.dgpf.gp.regression.base;

import org.dgpf.search.api.FitnessData;
import org.dgpf.search.api.FitnessFunction;
import org.dgpf.search.api.SearchContext;
import org.sfc.math.Mathematics;
import org.sfc.math.stochastic.statistics.StatisticInfo;
import org.sfc.utils.ICloneable;


/**
 * This class represents an target aggregation.
 *
 * @author Thomas Weise
 */

public class      AggregationFitnessFunction 
       extends    FitnessFunction<Calculation, AggregationFitnessData>
       implements ICloneable
  {
/**
 * The target function.
 * @see #get_target_function()
 */

          final ITargetFunction m_target  ;
/**
 * The target variable relative index.
 */

                int             m_ti  ;
/**
 * the e multiplicator.
 */

  static final double EE = (Math.exp(-1.0d));
  
/**
 * Create a new aggregation fitness function.
 * @param p_target The target function that should be approximated by this
 *                 fitness function.
 */

  public  AggregationFitnessFunction(final ITargetFunction p_target)
    {
    super(1, 10);
    this.m_target = p_target;
    }
  
  
/**
 * Obtain the target function that should be approximated by this fitness
 * function.
 * @return The target function that should be approximated by this fitness
 *         function.
 */

  public  final ITargetFunction get_target_function()
    {
    return this.m_target;
    }
  
/**
 * This method is called when the simulation proceeded to a step
 * <code>p_step</code>, where
 * <code>p_step % get_steps_between_inspections() == 0</code>, if
 * <code>get_steps_between_inspections() > 0</code>.
 * @param p_fitness       The fitness data record used in this simulation.
 * @param p_context       The context driving the simulation.
 * @param p_step          The current count of completed steps.
 */

  @Override
  protected  void inspect(final AggregationFitnessData      p_fitness,
                          final SearchContext<Calculation>  p_context,
                          final long                        p_step)
    {
    p_fitness.m_sum += calc(p_fitness, p_context);
    }
  
/**
 * This method is called when a simulation is about to begin. It should
 * perform any initialization needed.
 * @param p_fitness       The fitness data record to be used in this
 *                        simulation.
 * @param p_context       The context driving the simulation.
 */

  @Override
  protected  void begin_simulation(
                      final AggregationFitnessData    p_fitness,
                      final SearchContext<Calculation> p_context)
    {
    AggregationContext l_ac;
    
    super.begin_simulation(p_fitness, p_context);
    
    l_ac = ((AggregationContext)p_context);
    
    p_fitness.m_target     = l_ac.m_cur_res[this.m_ti];    
    p_fitness.m_target_var = ((AggregationContext)p_context).
                                                     m_targets[this.m_ti];
    p_fitness.m_sum        = 0.0d;
    }
  


  
/**
 * Obtain a human readable String representation of this fitness function.
 * @return A human readable String representation of this fitness function.
 */

  @Override
  public  final String  toString()
    {
    return ("Aggregation: " + this.m_target.toString());
    }  
  
/**
 * Create a new instance of the dynamic data container of this fitness
 * function. You must override this method if you aren't using default
 * <code>FitnessData</code> instances.
 * @return A new instance of the dynamic data container of this fitness
 *         function
 */

  @Override
  protected  final AggregationFitnessData  create_fitness_data ()
    {
    return new AggregationFitnessData();
    }
  /** */
static boolean xx=false;  
/**
 * Calculate the current fitness.
 * @param p_fitness       The fitness data record that was used in this
 *                        simulation.
 * @param p_context       The context that has driven the simulation.
 * @return  The current fitness. 
 */

  private static  final double  calc(
                      final AggregationFitnessData      p_fitness,
                      final SearchContext<Calculation>  p_context)
    {
          int                l_i, l_j;
          double             l_val, l_vmax, l_avg;
    final double             l_tar;
    final AggregationContext l_ac;
    final int                l_tv;
double q;
    l_vmax = Double.NEGATIVE_INFINITY;
    l_avg  = 0.0d;
    l_tv   = p_fitness.m_target_var;
    l_ac   = ((AggregationContext)p_context);
    l_tar  = p_fitness.m_target;
    l_j    = l_ac.get_node_count();
q=0;
// With this code, we compute the current convergence state     
    for(l_i = (l_j-1); l_i >= 0; l_i--)
      {
      l_val = Math.abs(l_ac.get_node(l_i).get_variable(l_tv)-l_tar);
      
if(xx)
  {
  q=Math.max(q,(l_val/l_tar));
//  System.out.println((l_val/l_tar) + " \t" + l_tar + " \t" + l_ac.get_node(l_i).get_variable(l_tv));
//  xx=false;
  }

      if(Mathematics.is_number(l_val))
        {   
        if(l_val > l_vmax) l_vmax = l_val;
        l_avg += l_val;
        }
      else
        {
        return Double.NEGATIVE_INFINITY;
        }
      }

if(xx)
  {
  System.out.println(q);
  }
        
    if(l_tar != 0.0d)
      {
      l_vmax /= Math.abs(l_tar);
      l_avg  /= (l_j*Math.abs(l_tar));
      }
    
//    return Math.exp(-(l_vmax + l_vmin));
    l_vmax += l_avg;
    if(l_vmax <= 0.0d) return 1.0d;
    if(l_vmax < 1.0d) return Math.exp(-l_vmax);    
    return (EE / l_vmax);
    }

  
/**
 * This method is called when a simulation has come to an end.
 * You must use this method to store a fitness value into the fitness
 * record <code>p_fitness</code>. Use <code>set_fitness</code> for this.
 * @param p_fitness       The fitness data record that was used in this
 *                        simulation.
 * @param p_context       The context that has driven the simulation.
 * @see FitnessData#set_fitness(double)
 */

  @Override
  protected  void end_simulation(
                      final AggregationFitnessData      p_fitness,
                      final SearchContext<Calculation>  p_context)
    {    
    double l_l1, l_l2;
    
    l_l1 = p_context.get_steps_per_simulation();
    l_l2 = Math.sqrt(l_l1);
    l_l1 += l_l2;
    xx=true;
    p_fitness.set_fitness(((l_l2*
        calc(p_fitness, p_context)) + 
                                         p_fitness.m_sum) / l_l1);
    xx=false;
    }
  
/**
 * Creates and returns a copy of this object.  The precise meaning
 * of "copy" may depend on the class of the object.
 *
 * @return     A clone of this instance.
 *
 * @see java.lang.Cloneable
 */

  @Override
  public  final Object  clone ()
    {
    try
      {
      return super.clone();
      }
    catch(Throwable l_t)
      {
      return this;
      }
    }
  
/**
 * <p>
 * Obtain the fitness value from the statistics gathered from the fitness
 * data on all simulation runs. You can select any of the statistic
 * parameters of the info record as fitness. To obtain stable results, for
 * example, you could choose the minimum fitness value to be returned.
 * To select individuals with good average fitness, return the average.
 * To select individuals which are good most often, select the median.
 * </p><p>
 * This default implementation checks if the median is numeric. If so, it
 * will be returned. Otherwise we repeat the same procedure with the
 * minimum, the average and the maximum. If none of them is numeric, we
 * return 0.0d.
 * </p>
 *
 * @param p_info  This statistical information record is the essence of all
 *                fitness information stored in the fitness data bags
 *                filled during the simulation runs. Each simulation run
 *                will contribute to this record.
 *
 * @return  A fitness value computed from the statistic info record of
 *          the fitness data bags.
 */

  @Override
  protected  double  calculate_fitness (final StatisticInfo p_info)
    {
    double  l_d;           
    
    l_d = p_info.get_minimum();
    if(l_d <= 0.0d) return 0.0d;

    return (l_d + (l_d*p_info.get_median()) +
                  (l_d*l_d*p_info.get_average()));
    }
  }

File Information:

file name:AggregationFitnessFunction.java
package:org.dgpf.gp.regression.base
qualified name:org.dgpf.gp.regression.base.AggregationFitnessFunction.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/regression/base/AggregationFitnessFunction.java
size:9.297 KB (9521 B)
uploaded: 2015-07-22 04:10:56 GMT+0000
last update: 2006-09-13 10:47:45 GMT+0000
last access: 2017-11-21 15:32:59 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-21 15:32:59 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo