Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.comparators.LayeredComparator.java

Here you can find all the information about the file org.dgpf.search.api.comparators.LayeredComparator.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-06-07 05:50:07
 * Original Filename: org.dgpf.search.api.comparators.LayeredComparator.java
 * Version          : 1.0.0
 * Last modification: 2006-06-07
 *                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.api.comparators;

import org.dgpf.search.api.Individual;
import org.dgpf.search.api.IndividualComparator;

/**
 * This class represents comparators that use some other comparators. When
 * comparing two individuals, it uses its first sub-comparator. If and only
 * if that one returnes 0, it uses the second one and so on.
 *
 * @author Thomas Weise
 */

public final  class LayeredComparator  extends IndividualComparator
  {
/**
 * The serial version uid.
 */

  private   static  final long    serialVersionUID        = 1;

/**
 * The individual comparator to be used.
 */

  private           IndividualComparator[]          m_comparators  ;
/**
 * This comparator's name.
 */

  private transient String                          m_name;
  
  
/**
 * Create a new instance of the layered individual comparator.
 * @param p_comparators The individual comparator algorithms to use. The
 *                      one at index 0 will be used first, the one at the
 *                      highest index last.
 */

  public  LayeredComparator(final IndividualComparator[]  p_comparators)
    {
    super();
    this.m_comparators = p_comparators;
    }



/**
 * Returns unique, textual identifier of this genetic selection algorithm.
 * @return The unique, textual identifier of this genetic selection
 *         algorithm.
 */

  @Override
  public  final String  toString  ()
    {
    StringBuilder l_sb;
    int           l_i;
    
    if(this.m_name != null) return this.m_name;
    
    l_sb = new StringBuilder(super.toString());
    l_sb.append('(');
    for(l_i = 0; l_i < this.m_comparators.length; l_i++)
      {
      if(l_i > 0) l_sb.append(',');
      l_sb.append(this.m_comparators[l_i]);      
      }
    l_sb.append(')');
    
    return (this.m_name = l_sb.toString());
    }
  
  

/**
 * <p>
 * Compares its two individuals for fitness.  Returns a negative integer,
 * zero, or a positive integer as the first argument is fitter than, equal
 * to, or less fit than the second.
 *</p><p>
 * The implementor must ensure that <tt>sgn(compare(x, y)) ==
 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
 * implies that <tt>compare(x, y)</tt> must throw an exception if and only
 * if <tt>compare(y, x)</tt> throws an exception.)<p>
 * </p><p>
 * The implementor must also ensure that the relation is transitive:
 * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
 * <tt>compare(x, z)&gt;0</tt>.<p>
 *</p><p>
 * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
 * <tt>z</tt>.<p>
 * </p><p>
 * It is generally the case, but <i>not</i> strictly required that
 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
 * any comparator that violates this condition should clearly indicate
 * this fact.  The recommended language is "Note: this comparator
 * imposes orderings that are inconsistent with equals."
 * </p>
 * @param p_i1 The first individual to be compared.
 * @param p_i2 The second individual to be compared.
 * @return a negative integer, zero, or a positive integer as the
 *         first individual is fitter than, equal to, or less fit than the
 *         second.
 * @throws ClassCastException if the arguments' types prevent them from
 *         being compared by this Comparator.
 * @see #get_scalar_difference(Individual, Individual)
 */

  @Override
  public  final int compare(final Individual<?>  p_i1,
                            final Individual<?>  p_i2)
    {
    IndividualComparator[]  l_c;
    int                     l_i, l_j, l_k;
    
    l_c = this.m_comparators;
    l_k = l_c.length;
    
    for(l_i = 0; l_i < l_k; l_i++)
      {
      l_j = l_c[l_i].compare(p_i1, p_i2);
      if(l_j != 0)
        {
        l_k -= l_i;
        if(l_j > 0) return l_k;
        return -l_k;
        }
      }
    
    return 0;
    }

  
  
/**
 * 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 final boolean equals  (final Object p_object)
    {
    LayeredComparator   l_r;
    IndividualComparator[] l_a, l_b;
    int                    l_i;
    
    if(p_object instanceof LayeredComparator)
      {
      l_r = ((LayeredComparator)p_object);
      
      l_a = this.m_comparators;
      l_b = l_r.m_comparators;
      
      l_i = l_a.length;
      if(l_i == l_b.length)
        {
        for(--l_i; l_i >= 0; l_i--)
          {
          if(!(l_a[l_i].equals(l_b[l_i])))
            {
            return false;
            }
          }
        
        return true;
        }
      }
    
    return false;
    }
  }

File Information:

file name:LayeredComparator.java
package:org.dgpf.search.api.comparators
qualified name:org.dgpf.search.api.comparators.LayeredComparator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/comparators/LayeredComparator.java
size:5.947 KB (6090 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-06-07 06:57:08 GMT+0000
last access: 2017-11-20 02:21: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-20 02:21:59 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo