Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.sa.p2p.P2PCSSAEngine.java

Here you can find all the information about the file org.dgpf.search.algorithms.sa.p2p.P2PCSSAEngine.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-05 13:27:42
 * Created by       : Stefan Niemczyk, Marc Kirchhoff
 * Original Filename: org.dgpf.search.algorithms.sa.p2p.P2PCSSAEngine.java
 * Version          : 1.0.1
 * 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.sa.p2p;

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

import org.dgpf.search.algorithms.sa.cs.CSSAEngine;
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.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.statistics.StatisticInfoBag;
import org.sfc.net.p2p.IPeerToPeer;
import org.sfc.utils.Typesafe;

/**
 * <p>
 * Instances of this class can form a peer-to-peer network of Simulated 
 * Annealing engines working all on the same problem. Unlike the normal
 * <code>P2PSAEngine</code>, this engine does not process the tasks
 * locally but let them be processed remotely in search servers.
 * </p><p>
 * This is the highest scalable approach, it serves for problems that need
 * really a lot time to be simulated and allows different computer farms or
 * cluster cooperate on the other hand.
 * </p>
 *
 * @param <Genotype> The genotype of the elements to be evolved.
 * @author Stefan Niemczyk, Marc Kirchhoff
 */

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

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

  private final int                   m_port  ;

/**
 * Create a new simulated annealing engine without providing additional
 * information or parameters. This constructor will be used only for
 * loading a snapshot immediately afterwards.
 * @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 P2PCSSAEngine  (final int                         p_port)
    {
    super();
    this.m_port = ((p_port > 1023) ? p_port
                   : SearchUtils.DEFAULT_INDIVIDUAL_EXCHANGE_PORT);
    }

/**
 * Create a p2p-extended simulated annealing engine.
 * @param p_parameters  The parameters object guiding the evolution process.
 * @param p_ndl_size    The size of the non-dominated list. Set this
 *                      to <code>-1</code> for don't care.
 * @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 P2PCSSAEngine(final P2PSAParameters<Genotype>    p_parameters,
                       final int                          p_ndl_size,
                       final int                          p_port)
    {
    super(p_parameters, 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  ()
    {
    P2PSAState<Genotype>       l_gs;
    P2PSAParameters<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 P2PSAStateBag<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  ()
    {
    this.m_p2p.abort();
    super.do_abort();
    }

/**
 * 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 CS Simulated Annealing Engine (port " + this.m_port + ')';
    }

/**
 * Called after the update, should be used to select individuals to be
 * emigrated.
 * @param p_population  The population array.
 * @param p_count       The population size, i.e. the count of valid items
 *                      inside the population array.
 */

  @Override
  protected void  emigate (final Individual<Genotype>[] p_population,
                           final int                    p_count)
    {
    final P2PSAState<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();
      l_gp.emmigrate(l_ndl.get(this.get_randomizer().nextInt(l_ndl.size())));
      }

    for(l_i = (Math.min(l_ps.get_allowed_emigrants(),
                        p_count)-1); l_i >= 0; l_i--)
      {
      l_gp.emmigrate(p_population[l_i]);
      }
    }


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

/**
 * This hook allows you to insert some additional individuals by overriding
 * existing ones.
 * @param p_population  The population array.
 * @param p_count       The population size, i.e. the count of valid items
 *                      inside 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 immigrate
                      (final Individual<Genotype>[]   p_population,
                             int                      p_count,
                       final IndividualComparator     p_comparator)
    {
    final SearchP2P<Genotype>           l_p2p;
    final P2PSAStateBag<Genotype>       l_gsb;
    final P2PSAState<Genotype>          l_gs;
          int                           l_i, l_k, l_j;
          Individual<Genotype>          l_d;
          FitnessAccessor               l_fa;
          boolean                       l_bb, l_bq;
    final P2PState                      l_ps;
    final IndividualComparator          l_cc;
    final StatisticInfoBag[]            l_b;

    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;
    }
  }

File Information:

file name:P2PCSSAEngine.java
package:org.dgpf.search.algorithms.sa.p2p
qualified name:org.dgpf.search.algorithms.sa.p2p.P2PCSSAEngine.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/sa/p2p/P2PCSSAEngine.java
size:13.272 KB (13591 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-07-14 06:40:03 GMT+0000
last access: 2017-11-18 04:21:10 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-18 21:27:47 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo