Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.regression.search.CalcShrinker.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-08-09 13:29:19
 * Original Filename: org.dgpf.gp.regression.search.CalcShrinker.java
 * Version          : 1.0.0
 * Last modification: 2006-08-09
 *                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.Calculation;
import org.dgpf.gp.regression.base.Expression;
import org.dgpf.gp.regression.base.Formula;
import org.dgpf.gp.regression.expressions.parameterless.Variable;
import org.dgpf.search.api.utils.IMutator;
import org.sfc.math.stochastic.Randomizer;

/**
 * This special mutator compresses the formulas inside a calculation.
 *
 * @author Thomas Weise
 */

final class CalcShrinker  extends CalcMutator
  {
/**
 * The new calculation shrinker.
 */

  static final IMutator<Calculation, AggregationContext, MutationInfo>
   INSTANCE = new CalcShrinker();
  
/**
 * Perform the mutation.
 * @param p_source  The source object to create a randomizedly altered
 *                  copy of.
 * @param p_context The context hosting the operation.
 * @param p_info    The information record.
 * @return  The new, randomizedly altered copy of the object
 *          <code>p_source</code> or <code>p_source</code> itself if no
 *          mutation was possible.
 */

  public final Calculation mutate  (final Calculation        p_source,
                                    final AggregationContext p_context,
                                    final MutationInfo       p_info)
    {
    int[]         l_is;
    int           l_m, l_i, l_j, l_k, l_si, l_n;
    Formula[]     l_f, l_f2;
    Formula       l_xx;
    Randomizer    l_r;
    Expression    l_q, l_oe;
    
    l_m  = p_source.get_variable_count();
    l_is = p_context.get_buffer().get_ints(l_m, 0);
    l_r  = p_context.get_randomizer();
    l_f  = get_formulas(p_source);
    for(l_i = (l_f.length-1); l_i >= 0; l_i--)
      {
      l_is[l_f[l_i].get_variable()]++;
      }
    
    l_j = 0;
    for(l_i = 0; l_i < l_m; l_i++)
      {
      if(l_is[l_i] > 1)
        {
        l_is[l_j++] = l_i;
        }
      }
    
    if(l_j <= 0) return p_source;
    if(l_j > 1)
      {
      for(l_i = 0; l_i < l_j; l_i++)
        {
        l_k       = l_r.nextInt(l_j);
        l_m       = l_is[l_k];
        l_is[l_k] = l_is[l_i];
        l_is[l_i] = l_m;
        }
      }
    
    l_k = l_f.length;
    
main:
    for(--l_j; l_j >= 0; l_j--)
      {
      l_m  = l_is[l_j];
      l_si = l_r.nextInt(l_f.length);
      
      l_i  = l_si;
      for(;;)
        {
        if(l_f[l_i].get_variable() == l_m) break;
        l_i = ((l_i + 1) % l_k);
        }
      
      l_n = l_i;
      
      for(;;)
        {
        l_i = ((l_i + 1) % l_k);
        if(l_f[l_i].get_variable() == l_m) break;             
        }
            
      if(l_i > l_n)
        {
        l_si = l_i;
        l_i  = l_n;
        l_n  = l_si;
        }
      
      l_oe = l_f[l_i].get_expression();
      
      for(l_si = (l_i+1); l_si < l_n; l_si++)
        {
        if( ((l_f[l_si].get_variable_usage(l_m) & 1) != 0) ||
            ((l_oe.get_variable_usage(l_f[l_si].get_variable()) & 1) != 0))
          {
          continue main;   
          }
        }
      
      --l_k;
      l_f2 = new Formula[l_k];
      System.arraycopy(l_f, 0, l_f2, 0, l_i);
      System.arraycopy(l_f, l_i+1, l_f2, l_i, l_k - l_i);
      l_xx = l_f[l_n];
      l_q  = l_xx.get_expression();
      l_q  = l_q.get_handler().replace_sub_expression(p_context, l_q, 
                  Variable.create(l_m), l_oe);
      l_f2[l_n-1] = new Formula(l_q, l_xx.get_variable());
      
      return new Calculation(l_f2, get_variable_types(p_source),
                             get_start_values(p_source));
      }
      
    return p_source;
    }
  }

File Information:

file name:CalcShrinker.java
package:org.dgpf.gp.regression.search
qualified name:org.dgpf.gp.regression.search.CalcShrinker.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/regression/search/CalcShrinker.java
size:4.735 KB (4849 B)
uploaded: 2015-07-22 04:10:56 GMT+0000
last update: 2006-08-11 04:08:41 GMT+0000
last access: 2017-11-22 09:26:22 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:26:22 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo