Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.search.algorithms.ga.GeneticParameters.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 13:16:12
 * Original Filename: org.dgpf.search.algorithms.ga.GeneticParameters.java
 * Version          : 2.0.2
 * Last modification: 2006-06-06
 *                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.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.dgpf.search.algorithms.ga.selection.OrderedSelection;
import org.dgpf.search.algorithms.ga.selection.RankedSelection;
import org.dgpf.search.algorithms.ga.selection.RouletteWheelSelection;
import org.dgpf.search.algorithms.ga.selection.RoundRobinSelection;
import org.dgpf.search.algorithms.ga.selection.TournamentSelection;
import org.dgpf.search.api.FitnessFunction;
import org.dgpf.search.api.SearchData;
import org.dgpf.search.api.SearchParameters;
import org.dgpf.search.api.SearchState;
import org.sfc.math.stochastic.Randomizer;
import org.sfc.meta.MetaInformation;
import org.sfc.meta.Property;
import org.sfc.utils.Typesafe;


/**
 * <p>
 * The genetic parameters contain the dynamic control data of the evolution.
 * They might get modified often and reflect their changes to the genetic
 * engine.</p><p>
 * Genetic parameters might adapt dynamically to the state of the evolution.
 * Therefore, any given sheme can be applied, adaption could be done
 * algorithmic or manually.
 * </p>
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 *
 * @author Thomas Weise
 */

public class GeneticParameters<Genotype extends Serializable>
                                        extends SearchParameters<Genotype>
  {
/**
 * The serial version uid.
 */

  private   static  final long    serialVersionUID        = 2;
  
/**
 * The meta information used for genetic parameters.
 */

  
  public  static  final MetaInformation GENETIC_PARAMETERS_METAINFO ;
  
/**
 * The property for the selection algorithm.
 */

  public  static  final Property<SelectionAlgorithm>  SELECTION_ALGORITHM ;
  
  static
    {
    SELECTION_ALGORITHM =
      new Property<SelectionAlgorithm>(GeneticState.SELECTION_ALGORITHM,
                                       SelectionAlgorithm.class);
    
    SELECTION_ALGORITHM.add_port(GeneticParameters.class,
                                 true, true, null);
    
    GeneticState.CROSSOVER_RATE.add_port(GeneticParameters.class,
                                 true, true, null);
    
    GeneticState.MUTATION_RATE.add_port(GeneticParameters.class
                                 true, true, null);
    
    GeneticState.DIVERSITY_SWAPS.add_port(GeneticParameters.class,
                                 true, true, null);
    
    GeneticState.POPULATION_SIZE.add_port(GeneticParameters.class,
                                 true, true, null);
    GeneticState.ELITISM_PERCENTAGE.add_port(GeneticParameters.class,
                                             true, true, null);
    
    GENETIC_PARAMETERS_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 the selection algorithm to be used.
 * @see #get_selection_algorithm()
 */

  private           SelectionAlgorithm      m_selection ;
/**
 * 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 parameter set.
 * @param p_data  The search data to assign to.
 */

  public GeneticParameters (final SearchData p_data)
    {
    super(p_data);

    if(!((p_data instanceof GeneticParameters) ||
         (p_data instanceof GeneticState)))
      {
      this.set_diversity_swaps(GeneticUtils.DEFAULT_DIVERSITY_SWAPS);
      }

    if(this.m_population_size <= 0)
      {
      this.set_population_size(GeneticUtils.DEFAULT_POPULATION_SIZE);
      }

    if(this.m_selection == null)
      {
      this.set_selection_algorithm(GeneticUtils.DEFAULT_SELECTION_ALGORITHM);
      }    
    }

/**
 * Create a new genetic parameter set.
 * @param p_functions   The fitness functions to be applied in this
 *                      evolution.
 */

   public GeneticParameters(final FitnessFunction<Genotype,?>[] p_functions)
     {
     this(p_functions.length);
     }

/**
 * Create a new genetic parameter set.
 * @param p_ff_count              The count of fitness functions evaluated
 *                                by this search.
 */

  public     GeneticParameters (final int p_ff_count)
    {
    super(p_ff_count);
    this.set_population_size(GeneticUtils.DEFAULT_POPULATION_SIZE);
    this.set_selection_algorithm(GeneticUtils.DEFAULT_SELECTION_ALGORITHM);
    this.set_diversity_swaps(GeneticUtils.DEFAULT_DIVERSITY_SWAPS);
    this.set_mutation_rate(GeneticUtils.DEFAULT_MUTATION_RATE);
    this.set_crossover_rate(GeneticUtils.DEFAULT_CROSSOVER_RATE);
    this.set_elitism_percentage(GeneticUtils.DEFAULT_ELITISM_PERCENTAGE);
    }

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

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

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

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

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


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

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

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

  public  void  set_selection_algorithm (final SelectionAlgorithm p_sa)
    {
    if(p_sa != null) 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.
 */

  public  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
  public void  assign  (final Object p_data)
    {
    GeneticState<Genotype>      l_gs;
    GeneticParameters<Genotype> l_gp;

    super.assign(p_data);

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

      this.set_crossover_rate(l_gs.get_crossover_rate());
      this.set_mutation_rate(l_gs.get_mutation_rate());
      this.set_diversity_swaps(l_gs.get_diversity_swaps());
      this.set_population_size(l_gs.get_population_size());
      this.set_elitism_percentage(l_gs.get_elitism_percentage());
      }

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

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


/**
 * 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.
 * @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<Genotype> p_state)
    {
    return (super.adapt(p_state)                        ||
            this.m_selection.adapt(p_state)             ||
            this.m_selection.get_sorting_algorithm().adapt(p_state));
    }



/**
 * Stores the <code>SfcEvent</code> into the stream.
 * @param p_s The output stream.
 * @throws  IOException If something io-like went wrong.
 */

  private final void writeObject(final ObjectOutputStream p_s)
        throws IOException
    {
    p_s.defaultWriteObject();
    p_s.writeUnshared(this.m_selection);
    }



/**
 * Reconstitute the <code>SfcEvent</code> instance from a stream (that is,
 * deserialize it).
 * @param p_s The input stream.
 * @throws  IOException If something io-like went wrong.
 * @throws  ClassNotFoundException  If a needed class could not be found.
 */

  private final void readObject(final ObjectInputStream p_s)
        throws IOException, ClassNotFoundException
    {
    p_s.defaultReadObject();
    this.set_selection_algorithm( (SelectionAlgorithm)(p_s.readUnshared()) );
    }

/**
 * 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 ()
    {
    GeneticParameters<Genotype>  l_sp;

    l_sp              = Typesafe.cast(super.clone());
    l_sp.m_selection  = Typesafe.cast(l_sp.m_selection.clone());
    return l_sp;
    }

/**
 * Create a selection algorithm.
 * @param p_r   The randomizer to be used.
 * @return  The selection algorithm.
 */

  private static  final SelectionAlgorithm  get_sel_a(final Randomizer p_r)
    {
    SelectionAlgorithm[] l_sa;
    int                  l_i;
    double               l_nr;
    
    switch(p_r.nextInt(5))
      {
      case  0:
        {
        return new TournamentSelection(2 + p_r.nextInt(3)
                                         + (int)(p_r.exponential()));
        }
      case 1:
        {
        do
          {
          l_nr = p_r.nextDouble();
          } while((l_nr < 0.1d) && (l_nr > 0.9d));
        return new OrderedSelection(1.0d / l_nr);
        }
        
      case 2:
        {
        return RankedSelection.DEFAULT_INSTANCE;
        }
        
      case 3:
        {
        return RouletteWheelSelection.INSTANCE;
        }
               
      default:
        {
        l_i = (p_r.nextInt(3) + 1 + ((int)(p_r.exponential())));        
        l_sa = new SelectionAlgorithm[l_i];
        
        for(--l_i; l_i >= 0; l_i--)
          {
          l_sa[l_i] = get_sel_a(p_r);
          }
        
        return new RoundRobinSelection(2 + p_r.nextInt(5) +
                                       ((int)(p_r.exponential())), l_sa);
        }
      }
    }

/**
 * After setting the non-fitness-influencing parameters of this search
 * parameter set to the allowed maximum, call this method to alter the
 * configuration randomly.
 * @param p_r The randomizer to be used.
 */

  @Override
  public  void  configure_randomly  (final Randomizer p_r)
    {
    double              l_mr, l_cr, l_nr;
    
    this.set_population_size(16 + 
        p_r.nextInt(Math.max(1, this.get_population_size()-16)));
    
    l_mr = Math.pow(p_r.nextDouble(), 0.8d);
    l_cr = p_r.nextDouble();
    l_nr = (Math.pow(p_r.nextDouble(), 5.2d) + l_mr + l_cr);
    
    this.set_mutation_rate(l_mr / l_nr);
    this.set_crossover_rate(l_cr / l_nr);
    
    this.set_elitism_percentage(this.get_elitism_percentage() *
                                p_r.nextDouble());
    
    this.set_selection_algorithm(get_sel_a(p_r));
    
    super.configure_randomly(p_r);
    }
  }

File Information:

file name:GeneticParameters.java
package:org.dgpf.search.algorithms.ga
qualified name:org.dgpf.search.algorithms.ga.GeneticParameters.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/algorithms/ga/GeneticParameters.java
size:16.286 KB (16677 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-08-27 12:34:51 GMT+0000
last access: 2017-11-21 15:41:55 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-21 15:41:55 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo