Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.automaton.search.Crossover.java

Here you can find all the information about the file org.dgpf.gp.automaton.search.Crossover.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2005 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2005-12-30 15:31:21
 * Original Filename: org.dgpf.automaton.genetics.Crossover.java
 * Version          : 2.2.1
 * Last modification: 2006-05-08
 *                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.gp.automaton.search;

import org.dgpf.gp.automaton.base.Instruction;
import org.dgpf.gp.automaton.base.ProgramAccessor;
import org.dgpf.gp.automaton.instructions.IfJump;
import org.sfc.collections.Arrays;
import org.sfc.math.stochastic.Randomizer;
import org.sfc.utils.Typesafe;

/**
 * This internal class provides crossover ability to our genetic
 * programming facility.
 *
 * @author Thomas Weise
 */

final class Crossover extends ProgramAccessor
  {  
  
/**
 * The internal constructor - you can't call that :-)
 */

  private Crossover()
    {
    Typesafe.do_not_call();
    }
  
    
/**
 * Create a new individual by mating.
 * @param p_p1      The first parent program.
 * @param p_p2      The second parent program.
 * @param p_random  The randomizer.
 * @return  The new child.
 */

  static  final Instruction[] crossover (final Instruction[] p_p1,
                                         final Instruction[] p_p2,
                                         final Randomizer    p_random)
    {
    final int             l_l1, l_l2, l_c1, l_p1, l_p2, l_ec;
          int             l_i, l_t;
          Instruction[]   l_n;
          Instruction     l_in;
          IfJump          l_ij;
         
    l_l1 = p_p1.length;
    l_l2 = p_p2.length;
    l_c1 = (p_random.nextInt(l_l1) + 1);
    l_p1 = p_random.nextInt(l_l1 - l_c1 + 1);
    l_p2 = p_random.nextInt(l_l2 + 1);
    l_ec = (l_p2 + l_c1);

    l_n = Arrays.insert_space_direct(p_p2, l_p2, l_c1);
    System.arraycopy(p_p1, l_p1, l_n, l_p2, l_c1);
    
    for(l_i = (l_n.length-1); l_i >= 0; l_i--)
      {
      l_in = l_n[l_i];
      if(l_in instanceof IfJump)
        {
        l_ij = ((IfJump)l_in);
        l_t  = l_ij.get_target();
          
        if( (l_i >= l_p2) && (l_i < l_ec) )
          {
// instruction came from parent one, has now rubbish jumps
          if( (l_t >= l_p1) && (l_t < (l_p1 + l_c1)))
            {
// the jump can be corrected properly.
            l_t = l_t + l_p2 - l_p1;
            }
          else
            {
            if(l_t < l_p1)
              {
              if(l_p2 <= 0) l_t = 0;
              else          l_t = p_random.nextInt(l_p2);
              }
            else
              {
              if(l_p2 >= l_l2) l_t = 0;
              else l_t = l_p2+l_c1+p_random.nextInt(l_l2 - l_p2);
              }
            }
            
          
          l_n[l_i] = new IfJump(l_ij.get_condition(), l_t);
          }
        else
          {
// instruction came from parent two, with misleading jump.
          if(l_t >= l_p2)
            {
            l_n[l_i] = new IfJump(l_ij.get_condition(), l_t + l_c1);
            }
          }
        }      
      }
        
    return l_n;
    }
  }

File Information:

file name:Crossover.java
package:org.dgpf.gp.automaton.search
qualified name:org.dgpf.gp.automaton.search.Crossover.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/automaton/search/Crossover.java
size:3.864 KB (3957 B)
uploaded: 2015-07-22 04:10:55 GMT+0000
last update: 2006-05-08 13:38:47 GMT+0000
last access: 2017-11-19 04:41:36 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-19 04:41:36 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo