Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.hc.adaptation.DefaultHCAdapter.java

Here you can find all the information about the file org.dgpf.search.algorithms.hc.adaptation.DefaultHCAdapter.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-11 10:53:52
 * Original Filename: org.dgpf.search.algorithms.hc.adaptation.DefaultHCAdapter.java
 * Version          : 1.0.1
 * 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.hc.adaptation;

import org.dgpf.search.algorithms.hc.HCParameters;
import org.dgpf.search.api.AdaptationStrategy;
import org.dgpf.search.api.SearchParameters;
import org.dgpf.search.api.SearchState;
import org.dgpf.search.api.adaptation.DefaultSearchAdapter;
import org.sfc.utils.Typesafe;

/**
 * The default hill climbing adaptation strategy.
 *
 * @author Thomas Weise
 */

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

  private static final long serialVersionUID            = 1;
/**
 * The default update threshold.
 */

  public  static  final long  DEFAULT_HC_UPDATE_THRESHOLD  = 
                                      (DEFAULT_UPDATE_THRESHOLD << 2);
  
/**
 * The default spacing between the non-use_rel_best-steps.
 */

  public  static  final long  DEFAULT_NURB_SPACING =
        (DEFAULT_HC_UPDATE_THRESHOLD >> 4);

/**
 * The initial non-use_rel_best update space.
 */

  private long  m_initial_space ;
/**
 * The current non-use_rel_best update space.
 */

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

  private long  m_next_gen  ;
/**
 * Is non-use_rel_best 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 DefaultHCAdapter(final SearchParameters<?> p_parameters)
    {
    super(p_parameters);

    this.set_update_threshold(DEFAULT_HC_UPDATE_THRESHOLD);
    this.set_nurb_spacing(DEFAULT_NURB_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  DefaultHCAdapter(final SearchParameters<?> p_parameters,
                           final AdaptationStrategy  p_strategy)
    {
    super(p_parameters, p_strategy);
    if(this.m_initial_space <= 0)
      {      
      this.set_nurb_spacing(DEFAULT_NURB_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 ()
    {
    HCParameters<?>  l_p;
    
    super.reset();

    l_p               = Typesafe.cast(this.get_parameters());
    this.m_space      = this.m_initial_space;
    this.m_next_gen   = this.m_initial_space;
    this.m_on         = false;
    l_p.set_use_rel_best(true);
    }

/**
 * 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;
    HCParameters<?>  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_use_rel_best(true);
        }
      this.m_next_gen   = (l_r + this.m_space);
      }
    else
      {
      if(this.m_next_gen <= l_r)
        {
        if(this.m_on)
          {
          l_p.set_use_rel_best(true);
          this.m_on        = false;
          }
        else
          {
          l_p.set_use_rel_best(false);
          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 non-use_rel_best spacing, that is the spacing between
 * use_rel_bests witches which is initially used.
 * @return The initial non-use_rel_best spacing, that is the spacing
 *         between use_rel_best switches which is initially used.
 */

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


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

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


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

  public  final void  set_nurb_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 DefaultHCAdapter)
      {
      this.set_nurb_spacing(((DefaultHCAdapter)p_data).m_initial_space);
      }
    }

  }

File Information:

file name:DefaultHCAdapter.java
package:org.dgpf.search.algorithms.hc.adaptation
qualified name:org.dgpf.search.algorithms.hc.adaptation.DefaultHCAdapter.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/hc/adaptation/DefaultHCAdapter.java
size:6.805 KB (6969 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-06-01 02:32:06 GMT+0000
last access: 2017-11-20 18:57:21 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 18:57:22 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo