Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.vm.instructions.base.MemInstr2Handler.java

Here you can find all the information about the file org.dgpf.gp.vm.instructions.base.MemInstr2Handler.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 04:10:21
 * Original Filename: org.dgpf.gp.vm.instructions.base.MemInstr2Handler.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.instructions.base;

import org.dgpf.gp.vm.base.IInstructionHandler;
import org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.mutation.MutationInfo;
import org.dgpf.search.api.utils.IMutator;
import org.dgpf.search.api.utils.WeightedSet;
import org.dgpf.search.api.utils.WeightedSetBuilder;
import org.sfc.math.stochastic.Randomizer;


/**
 * The base class for memory 1 instruction handlers.
 *
 * @author Thomas Weise
 */

public abstract class       MemInstr2Handler 
                extends     MemInstr1Handler
  {
///**
// * The first internal mutator.
// */

//  private static  final IMutator<Instruction, VMContext, MutationInfo> M21 =
//    new IMutator<Instruction, VMContext, MutationInfo>()
//      {
//      public  final  Instruction  mutate  (final Instruction  p_source,
//                                           final VMContext    p_context,
//                                           final MutationInfo p_info)
//        {
//        MemInstr2 l_i;
//        
//        if(p_context.can_relative())
//          {
//          l_i = ((MemInstr2)(p_source.clone()));
//          l_i.m_relative_2 = !(l_i.m_relative_2);
//          return l_i;
//          }
//        return p_source;
//        }
//      };
//      
//
///**
// * The second internal mutator.
// */

//  private static  final IMutator<Instruction, VMContext, MutationInfo> M22 =
//    new IMutator<Instruction, VMContext, MutationInfo>()
//      {
//      public  final  Instruction  mutate  (final Instruction  p_source,
//                                           final VMContext    p_context,
//                                           final MutationInfo p_info)
//        {
//        MemInstr2 l_i;
//        
//        l_i = ((MemInstr2)(p_source.clone()));
//        l_i.m_addr_2 = p_context.mutate_address(l_i.m_addr_2, p_info);
//        return l_i;
//        }
//      };
//  


/**
 * The third internal mutator.
 */

  private static  final IMutator<Instruction, VMContext, MutationInfo> M23 =
    new IMutator<Instruction, VMContext, MutationInfo>()
      {
      public  final  Instruction  mutate  (final Instruction  p_source,
                                           final VMContext    p_context,
                                           final MutationInfo p_info)
        {
        MemInstr2                                 l_i, l_i2;
        IInstructionHandler                       l_ii, l_io;
        WeightedSet<IInstructionHandler>.Session  l_s;
        
        l_i  = ((MemInstr2)(p_source));
        l_io = l_i.get_handler();
        l_s  = p_context.get_instruction_set().open_session(
                                               p_context.get_randomizer());
        
        while(l_s.hasNext())
          {
          l_ii = l_s.next();
          if((l_ii != l_io) && (l_ii instanceof MemInstr2Handler))
            {
            l_i2 = ((MemInstr2)(l_ii.create(p_context, p_info)));
            l_i2.m_addr_1     = l_i.m_addr_1;
            l_i2.m_relative_1 = l_i.m_relative_1;
            l_i2.m_addr_2     = l_i.m_addr_2;
            l_i2.m_relative_2 = l_i.m_relative_2;
            l_s.close();
            return l_i2;
            }
          }
        
        l_s.close();
        return l_i;
        }
      };
      
      
/**
 * The default weighted set builder.
 */

  static  final WeightedSetBuilder<IMutator
                             <Instruction, VMContext, MutationInfo>>
    MI2B = new WeightedSetBuilder<IMutator
                   <Instruction, VMContext, MutationInfo>>(IMutator.class);
  
  static
    {
    MI2B.add(MI1B);
//    MI2B.add(M21, M1_WEIGHT);    
//    MI2B.add(M22, M2_WEIGHT);    
    MI2B.add(M23, M3_WEIGHT);
    }
  
/**
 * Create a new mem instr 1 handler. Such handlers use internal mutator
 * lists.
 * @param p_wsb The weighted set builder to use. Leave this
 *              <code>null</code> if no additional mutators are needed.
 */

  protected MemInstr2Handler(final WeightedSetBuilder<IMutator
                             <Instruction, VMContext, MutationInfo>> p_wsb)
    {
    super(build_set(p_wsb));
    }
  
/**
 * The internal set conversion routine.
 * @param p_set The normal mutator set.
 * @return  The modified mutator set.
 */

  private static final WeightedSetBuilder<IMutator
                       <Instruction, VMContext, MutationInfo>> build_set
                       (final WeightedSetBuilder<IMutator
                        <Instruction, VMContext, MutationInfo>> p_set)
    {
    if(p_set == null) return MI2B;
    
//    if(!(p_set.contains(M21))) p_set.add(M21, M1_WEIGHT);    
//    if(!(p_set.contains(M22))) p_set.add(M22, M2_WEIGHT);    
    if(!(p_set.contains(M23))) p_set.add(M23, M3_WEIGHT);
    
    return p_set;
    }
              

/**
 * Replace a memory access address by another one.
 * @param p_instruction The instruction to replace the address.
 * @param p_old         The old address.
 * @param p_new         The new address.
 * @return  The new instruction where the address relacement has been
 *          performed, or the old one if no replacement was needed.  
 */

  @Override
  public Instruction swap_address(final Instruction p_instruction,
                                  final int         p_old,
                                  final int         p_new)
    {
    MemInstr2 l_m2;
    
    l_m2 = ((MemInstr2)(super.swap_address(p_instruction, p_old, p_new)));
    if(l_m2.m_addr_2 == p_old)
      {
      if(p_instruction == l_m2) l_m2 = ((MemInstr2)(l_m2.clone()));
      l_m2.m_addr_2 = p_new;
      return l_m2;
      }
    
    return p_instruction;
    }
  
/**
 * An address helper object.
 *
 * @author Thomas Weise
 */

  protected static  final class Addresses
    {
/**
 * The address data.
 */

    public  final int[]       m_addr  ;
/**
 * The relative data.
 */

    public  final boolean[]   m_rel  ;
    
/**
 * Create a new set of addresses.
 * @param p_count   The count of addresses required.
 * @param p_c       The VM-context to be used.
 * @param p_info    The mutation info.
 */

    public  Addresses (final int          p_count,
                       final VMContext    p_c,
                       final MutationInfo p_info)
      {
      super();
           
      this.m_addr = new int[p_count];
      this.m_rel  = new boolean[p_count];
      this.compute(p_c, p_info);
      }
    
/**
 * Compute the values of this address set.
 * @param p_c     The context.
 * @param p_info  The info.
 */

    public  final void  compute(final VMContext    p_c,
                                final MutationInfo p_info)
      {
      int[]       l_a;
      boolean[]   l_r;
      int         l_i, l_j, l_z, l_count;
      boolean     l_b, l_t;
      Randomizer  l_x;
      
      l_r     = this.m_rel;
      l_a     = this.m_addr;
      l_count = l_a.length;
      l_x     = p_c.get_randomizer();
      l_z     = (Math.min(l_count, 2));
      l_t     = ((p_c.get_max_mem_size()<<1) > l_z);
                    
main:
      for(l_i = 0; l_i < l_z; l_i++)
        {
        l_a[l_i] = p_c.random_address(p_info);
        l_r[l_i] = p_c.create_relative();
        
        for(l_j = (l_i-1); l_j >= 0; l_j--)
          {
          if((l_a[l_i] == l_a[l_j]) && (l_r[l_i] == l_r[l_j]) &&
             (l_x.nextInt(10) > 0) && l_t && l_x.nextBoolean())
            {
            l_i--;            
            continue main;
            }
          }
        }
     
      for(; l_z < l_count; l_z++)
        {
        l_i = l_x.nextInt(l_z);
        l_r[l_z] = l_r[l_i];
        l_a[l_z] = l_a[l_i];
        }                    
      
    
      for(l_i = (l_count-1); l_i >= 0; l_i--)
        {
        l_j = l_x.nextInt(l_count);
        
        l_z      = l_a[l_j];
        l_a[l_j] = l_a[l_i];
        l_a[l_i] = l_z;
        
        l_b      = l_r[l_j];
        l_r[l_j] = l_r[l_i];
        l_r[l_i] = l_b;
        }
      }
    }
  }

File Information:

file name:MemInstr2Handler.java
package:org.dgpf.gp.vm.instructions.base
qualified name:org.dgpf.gp.vm.instructions.base.MemInstr2Handler.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/instructions/base/MemInstr2Handler.java
size:9.132 KB (9352 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-07-28 08:10:29 GMT+0000
last access: 2017-11-22 07:31:26 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-23 05:36:56 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo