Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.search.algorithms.ga.sorting.DominationBasedSorting.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-12 14:24:15
 * Original Filename: org.dgpf.search.algorithms.ga.sorting.DominationBasedSorting.java
 * Version          : 1.0.2
 * Last modification: 2006-06-01
 *                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 java.util.Comparator;

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

/**
 * Instances of this class sort individuals based on the count of other
 * individuals they dominate.
 *
 * @author Thomas Weise
 */

public  class DominationBasedSorting  extends SortingAlgorithm
  {
/**
 * The serial version uid.
 */

  private   static  final long    serialVersionUID        = 1;
  
/**
 * The internal individual comparator.
 */

  private static  final Comparator<IndividualHolder>  COMP =
   new Comparator<IndividualHolder>()
     {     
     public final  int compare(final IndividualHolder p_o1,
                               final IndividualHolder p_o2)
       {
       int l_i;
       
       l_i = (p_o2.m_nondom - p_o1.m_nondom);
       if(l_i != 0) return l_i;
       return (p_o1.m_dom - p_o2.m_dom);
       }
     };
  
/**
 * The temporary array.
 */

  transient     IndividualHolder[]  m_temp  ;
/**
 * This parameter is <code>true</code> if not only the count of individuals
 * one individual is not dominated is used for sorting, but also the count
 * of individuals it is dominating itself is used.
 */

  private final boolean             m_count_dominated;             
          
   
/**
 * Create a new domination based sorting instance.
 * @param p_count_dominated This parameter is <code>true</code> if not only
 *                          the count of individuals one individual is not
 *                          dominated is used for sorting, but also the
 *                          count of individuals it is dominating itself is
 *                          used.
 */

  public  DominationBasedSorting  (final boolean p_count_dominated)
    {    
    super();
    
    IndividualHolder[]  l_d;
    int                 l_i;
        
    l_i              = 1024;
    l_d              = this.create_holder_array(l_i);
    
    for(--l_i; l_i >= 0; l_i--)
      {
      l_d[l_i] = this.create_holder();
      }
    
    this.m_temp = l_d;    
    this.m_count_dominated = p_count_dominated;
    }
  
/**
 * 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();    
    
    IndividualHolder[]  l_d;
    int                 l_i;
    
    l_i              = 1024;
    l_d              = this.create_holder_array(l_i);
    
    for(--l_i; l_i >= 0; l_i--)
      {
      l_d[l_i] = this.create_holder();
      }
    
    this.m_temp = l_d;    
    }
  
/**
 * Internally create a new individual holder.
 * @return A new individual holder.
 */

  IndividualHolder  create_holder()
    {
    return new IndividualHolder();
    }
  
/**
 * Internally create a new individual holder array.
 * @param p_len The length of the wanted array.
 * @return A new individual holder.
 */

  IndividualHolder[]  create_holder_array(final int p_len)
    {
    return new IndividualHolder[p_len];
    }

/**
 * 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)
    {
          IndividualHolder[]  l_d;
          IndividualHolder    l_h;
          Individual<?>       l_z;
          int                 l_i, l_j, l_k;
    final boolean             l_cd;  
    
    l_d  = this.m_temp;        
    l_cd = this.m_count_dominated;    
    
    if(l_d.length < p_count)
      {
      l_d = this.create_holder_array(p_count << 1);
      System.arraycopy(this.m_temp, 0, l_d, 0, this.m_temp.length);
      for(l_i = this.m_temp.length; l_i < l_d.length; l_i++)
        {
        l_d[l_i] = this.create_holder();
        }
      this.m_temp      = l_d;
      }
    
    for(l_i = (p_count-1); l_i >= 0; l_i--)
      {
      l_h          = l_d[l_i];
      l_h.m_data   = l_z = p_individuals[l_i];
      l_h.m_dom    = 0;
      l_h.m_nondom = 0;
      for(l_j = (p_count-1); l_j >= 0; l_j--)
        {
        if(l_j != l_i)
          {
          l_k = p_comparator.compare(l_z, p_individuals[l_j]);
               if(l_k < 0) l_h.m_nondom++;
          else if(l_cd && (l_k > 0)) l_h.m_dom++;
          }
        }
      }
    
    Arrays.sort(l_d, p_count, COMP);
        
    for(l_i = (p_count-1); l_i >= 0; l_i--)
      {
      l_h                = l_d[l_i];
      p_individuals[l_i] = l_h.m_data;
      }    
    }
  

/**
 * 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)
    {
    if(p_object == thisreturn true;
    
    if(p_object instanceof DominationBasedSorting)
      {      
      return (((DominationBasedSorting)p_object).m_count_dominated ==
                this.m_count_dominated);
      }
    
    return false;
    }
  
/**
 * This class is used to hold the reference to an individual.
 *
 * @author Thomas Weise
 */

  static  class IndividualHolder
    {
/**
 * The individual itself.
 */

    Individual<?> m_data  ;
/**
 * The count of individuals this one is dominated by.
 */

    int           m_dom ;
/**
 * The count of individuals this one is not dominated by.
 */

    
    int           m_nondom;
    }
  }

File Information:

file name:DominationBasedSorting.java
package:org.dgpf.search.algorithms.ga.sorting
qualified name:org.dgpf.search.algorithms.ga.sorting.DominationBasedSorting.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/sorting/DominationBasedSorting.java
size:7.621 KB (7804 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-06-01 08:09:08 GMT+0000
last access: 2017-11-21 15:30:43 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:30:43 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo