Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.automaton.search.ProgramMutator.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-29 13:40:22
 * Original Filename: org.dgpf.automaton.genetics.ProgramMutator.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.instructions.IfJump;
import org.sfc.collections.Arrays;
import org.sfc.math.stochastic.Randomizer;

/**
 * With this mutator, a sequence of code can be mutated. Sequences of code
 * represent programs.
 *
 * @author Thomas Weise
 */

final class   ProgramMutator
      extends MutationDispatcher<Instruction[], Instruction[]>
  {
/**
 * Delete some instructions.
 */

  private static  final Mutator<Instruction[], Instruction[]> MUTATE_DELETE =
                    new Mutator<Instruction[], Instruction[]>()
    {
/**
 * Mutate an existing instance of the genotype.
 * @param p_source        The source instance.
 * @param p_context       The (calling) context to be used.
 * @param p_info          Reserved for future purposes.
 * @param p_memory_size   The memory size currently available.
 * @return The randomly mutated copy of the source or <code>p_source</code>
 *         itself if it cannot be mutated in any way.
 */

    @Override
    protected Instruction[] mutate(final  Instruction[]     p_source,
                                   final  ProgramContext    p_context,
                                   final  int               p_info,
                                   final  int               p_memory_size)
      {
      Instruction[] l_n;
      int           l_p, l_l, l_i, l_t, l_c;
      Instruction   l_ins;
      IfJump        l_ij;
      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.nextInt(l_l-1) + 1);
      l_p = l_r.nextInt(l_l - l_c);
      l_n = Arrays.delete_direct(p_source, l_p, l_c);
      l_l-=l_c;

      for(l_i = (l_l-1); l_i >= 0; l_i--)
        {
        l_ins = l_n[l_i];

        if(l_ins instanceof IfJump)
          {
          l_ij = ((IfJump)l_ins);
          l_t  = l_ij.get_target();
          if(l_t >= l_p)
            {
            if(l_t < (l_p+l_c))
              {
              l_t = l_p;
              if(l_t >= l_l) l_t = 0;
              }
            else
              {
              l_t -= l_c;
              }

            l_n[l_i] = new IfJump(l_ij.get_condition(), l_t);
            }
          }
        }

      return l_n;
      }
    };


/**
 * Insert some instructions.
 */

  private static  final Mutator<Instruction[], Instruction[]> MUTATE_INSERT =
                    new Mutator<Instruction[], Instruction[]>()
    {
/**
 * Mutate an existing instance of the genotype.
 * @param p_source        The source instance.
 * @param p_context       The (calling) context to be used.
 * @param p_info          Reserved for future purposes.
 * @param p_memory_size   The memory size currently available.
 * @return The randomly mutated copy of the source or <code>p_source</code>
 *         itself if it cannot be mutated in any way.
 */

    @Override
    protected Instruction[] mutate(final  Instruction[]     p_source,
                                   final  ProgramContext    p_context,
                                   final  int               p_info,
                                   final  int               p_memory_size)
      {
      Instruction[] l_n;
      int           l_p, l_l, l_i, l_t, l_c;
      Instruction   l_ins;
      IfJump        l_ij;
      Randomizer    l_r;

      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 = Arrays.insert_space_direct(p_source, l_p, l_c);

      if(l_p < l_l)
        {
        for(l_i = l_l; l_i >= 0; l_i--)
          {
          if((l_i < l_p) || (l_i >= (l_p + l_c)))
            {
            l_ins = l_n[l_i];

            if(l_ins instanceof IfJump)
              {
              l_ij = ((IfJump)l_ins);
              l_t  = l_ij.get_target();
              if((l_t > l_p) || ((l_t == l_p) && l_r.nextBoolean()))
                {
                l_n[l_i] = new IfJump(l_ij.get_condition(), l_t+l_c);
                }
              }
            }
          }
        }

      for(l_i = (l_p + l_c - 1); l_i >= l_p; l_i--)
        {
        l_n[l_i] = p_context.create_instruction(l_n.length,
                                                p_memory_size);
        }

      return l_n;
      }
    };


/**
 * Mutate an instruction.
 */

  private static  final Mutator<Instruction[], Instruction[]> MUTATE_MUTATE =
                    new Mutator<Instruction[], Instruction[]>()
    {
/**
 * Mutate an existing instance of the genotype.
 * @param p_source        The source instance.
 * @param p_context       The (calling) context to be used.
 * @param p_info          Reserved for future purposes.
 * @param p_memory_size   The memory size currently available.
 * @return The randomly mutated copy of the source or <code>p_source</code>
 *         itself if it cannot be mutated in any way.
 */

    @Override
    protected Instruction[] mutate(final  Instruction[]     p_source,
                                   final  ProgramContext    p_context,
                                   final  int               p_info,
                                   final  int               p_memory_size)
      {
      int           l_i, l_l, l_k;
      Instruction[] l_n;
      Instruction   l_x;
      boolean[]     l_b;
      Randomizer    l_r;

      l_r       = p_context.get_randomizer();
      l_l       = p_source.length;
      l_i       = l_r.nextInt(l_l);
      l_n       = p_source.clone();
      l_x       = p_context.mutate_instruction(p_source[l_i], 
                                                p_source.length,
                                                p_memory_size);

      if((l_x != null) && (l_x != p_source[l_i]))
        {
        l_n[l_i] = l_x;
        return l_n;
        }
      if(l_l <= 1) return p_source;

      l_b      = new boolean[l_l];
      l_b[l_i] = true;

      for(l_k = l_l; l_k > 1; l_k--)
        {
        do
          {
          l_i = l_r.nextInt(l_l);
          } while(l_b[l_i]);

        l_x = p_context.mutate_instruction(p_source[l_i],
                                            p_source.length,
                                            p_memory_size);
        if((l_x != null) && (l_x != p_source[l_i]))
          {
          l_n[l_i] = l_x;
          return l_n;
          }
        l_b[l_i] = true;
        }

      return p_source;
      }
    };


/**
 * Insert a sequence create by a sequence factory.
 */

  private static  final Mutator<Instruction[], Instruction[]>
    MUTATE_SEQUENCE = new Mutator<Instruction[], Instruction[]>()
    {

/**
 * Mutate an existing instance of the genotype.
 * @param p_source        The source instance.
 * @param p_context       The (calling) context to be used.
 * @param p_info          Some useles integer parameter, only here for
 *                        compatibility with <code>Mutator</code>
 * @param p_memory_size   The memory size currently available.
 * @return The randomly mutated copy of the source or <code>p_source</code>
 *         itself if it cannot be mutated in any way.
 */

    @Override
    protected Instruction[]   mutate(final  Instruction[]     p_source,
                                     final  ProgramContext    p_context,
                                     final  int               p_info,
                                     final  int               p_memory_size)
      {
      Instruction[]   l_n, l_sq;
      int             l_p, l_l, l_sl, l_i, l_t;
      Instruction     l_ins;
      IfJump          l_ij;
      Randomizer      l_r;

      l_l = p_source.length;
      l_r = p_context.get_randomizer();
      l_p = l_r.nextInt(l_l+1);

      l_sq = p_context.create_sequence(l_l, p_memory_size, l_p,
                                       l_p >= l_l);

      if((l_sq == null) || ((l_sl = l_sq.length) <= 0)) return p_source;

      l_n = Arrays.insert_array_direct(p_source, l_p, l_sq);

      if(l_p < l_l)
        {
        for(l_i = (l_l+l_sl-1); l_i >= 0; l_i--)
          {
          if( (l_i < l_p) || (l_i >= (l_p+l_sl)) )
            {
            l_ins = l_n[l_i];

            if(l_ins instanceof IfJump)
              {
              l_ij = ((IfJump)l_ins);
              l_t  = l_ij.get_target();
              if((l_t > l_p) || ((l_t == l_p) && l_r.nextBoolean()))
                {
                l_n[l_i] = new IfJump(l_ij.get_condition(), l_t+l_sl);
                }
              }
            }
          }
        }

      return l_n;
      }
    };

/**
 * The possible program changing mutators.
 */

  private static  final Mutator<Instruction[], Instruction[]>[] MUTATORS =
           Arrays.create(MUTATE_DELETE, MUTATE_INSERT, MUTATE_SEQUENCE,
                         MUTATE_MUTATE);
/**
 * The frequencies at which the mutations are selected.
 */
                                                        //0.3d
  private static  final double[]  FREQUENCIES = {6.0d, 2.0d, 0.4d, 6.0d};

/**
 * The static, shared program mutator instance.
 */

  static  final ProgramMutator  INSTANCE      = new ProgramMutator();

/**
 * Create a new mutation dispatcher.
 */

  private ProgramMutator()
    {
    super(MUTATORS, FREQUENCIES);
    }
  }

File Information:

file name:ProgramMutator.java
package:org.dgpf.gp.automaton.search
qualified name:org.dgpf.gp.automaton.search.ProgramMutator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/automaton/search/ProgramMutator.java
size:10.409 KB (10659 B)
uploaded: 2015-07-22 04:10:55 GMT+0000
last update: 2006-06-08 08:15:49 GMT+0000
last access: 2017-11-21 15:39:43 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-21 15:39:43 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo