Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.regression.search.CalculationMutator.java

Here you can find all the information about the file org.dgpf.gp.regression.search.CalculationMutator.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-26 08:55:28
 * Original Filename: org.dgpf.gp.regression.search.CalculationMutator.java
 * Version          : 1.0.0
 * Last modification: 2006-07-26
 *                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.regression.search;

import org.dgpf.gp.regression.base.AggregationContext;
import org.dgpf.gp.regression.base.AggregationUtils;
import org.dgpf.gp.regression.base.Calculation;
import org.dgpf.gp.regression.base.Expression;
import org.dgpf.gp.regression.base.ExpressionSet;
import org.dgpf.gp.regression.base.Formula;
import org.dgpf.gp.regression.expressions.parameterless.Variable;
import org.dgpf.search.api.utils.IMutator;
import org.dgpf.search.api.utils.WeightedMutator2;
import org.dgpf.search.api.utils.WeightedSetBuilder;
import org.sfc.math.stochastic.Randomizer;

/**
 * This is the mutator allowing us to mutate calculations. 
 *
 * @author Thomas Weise
 */

public final class   CalculationMutator
             extends WeightedMutator2<Calculation, AggregationContext,
                                      MutationInfo>
  {

/**
 * The serial version uid.
 */

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

  static final IMutator<Calculation, AggregationContext, MutationInfo>
    MUTATOR = new CalcMutator()
      {
      public final Calculation mutate  (      Calculation       p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo      p_info)
        {
        byte[]      l_b, l_v;
        int         l_i, l_k, l_z;
        Randomizer  l_r;
        Expression  l_e1, l_e2;
        Formula     l_f;
        Formula[]   l_ff;
        
        l_ff = get_formulas(p_source).clone();
        l_i  = l_ff.length;
        l_v  = get_variable_types(p_source);
        
        l_b  = p_context.get_buffer().get_bytes(l_i, (byte)0);
        l_r  = p_context.get_randomizer();
        p_info.m_vc = l_v.length;
        
        for(l_z = Math.min(l_i-1, ((int)(l_r.exponential()))); l_z >= 0; l_z--)
          {
          do
            {
            l_k = l_r.nextInt(l_i);
            } while(l_b[l_k] != 0);
          l_b[l_k] = 1;
                    
          l_f  = l_ff[l_k];
          l_e1 = l_f.get_expression();
          do
            {                        
            l_e2 = l_e1.get_handler().mutate(l_e1, p_context, p_info);
            } while((l_e2 == null) ||
                   ((l_e2 = l_e2.get_handler().optimize(l_e2)).equals(l_e1))
                   || l_e2.is_constant());
          
          l_ff[l_k] = new Formula(l_e2, l_f.get_variable());          
          }
        
        return new Calculation(l_ff, l_v, get_start_values(p_source));
        }
      };
      
      
/**
 * This internal mutator deletes a single formula from a calculation.
 */

  static final IMutator<Calculation, AggregationContext, MutationInfo>
    DELETOR = new CalcMutator()
      {
      public final Calculation mutate  (      Calculation        p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo       p_info)
        {
        Formula[] l_f, l_q;        
        int       l_i, l_t;
        
        l_q = get_formulas(p_source);
        l_i = l_q.length;
        if(l_i <= p_context.get_target_count()) return p_source;
                
        l_f = new Formula[l_i-1];
        l_t = p_context.get_randomizer().nextInt(l_i);
        
        for(--l_i; l_i >= 0; l_i--)
          {
               if(l_i > l_t) l_f[l_i-1] = l_q[l_i];
          else if(l_i < l_t) l_f[l_i]   = l_q[l_i];
          }
                
        return new Calculation(l_f, get_variable_types(p_source),
                               get_start_values(p_source));
        }
      };
      
/**
 * This internal mutator inserts a single formula from a calculation.
 */

  private static final IMutator<Calculation, AggregationContext, MutationInfo>
    INSERTER = new CalcMutator()
      {
      public final Calculation mutate  (      Calculation       p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo      p_info)
        {
        Formula[]     l_f, l_q;
        byte[]        l_z;
        int           l_i, l_t;
        ExpressionSet l_exp;
        Expression    l_e;
        Randomizer    l_r;
                
        l_q = get_formulas(p_source);
        l_i = l_q.length;
                
        l_f = new Formula[l_i+1];
        l_r = p_context.get_randomizer();
        l_t = l_r.nextInt(l_i+1);
        l_z = get_variable_types(p_source);
        p_info.m_vc = l_z.length;
        
        for(--l_i; l_i >= 0; l_i--)
          {
               if(l_i >= l_t) l_f[l_i+1] = l_q[l_i];
          else if(l_i <  l_t) l_f[l_i]   = l_q[l_i];
          }
                
        l_exp = p_context.get_expression_set();
        do
          {
          p_info.begin_expression();
          l_e = l_exp.create(p_context, p_info);
          } while( l_e.is_constant() || (l_e instanceof Variable) );
                
        
        l_f[l_t] = new Formula(l_e, l_r.nextInt(l_z.length));
        
        return new Calculation(l_f, l_z, get_start_values(p_source));
        }
      };
      
      
/**
 * This internal mutator mutates the start values.
 */

  private static final IMutator<Calculation, AggregationContext, MutationInfo>
    START = new CalcMutator()
      {
      public final Calculation mutate  (      Calculation        p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo       p_info)
        {
        double[]    l_d;
        int         l_i, l_j;
        Randomizer  l_r;
        
        l_d = get_start_values(p_source).clone();
        l_r = p_context.get_randomizer();
        
        for(l_i = Math.min(l_d.length, Math.max(1,
                          ((int)(l_r.exponential())))); l_i > 0; l_i--)
          {
          l_j = l_r.nextInt(l_d.length);
          l_d[l_j] = AggregationUtils.mutate_constant(l_d[l_j], l_r);
          }
        
        return new Calculation(get_formulas(p_source),
                               get_variable_types(p_source),
                               l_d);
        }
      };

      
/**
 * This internal mutator mutates a variable.
 */

  private static final IMutator<Calculation, AggregationContext, MutationInfo>
    INS_VAR = new CalcMutator()
      {
      public final Calculation mutate  (      Calculation        p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo       p_info)
        {
        double[]    l_d;
        byte[]      l_b, l_ob;
        Formula[]   l_f;
        int         l_i, l_j, l_t;
        Randomizer  l_r;
        Formula     l_x;
        Expression  l_q;
        
        l_r  = p_context.get_randomizer();
        l_ob = get_variable_types(p_source);
        l_i  = l_ob.length;
        l_b  = new byte[l_i+1];
        System.arraycopy(l_ob, 0, l_b, 0, l_i);
        l_d  = new double[l_i+1];
        System.arraycopy(get_start_values(p_source), 0, l_d, 0, l_i);
        l_f  = get_formulas(p_source).clone();
        l_t  = p_context.get_target_count();
        
        if(l_r.nextInt(3) <= 0)
          {
          if(l_r.nextBoolean()) l_b[l_i] |= Calculation.PROVIDED_VARIABLE;
                                l_b[l_i] |= Calculation.INPUT_VARIABLE;
          }
        if(l_r.nextInt(3) <= 0) l_b[l_i] |= Calculation.OUTPUT_VARIABLE;
        
        if( ((l_b[l_i] & Calculation.INPUT_VARIABLE)  != 0) &&
            ((l_b[l_i] & Calculation.OUTPUT_VARIABLE) == 0) )
          {
          l_b[l_r.nextInt(l_i)] |= Calculation.OUTPUT_VARIABLE;
          }
        
        l_d[l_i] = AggregationUtils.create_constant(l_r);
        
        
main:
        for(;;)
          {
          if(l_r.nextInt(3) <= 0)
            {          
            l_j = l_r.nextInt(l_f.length);
            l_x = l_f[l_j];
            if((l_x.get_variable() > l_t) || (l_r.nextInt(5) <= 0))
              {
              l_x = l_x.replace_var(l_r.nextInt(l_i), l_i);
              if(l_x != l_f[l_j])
                {
                l_f[l_j] = l_x;
                if(l_x.get_variable() == l_i) break;
                }
              }          
            }
          else
            {
            for(;;)
              {
              l_j = l_r.nextInt(l_f.length);
              l_x = l_f[l_j];
              if((l_x.get_variable() > l_t) || (l_r.nextInt(5) <= 0))
                {
                l_f[l_j] = new Formula(l_x.get_expression(), l_i);
                break;
                }
              }
            for(;;)
              {
              l_j = l_r.nextInt(l_f.length);
              l_x = l_f[l_j];
              if((l_x.get_variable() > l_t) || (l_r.nextInt(5) <= 0))
                {
                l_q = l_x.get_expression();
                l_q = l_q.get_handler().replace_sub_expression(p_context,
                                          l_q, null, Variable.create(l_i));
                l_f[l_j] = new Formula(l_q, l_i);
                break main;
                }
              }
            }
          }
        
        return new Calculation(l_f, l_b, l_d);
        }
      };
      
      
/**
 * This internal mutator deletes a variable.
 */

  static final IMutator<Calculation, AggregationContext, MutationInfo>
    DEL_VAR = new CalcMutator()
      {
      public final Calculation mutate  (      Calculation        p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo       p_info)
        {
        double[]    l_d, l_od;
        byte[]      l_b, l_ob;
        Formula[]   l_f;
        int         l_p, l_i, l_j, l_t;
        Randomizer  l_r;
        
        l_r  = p_context.get_randomizer();
        l_ob = get_variable_types(p_source);        
        l_i  = l_ob.length;
        l_t  = p_context.get_target_count();
        if(l_i <= l_t) return p_source;
        
        l_p  = (l_t + l_r.nextInt(l_i - l_t)); 
        
        l_i--;
        l_b  = new byte[l_i];
        System.arraycopy(l_ob, 0, l_b, 0, l_p);
        System.arraycopy(l_ob, l_p+1, l_b, l_p, l_i-l_p);
        l_d  = new double[l_i];
        l_od = get_start_values(p_source);
        System.arraycopy(l_od, 0, l_d, 0, l_p);
        System.arraycopy(l_od, l_p+1, l_d, l_p, l_i-l_p);
        
        l_f = get_formulas(p_source).clone();
        l_j = l_r.nextInt(l_i);
        for(l_i = (l_f.length-1); l_i >= 0; l_i--)
          {
          l_f[l_i] = l_f[l_i].replace_var(l_p, l_j);
          }
        
        return new Calculation(l_f, l_b, l_d);
        }
      };
        
  
/**
 * This internal mutator mutates the start values.
 */

  private static final IMutator<Calculation, AggregationContext, MutationInfo>
    MUT_VAR = new CalcMutator()
      {
/**
 * The muation constant.
 */

      private static final int  X = (Calculation.INPUT_VARIABLE |
                                     Calculation.OUTPUT_VARIABLE |
                                     Calculation.PROVIDED_VARIABLE);
      
      public final Calculation mutate  (      Calculation        p_source,
                                        final AggregationContext p_context,
                                        final MutationInfo       p_info)
        {
        byte[]      l_b;
        int         l_i, l_d;
        Randomizer  l_r;
        
        l_r  = p_context.get_randomizer();
        l_b  = get_variable_types(p_source).clone();        
        l_i  = l_r.nextInt(l_b.length);
        
        do
          {
          l_d = ((l_r.nextInt(X)+1) & X);
          } while(l_d == 0);
        
        l_b[l_i] ^= l_d;
        
        return new Calculation(get_formulas(p_source), l_b,
                               get_start_values(p_source));
        }
      };
/**
 * The shared singleton instance of this mutator.
 */

  public static final IMutator<Calculation, AggregationContext, MutationInfo>
      INSTANCE = new CalculationMutator();
      
/**
 * The internal hidden constructor, use the package private shared
 * singleton instead.
 * @see #INSTANCE
 */

  private CalculationMutator()
    {
    super(build());
    }
  
/**
 * Build the weighted set internally.
 * @return The weighted set to be used internally.
 */
  
  private static  final WeightedSetBuilder<IMutator
                  <Calculation, AggregationContext, MutationInfo>> build()
    {
    WeightedSetBuilder<IMutator<Calculation, AggregationContext,
                                             MutationInfo>> l_m;
      
    l_m = new WeightedSetBuilder<IMutator
          <Calculation, AggregationContext, MutationInfo>>(IMutator.class);
    
    l_m.add(MUTATOR,                6.0d);
    l_m.add(DELETOR,                1.0d);
    l_m.add(INSERTER,               1.0d);
    l_m.add(START,                  0.6d);
    l_m.add(CalcShrinker.INSTANCE,  1.0d);
    l_m.add(VarShrinker.INSTANCE,   1.0d);
    l_m.add(INS_VAR,                0.4d);
    l_m.add(MUT_VAR,                0.4d);
    l_m.add(DEL_VAR,                0.4d);
   
    return l_m;
    }
  }

File Information:

file name:CalculationMutator.java
package:org.dgpf.gp.regression.search
qualified name:org.dgpf.gp.regression.search.CalculationMutator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/regression/search/CalculationMutator.java
size:14.414 KB (14760 B)
uploaded: 2015-07-22 04:10:56 GMT+0000
last update: 2006-08-14 13:44:20 GMT+0000
last access: 2017-11-18 23:25: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-18 23:25:49 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo