Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.regression.search.CalculationCrossover.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 14:19:46
 * Original Filename: org.dgpf.gp.regression.search.CalculationCrossover.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.Formula;
import org.dgpf.gp.regression.base.IExpressionHandler;
import org.dgpf.gp.regression.expressions.binary.Add;
import org.dgpf.gp.regression.expressions.binary.BinaryExpressionHandler;
import org.dgpf.gp.regression.expressions.binary.Div;
import org.dgpf.gp.regression.expressions.binary.Mul;
import org.dgpf.gp.regression.expressions.binary.Power;
import org.dgpf.gp.regression.expressions.parameterless.Constant;
import org.dgpf.gp.regression.expressions.parameterless.Variable;
import org.dgpf.search.api.utils.ICrossover;
import org.dgpf.search.api.utils.WeightedCrossover;
import org.dgpf.search.api.utils.WeightedSet;
import org.dgpf.search.api.utils.WeightedSetBuilder;
import org.sfc.math.stochastic.Randomizer;

/**
 * This class provides the means to perform crossover on calculations.
 */

public final  class CalculationCrossover
             extends WeightedCrossover<Calculation, AggregationContext>
  {

/**
 * The serial version uid.
 */

  private static final long serialVersionUID = 1;
  
/**
 * This crossover performs simple formula exchange.
 */

  private static final ICrossover<Calculation, AggregationContext>
    FORMULA = new CalcCrossover()
      {

/**
 * Perform the crossover.
 * @param p_parent_1    The first parent individual.
 * @param p_parent_2    The second parent individual.
 * @param p_context     The hosting search context.
 * @return  The new individual which consists of parts of both parent
 *          individuals.
 */

      public final Calculation crossover(final Calculation    p_parent_1,
                                         final Calculation    p_parent_2,
                                         final AggregationContext p_context)
        {
        Randomizer  l_r;
        int         l_l1, l_l2, l_l, l_z;
        Formula[]   l_f, l_o1, l_o2;
        byte[]      l_b, l_b1, l_b2;
        double[]    l_sv, l_d1, l_d2;
        
        l_o1 = get_formulas(p_parent_1);
        l_o2 = get_formulas(p_parent_2);
        l_l1 = l_o1.length;
        l_l2 = l_o2.length;
        l_r  = p_context.get_randomizer();
        
        if(l_l1 != l_l2)
          {
          l_l  = Math.min(l_l1, l_l2);        
          l_l += l_r.nextInt(Math.max(l_l1, l_l2) - l_l + 1);
          }
        else l_l = l_l1;
        
        l_f = new Formula[l_l];
        for(--l_l; l_l >= 0; l_l--)
          {
          if( (l_l1 > l_l) && (l_l2 > l_l) )
            {
            l_f[l_l] = (l_r.nextBoolean() ? l_o1[l_l]
                                          : l_o2[l_l] );
            }
          else if(l_l1 > l_l) l_f[l_l] = l_o1[l_l];
          else                l_f[l_l] = l_o2[l_l];
          }
        
        l_b1 = get_variable_types(p_parent_1);
        l_b2 = get_variable_types(p_parent_2);
        l_d1 = get_start_values(p_parent_1);
        l_d2 = get_start_values(p_parent_2);
        l_l1 = Math.min(l_b1.length, l_b2.length);
        l_l2 = Math.max(l_b1.length, l_b2.length);
        l_l  = Math.max((l_z=p_context.get_target_count()) + 1,
                        l_l1 + l_r.nextInt(Math.max(1, l_l2-l_l1)));
        l_b  = new byte[l_l];
        l_sv = new double[l_l];
        
        for(--l_z; l_z >= 0; l_z--)
          {
          l_b[l_z] = Calculation.TARGET_VARIABLE;
          }
        
        l_l1 = l_b1.length;
        l_l2 = l_b2.length;
        for(l_l--; l_l >= 0; l_l--)
          {
          if( (l_l1 > l_l) && (l_l2 > l_l) )
            {
            l_b[l_l] |= (l_r.nextBoolean() ? l_b1[l_l]
                                           : l_b2[l_l] );
            }
          else if(l_l1 > l_l) l_b[l_l] |= l_b1[l_l];
          else if(l_l2 > l_l) l_b[l_l] |= l_b2[l_l];
          else l_b[l_l] ^= (l_r.nextInt(0xff) &
                           (Calculation.INPUT_VARIABLE |
                            Calculation.OUTPUT_VARIABLE |
                            Calculation.PROVIDED_VARIABLE));
                    
          if( (l_l1 > l_l) && (l_l2 > l_l) )
            {
            l_sv[l_l] = ((l_r.nextInt(5)>0) ?
                         ((l_d1[l_l] + l_d2[l_l])*0.5d) :
                          (l_r.nextBoolean() ? l_d1[l_l]
                                             : l_d2[l_l]));
            }
          else if(l_l1 > l_l) l_sv[l_l] = l_d1[l_l];
          else if(l_l2 > l_l) l_sv[l_l] = l_d2[l_l];
          else  l_sv[l_l] = AggregationUtils.create_constant(l_r);
          }
        
        l_z  = 0;
        l_l1 = 0;
        for(l_l = (l_b.length-1); l_l >= 0; l_l--)
          {
          if( (l_b[l_l] & Calculation.INPUT_VARIABLE) != 0 )
            {
            l_z++;
            l_l1++;
            }          
          if( (l_b[l_l] & Calculation.OUTPUT_VARIABLE) != 0 )
            {
            l_z--;
            l_l1++;
            }
          }
        
        while(l_z < 0)
          {
          l_l = l_r.nextInt(l_b.length);
          if( (l_b[l_l] & Calculation.INPUT_VARIABLE) == 0 )
            {
            l_b[l_l] |= Calculation.INPUT_VARIABLE;
            l_z++;
            }
          }
        
        while(l_z > 0)
          {
          l_l = l_r.nextInt(l_b.length);
          if( (l_b[l_l] & Calculation.OUTPUT_VARIABLE) == 0 )
            {
            l_b[l_l] |= Calculation.OUTPUT_VARIABLE;
            l_z--;
            }
          }
        
        if(l_l1 == 0)
          {
          l_b[l_r.nextInt(l_b.length)] |= Calculation.OUTPUT_VARIABLE;
          l_b[l_r.nextInt(l_b.length)] |= Calculation.INPUT_VARIABLE;
          }
        
        return new Calculation(l_f, l_b, l_sv);
        }
      };
            
/**
 * This crossover performs an exchange of two sub-expressions.
 */

  private static final ICrossover<Calculation, AggregationContext>
    EXCHANGE = new CalcCrossover()
      {

/**
 * Perform the crossover.
 * @param p_parent_1    The first parent individual.
 * @param p_parent_2    The second parent individual.
 * @param p_context     The hosting search context.
 * @return  The new individual which consists of parts of both parent
 *          individuals.
 */

      public final Calculation crossover(final Calculation        p_parent_1,
                                         final Calculation        p_parent_2,
                                         final AggregationContext p_context)
        {
        Randomizer  l_r;
        int         l_l, l_i;
        Formula[]   l_f;
        Expression  l_e1, l_e2;
                
        l_f  = get_formulas(p_parent_1).clone();
        l_l  = l_f.length;
        l_r  = p_context.get_randomizer();
        l_i  = l_r.nextInt(Math.min(l_l, p_parent_2.get_formula_count()));
        l_e1 = l_f[l_i].get_expression();
        l_e2 = p_parent_2.get_formula(l_i).get_expression();
        
        l_f[l_i] = new Formula(
            l_e1.get_handler().replace_sub_expression(p_context, l_e1,
                null,
            l_e2.get_handler().get_sub_expression(p_context, l_e2)),
            l_f[l_i].get_variable()
            );
            
        return new Calculation(l_f, get_variable_types(p_parent_1),
                               get_start_values(p_parent_1));
        }
      };
      
/**
 * This crossover performs an exchange of two formulas.
 */

  private static final ICrossover<Calculation, AggregationContext>
    EXCHANGE2 = new CalcCrossover()
      {

/**
 * Perform the crossover.
 * @param p_parent_1    The first parent individual.
 * @param p_parent_2    The second parent individual.
 * @param p_context     The hosting search context.
 * @return  The new individual which consists of parts of both parent
 *          individuals.
 */

      public final Calculation crossover(final Calculation        p_parent_1,
                                         final Calculation        p_parent_2,
                                         final AggregationContext p_context)
        {
        Randomizer  l_r;
        Formula[]   l_f, l_f2;
                
        l_f  = get_formulas(p_parent_1).clone();
        l_f2 = get_formulas(p_parent_2);        
        l_r  = p_context.get_randomizer();
        l_f[l_r.nextInt(l_f.length)] = l_f2[l_r.nextInt(l_f2.length)];
            
        return new Calculation(l_f, get_variable_types(p_parent_1),
                               get_start_values(p_parent_1));
        }
      };
      
      

/**
 * This crossover tries to compute the average of two calculations. 
 */

  private static final ICrossover<Calculation, AggregationContext>
    AVG = new CalcCrossover()
      {

/**
 * Perform the crossover.
 * @param p_parent_1    The first parent individual.
 * @param p_parent_2    The second parent individual.
 * @param p_context     The hosting search context.
 * @return  The new individual which consists of parts of both parent
 *          individuals.
 */

      public final Calculation crossover(final Calculation        p_parent_1,
                                         final Calculation        p_parent_2,
                                         final AggregationContext p_context)
        {
        Formula[]                                 l_f1, l_f2, l_fr;
        byte[]                                    l_b1, l_b2, l_br;
        double[]                                  l_d1, l_d2, l_dr;
        int                                       l_t, l_l1, l_l2, l_vl1,
                                                  l_vl2, l_i;
        Formula                                   l_f;
        Expression                                l_e1, l_e2;
        Randomizer                                l_r;
        WeightedSet<IExpressionHandler>.Session   l_s;
        IExpressionHandler                        l_x;
        Calculation                               l_cxx;
        MutationInfo                              l_iif;
        double                                    l_vv;
        
        l_f1  = get_formulas(p_parent_1);
        l_f2  = get_formulas(p_parent_2);
        
        if((l_f1.length + l_f2.length) >= (p_context.get_max_formulas()-1))
          {
          return null;
          }
        
        l_b1  = get_variable_types(p_parent_1);
        l_b2  = get_variable_types(p_parent_2);
          
        if((l_b1.length + l_b2.length) >= p_context.get_max_variables())
          {
          return null;
          }
        
        l_d1  = get_start_values(p_parent_1);
        l_d2  = get_start_values(p_parent_2);
        l_t   = p_context.get_target_count();
        l_r   = p_context.get_randomizer();
        
        l_vl1 = l_b1.length;
        l_vl2 = l_b2.length;
        l_br  = new byte[l_vl1 + l_vl2];
        l_dr  = new double[l_vl1 + l_vl2];
        System.arraycopy(l_b1, 0, l_br, 0, l_vl1);
        System.arraycopy(l_b2, 0, l_br, l_vl1, l_vl2);
        
        for(l_i = 0; l_i < l_t; l_i++)
          {
          l_br[l_vl1 + l_i] &= (~(Calculation.TARGET_VARIABLE));
          }
        
        System.arraycopy(l_d1, 0, l_dr, 0, l_vl1);
        System.arraycopy(l_d2, 0, l_dr, l_vl1, l_vl2);        
        
        l_l1 = l_f1.length;
        l_l2 = l_f2.length;
        l_fr = new Formula[l_l1 + l_l2 + l_t];
        
        System.arraycopy(l_f1, 0, l_fr, 0, l_l1);
        System.arraycopy(l_f2, 0, l_fr, l_l1, l_l2);
        l_l2 += l_l1;
        for(; l_l1 < l_l2; l_l1++)
          {
          l_f = l_fr[l_l1];
          for(l_i = (l_vl2-1); l_i >= 0; l_i--)
            {
            l_f = l_f.replace_var(l_i, l_i+l_vl1);
            }
          l_fr[l_l1] = l_f;
          }
               
        for(l_i = 0; l_i < l_t; l_i++)
          {
          l_e1 = Variable.create(l_i);
          l_e2 = Variable.create(l_i+l_vl1);
          
main:
          for(;;)
            {
            switch(l_r.nextInt(10))
              {
              case 0:
                {
                l_f = new Formula(new Power(new Mul(l_e1, l_e2),
                                  Constant.create(0.5d)), l_i);
                break main;
                }
                
              case 1: //case 4:
                {
                l_f = new Formula(new Div(Constant.create(2.0d),
                                  new Add(new Div(Constant.create(1.0d),l_e2),
                                  new Div(Constant.create(1.0d), l_e2))), l_i);
                break main;
                }
                
              case 2:
                {
                l_s = p_context.get_expression_set().open_session(l_r);
                
                while(l_s.hasNext())
                  {
                  l_x = l_s.next();
                  if(l_x instanceof BinaryExpressionHandler)
                    {
                    l_f = new Formula(
                        ((BinaryExpressionHandler)l_x).do_create(l_e1, l_e2),
                        l_i);
                    break main;
                    }
                  }
                
                break;
                }             
                
              case 3: //case 4:
                {
                l_vv = l_r.nextDouble();
                l_f = new Formula(new Add(
                          new Mul(Constant.create(l_vv), l_e1),
                          new Mul(Constant.create(1.0d-l_vv), l_e2)), l_i);
                break main;
                }
                
              default:
                {
                l_f = new Formula(new Mul(Constant.create(0.5d),
                      new Add(l_e1, l_e2)), l_i);
                break main;
                }
              }
              
            }
          
          l_fr[l_l2++] = l_f;
          }
        
        l_cxx = new Calculation(l_fr, l_br, l_dr);
        l_iif = new MutationInfo();   
        while(l_r.nextBoolean())
          {
          if(l_r.nextBoolean())
            {
            l_cxx = CalcShrinker.INSTANCE.mutate(l_cxx, p_context, l_iif);
            }
          else
            {
            l_cxx = VarShrinker.INSTANCE.mutate(l_cxx, p_context, l_iif);
            }
          }
        
        return l_cxx;
        }
      };
      
/**
 * The shared singleton instance of this crossover.
 */

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

  private CalculationCrossover()
    {
    super(build());
    }
  
/**
 * Build the weighted set internally.
 * @return The weighted set to be used internally.
 */
  
  private static  final WeightedSetBuilder<ICrossover
                  <Calculation, AggregationContext>> build()
    {
    WeightedSetBuilder<ICrossover<Calculation, AggregationContext>> l_m;
      
    l_m = new WeightedSetBuilder<ICrossover
          <Calculation, AggregationContext>>(ICrossover.class);
    
    l_m.add(FORMULA,   1.0d);
    l_m.add(EXCHANGE,  1.0d);
    l_m.add(EXCHANGE2, 0.3d);
    l_m.add(AVG,       1.0d);
   
    return l_m;
    }
  

  }

File Information:

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