Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.vm.mutation.ProcMutator.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 11:41:07
 * Original Filename: org.dgpf.gp.vm.mutation.ProcMutator.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.InstructionSet;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.arith.CompareHandler;
import org.dgpf.gp.vm.instructions.ctrl.Call;
import org.dgpf.gp.vm.instructions.ctrl.CallHandler;
import org.dgpf.gp.vm.instructions.ctrl.Goto;
import org.dgpf.gp.vm.instructions.ctrl.GotoHandler;
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;
import org.sfc.utils.Typesafe;

/**
 * The internal mutator used for whole procedures.
 *
 * @author Thomas Weise
 */

final class ProcMutator 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);
        p_info.m_instr_index = l_d;
        l_j = p_source[l_d];
        l_i = l_j.get_handler().mutate(l_j, p_context, p_info);
        

        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_d = l_r.nextInt(l_l);
              p_info.m_instr_index = l_d;
              l_j = p_source[l_d];
              l_i = l_j.get_handler().mutate(l_j, p_context, p_info);
              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_c;
        Randomizer    l_r;
  
        l_l = p_source.length;
        if(l_l <= 1) return p_source;
  
        l_r = p_context.get_randomizer();
        l_c = (l_r.nextBoolean() ? 1 : (
               Math.min(l_l-1, Math.max(1, (int)(l_r.exponential())))));
        
        return CodeEditor.delete_instr(p_source, l_r.nextInt(l_l-l_c), l_c);
        }
      };
  
/**
 * This internal mutator replaces some instructions of a procedure.
 */

  private static  final IMutator<Instruction[], VMContext, MutationInfo>
    REPLACE = new IMutator<Instruction[], VMContext, MutationInfo>()
      {
      public  final  Instruction[]  mutate  (      Instruction[]  p_source,
                                             final VMContext      p_context,
                                             final MutationInfo   p_info)
        {
        int             l_l, l_c, l_p;
        Randomizer      l_r;
        InstructionSet  l_x;
        
        
        l_l = p_source.length;
        if(l_l < 1) return p_source;
  
        l_r = p_context.get_randomizer();
        l_c = (l_r.nextBoolean() ? 1 : (
               Math.min(l_l, Math.max(1, (int)(l_r.exponential())))));
        l_p = ((l_c < l_l) ? l_r.nextInt(l_l-l_c) : 0);
        p_source = p_source.clone();
        
        l_x = p_context.get_instruction_set();
        
        for(; l_c > 0; l_c--, l_p++)
          {
          p_source[l_p] = l_x.create(p_context, p_info);
          }
        
        return p_source;
        }
      };
      
/**
 * 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;
        int             l_p, l_l, l_i, l_c;
        Randomizer      l_r;
        InstructionSet  l_is;
        
        l_is = p_context.get_instruction_set();          
        l_l  = p_source.length;
        l_r  = p_context.get_randomizer();
        l_p  = l_r.nextInt(l_l+1);
        l_c  = (1 + ((int)(l_r.exponential())));
        l_n  = CodeEditor.insert_instr(p_source, l_p, l_c);  
        p_info.m_cur_proc_len = l_n.length;
  
        for(l_i = (l_p + l_c - 1); l_i >= l_p; l_i--)
          {
          p_info.m_instr_index = l_i;
          p_info.assign_instr(l_n[l_i] = l_is.create(p_context, p_info));
          }
  
        return l_n;        
        }
      };
      
      
/**
 * This internal mutator inserts a conditional jump instruction.
 */

  private static  final IMutator<Instruction[], VMContext, MutationInfo>
    INSERT_JUMP = new IMutator<Instruction[], VMContext, MutationInfo>()
      {
      public  final  Instruction[]  mutate  (      Instruction[]  p_source,
                                             final VMContext      p_context,
                                             final MutationInfo   p_info)
        {
        Instruction[]   l_n;
        int             l_p, l_l, l_c;
        Randomizer      l_r;
        InstructionSet  l_is;
        Goto            l_g;
                
        l_is = p_context.get_instruction_set();
        if(!(l_is.contains(GotoHandler.INSTANCE) &&
             l_is.contains(CompareHandler.INSTANCE))) return p_source;
        
        l_l  = p_source.length;
        l_r  = p_context.get_randomizer();
        l_p  = l_r.nextInt(l_l+1);
        l_c  = 2;
        l_n  = CodeEditor.insert_instr(p_source, l_p, l_c);  
        p_info.m_cur_proc_len = l_n.length;
          
        p_info.m_instr_index = l_p;
        p_info.assign_instr(l_n[l_p] = CompareHandler.INSTANCE
                                           .create(p_context, p_info));
        
        p_info.m_instr_index = l_p+1;
        
        do
          {
          l_g = Typesafe.cast(GotoHandler.INSTANCE.create(p_context, p_info));
          } while(l_g.is_unconditional());
        
        p_info.assign_instr(l_n[l_p+1] = l_g);
  
        return l_n;        
        }
      };
      
/**
 * This internal mutator inserts a conditional jump instruction.
 */

  private static  final IMutator<Instruction[], VMContext, MutationInfo>
    INSERT_CALL = new IMutator<Instruction[], VMContext, MutationInfo>()
      {
      public  final  Instruction[]  mutate  (      Instruction[]  p_source,
                                             final VMContext      p_context,
                                             final MutationInfo   p_info)
        {
        Instruction[]   l_n;
        int             l_p, l_l, l_c;
        Randomizer      l_r;
        InstructionSet  l_is;
        Call            l_g;
                
        l_is = p_context.get_instruction_set();
        if(!(l_is.contains(CallHandler.INSTANCE) &&
             l_is.contains(CompareHandler.INSTANCE))) return p_source;
        
        l_l  = p_source.length;
        l_r  = p_context.get_randomizer();
        l_p  = l_r.nextInt(l_l+1);
        l_c  = 2;
        l_n  = CodeEditor.insert_instr(p_source, l_p, l_c);  
        p_info.m_cur_proc_len = l_n.length;
          
        p_info.m_instr_index = l_p;
        p_info.assign_instr(l_n[l_p] = CompareHandler.INSTANCE
                                           .create(p_context, p_info));
        
        p_info.m_instr_index = l_p+1;
        
        do
          {
          l_g = Typesafe.cast(CallHandler.INSTANCE.create(p_context, p_info));
          } while(l_g.is_unconditional());
        
        p_info.assign_instr(l_n[l_p+1] = l_g);
  
        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;
        Instruction   l_q, l_o;
        
        l_r = p_context.get_randomizer();
        l_h = p_info.m_highest_proc_addr;
        l_l = p_info.m_lowest_proc_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_o = l_x[l_i];
          l_q = l_o.get_handler().swap_address(l_o, l_pp, l_p);
          if(l_q != l_o)
            {
            if(l_x == p_source) l_x = p_source.clone();
            l_x[l_i] = l_q;
            }
          }
        
        return l_x;
        }
      };
      

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

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

  private ProcMutator()
    {
    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,       1d);
    l_m.add(DELETE,        3d);
    l_m.add(INSERT,        2d);
    l_m.add(INSERT_JUMP,   5d);
    l_m.add(INSERT_CALL,   2.25d);
    l_m.add(COMPRESS,      1d);
    l_m.add(REPLACE,       3d);
   
    return l_m;
    }

  }

File Information:

file name:ProcMutator.java
package:org.dgpf.gp.vm.mutation
qualified name:org.dgpf.gp.vm.mutation.ProcMutator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/mutation/ProcMutator.java
size:12.577 KB (12879 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-08-22 07:17:42 GMT+0000
last access: 2017-11-22 09:23:49 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-22 09:23:49 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo