Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.vm.mutation.ProgramMutator.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-03 15:04:42
 * Original Filename: org.dgpf.gp.vm.mutation.ProgramMutator.java
 * Version          : 1.0.0
 * Last modification: 2006-07-03
 *                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.VMContext;
import org.dgpf.gp.vm.instructions.ctrl.Call;
import org.dgpf.search.api.utils.IMutator;
import org.dgpf.search.api.utils.WeightedMutator;
import org.dgpf.search.api.utils.WeightedSetBuilder;
import org.sfc.math.stochastic.Randomizer;

/**
 * This class comes with the ability to mutate and create programs.
 *
 * @author Thomas Weise
 */

public final class ProgramMutator extends WeightedMutator<Instruction[][],
                                                VMContext,
                                                MutationInfo>
  {
/**
 * The serial version uid.
 */

  private static final long serialVersionUID = 1;
  
/**
 * This internal mutator mutates a single instruction of a procedure.
 */

  private static  final IMutator<Instruction[][], VMContext, MutationInfo>
    MUTATOR = new IMutator<Instruction[][], VMContext, MutationInfo>()
      {
      public final Instruction[][] mutate(      Instruction[][] p_source,
                                          final VMContext       p_context,
                                          final MutationInfo    p_info)
        {
        Randomizer     l_r;
        int            l_d, l_l, l_k;
        Instruction[]  l_i, l_j;
        boolean[]      l_b;

        l_l = p_source.length;
        l_r = p_context.get_randomizer();
        
        l_d = l_r.nextInt(l_l);
        l_j = p_source[l_d];
        
        if(l_j.length > 0)
          {
          p_info.m_instr_index = l_d;
          l_i = ProcMutator.INSTANCE.mutate(l_j, p_context, p_info);
          }
        else
          {
          l_i = l_j;
          }
        

        if((l_i == l_j) || (l_i == null))
          {
          if(l_l <= 1) return p_source;
main: 
            {
            l_b      = new boolean[l_l];
            l_b[l_d] = true;
            for(l_k = l_l; l_k > 0; l_k--)
              {
              do
                {
                l_d = l_r.nextInt(l_l);              
                } while(l_b[l_d]);
              
              l_b[l_d] = true;
              l_j      = p_source[l_d];
              if(l_j.length > 0)
                {
                p_info.m_instr_index = l_d;
                l_i = ProcMutator.INSTANCE.mutate(l_j, p_context, p_info);
                }
              else
                {
                l_i = l_j;
                }
              if((l_i != l_j) && (l_i != null)) break main;
              }
            
        
            return p_source;
            }
          }
        
        p_source      = p_source.clone();
        p_source[l_d] = l_i;
        return p_source;
        }
      };
      

/**
 * This internal mutator deletes some instructions of a procedure.
 */

  private static  final IMutator<Instruction[][], VMContext, MutationInfo>
    DELETE = new IMutator<Instruction[][], VMContext, MutationInfo>()
      {
      public  final  Instruction[][] mutate(       Instruction[][] p_source,
                                             final VMContext       p_context,
                                             final MutationInfo    p_info)
        {
        int             l_l, l_j,l_c;
        Randomizer      l_r;
          
        l_l = p_source.length;
        l_j = p_context.get_min_proc_count();
        if(l_l <= l_j) return p_source;
  
        l_r = p_context.get_randomizer();
        l_c = (l_r.nextBoolean() ? 1 : (l_r.nextInt(l_l-l_j) + 1));
        
        return CodeEditor.delete_proc(p_source, l_r.nextInt(l_l - l_c), l_c);
        }
      };
  
/**
 * This internal mutator inserts some instructions into a procedure.
 */

  private static  final IMutator<Instruction[][], VMContext, MutationInfo>
    INSERT = new IMutator<Instruction[][], VMContext, MutationInfo>()
      {
      public final Instruction[][] mutate  (       Instruction[][] p_source,
                                             final VMContext       p_context,
                                             final MutationInfo    p_info)
        {
        Instruction[][] l_n;
        Instruction[]   l_v;
        int             l_p, l_l, l_i, l_t, l_c, l_j;
        Randomizer      l_r;
              
        l_l  = p_source.length;
        l_r  = p_context.get_randomizer();
        l_p  = l_r.nextInt(l_l+1);
        l_c  = Math.min(1 + ((int)(l_r.exponential())), 100);
        
        l_n  = CodeEditor.insert_proc(p_source, l_p, l_c);
        p_info.m_proc_count = l_n.length;
  
        for(l_i = (l_p + l_c - 1); l_i >= l_p; l_i--)
          {
          p_info.m_cur_proc = l_i;
          l_n[l_i]          = CodeEditor.create_proc(p_context, p_info);
          
          do
            {
            l_t = l_r.nextInt(l_n.length);
            } while((l_t >= l_p) && (l_t < (l_p + l_c)));
                 
          
          l_v      = l_n[l_t];
          if(l_v.length > 0)
            {
            l_v = CodeEditor.insert_instr(
                            l_v, l_j = l_r.nextInt(l_v.length), 1);
            }
          else
            {
            l_v = new Instruction[1];
            l_j = 0;
            }
          l_v[l_j] = Call.create(l_i, l_r.nextBoolean());
          l_n[l_t] = l_v;
          }
  
        return l_n;        
        }
      };
      
      
/**
 * This internal mutator compresses the memory addressing.
 */

  private static  final IMutator<Instruction[][], VMContext, MutationInfo>
    COMPRESS = new IMutator<Instruction[][], VMContext, MutationInfo>()
      {
      public  final Instruction[][] mutate(      Instruction[][]  p_source,
                                             final VMContext      p_context,
                                             final MutationInfo   p_info)
        {
        Randomizer      l_r;
        Instruction[][] l_x;
        int             l_i, l_p, l_pp, l_h, l_l, l_j;
        Instruction     l_q, l_o;
        Instruction[]   l_c;
        
        l_r = p_context.get_randomizer();
        l_h = p_info.m_highest_addr;
        l_l = p_info.m_lowest_addr;
        
        if(l_r.nextBoolean() && (l_h > 0))
          {
          l_pp = l_h;
          l_p  = (l_l + l_r.nextInt(l_h - l_l));
          }
        else
          {
          if(l_l >= -1) return p_source;
          l_pp = l_l;
          l_p  = (l_h - l_r.nextInt(l_h - l_l));
          }
        
        l_x = p_source;
        for(l_i = (l_x.length-1); l_i >= 0; l_i--)
          {
          l_c = l_x[l_i];
          
          for(l_j = (l_c.length-1); l_j >= 0; l_j--)
            {
            l_o = l_c[l_j];
            l_q = l_o.get_handler().swap_address(l_o, l_pp, l_p);
            if(l_q != l_o)
              {
              if(l_c == l_x[l_i]) l_c = l_c.clone();
              l_c[l_j] = l_q;
              }
            }
          
          if(l_c != l_x[l_i])
            {
            if(l_x == p_source) l_x = p_source.clone();
            l_x[l_i] = l_c;
            }
          }
        
        return l_x;
        }
      };
      

/**
 * The shared singleton instance of this mutator.
 */

  private static  final IMutator<Instruction[][], VMContext, MutationInfo>
                 INSTANCE = new ProgramMutator();
      
/**
 * The internal hidden constructor, use the package private shared
 * singleton instead.
 * @see #INSTANCE
 */

  private ProgramMutator()
    {
    super(build());
    }
  
/**
 * Build the weighted set internally.
 * @return The weighted set to be used internally.
 */
  
  private static  final WeightedSetBuilder<IMutator
                       <Instruction[][], VMContext, MutationInfo>> build()
    {
    WeightedSetBuilder<IMutator<Instruction[][], VMContext, MutationInfo>> l_m;
      
    l_m = new WeightedSetBuilder<IMutator
          <Instruction[][], VMContext, MutationInfo>>(IMutator.class);
    
    l_m.add(MUTATOR, 70.0d);
    l_m.add(DELETE,   3.0d);
    l_m.add(INSERT,   3.0d);
    l_m.add(COMPRESS, 1.0d);
    
    return l_m;
    }
  

/**
 * Create a new randomized program.
 * @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 program.
 */

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

    l_random = p_context.get_randomizer();    
    l_sc     = Math.max(p_context.get_min_proc_count(),
               Math.min((((int)(l_random.exponential()))+
                               p_context.get_min_proc_count()), 500));
    l_ins    = new Instruction[l_sc][];
    
    p_info.m_proc_count        = l_sc;
    p_info.m_highest_proc_addr = 0;
    p_info.m_lowest_proc_addr  = 0;
    
    while(l_sc > 0)
      {
      p_info.m_cur_proc = (--l_sc);
      l_ins[l_sc]       = CodeEditor.create_proc(p_context, p_info);
      }
    
    return l_ins;
    }
  
/**
 * Mutate an existing program.
 * @param p_code    The program code to be mutated.
 * @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  A mutated copy of the program or the program itself if no
 *          copy could be created.
 */

  public static final Instruction[][] mutate(
                                           final VMContext       p_context,
                                           final MutationInfo    p_info,
                                                 Instruction[][] p_code)  
    {    
    if(p_code.length <= 0) return create(p_context, p_info);
    
    int l_i;
    
    for(l_i = Math.max(1,((int)(p_context.get_randomizer().exponential())));
        l_i > 0; l_i--)
      {
      p_info.assign_code(p_code);
      p_code = INSTANCE.mutate(p_code, p_context, p_info);
      }
    
    return p_code;
    }
  }

File Information:

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