Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.ga.p2p.P2PGeneticEngine.java

Here you can find all the information about the file org.dgpf.search.algorithms.ga.p2p.P2PGeneticEngine.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 06:33:05
 * Original Filename: org.dgpf.search.algorithms.ga.p2p.P2PGeneticEngine.java
 * Version          : 2.1.7
 * Last modification: 2006-06-22
 *                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.p2p;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import org.dgpf.search.algorithms.ga.SelectionAlgorithm;
import org.dgpf.search.algorithms.ga.local.LocalGeneticEngine;
import org.dgpf.search.api.Assigner;
import org.dgpf.search.api.FitnessAccessor;
import org.dgpf.search.api.Individual;
import org.dgpf.search.api.IndividualComparator;
import org.dgpf.search.api.NonDominatedList;
import org.dgpf.search.api.SearchDefinition;
import org.dgpf.search.api.SearchStateBag;
import org.dgpf.search.api.SearchUtils;
import org.dgpf.search.api.p2p.P2PState;
import org.dgpf.search.api.p2p.SearchP2P;
import org.sfc.collections.Arrays;
import org.sfc.math.stochastic.Randomizer;
import org.sfc.math.stochastic.statistics.StatisticInfoBag;
import org.sfc.net.p2p.IPeerToPeer;
import org.sfc.utils.Typesafe;

/**
 * Instances of this class can form a peer-to-peer network of genetic
 * engines working all on the same problem and evolving a distributed
 * population using the island-hopping mechanism.
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 * @author Thomas Weise
 */

public class P2PGeneticEngine<Genotype extends Serializable>
                                       extends LocalGeneticEngine<Genotype>
                                       implements IPeerToPeer
  {
/**
 * The internal peer-to-peer node.
 */

  private       SearchP2P<Genotype>   m_p2p;
/**
 * The port to be used.
 */

  private final int                   m_port  ;

/**
 * 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.
 * @param p_port        The port where the peer-to-peer service of the
 *                      genetic engine should reside. If you leave this
 *                      <code>lt;1024</code>, the default port will be taken.
 */

  public P2PGeneticEngine  (final SearchDefinition<Genotype>  p_definition,
                            final int                         p_port)
    {
    super(p_definition);
    this.m_port = ((p_port > 1023) ? p_port
                   : SearchUtils.DEFAULT_INDIVIDUAL_EXCHANGE_PORT);
    }

/**
 * 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_port        The port where the peer-to-peer service of the
 *                      genetic engine should reside. If you leave this
 *                      <code>lt;1024</code>, the default port will be taken.
 * @param p_ndl_size    The size of the non-dominated list. Set this
 *                      to <code>-1</code> for don't care.
 */

  public P2PGeneticEngine(final P2PGeneticParameters<Genotype> p_parameters,
                          final SearchDefinition<Genotype>     p_definition,
                          final int                            p_ndl_size,
                          final int                            p_port)
    {
    super(p_parameters, p_definition, p_ndl_size);
    this.m_port = ((p_port > 1023) ? p_port
                   : SearchUtils.DEFAULT_INDIVIDUAL_EXCHANGE_PORT);
    this.init_p2p();
    }

/**
 * Initialize the peer-to-peer structure.
 */

  private final void  init_p2p  ()
    {
    P2PGeneticState<Genotype>       l_gs;
    P2PGeneticParameters<Genotype>  l_gp;
    P2PState                        l_g;

    l_gs = Typesafe.cast(this.get_state());
    l_gp = Typesafe.cast(this.get_parameters());
    l_g  = l_gs.get_p2p_state();
    
    this.m_p2p = new SearchP2P<Genotype>(
                     thisthis.get_thread_group(),
                     this.get_event_propagator(), this.m_port,
                     l_g.get_allowed_emigrants(),
                     l_g.get_allowed_immigrants(),
                     l_gs.get_fitness_function_count(),
                     l_gp.get_comparator());
    
    this.add_compound_activity(this.m_p2p);
    }

/**
 * 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_p2p();
    }

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

  @Override
  protected void  do_start  ()
    {
    super.do_start();
    this.m_p2p.start();
    }

/**
 * This method is used to create the search state (and its accessor bag) to
 * be used by this search engine.
 * @return  A new search state (and its accessor bag).
 */

  @Override
  protected SearchStateBag<Genotype>  create_state  ()
    {
    return new P2PGeneticStateBag<Genotype>(
                this.get_parameters().get_fitness_function_count());
    }

  
/**
 * Perform a shutdown. It is guaranteed to be called only once for shutdown.
 * This method should only perform the actions needed to shutdown the
 * network of activities. The closing of the activity itself will be done
 * by <code>do_abort</code>, which will subsequently be called
 * automatically.
 * @see #abort()
 * @see #shutdown()
 */

  @Override
  protected void  do_shutdown ()
    {
    super.do_shutdown();
    this.m_p2p.shutdown();
    }
  
/**
 * 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  ()
    {
    super.do_abort();
    this.m_p2p.abort();
    }

/**
 * This hook allows you to insert some additional individuals by overriding
 * existing ones.
 * @param p_population  The population array.
 * @param p_count       The count of individuals in the population array.
 * @param p_comparator  The comparator to be used for insertion.
 * @return  <code>true</code> if and only if at least one new individual
 *          has been injected into the population, <code>false</code>
 *          otherwise.
 */

  @Override
  protected boolean insert_additional
                      (final Individual<Genotype>[]   p_population,
                             int                      p_count,
                       final IndividualComparator     p_comparator)
    {
    final SearchP2P<Genotype>           l_p2p;
    final P2PGeneticStateBag<Genotype>  l_gsb;
    final P2PGeneticState<Genotype>     l_gs;
          int                           l_i, l_j, l_k;
          Individual<Genotype>          l_d;
    final StatisticInfoBag[]            l_b;
          FitnessAccessor               l_fa;
          boolean                       l_bb, l_bq;
    final P2PState                      l_ps;
    final IndividualComparator          l_cc;

    l_p2p = this.m_p2p;
    l_gs  = Typesafe.cast(this.get_state());
    l_gsb = Typesafe.cast(this.get_state_bag());
    l_ps  = l_gs.get_p2p_state();
    l_bb  = (!(l_ps.get_challenge_immigrants()));
    l_b   = l_gsb.m_p2p_state.get_immigrant_stat();
    
    l_k   = 0;
    l_j   = (p_count-1);
    l_bq  = false;
    
main:
    for(l_i = l_ps.get_allowed_emigrants(); l_i > 0; l_i--) 
      {
inner:
      for(;;)
        {
        l_d = l_p2p.immigrate();
        if(l_d == null) break main;
        this.notify_nondominated(l_d);
        if(l_bb || (p_comparator.compare(p_population[l_j], l_d) >= 0))
          {
          Assigner.assign(p_population[l_j--], l_d);
          if(l_j < 0)
            {
            l_j += p_count;
            l_bq = true;
            }
          break inner;
          }
       
        l_k++;
        }
      }
    
    l_gsb.m_p2p_state.update_killed(l_k);
    
    if(l_bq) l_j = 0;
    else     
      {
      l_j++;
      p_count -= l_j;
      }

    if(p_count <= 0)
      {
      this.m_p2p.update_state(l_gsb.m_p2p_state);
      return false;
      }

    for(l_k = (l_gs.get_fitness_function_count()-1); l_k >= 0; l_k--)
      {
      l_fa = FitnessAccessor.get_accessor(l_k);

      Arrays.sort(p_population, l_j, p_count, l_fa);
      // <!-- Check fitness best
      l_gsb.check_best(p_population[l_j], l_k);
      // -->     
      l_b[l_k].gather_info_sorted(p_population, l_fa, l_j, p_count);
      }
    
    // <!-- Check total best
    l_cc = this.get_parameters().get_comparator();
    Arrays.sort(p_population, l_j, p_count, l_cc);
//    l_gsb.check_best(p_population[l_j], l_cc);
    // -->
    
    this.m_p2p.update_state(l_gsb.m_p2p_state);

    return true;  
    }

/**
 * This method allows you to perform additional selection based operations
 * on the population. By doing so, you can, for example, select some
 * individuals for migration if implementing genetic algorithms that use
 * island hopping.  The individuals in the array are ordered by the search's
 * current comparator, with the fittest individual at index <code>0</code>
 * and the worst at index <code>p_count-1</code>.
 * @param p_population    The population array.
 * @param p_count         The population count.
 * @param p_selection     The selection algorithm to use.
 * @param p_random        A randomizer that can be used.
 * @param p_ndl           The non-dominated individual list.
 * @param p_elp           The elitism percentage.
 */

  @Override
  protected void  select_additional
                      (final Individual<Genotype>[] p_population,
                       final int                    p_count,
                       final SelectionAlgorithm     p_selection,
                       final Randomizer             p_random,
                       final NonDominatedList<Genotype> p_ndl,
                       final double                     p_elp)
    {
    final P2PGeneticState<Genotype>  l_gs;
          int                        l_i;
    final SearchP2P<Genotype>        l_gp;
    final P2PState                   l_ps;
    final NonDominatedList<Genotype> l_ndl;

    l_gs = Typesafe.cast(this.get_state());
    l_ps = l_gs.get_p2p_state();
    l_gp = this.m_p2p;

    if(l_ps.get_emigrate_best())
      {
      l_ndl = this.get_rel_non_dominated();
      if(!(l_ndl.isEmpty()))
        {
        l_gp.emmigrate(l_ndl.get(this.get_randomizer()));
        }
      }

    for(l_i = l_ps.get_allowed_emigrants(); l_i > 0; l_i--)
      {
      l_gp.emmigrate((p_random.nextDouble() < p_elp) ? p_ndl.get(p_random) :
                      p_selection.select(p_random, p_population, p_count));
      }
    }

/**
 * <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>
 */

  @Override
  protected void  inner_update  ()
    {
    P2PGeneticState<Genotype> l_gs;

    super.inner_update();
    l_gs = Typesafe.cast(this.get_state());
    this.m_p2p.assign(l_gs, l_gs.get_p2p_state(),
                      this.get_parameters().get_comparator());
    }

/**
 * Add a peer to the internal peer list.
 * @param p_port  The port where the peer listens, if this parameter is
 *                invalid, we will try to use the same port we're currently
 *                running on.
 * @param p_i     The internet address the peer is located at.
 * @return  <code>true</code> if and only if the peer was not yet known and
 *          has been added successfully.
 */

  public  final boolean add_peer  (final InetAddress p_i,
                                   final int         p_port)
    {
    return this.m_p2p.add_peer(p_i, p_port);
    }



/**
 * Add a peer to the internal peer list.
 * @param p_isa   The internet address and port the peer is located at.
 * @return  <code>true</code> if and only if the peer was not yet known and
 *          has been added successfully.
 */

  public  final boolean add_peer  (final InetSocketAddress p_isa)
    {
    return this.m_p2p.add_peer(p_isa);
    }


/**
 * Remove a peer from the internal peer list.
 * @param p_isa   The internet address and port the peer is located at.
 * @return  <code>true</code> if and only if the peer was known and
 *          has been removed successfully.
 */

  public  final boolean remove_peer  (final InetSocketAddress p_isa)
    {
    return this.m_p2p.remove_peer(p_isa);
    }


/**
 * Wait for this object.
 */

  @Override
  public  void  wait_for  ()
    {    
    this.m_p2p.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 "P2P Genetic Engine (port " + this.m_port + ')';
    }
  }

File Information:

file name:P2PGeneticEngine.java
package:org.dgpf.search.algorithms.ga.p2p
qualified name:org.dgpf.search.algorithms.ga.p2p.P2PGeneticEngine.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/p2p/P2PGeneticEngine.java
size:14.198 KB (14539 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-07-14 06:39:34 GMT+0000
last access: 2017-11-20 02:19:22 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:19:22 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo