Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.ga.local.LocalGeneticEngine.java

Here you can find all the information about the file org.dgpf.search.algorithms.ga.local.LocalGeneticEngine.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 16:56:26
 * Original Filename: org.dgpf.search.algorithms.ga.local.LocalGeneticEngine.java
 * Version          : 2.1.2
 * Last modification: 2006-07-15
 *                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.local;

import java.io.Serializable;

import org.dgpf.search.algorithms.ga.GeneticEngine;
import org.dgpf.search.algorithms.ga.GeneticParameters;
import org.dgpf.search.api.SearchDefinition;
import org.dgpf.search.api.SearchTaskQueue;
import org.dgpf.search.api.SearchWorkerThread;
import org.sfc.collections.Arrays;
import org.sfc.events.EventPropagator;
import org.sfc.utils.Typesafe;

/**
 * This genetic engine runs on a single computer and does not distribute
 * load in any fashion. It is simple and efficient.
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 *
 * @author Thomas Weise
 */

public class LocalGeneticEngine<Genotype extends Serializable>
                                         extends GeneticEngine<Genotype>
  {
/**
 * The search definition used by this engine.
 */

  private final SearchDefinition<Genotype>      m_definition  ;
/**
 * The search worker threads used by this engine.
 */

  private       SearchWorkerThread<Genotype>[]  m_swt ;

/**
 * Create a new genetic engine without providing additional information or
 * parameters. This constructor will be used only for loading a snapshot
 * immediately afterwards.
 * @param p_definition  The definition of the evolution process.
 */

  public LocalGeneticEngine  (final SearchDefinition<Genotype>  p_definition)
    {
    super();
    this.m_definition = p_definition;
    }

/**
 * Create a genetic engine.
 * @param p_parameters  The parameters object guiding the evolution process.
 * @param p_definition  The definition of the evolution process.
 * @param p_ndl_size    The size of the non-dominated list. Set this
 *                      to <code>-1</code> for don't care.
 */

  public LocalGeneticEngine(final GeneticParameters<Genotype> p_parameters,
                            final SearchDefinition<Genotype>  p_definition,
                            final int                         p_ndl_size)
    {
    super(p_parameters, p_ndl_size);
    this.m_definition = p_definition;
    this.init_threads(p_definition);
    }

/**
 * <p>
 * This method will be called every generation after the fitness has been
 * calculated and the state was updated, but before the genetic tasks are
 * entered in the task queue.</p><p>
 * You may use it to update worker threads.</p>
 */

  @Override
  protected void  inner_update  ()
    {
    SearchWorkerThread<Genotype>[]  l_swt;
    int                             l_i;
    GeneticParameters<Genotype>     l_gp;

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

    l_swt = this.m_swt;
    for(l_i = (l_swt.length-1); l_i >= 0; l_i--)
      {
      l_swt[l_i].update_context(l_gp);
      }
    }


/**
 * Initialize the worker threads.
 * @param p_def The search definition to be used for initializing the
 *              threads.
 */

  private final void init_threads(final SearchDefinition<Genotype> p_def)
    {
    SearchWorkerThread<Genotype>[]  l_swt;
    int                             l_i;
    EventPropagator                 l_ep;
    SearchTaskQueue<Genotype>       l_stq;
    GeneticParameters<Genotype>     l_gp;


    l_i   = Runtime.getRuntime().availableProcessors();
    l_swt = Arrays.create(SearchWorkerThread.class, l_i);
    l_ep  = this.get_event_propagator();
    l_stq = this.get_task_queue();

    for(--l_i; l_i >= 0; l_i--)
      {
      l_swt[l_i] = new SearchWorkerThread<Genotype>(
                                          this, l_stq,
                                          p_def.create_context(l_ep));
      }

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

    for(l_i = (l_swt.length-1); l_i >= 0; l_i--)
      {
      l_swt[l_i].update_context(l_gp);
      }

    this.m_swt = l_swt;
    }

/**
 * This method is called to initialize the search engine. You must all
 * initialization code here.
 * @param p_deserialized  <code>true</code> if and only if this
 *                        initialization was called due to an
 *                        deserialization process, <code>false</code> if it
 *                        is the normal initialization in the constructor.
 */

  @Override
  protected void  initialize  (final boolean p_deserialized)
    {
    super.initialize(p_deserialized);
    if(p_deserialized) this.init_threads(this.m_definition);
    }

/**
 * Startup the search. This method will be called by <code>start()</code>.
 * @see #start()
 */

  @Override
  protected void  do_start  ()
    {
    SearchWorkerThread<Genotype>[]  l_swt;
    int                             l_i;

    super.do_start();

    l_swt = this.m_swt;
    for(l_i = (l_swt.length-1); l_i >= 0; l_i--)
      {
      l_swt[l_i].start();
      }
    }


/**
 * This method aborts the activity. It is guaranteed to be called only once
 * for aborting and once for shutdown.
 * @see #abort()
 * @see #shutdown()
 */

  @Override
  protected void  do_abort  ()
    {
    SearchWorkerThread<Genotype>[]  l_swt;
    int                             l_i;

    super.do_abort();

    l_swt = this.m_swt;
    for(l_i = (l_swt.length-1); l_i >= 0; l_i--)
      {
      l_swt[l_i].abort();
      }
    }


/**
 * Wait for this object.
 */

  @Override
  public  void  wait_for  ()
    {
    SearchWorkerThread<Genotype>[]  l_swt;
    int                             l_i;
    
    l_swt = this.m_swt;

    for(l_i = (l_swt.length-1); l_i >= 0; l_i--)
      {
      l_swt[l_i].wait_for();
      }
    
    super.wait_for();
    }

/**
 * The human readable name of the search engine.
 * @return The human readable name of the search engine.
 */

  @Override
  public  String  toString  ()
    {
    return "Local Genetic Engine";
    }
  }

File Information:

file name:LocalGeneticEngine.java
package:org.dgpf.search.algorithms.ga.local
qualified name:org.dgpf.search.algorithms.ga.local.LocalGeneticEngine.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/local/LocalGeneticEngine.java
size:6.871 KB (7036 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-07-15 09:36:31 GMT+0000
last access: 2017-11-23 01:31:03 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-23 05:22:32 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo