Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.vm.mutation.CodeEditor.java

Here you can find all the information about the file org.dgpf.gp.vm.mutation.CodeEditor.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-07-04 10:07:11
 * Original Filename: org.dgpf.gp.vm.mutation.CodeEditor.java
 * Version          : 1.0.0
 * Last modification: 2006-07-04
 *                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.vm.mutation;

import org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.InstructionSet;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.ctrl.Call;
import org.dgpf.gp.vm.instructions.ctrl.Goto;
import org.sfc.math.stochastic.Randomizer;
import org.sfc.utils.Typesafe;

/**
 * This class comes with some default methods useful when modifying code.
 *
 * @author Thomas Weise
 */

public final  class CodeEditor
  {
/**
 * No instance of this class is allowed.
 */

  private CodeEditor()
    {
    Typesafe.do_not_call();
    }
  
  

/**
 * Create space for an insertion into an instruction array.
 * @param p_source  The original code.
 * @param p_p       The insertion position.
 * @param p_c       The insert count.
 * @return  The new procedure, with <code>null</code>s where the
 *          insertion space is created.
 */

  public static final Instruction[] insert_instr(final Instruction[] p_source,
                                                 final int           p_p,
                                                 final int           p_c)
    {
    int           l_i, l_t, l_l;
    Instruction   l_ins;
    Goto          l_ij;
    Instruction[] l_n;
    
    if(p_c <= 0) return p_source;
    
    l_l  = p_source.length;
    l_n  = new Instruction[l_l + p_c];
    System.arraycopy(p_source, 0, l_n, 0, p_p);
    System.arraycopy(p_source, p_p, l_n, p_p+p_c, l_l-p_p);
    
    if(p_p < l_l)
      {
      for(l_i = (l_n.length-1); l_i >= 0; l_i--)
        {
        if((l_i < p_p) || (l_i >= (p_p + p_c)))
          {
          l_ins = l_n[l_i];
  
          if(l_ins instanceof Goto)
            {
            l_ij = ((Goto)l_ins);
            l_t  = l_ij.get_target();
            if(l_t >= p_p)
              {
              l_n[l_i] = Goto.create(l_t+p_c, l_ij.is_unconditional());
              }
            }
          }
        }
      }
    
    return l_n;
    }
  
  


/**
 * Delete some instructions from a procedure.
 * @param p_source  The original code.
 * @param p_p       The deletion position.
 * @param p_c       The count of instructions to delete.
 * @param p_l       The valid length of the instruction array.
 * @param p_dest    The destination procedure.
 */

  public static final void  delete_instr(final Instruction[] p_source,
                                         final int           p_p,
                                         final int           p_c,
                                               int           p_l,
                                         final Instruction[] p_dest)
    {
    int           l_i, l_t;
    Instruction   l_ins;
    Goto          l_ij;
    
    p_l -= p_c;
    
    if(p_dest != p_source) System.arraycopy(p_source, 0, p_dest, 0, p_p);
    System.arraycopy(p_source, p_p+p_c, p_dest, p_p, p_l - p_p);
    
    for(l_i = (p_l-1); l_i >= 0; l_i--)
      {
      l_ins = p_dest[l_i];

      if(l_ins instanceof Goto)
        {
        l_ij = ((Goto)l_ins);
        l_t  = l_ij.get_target();
        if(l_t >= p_p)
          {
          if(l_t < (p_p+p_c))
            {
            l_t = p_p;
            if(l_t >= p_l) l_t = 0;
            }
          else
            {
            l_t -= p_c;
            }

          p_dest[l_i] = Goto.create(l_t, l_ij.is_unconditional());
          }
        }
      }
    }
  

/**
 * Delete some instructions from a procedure.
 * @param p_source  The original code.
 * @param p_p       The deletion position.
 * @param p_c       The count of instructions to delete.
 * @return  The new procedure, with <code>null</code>s where the
 *          deletion has been performed.
 */

  public static final Instruction[] delete_instr(final Instruction[] p_source,
                                                 final int           p_p,
                                                 final int           p_c)
    {
    Instruction[] l_n;
    int           l_l;
        
    if(p_c <= 0) return p_source;
    
    l_l = p_source.length;
    l_n = new Instruction[l_l - p_c];
    if(l_n.length <= 0)
      {
      return p_source;
      }
    delete_instr(p_source, p_p, p_c, l_l, l_n);
    
    return l_n;
    }
  
  
  


/**
 * Create space for an insertion into an procedure array.
 * @param p_source  The original code.
 * @param p_p       The insertion position.
 * @param p_c       The insert count.
 * @return  The new code, with <code>null</code>s where the
 *          insertion space is created.
 */

  public static final Instruction[][] insert_proc(
                                           final Instruction[][] p_source,
                                           final int             p_p,
                                           final int             p_c)
    {
    int             l_l, l_i, l_j, l_t;
    Instruction[][] l_n;
    Instruction[]   l_v;
    Instruction     l_ins;
    Call            l_ij;
    
    if(p_c <= 0) return p_source;
    
    l_l  = p_source.length;
    l_n  = new Instruction[l_l + p_c][];
    System.arraycopy(p_source, 0, l_n, 0, p_p);
    System.arraycopy(p_source, p_p, l_n, p_p+p_c, l_l-p_p);
        
    if(p_p < l_l)
      {
      for(l_i = (l_n.length-1); l_i >= 0; l_i--)
        {
        if((l_i < p_p) || (l_i >= (p_p + p_c)))
          {
          l_v = l_n[l_i];
          for(l_j = (l_v.length-1); l_j >= 0; l_j--)
            {
            l_ins = l_v[l_j];

            if(l_ins instanceof Call)
              {
              l_ij = ((Call)l_ins);
              l_t  = l_ij.get_index();
              if(l_t >= p_p)
                {
                if(l_v == l_n[l_i]) l_v = l_v.clone();                    
                l_v[l_j] = Call.create(l_t+p_c, l_ij.is_unconditional());
                }
              }
            }
          l_n[l_i] = l_v;
          }
        }
      }
    
    return l_n;
    }
  
  

/**
 * Delete some procedures from a program.
 * @param p_source  The original code.
 * @param p_p       The deletion position.
 * @param p_c       The count of procedres to delete.
 * @param p_l       The original valid length of the code.
 * @param p_dest    The program set, with <code>null</code>s where the
 *                  deletion has been performed.
 */

  public static final void  delete_proc(final Instruction[][] p_source,
                                        final int             p_p,
                                        final int             p_c,
                                              int             p_l,
                                        final Instruction[][] p_dest)
    {
    Instruction[]   l_v, l_v2;
    int             l_i, l_j, l_t, l_vl;
    Instruction     l_ins;
    Call            l_ij;    
    boolean         l_b;
    
    p_l -= p_c;
    
    if(p_dest != p_source) System.arraycopy(p_source, 0, p_dest, 0, p_p);
    System.arraycopy(p_source, p_p+p_c, p_dest, p_p, p_l - p_p);
        
    for(l_i = (p_l-1); l_i >= 0; l_i--)
      {
      l_v  = p_dest[l_i];
      l_vl = l_v.length;
      l_v2 = l_v;
      l_b  = true;
      
      for(l_j = (l_v.length-1); l_j >= 0; l_j--)
        {
        l_ins = l_v[l_j];

        if(l_ins instanceof Call)
          {
          l_ij = ((Call)l_ins);
          l_t  = l_ij.get_index();
          if(l_t >= p_p)
            {
            if(l_t < (p_p+p_c))
              {              
              l_vl--;
                        
              if(l_b)
                {
                l_v = new Instruction[l_vl];
                l_b = false;
                }
              
              CodeEditor.delete_instr(l_v2, l_j, 1, l_vl+1, l_v);
              l_v2 = l_v;
                
              }
            else
              {
              l_t -= p_c;
              if(l_b)
                {
                l_v  = l_v.clone();
                l_v2 = l_v;
                l_b  = false;
                }
              l_v[l_j] = Call.create(l_t, l_ij.is_unconditional());              
              }
            }            
          }
        }
      
      if(l_vl != l_v.length)
        {
        l_v2 = new Instruction[l_vl];
        System.arraycopy(l_v, 0, l_v2, 0, l_vl);
        l_v = l_v2;
        }
      p_dest[l_i] = l_v;
      }
  
    }
  
/**
 * Delete some procedures from a program.
 * @param p_source  The original code.
 * @param p_p       The deletion position.
 * @param p_c       The count of procedres to delete.
 * @return  The program set, with <code>null</code>s where the
 *          deletion has been performed.
 */

  public static final Instruction[][] delete_proc(
                                       final Instruction[][] p_source,
                                       final int           p_p,
                                       final int           p_c)
    {    
    Instruction[][] l_n;
    int             l_l;
    
    if(p_c <= 0) return p_source;
    
    l_l = p_source.length;    
    l_n = new Instruction[l_l - p_c][];
    
    delete_proc(p_source, p_p, p_c, l_l, l_n);
    
    return l_n;
    }
  
/**
 * Create a new randomized procedure.
 * @param p_context The vm context to be used.
 * @param p_info    The information record holding the infos on the
 *                  instruction to be created.
 * @return  The new procedure.
 */

  public static final Instruction[] create_proc(final VMContext    p_context,
                                                final MutationInfo p_info)
    {
          int             l_sc;
          Instruction[]   l_ins;
    final Randomizer      l_random;
    final InstructionSet  l_is;

    l_random = p_context.get_randomizer();
    l_sc     = Math.max(1, (int)(l_random.normal(5.0d, 1.0d)));
    l_sc     = Math.min((((int)(l_random.exponential(l_sc)+1))), 500);
    l_ins    = new Instruction[l_sc];
    l_is     = p_context.get_instruction_set();
    
    p_info.m_cur_proc_len      = l_sc;
    p_info.m_highest_proc_addr = p_info.m_highest_addr;
    p_info.m_lowest_proc_addr  = 0;
    
    for(--l_sc; l_sc >= 0; l_sc--)
      {
      p_info.m_instr_index = l_sc;
      p_info.assign_instr(l_ins[l_sc] = l_is.create(p_context, p_info));
      }
    
    return l_ins;
    }
  }

File Information:

file name:CodeEditor.java
package:org.dgpf.gp.vm.mutation
qualified name:org.dgpf.gp.vm.mutation.CodeEditor.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/mutation/CodeEditor.java
size:11.358 KB (11631 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-08-22 07:01:29 GMT+0000
last access: 2017-11-18 06:38: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 06:38:10 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo