Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.algorithms.ga.GeneticState.java

Here you can find all the information about the file org.dgpf.search.algorithms.ga.GeneticState.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 09:57:17
 * Original Filename: org.dgpf.search.algorithms.ga.GeneticState.java
 * Version          : 2.0.3
 * Last modification: 2006-07-21
 *                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;

import java.io.Serializable;

import org.dgpf.search.api.SearchState;
import org.dgpf.search.api.SearchUtils;
import org.sfc.math.stochastic.statistics.StatisticInfo;
import org.sfc.math.stochastic.statistics.StatisticInfoBag;
import org.sfc.meta.BoundedDoubleProperty;
import org.sfc.meta.BoundedIntProperty;
import org.sfc.meta.MetaInformation;
import org.sfc.meta.Property;
import org.sfc.utils.Typesafe;

/**
 * <p>
 * This class encapsulates the dynamic state of a genetic evolution.
 * This state will change every generation and includes statistical
 * information.
 * </p><p>
 * <h2>SERIALIZATION WARNING</h2>
 * Serializing instances of this class more than once into the same
 * <code>ObjectOutputStream</code> will lead to corruption.
 * <code>ObjectOutputStream</code>s serialize the same object only once,
 * thus serializing an instance of this class twice will lead to only
 * one real copy, and reading it will return the same instance with the
 * same values twice. So if you keep a <code>Geneticstate</code>, wait
 * until its values change and than storing it again will lead to the
 * changed data getting lost.
 * </p>
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 * @see GeneticStateBag
 * @author Thomas Weise
 */

public class GeneticState<Genotype extends Serializable>
                                   extends SearchState<Genotype>
  {
/**
 * The serial version uid.
 */

  private static final long serialVersionUID = 2;

/**
 * The meta information of genetic states.
 */

  public  static  final MetaInformation GENETIC_STATE_METAINFO  ;
  
  
/**
 * The property for crossover rate.
 */

  public  static  final BoundedDoubleProperty       CROSSOVER_RATE ;
/**
 * The property for mutation rate.
 */

  public  static  final BoundedDoubleProperty       MUTATION_RATE;
/**
 * The property for the count of diversity enhancement swaps.
 */

  public  static  final BoundedIntProperty          DIVERSITY_SWAPS;
/**
 * The property for the selection algorithm.
 */

  public  static  final Property<String>            SELECTION_ALGORITHM ;
/**
 * The property for the population size.
 */

  public  static  final BoundedIntProperty          POPULATION_SIZE;
/**
 * The elitism percentage.
 */

  public  static  final BoundedDoubleProperty       ELITISM_PERCENTAGE;
  
  
  
  static
    {
    CROSSOVER_RATE = new BoundedDoubleProperty("crossover_rate",
                                 GeneticUtils.GENETIC_PROPERTIES,
                                 0.0d, 1.0d);
    CROSSOVER_RATE.add_port(GeneticState.class, true, false, null);
    
    MUTATION_RATE = new BoundedDoubleProperty("mutation_rate",
                                 GeneticUtils.GENETIC_PROPERTIES,
                                 0.0d, 1.0d);
    MUTATION_RATE.add_port(GeneticState.class, true, false, null);
    
    DIVERSITY_SWAPS = new BoundedIntProperty("diversity_swaps",
                                 GeneticUtils.GENETIC_PROPERTIES,
                                 0, SearchUtils.DEFAULT_MAX_INT);
    DIVERSITY_SWAPS.add_port(GeneticState.class, true, false, null);
    
    SELECTION_ALGORITHM = new Property<String>("selection_algorithm",
                                 GeneticUtils.GENETIC_PROPERTIES,
                                 String.class);
    SELECTION_ALGORITHM.add_port(GeneticState.class, true, false, null);

    POPULATION_SIZE = new BoundedIntProperty("population_size",
                                 GeneticUtils.GENETIC_PROPERTIES,
                                 0, SearchUtils.DEFAULT_MAX_INT);
    POPULATION_SIZE.add_port(GeneticState.class, true, false, null);

    ELITISM_PERCENTAGE = new BoundedDoubleProperty("elitism_percentage",
                                 GeneticUtils.GENETIC_PROPERTIES,
                                 0.0d, 1.0d);
    ELITISM_PERCENTAGE.add_port(GeneticState.class, true, false, null);
    
    
    SearchUtils.CROSSOVER_STAT.add_port(GeneticState.class, true, false,
                            SearchUtils.FITNESS_FUNCTION_COUNT_FUNC);
    SearchUtils.NEW_STAT.add_port(GeneticState.class, true, false,
                            SearchUtils.FITNESS_FUNCTION_COUNT_FUNC);
    SearchUtils.MUTATION_STAT.add_port(GeneticState.class, true, false,
                            SearchUtils.FITNESS_FUNCTION_COUNT_FUNC);
    SearchUtils.ALL_STAT.add_port(GeneticState.class, true, false,
                            SearchUtils.FITNESS_FUNCTION_COUNT_FUNC);
  
    
    GENETIC_STATE_METAINFO = new MetaInformation();
    }
  
/**
 * The size of the population to evolve.
 * @see #get_population_size()
 */

   private      int                     m_population_size     ;

/**
 * The mutation rate, representing the portion of individuals created by
 * muation.
 * @see #get_mutation_rate()
 */

  private       double                  m_mutation_rate        ;

/**
 * The crossover rate, representing the portion of individuals created by
 * crossing over two other individuals.
 * @see #get_crossover_rate()
 */

  private       double                  m_crossover_rate      ;
/**
 * The count of random individual swaps to increase diversity.
 * @see #get_diversity_swaps()
 */

  private       int                     m_diversity_swaps ;
/**
 * The id of the selection algorithm to be used.
 * @see #get_selection_algorithm()
 */

  private       String                  m_selection ;

/**
 * The statistic info for the mutated elements.
 * @see #get_mutation_stat(int)
 */

  private       StatisticInfo[]         m_mutation_stat    ;
/**
 * The statistic info for the elements derived using crossover.
 * @see #get_crossover_stat(int)
 */

  private       StatisticInfo[]         m_crossover_stat  ;
/**
 * The statistic info for the elements randomly created.
 * @see #get_new_stat(int)
 */

  private       StatisticInfo[]         m_new_stat         ;
/**
 * The statistic info for all the elements.
 * @see #get_all_stat(int)
 */

  private       StatisticInfo[]         m_all_stat       ;
/**
 * The percentage of new individuals to be offspring of individuals in the
 * non-dominated list.
 * @see #get_elitism_percentage()
 */

  private       double                  m_elitism_percentage  ;

/**
 * Create a new genetic state.
 * @param p_ff_count  The count of fitness functions evaluated by this
 *                    search.
 * @param p_bag       The genetic state bag that owns this search state
 *                    instance.
 */

  protected GeneticState (final int                         p_ff_count,
                          final GeneticStateBag<Genotype>   p_bag)
    {
    super(p_ff_count);

    StatisticInfoBag[]  l_msb, l_csb, l_nsb, l_asb;
    StatisticInfo[]     l_ms, l_cs, l_ns, l_as;
    int                 l_i;

    this.set_population_size(GeneticUtils.DEFAULT_POPULATION_SIZE);
    this.set_diversity_swaps(GeneticUtils.DEFAULT_DIVERSITY_SWAPS);
    this.set_selection_algorithm(GeneticUtils.DEFAULT_SELECTION_ALGORITHM
                                             .toString());

    l_msb = p_bag.m_mutation_stat;
    l_csb = p_bag.m_crossover_stat;
    l_nsb = p_bag.m_new_stat;
    l_asb = p_bag.m_all_stat;
    l_i   = l_asb.length;

    this.m_all_stat       = l_as = new StatisticInfo[l_i];
    this.m_crossover_stat = l_cs = new StatisticInfo[l_i];
    this.m_mutation_stat  = l_ms = new StatisticInfo[l_i];
    this.m_new_stat       = l_ns = new StatisticInfo[l_i];

    for(--l_i; l_i >= 0; l_i--)
      {
      l_as[l_i] = l_asb[l_i].unpack();
      l_ms[l_i] = l_msb[l_i].unpack();
      l_cs[l_i] = l_csb[l_i].unpack();
      l_ns[l_i] = l_nsb[l_i].unpack();
      }
    
    this.set_elitism_percentage(GeneticUtils.DEFAULT_ELITISM_PERCENTAGE);
    }

/**
 * Obtain the statistics for the specified fitness function for the "new"
 * individuals of this generation. The new individuals are those created
 * completely randomly. For each fitness function, one statistical record
 * is available.
 * @param p_ff_index  The index of the fitness function to obtain the
 *                    statistics of.
 * @return  The statistics record for the new individuals and the specified
 *          fitness function.
 */

  public  final StatisticInfo get_new_stat (final int p_ff_index)
    {
    return this.m_new_stat[p_ff_index];
    }

/**
 * Obtain the statistics for the specified fitness function for the
 * "mutated" individuals of this generation. The mutated individuals are
 * those created by randomly mutating an existing individual.
 * For each fitness function, one statistical record is available.
 * @param p_ff_index  The index of the fitness function to obtain the
 *                    statistics of.
 * @return  The statistics record for the mutated individuals and the
 *          specified fitness function.
 */

  public  final StatisticInfo get_mutation_stat (final int p_ff_index)
    {
    return this.m_mutation_stat[p_ff_index];
    }



/**
 * Obtain the statistics for the specified fitness function for the
 * "crossovered" individuals of this generation. These individuals are
 * those created by joining two existing individuals. For each fitness
 * function, one statistical record is available.
 * @param p_ff_index  The index of the fitness function to obtain the
 *                    statistics of.
 * @return  The statistics record for the individuals created by crossover
 *          and the specified fitness function.
 */

  public  final StatisticInfo get_crossover_stat (final int p_ff_index)
    {
    return this.m_crossover_stat[p_ff_index];
    }

/**
 * Obtain the statistics for the specified fitness function for the
 * the whole population of this generation. The whole popultion contains
 * the individuals which are created due to new, mutation and crossover
 * tasks. For each fitness function, one statistical record is available.
 * @param p_ff_index  The index of the fitness function to obtain the
 *                    statistics of.
 * @return  The statistics record for the whole population.
 */

  public  final StatisticInfo get_all_stat (final int p_ff_index)
    {
    return this.m_all_stat[p_ff_index];
    }

/**
 * Obtain the size of the population. This is the count of inidivduals to
 * be tested each generation.
 * @return  The size of the population. This is the count of inidivduals to
 *          be tested each generation.
 */

  public  final  int get_population_size  ()
    {
    return this.m_population_size;
    }

/**
 * Set the size of the population. This is the count of inidivduals to
 * be tested each generation. This will be at least 2.
 * @param p_population_size  The size of the population. This is the count
 *                           of inidivduals to be tested each generation.
 */

  private final void  set_population_size(final int p_population_size)
    {
    this.m_population_size = ((p_population_size > 4) ?
               ((p_population_size < GeneticUtils.MAX_POPULATION_SIZE) ?
                 p_population_size : GeneticUtils.MAX_POPULATION_SIZE) : 4);
    }

/**
 * Obtain the current mutation ratio, representing the portion of
 * individuals created by muation.
 * @return The current mutation ration, representing the portion of
 *         individuals created by muation.
 */

  public  final double  get_mutation_rate ()
    {
    return this.m_mutation_rate;
    }

/**
 * Set the next mutation ratio, representing the portion of
 * individuals created by muation.
 * @param p_rate The next  mutation ration, representing the
 *                portion of individuals created by muation.
 */

  private final void  set_mutation_rate (final  double p_rate)
    {
    if( (p_rate >= 0.0d) && (p_rate <= 1.0d))
      {
      this.m_mutation_rate = p_rate;
      }
    }

/**
 * Obtain the crossover rate, representing the portion of individuals
 * created by crossing over two other individuals.
 * @return The crossover rate, representing the portion of individuals
 *         created by crossing over two other individuals.
 */

  public  final double  get_crossover_rate ()
    {
    return this.m_crossover_rate;
    }

/**
 * Set the crossover rate, representing the portion of individuals created
 * by crossing over two other individuals.
 * @param p_rate The crossover rate, representing the portion of
 *               individuals created by crossing over two other individuals.
 */

  private final void  set_crossover_rate (final double p_rate)
    {
    if( (p_rate >= 0.0d) && (p_rate <= 1.0d) )
      {
      this.m_crossover_rate = p_rate;
      }
    }

/**
 * Obtain the count of random individual swaps to increase diversity.
 * @return  The count of random individual swaps to increase diversity.
 */

  public  final int get_diversity_swaps ()
    {
    return this.m_diversity_swaps;
    }

/**
 * Internally set the count of random individual swaps to increase diversity.
 * @param p_diversity_swaps The count of random individual swaps to
 *                          increase diversity.
 */

  private final void  set_diversity_swaps (final int  p_diversity_swaps)
    {
    if(p_diversity_swaps >= 0)
      {
      this.m_diversity_swaps = p_diversity_swaps;
      }
    }



/**
 * Obtain the id of the selection algorithm to be used by the genetic
 * engine.
 * @return  The id of the selection algorithm used by the genetic engine.
 */

  public  final String get_selection_algorithm()
    {
    return this.m_selection;
    }

/**
 * Set the selection algorithm used.
 * @param p_sa  The name of the selection algorithm to be used.
 */

  private final void  set_selection_algorithm (final String p_sa)
    {
    if((p_sa != null) && (p_sa.length() > 0))
      {
      this.m_selection = p_sa;
      }
    }


/**
 * Obtain the percentage of new individuals to be offspring of individuals
 * in the non-dominated list.
 * @return The percentage of new individuals to be offspring of individuals
 *         in the non-dominated list.
 */

  public  final double  get_elitism_percentage()
    {
    return this.m_elitism_percentage;
    }
  
/**
 * Set the percentage of new individuals to be offspring of individuals
 * in the non-dominated list.
 * @param p_percentage   The new percentage of new individuals to be
 *                       offspring of individuals in the non-dominated list.
 */

  private final void  set_elitism_percentage  (final double p_percentage)
    {
    if((p_percentage >= 0.0d) && (p_percentage <= 1.0d))
      {
      this.m_elitism_percentage = p_percentage;
      }
    }
  
/**
 * Assign the internal data of this search data to the data of
 * another one.
 * @param p_data  The simulation data  to assign to.
 */

  @Override
  protected void  assign  (final Object p_data)
    {
    GeneticState<Genotype>      l_gs;
    GeneticParameters<Genotype> l_gp;
    Object                      l_o;

    super.assign(p_data);


    if(p_data instanceof GeneticState)
      {
      l_gs = Typesafe.cast(p_data);

      this.set_crossover_rate(l_gs.m_crossover_rate);
      this.set_mutation_rate(l_gs.m_mutation_rate);
      this.set_diversity_swaps(l_gs.m_diversity_swaps);
      this.set_population_size(l_gs.m_population_size);
      this.set_selection_algorithm(l_gs.m_selection);
      this.set_elitism_percentage(l_gs.m_elitism_percentage);
      }

    if(p_data instanceof GeneticParameters)
      {
      l_gp = Typesafe.cast(p_data);

      this.set_crossover_rate(l_gp.get_crossover_rate());
      this.set_mutation_rate(l_gp.get_mutation_rate());
      this.set_diversity_swaps(l_gp.get_diversity_swaps());
      this.set_population_size(l_gp.get_population_size());
      this.set_elitism_percentage(l_gp.get_elitism_percentage());
      
      l_o = l_gp.get_selection_algorithm();
      if(l_o != null)
        {
        this.set_selection_algorithm(l_o.toString());
        }
      }
    }
  
  

  
/**
 * Creates and returns a copy of this object.  The precise meaning 
 * of "copy" may depend on the class of the object. 
 *
 * @return     A clone of this instance.
 * 
 * @see java.lang.Cloneable
 */

  @Override
  public  Object  clone ()
    {
    final GeneticState<Genotype>  l_ss;
    final StatisticInfo[]         l_m, l_n, l_c, l_a;
          int                     l_i;
  
    l_ss = Typesafe.cast(super.clone());      
    l_ss.m_mutation_stat   = l_m = l_ss.m_mutation_stat.clone();
    l_ss.m_new_stat        = l_n = l_ss.m_new_stat.clone();
    l_ss.m_crossover_stat  = l_c = l_ss.m_crossover_stat.clone();
    l_ss.m_all_stat        = l_a = l_ss.m_all_stat.clone();
    
    for(l_i = (l_a.length-1); l_i >= 0; l_i--)
      {
      l_a[l_i] = ((StatisticInfo)(l_a[l_i].clone()));
      l_m[l_i] = ((StatisticInfo)(l_m[l_i].clone()));
      l_n[l_i] = ((StatisticInfo)(l_n[l_i].clone()));
      l_c[l_i] = ((StatisticInfo)(l_c[l_i].clone()));
      }
    
    return l_ss;
    }
  }

File Information:

file name:GeneticState.java
package:org.dgpf.search.algorithms.ga
qualified name:org.dgpf.search.algorithms.ga.GeneticState.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/GeneticState.java
size:18.083 KB (18517 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-07-21 13:11:55 GMT+0000
last access: 2017-11-17 21:26:35 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-17 21:26:35 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo