Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.ga.adaptation.DefaultGeneticAdapter.java

Here you can find all the information about the file org.dgpf.search.algorithms.ga.adaptation.DefaultGeneticAdapter.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-04-10 08:14:32
 * Original Filename: org.dgpf.search.algorithms.ga.adaptation.DefaultGeneticAdapter.java
 * Version          : 2.0.1
 * Last modification: 2006-05-29
 *                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.adaptation;

import org.dgpf.search.algorithms.ga.GeneticParameters;
import org.dgpf.search.api.AdaptationStrategy;
import org.dgpf.search.api.SearchState;
import org.dgpf.search.api.adaptation.DefaultSearchAdapter;
import org.sfc.utils.Typesafe;

/**
 * This is the default adaptation strategy for genetic evolutions.
 * Basically it uses the diversity-swap rate to increase diversity if
 * the evolution gets stuck.
 *
 * @author Thomas Weise
 */

public class DefaultGeneticAdapter  extends DefaultSearchAdapter
  {
/**
 * The serial version uid.
 */

  private static final long serialVersionUID            = 1;
  
  
/**
 * The default spacing between the diversity steps.
 */

  public  static  final long  DEFAULT_DIVERSITY_SPACING =
        (DEFAULT_UPDATE_THRESHOLD >> 4);

/**
 * The initial diversity generation space.
 */

  private long  m_initial_space ;
/**
 * The current diversity generation space.
 */

  private long  m_space   ;
/**
 * The current next generation where a change should occure.
 */

  private long  m_next_gen  ;
/**
 * The current diversity swap count-
 */

  private int   m_diversity ;
/**
 * Is diversity turned on or not?
 */

  private boolean m_on  ;

/**
 * Create a new stateful adaptation strategy.
 * @param p_parameters  The search parameter set to be adapted.
 */

  public DefaultGeneticAdapter(final GeneticParameters<?> p_parameters)
    {
    super(p_parameters);
    this.set_diversity_spacing(DEFAULT_DIVERSITY_SPACING);
    }

/**
 * Create a new stateful adaptation strategy by using another search
 * adaptation strategy as blueprint.
 * @param p_parameters  The search parameter set to be adapted.
 * @param p_strategy    The blueprint to copy the internal data from.
 */

  public  DefaultGeneticAdapter(final GeneticParameters<?>   p_parameters,
                                final AdaptationStrategy     p_strategy)
    {
    super(p_parameters, p_strategy);
    if(this.m_initial_space <= 0)
      {
      this.set_diversity_spacing(DEFAULT_DIVERSITY_SPACING);
      }
    }
  
/**
 * This method will be called whenever the search parameters perform a
 * reset, meaning that the search level is set back to 0.
 */

  @Override
  protected void  reset ()
    {
    super.reset();
    
    GeneticParameters<?>  l_p;

    l_p = Typesafe.cast(this.get_parameters());
    
    this.m_diversity  = 0;
    this.m_space      = this.m_initial_space;
    this.m_next_gen   = this.m_initial_space;
    this.m_on         = false;
    l_p.set_diversity_swaps(0);
    
//TODO: this might be risky
    l_p.set_challenge_parent(!(l_p.get_challenge_parent()));
    }


/**
 * 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.
 * @see SearchState#get_update_level()
 */

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

    l_p = Typesafe.cast(this.get_parameters());

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

    l_r = p_state.get_update_level();

    if(p_state.get_rel_improvement() > 0)
      {
      if(this.m_on)
        {
        this.m_on       = false;
        l_p.set_diversity_swaps(0);
        }
      this.m_diversity  = 0;
      this.m_next_gen   = (l_r + this.m_space);
      }
    else
      {
      if(this.m_next_gen <= l_r)
        {
        if(this.m_on)
          {
          l_p.set_diversity_swaps(0);
          this.m_diversity = Math.max(this.m_diversity+1,
                                     (this.m_diversity*11)/10);
          this.m_on        = false;
          }
        else
          {
          if(this.m_diversity <= 0)
            {
            this.m_diversity = (l_p.get_population_size() >>> 4);
            }
          l_p.set_diversity_swaps(this.m_diversity);
          this.m_on        = true;
          }

        this.m_space    = Math.max(this.m_space+1, (this.m_space*11)/10);
        this.m_next_gen = (l_r + this.m_space);
        }
      }

    return false;
    }

/**
 * Obtain the initial diversity spacing, that is the spacing between
 * diversity switches which is initially used.
 * @return The initial diversity spacing, that is the spacing between
 *         diversity switches which is initially used.
 */

  public  final long  get_diversity_spacing ()
    {
    return this.m_initial_space;
    }


/**
 * Obtain the current diversity spacing, that is the spacing between
 * diversity switches which is initially used.
 * @return The initial diversity spacing, that is the spacing between
 *         diversity switches which is currently used.
 */

  public  final long  get_current_diversity_spacing ()
    {
    return this.m_space;
    }


/**
 * Set the diversity spacing, that is the spacing between diversity
 * switches.
 * @param p_spacing The new diversity spacing, that is the
 *                  spacing between diversity switches.
 */

  public  final void  set_diversity_spacing (final long p_spacing)
    {
    if(p_spacing > 0)
      {
      this.m_initial_space = p_spacing;
      this.m_space         = p_spacing;
      }
    }

/**
 * Assign the internal data of this adaptation strategy to another one.
 * This method copies the settings, but not the statistics.
 * @param p_data  The adaptation strategy to assign to.
 */

  @Override
  protected void  assign  (final AdaptationStrategy p_data)
    {
    super.assign(p_data);

    if(p_data instanceof DefaultGeneticAdapter)
      {
      this.set_diversity_spacing(((DefaultGeneticAdapter)p_data).m_initial_space);
      }
    }
  }

File Information:

file name:DefaultGeneticAdapter.java
package:org.dgpf.search.algorithms.ga.adaptation
qualified name:org.dgpf.search.algorithms.ga.adaptation.DefaultGeneticAdapter.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/adaptation/DefaultGeneticAdapter.java
size:7.125 KB (7296 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-05-29 14:19:05 GMT+0000
last access: 2017-11-19 02:47:36 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 02:47:36 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo