Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.ga.sorting.ScalarSorting.java

Here you can find all the information about the file org.dgpf.search.algorithms.ga.sorting.ScalarSorting.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-05-28 08:53:45
 * Original Filename: org.dgpf.search.algorithms.ga.sorting.ScalarSorting.java
 * Version          : 1.0.0
 * Last modification: 2006-05-28
 *                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.search.algorithms.ga.sorting;

import java.io.IOException;
import java.io.ObjectInputStream;

import org.dgpf.search.api.Individual;
import org.dgpf.search.api.IndividualComparator;
import org.sfc.collections.Arrays;

/**
 * This sorting mechanism builds a fitness array containing scalar fitness
 * values which span from 0.0 to 1.0 allowing you to select individuals
 * using binary search or such and such as done when using roulette
 * selection.
 *
 * @author Thomas Weise
 */

public class ScalarSorting  extends ComparatorBasedSorting
  {
/**
 * The serial version uid.
 */

  private   static  final long    serialVersionUID        = 1;
  
/**
 * The internal fitness array.
 */

  private transient double[]  m_fitnesses ;
/**
 * The normed fitnesses.
 */

  private transient double[]  m_normed  ;

/**
 * Prevent you from instantiating this class.
 */

  public ScalarSorting()
    {
    super();
    this.m_fitnesses = new double[1024];
    this.m_normed    = new double[1024];
    }

/**
 * Obtain the fitness of the individual at the specified index.
 * @param p_index   The index of the individual to obtain the fitness of.
 * @return The fitness of the individual at the specified index. This
 *         value is guaranteed to be in [0.0d, infinite]
 */

  public  final double  get_fitness (final int p_index)
    {
    return this.m_fitnesses[p_index];
    }
  
  
/**
 * Obtain the normed fitness of the individual at the specified index.
 * The normed fitness is is growing from the individual at index 0 (the
 * best one) to the individual at the highest index (the worst one). You
 * can use binary searching to performe roulette wheel selection or so.
 * @param p_index   The index of the individual to obtain the normed
 *                  fitness of.
 * @return The normed fitness of the individual at the specified index.
 *         This value is guaranteed to be in [0.0d, 1.0d] (except the
 *         individual at the highest index which has fitness 1E70 to ease
 *         binary searching) 
 */

  public  final double  get_normed_fitness (final int p_index)
    {
    return this.m_normed[p_index];
    }
  
/**
 * Obtain the index of the specified norm.
 * @param p_value The value of the normed fitness to find the index of.
 * @param p_count The count of elements of interest.
 * @return  The index of the specified norm.
 */

  public  final int get_norm_index  (final double p_value,
                                     final int    p_count)
    {
    int l_i;
    
    l_i = Arrays.search(this.m_normed, p_value, p_count);    
    if(l_i < 0) return (-(l_i + 1));
    return l_i;
    }
  
/**
 * Sort the individuals so that they can be used by the selection algorithm
 * optimally.
 * @param p_individuals The array with the individuals to be sorted.
 * @param p_count       The count of individuals in that array.
 * @param p_comparator  The comparator to be used.
 * @param p_unsorted    This parameter is <code>false</code>, if and only
 *                      if the population is already sorted according to
 *                      the comparator provided. Otherwise it is
 *                      <code>true</code> indicating that list is not yet
 *                      sorted.
 */

  @Override
  public     void  sort  (final Individual<?>[]      p_individuals,
                          final int                  p_count,
                          final IndividualComparator p_comparator,
                          final boolean              p_unsorted)
    {
    double[]  l_d, l_d2;
    int       l_i;
    double    l_f;
    Individual<?> l_i1, l_i2;
    
    super.sort(p_individuals, p_count, p_comparator, p_unsorted);
    
    l_d = this.m_fitnesses;
    if(l_d.length < p_count)
      {
      l_d              = new double[p_count<<1];
      this.m_fitnesses = l_d;
      l_d2             = new double[p_count<<1];
      this.m_normed    = l_d2;
      }
    else l_d2 = this.m_normed;
    
    l_i      = (p_count-1);
    l_i1     = p_individuals[l_i];
    l_d[l_i] = 0.0d;
    l_f      = 0.0d;
    for(--l_i; l_i >= 0; l_i--)
      {
      l_i2     = l_i1;
      l_i1     = p_individuals[l_i];
      l_f     += p_comparator.get_scalar_difference(l_i2, l_i1);
      l_d[l_i] = l_f;
      }   
    
    l_f = 0.0d;
    for(l_i = 0; l_i < p_count; l_i++)
      {
      l_f      += l_d[l_i];
      l_d2[l_i] = l_f;
      }
    
    l_f = (1.0d / l_f);
    for(l_i = (p_count-2); l_i >= 0; l_i--)
      {
      l_d2[l_i] *= l_f;
      }
    
    l_d2[p_count-1] = 1E70d;
    }
  
/**
 * Reconstitute the <code>SfcEvent</code> instance from a stream (that is,
 * deserialize it).
 * @param p_s The input stream.
 * @throws  IOException If something io-like went wrong.
 * @throws  ClassNotFoundException  If a needed class could not be found.
 */

  private final void readObject(final ObjectInputStream p_s)
        throws IOException, ClassNotFoundException
    {
    p_s.defaultReadObject();
    this.m_fitnesses = new double[1024];
    this.m_normed    = new double[1024];
    }
  
/**
 * Check whether this object equals another one.
 * @param p_object  The object to compare with.
 * @return <code>true</code> if and only if this object equals to the other
 *         one.
 */

  @Override
  public  boolean equals  (final Object p_object)
    {
    return ((p_object == this) ||
            (p_object instanceof ScalarSorting));
    }
  }

File Information:

file name:ScalarSorting.java
package:org.dgpf.search.algorithms.ga.sorting
qualified name:org.dgpf.search.algorithms.ga.sorting.ScalarSorting.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/sorting/ScalarSorting.java
size:6.560 KB (6718 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-05-31 03:02:21 GMT+0000
last access: 2017-11-19 04:56:13 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-19 04:56:13 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo