Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.regression.base.Calculator.java

Here you can find all the information about the file org.dgpf.gp.regression.base.Calculator.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-24 16:46:53
 * Original Filename: org.dgpf.gp.regression.Calculator.java
 * Version          : 1.0.0
 * Last modification: 2006-07-24
 *                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.base;

import org.sfc.text.Text;

/**
 * A calculator hosts a calculation and simulates it.
 *
 * @author Thomas Weise
 */

public class Calculator extends Calculation
  {
/**
 * The serial version uid.
 */

  private static  final long            serialVersionUID  = 1;
  
  
/**
 * The context the calculator runs inside.
 */

  private final AggregationContext m_context     ;
/**
 * The variables held by the calculator.
 */

  private final double[]          m_variables   ;
  
/**
 * Create a new calculator.
 * @param p_context   The context the calculator should run inside.
 */

  public  Calculator(final AggregationContext p_context)
    {
    super();
    this.m_context   = p_context;
    this.m_variables = new double[p_context.get_max_variables()];
    }
  
/**
 * Initialize a calculator and prepare it for running the specified
 * calculation.
 * @param p_calculation   The calculation to be run.
 */

  protected void  init  (final Calculation p_calculation)
    {
    this.m_formulas       = p_calculation.m_formulas;
    this.m_variable_types = p_calculation.m_variable_types;
    this.m_start_values   = p_calculation.m_start_values;
    this.m_costs          = 0;
    System.arraycopy(this.m_start_values, 0, this.m_variables, 0,
                     this.m_start_values.length);
//    Arrays.fill(this.m_variables, 0.0d, 0, this.m_variable_types.length);
    }
  
/**
 * Obtain the regression context hosting this calculation.
 * @return The regression context hosting this calculation.
 */

  public  final AggregationContext get_context()
    {
    return this.m_context;
    }
  
  
  
/**
 * Obtain the value of a variable.
 * @param p_index   The index of the variable to obtain the value from.
 * @return  The value of the variable identified by <code>p_index</code>.
 */

  public  final double  get_variable  (final int p_index)
    {
    return this.m_variables[p_index];
    }
  
/**
 * Set the value of a variable.
 * @param p_index   The index of the variable to be set.
 * @param p_value   The new value for the variable at index
 *                  <code<p_index</code>.
 */

  public  final void    set_variable  (final int    p_index,
                                       final double p_value)
    {
    this.m_variables[p_index] = p_value;
    }
  

/**
 * Store this objects content into a string builder.
 * @param p_sb  The string builder to store stuff into.
 */

 @Override
  public final void  to_string (final StringBuilder p_sb)
    {
    double[]    l_d;
    int         l_i, l_j;
        
    l_d = this.m_variables;
    
    super.to_string(p_sb);
    
    l_j = this.get_variable_count();
    if(l_j > 0)
      {
      p_sb.append(Text.LINE_BREAK_CHARS);
      p_sb.append(Text.LINE_BREAK_CHARS);
      p_sb.append("values:");
      for(l_i = 0; l_i < l_j; l_i++)
        {
        p_sb.append(Text.LINE_BREAK_CHARS);
        AggregationUtils.get_variable_name(l_i, p_sb);
        p_sb.append(AggregationUtils.EQU);
        p_sb.append(l_d[l_i]);
        }
      }
    }
 
/**
 * Check if another object is equal to this one.
 * @param p_o   The object to check.
 * @return  <code>true</code> if and only if <code>p_o</code> is exactly
 *          the same object than this one.
 */

  @Override
  public  boolean equals  (final Object p_o)
    {
    double[]  l_d1, l_d2;
    int       l_i;
    
    if(p_o == thisreturn true;
    
    if(super.equals(p_o) && (p_o instanceof Calculator))
      {
      l_d1 = this.m_variables;
      l_d2 = ((Calculator)p_o).m_variables;
      
      for(l_i = (this.m_variable_types.length-1); l_i >= 0; l_i--)
        {
        if(Double.compare(l_d1[l_i], l_d2[l_i]) != 0) return false;
        }
      
      return true;
      }
    
    return false;
    }
  
/**
 * Execute the calculation assigned to this calculator exactly once.
 */

  public  final  void step()
    {    
    Formula[]  l_e;
    int        l_i, l_j;
    
    l_e = this.m_formulas;
    l_j = l_e.length;
    
    for(l_i = 0; l_i < l_j; l_i++)
      {
      l_e[l_i].compute(this);
      }
    }
  }

File Information:

file name:Calculator.java
package:org.dgpf.gp.regression.base
qualified name:org.dgpf.gp.regression.base.Calculator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/regression/base/Calculator.java
size:5.244 KB (5370 B)
uploaded: 2015-07-22 04:10:56 GMT+0000
last update: 2006-08-18 08:44:45 GMT+0000
last access: 2017-11-23 05:27:21 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-23 05:27:21 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo