Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.regression.search.CalculationCreator.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:28:03
 * Original Filename: org.dgpf.gp.regression.search.CalculationCreator.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.sfc.math.stochastic.Randomizer;
import org.sfc.utils.Typesafe;

/**
 * This class comes with the ability to create calculations.
 *
 * @author Thomas Weise
 */

public final  class CalculationCreator
  {
/**
 * Ya cannot call this anyway - dont even try.
 */

  private CalculationCreator()
    {
    Typesafe.do_not_call();
    }
  

/**
 * Create a new randomized calculation.
 * @param p_context The regression context to be used.
 * @param p_info    The information record holding the infos on the
 *                  instruction to be created.
 * @return  The new calculation.
 */

  public static final Calculation create(final AggregationContext p_context,
                                         final MutationInfo       p_info)
    {
    final Formula[]     l_fs;
    final byte[]        l_ms;
    final double[]      l_is;
    final int           l_fc, l_mc, l_tar;
    final Randomizer    l_r;
    final ExpressionSet l_exp;
          int           l_i, l_j, l_pr, l_tr;
          Expression    l_e;
          Formula       l_f;
      
    
    l_r         = p_context.get_randomizer();
    l_tar       = p_context.get_target_count();
    l_exp       = p_context.get_expression_set();
    
    l_mc        = Math.max(l_tar,
                  Math.min(p_context.get_max_variables(),
                  (l_tar + 1 + (int)(l_r.exponential()))));
    l_i         = (l_tar + (int)(l_r.exponential()));
    l_fc        = Math.max(l_tar,
                  Math.min(p_context.get_max_formulas(),
                  Math.max(l_i, ((int)((Math.min(1.0d,
                           l_r.normal(0.5d, 0.1d)) * (l_mc+l_i)))))));
    l_is        = new double[l_mc];
    l_ms        = new byte[l_mc];
    l_fs        = new Formula[l_fc];
    
    p_info.m_vc = l_mc;
    
// create the formulas.
    
    for(l_i = (l_fc-1); l_i >= l_tar; l_i--)
      {
      do
        {
        p_info.begin_expression();
        l_e = l_exp.create(p_context, p_info);
        } while(l_e.is_constant() || (l_e instanceof Variable));
      
      l_fs[l_i] = new Formula(l_e, l_r.nextInt(l_mc));
      }
    
    for(l_i = (l_tar-1); l_i >= 0; l_i--)
      {
      do
        {
        p_info.begin_expression();
        l_e = l_exp.create(p_context, p_info);
        } while(l_e.is_constant() || (l_e instanceof Variable));
      
      l_fs[l_i] = new Formula(l_e, l_i);
      }
    
    if(l_fc > 1)
      {
      for(l_i = (l_fc-1); l_i >= 0; l_i--)
        {
        do
          {
          l_j = l_r.nextInt(l_fc);
          } while(l_j == l_i);
        
        l_f       = l_fs[l_i];
        l_fs[l_i] = l_fs[l_j];
        l_fs[l_j] = l_f;
        }
      }
    
// create the variable descriptors
    
    for(;;)
      {
      for(l_i = (l_mc-1); l_i >= l_tar; l_i--)
        {
        l_ms[l_i] = 0;
        }
      for(; l_i >= 0; l_i--)
        {
        l_ms[l_i] = Calculation.TARGET_VARIABLE;
        }
      
      l_pr = Math.max(1, Math.min(l_mc-1, ((int)(l_r.exponential()))));    
      l_i  = (l_pr - (l_mc-l_tar-1));
  
      if(l_i > 0)
        {
        l_pr -= l_i;      
        do
          {
          l_j = l_r.nextInt(l_tar);
          } while((l_ms[l_j] & Calculation.PROVIDED_VARIABLE) != 0);
        l_ms[l_j] |= Calculation.PROVIDED_VARIABLE;
        }    
      
      
      for(l_i = l_pr; l_i > 0; l_i--)
        {
        do
          {
          l_j = l_r.nextInt(l_mc);
          } while((l_ms[l_j] & Calculation.PROVIDED_VARIABLE) != 0);
        
        l_ms[l_j] |= Calculation.PROVIDED_VARIABLE;
        }
       
      l_tr = l_mc;
      for(l_i = (l_mc - 1); l_i >= 0; l_i--)
        {
        if((l_ms[l_i] & (Calculation.TARGET_VARIABLE |
                         Calculation.PROVIDED_VARIABLE)) != 0)
          {
          l_tr--;
          }
        }
      
      if(l_tr <= 0) continue;
      
      l_tr = (l_r.nextInt(l_tr)+1);
      
      for(l_i = l_tr; l_i > 0; l_i--)
        {
        do
          {
          l_j = l_r.nextInt(l_mc);
          } while((l_ms[l_j] & (Calculation.INPUT_VARIABLE    |
                                Calculation.PROVIDED_VARIABLE |
                                Calculation.TARGET_VARIABLE)) != 0);
        l_ms[l_j] |= Calculation.INPUT_VARIABLE;
        
        do
          {
          l_j = l_r.nextInt(l_mc);
          } while((l_ms[l_j] & (Calculation.OUTPUT_VARIABLE)) != 0);
        l_ms[l_j] |= Calculation.OUTPUT_VARIABLE;
        }
      
      break;
      }
    
    
    
    
// create the initial values.
    
    for(l_i = (l_mc-1); l_i >= 0; l_i--)
      {
      l_is[l_i] = AggregationUtils.create_constant(l_r); 
      }
    
    return new Calculation(l_fs, l_ms, l_is);
            
    
//          Formula[]     l_f;
//          byte[]        l_v;
//          Formula       l_y;
//          Randomizer    l_r;
//          int           l_l, l_i, l_j, l_x;
//    final int           l_tar;
//    final ExpressionSet l_ex;
//          byte[]        l_b2;
//          double[]      l_ii;
//    
//    l_r   = p_context.get_randomizer();    
//    l_tar = p_context.get_target_count();
//    l_ex  = p_context.get_expression_set();
//    
//    l_l    = (l_tar + 2 + ((int)(l_r.exponential())));
//    
//    l_v    = new byte[l_l];
//    l_ii   = new double[l_l];
//    for(l_i = (l_tar-1); l_i >= 0; l_i--)
//      {
//      l_v[l_i] = Calculation.TARGET_VARIABLE;
//      }
//    
//    for(l_i = l_r.nextInt(l_l); l_i >= 0; l_i--)
//      {
//      l_v[l_r.nextInt(l_l)] |= Calculation.PROVIDED_VARIABLE;
//      }
//    
//    for(l_i = Math.min(l_l - l_tar - 1,
//                      ((int)(l_r.exponential()))); l_i >= 0; l_i--)
//      {
//      do
//        {
//        l_x = l_r.nextInt(l_l);
//        }  while( (l_v[l_x] & Calculation.TARGET_VARIABLE) != 0);
//      l_v[l_x]              |= Calculation.INPUT_VARIABLE;
//      
//      l_v[l_r.nextInt(l_l)] |= Calculation.OUTPUT_VARIABLE;
//      }
//    
//    p_info.m_vc = l_l;
//    
//    l_i  = (l_l + ((int)(l_r.exponential())));
//    
//    l_f  = new Formula[l_i];
//    l_i -= l_tar;
//    for(l_j = 0; l_j < l_i; l_j++)      
//      {
//      l_x      = l_r.nextInt(l_l);      
//      p_info.begin_expression();      
//      l_f[l_j] = new Formula(l_ex.create(p_context, p_info), l_x);    
//      }
//    
//    for(l_x = (l_ii.length-1); l_x >= 0; l_x--)
//      {
//      l_ii[l_x] = AggregationUtils.create_constant(l_r);
//      }
//    
//    l_b2 = p_context.get_buffer().get_bytes(l_tar, (byte)0);
//    for(l_i = l_tar; l_i > 0; l_i--, l_j++)
//      {
//      do
//        {
//        l_x = l_r.nextInt(l_tar);
//        } while(l_b2[l_x] != 0);
//      l_b2[l_x] = 1;
//      
//      p_info.begin_expression();
//      l_f[l_j] = new Formula(l_ex.create(p_context, p_info), l_x);  
//      }
//    
//    l_l = l_f.length;
//    for(l_i = (l_l-1); l_i >= 0; l_i--)
//      {
//      l_j      = l_r.nextInt(l_l);
//      l_y      = l_f[l_j];
//      l_f[l_j] = l_f[l_i];
//      l_f[l_i] = l_y;
//      }
//    
//    return new Calculation(l_f, l_v, l_ii);
    }
  }

File Information:

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