Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.search.api.comparators.RoundRobinComparator.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-01 15:50:07
 * Original Filename: org.dgpf.search.api.comparators.RoundRobinComparator.java
 * Version          : 1.0.0
 * 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.api.comparators;

import org.dgpf.search.api.Individual;
import org.dgpf.search.api.IndividualComparator;
import org.dgpf.search.api.SearchState;
import org.dgpf.search.api.adaptation.DefaultSearchAdapter;

/**
 * This class represents an comparator which delegates the comparison to
 * sub-comparators. These internal comparators are switched periodically.
 *
 * @author Thomas Weise
 */

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

  private   static  final long    serialVersionUID        = 1;

/**
 * The individual comparators used internally.
 */

  private   static  final IndividualComparator[]  CONFIG  =
   new IndividualComparator[] { MajorityComparator.INSTANCE,
                                StaggeredComparator.INSTANCE,
                                SumComparator.INSTANCE,
                                DominationComparator.INSTANCE};
/**
 * The default modulo.
 */

  private   static  final int                   DEFAULT_MODULO  =
     ((int)(Math.max(
        ((DefaultSearchAdapter.DEFAULT_UPDATE_THRESHOLD/
            (2*CONFIG.length))), 10)));

/**
 * The index of the individual comparator currently used.
 */

  private volatile  int                             m_index ;
/**
 * The modulo for changing the individual comparator.
 */

  private volatile  int                             m_modulo  ;
/**
 * The individual comparator to be used.
 */

  private           IndividualComparator[]          m_comparators  ;
/**
 * The base modulo.
 */

  private final     int                             m_base_modulo ;
/**
 * The current individual comparator.
 */

  private volatile  IndividualComparator            m_comp;

/**
 * Create a new instance of the round robin individual comparator.
 */

  public  RoundRobinComparator()
    {
    this(-1, null);
    }
  
  
/**
 * Create a new instance of the round robin individual comparator.
 * @param p_modulo      The modulo to use for the round robin individual
 *                      comparator.
 * @param p_comparators The individual comparator algorithms to alternate
 *                      between.
 */

  public  RoundRobinComparator(final int                     p_modulo,
                               final IndividualComparator[]  p_comparators)
    {
    super();
    this.m_comparators = (((p_comparators != null) &&
                           (p_comparators.length > 0)) ?
                            p_comparators : CONFIG);
    this.m_modulo      = ((p_modulo > 0) ? p_modulo : DEFAULT_MODULO);
    this.m_base_modulo = this.m_modulo;
    this.m_comp        = p_comparators[0];
    }

/**
 * This method will be called by the search engine whenever it feels like
 * it is time to adapt the current search parameters to the current search
 * state. If you override this method, you must also declare your class
 * as instance of <code>ICloneable</code>.
 * @param p_state The current state of the search.
 * @return  <code>true</code> if and only if the update level should be
 *          resetted, <code>false</code> if everything should continue
 *          normal.
 */

  @Override
  protected final boolean  adapt (final SearchState<?> p_state)
    {
    long l_i;

    if(super.adapt(p_state)) return true;

    l_i = p_state.get_update_level();

    if( (l_i % this.m_modulo) == 0 )
      {
      this.m_index++;
      if(this.m_index >= this.m_comparators.length)
        {
        this.m_modulo++;
        this.m_index = 0;
        }
      this.m_comp = this.m_comparators[this.m_index];
      }

    return false;
    }


/**
 * This method will be called whenever the search parameters perform a
 * reset, meaning that the search level is set back to 0.
 */

  @Override
  protected final void  reset ()
    {
    super.reset();
    this.m_modulo = this.m_base_modulo;
    this.m_index  = 0;
    }
  


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

  @Override
  public  final String  toString  ()
    {
    return super.toString() + '(' + this.m_comp.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)
    {
    return  this.m_comp.compare(p_i1, p_i2);
    }

  
/**
 * <p>
 * Obtain the scalar difference of two individuals. This method is
 * compatible with <code>compare</code> by fulfilling the contract that
 * <code>sign(compare(a,b)) == sign(get_scalar_difference(a,b))</code>
 * where <code>sign(x)</code> computes the sign of the number
 * <code>x</code>. <code>get_scalar_difference</code> is used by some
 * search techniques that require a numeric resolution of the comparison
 * of two individuals more precise than <code>compare</code> can deliver.
 * However, <code>compare</code> should be the prefered way to compare
 * two individuals. 
 * </p>
 * @param p_i1 The first individual to be compared.
 * @param p_i2 The second individual to be compared.
 * @return a negative double, zero, or a positive double as the
 *         first individual is fitter than, equal to, or less fit than the
 *         second denoting the difference of the two individuals.
 * @throws ClassCastException if the arguments' types prevent them from
 *         being compared by this Comparator.
 * @see #compare(Individual, Individual)
 */

  @Override
  public final  double get_scalar_difference(final Individual<?>  p_i1,
                                             final Individual<?>  p_i2)
    {
    return this.m_comp.get_scalar_difference(p_i1, p_i2);
    }
  
/**
 * 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)
    {
    RoundRobinComparator   l_r;
    IndividualComparator[] l_a, l_b;
    int                    l_i;
    
    if(p_object instanceof RoundRobinComparator)
      {
      l_r = ((RoundRobinComparator)p_object);
      
      if( (l_r.m_index       == this.m_index)  &&
          (l_r.m_modulo      == this.m_modulo) &&
          (l_r.m_base_modulo == this.m_base_modulo) )
        {
        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:RoundRobinComparator.java
package:org.dgpf.search.api.comparators
qualified name:org.dgpf.search.api.comparators.RoundRobinComparator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/comparators/RoundRobinComparator.java
size:9.532 KB (9761 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-06-07 05:53:29 GMT+0000
last access: 2017-11-22 09:18: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-22 09:18:43 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo