Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.SearchUtils.java

Here you can find all the information about the file org.dgpf.search.api.SearchUtils.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-04-10 12:58:22
 * Original Filename: org.dgpf.search..apiSearchUtils.java
 * Version          : 2.0.2
 * Last modification: 2006-05-29
 *                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.search.api;

import org.dgpf.search.api.adaptation.NoAdaptation;
import org.dgpf.search.api.comparators.StaggeredComparator;
import org.dgpf.search.api.halt.NeverHaltParameters;
import org.sfc.collections.Arrays;
import org.sfc.math.Mathematics;
import org.sfc.math.stochastic.statistics.StatisticInfo;
import org.sfc.meta.BoundedLongProperty;
import org.sfc.meta.Property;
import org.sfc.meta.PropertyGroup;
import org.sfc.meta.TimeProperty;
import org.sfc.utils.Typesafe;


/**
 * This static class holds some helper functions.
 *
 * @author Thomas Weise
 */

public final class SearchUtils
  {
/**
 * A convenient maximum value for integers where the probability of an
 * overflow in an arithmetic computation is relatively low.
 */

  public  static  final int     DEFAULT_MAX_INT                   =
                                  (Integer.MAX_VALUE >>> 4);
/**
 * A convenient maximum value for longs where the probability of an
 * overflow in an arithmetic computation is relatively low.
 */

  public  static  final long    DEFAULT_MAX_LONG                  =
                                  (Long.MAX_VALUE >>> 9);
/**
 * A convenient maximum value for doubles where the probability of an
 * overflow in an arithmetic computation is relatively low.
 */

  public  static  final double  DEFAULT_MAX_DOUBLE                =
                                  (Math.pow(Double.MAX_VALUE, 0.3d));
  
/**
 * The default simulation count.
 */

  public  static  final int     DEFAULT_SIMULATION_COUNT          = 10;
/**
 * The default count of steps per simulation.
 */

  public  static  final long    DEFAULT_STEPS_PER_SIMULATION      = 100;
/**
 * The default cache size.
 */

  public  static  final int     DEFAULT_CACHE_SIZE                = 128;

/**
 * The maximum cache size depends on the memory available on your system.
 */

  public  static  final int     MAX_CACHE_SIZE                    =
    Math.max(DEFAULT_CACHE_SIZE,
            (int)(Runtime.getRuntime().maxMemory() >> 10));

/**
 * The default maximum creation trials.
 */

  public  static  final int     DEFAULT_MAX_CREATION_TRIALS       = 10240;
/**
 * The default maximum improvement trials.
 */

  public  static  final int     DEFAULT_MAX_IMPROVEMENT_TRIALS    = 1;
/**
 * Default for wether the first improvement should be returned or not.
 */

  public  static  final boolean DEFAULT_RETURN_FIRST_IMPROVEMENT  = true;
/**
 * The default comparator instance.
 */

  public  static  final IndividualComparator  DEFAULT_COMPARATOR  =
                          StaggeredComparator.INSTANCE;
/**
 * The default halt parameter set.
 */

  public  static  final HaltParameters  DEFAULT_HALT_PARAMETERS =
                      NeverHaltParameters.INSTANCE;

/**
 * The default adaptation strategy.
 */

  public  static  final AdaptationStrategy    DEFAULT_ADAPTATION_STRATEGY =
                      NoAdaptation.INSTANCE;

/**
 * Default for wether new individuals must challange their parents to
 * survive.
 */

  public  static  final boolean DEFAULT_CHALLENGE_PARENT          = false;

/**
 * The default port used to exchange search data (individuals). This port
 * is registered at IANA
 * (a href="http://www.iana.org/assignments/port-numbers">http://www.iana.org/assignments/port-numbers</a>),
 * as <code>dgpf-exchg  6785/tcp   DGPF Individual Exchange</code> and
 * <code>dgpf-exchg  6785/udp   DGPF Individual Exchange</code>.
 */

  public  static  final int     DEFAULT_INDIVIDUAL_EXCHANGE_PORT  = 6785;

/**
 * The name of the fitness function count function.
 */

  public  static  final String  FITNESS_FUNCTION_COUNT_FUNC =
                                            "get_fitness_function_count";
/**
 * The fitness prefix.
 */

  public  static  final String    FITNESS_PREFIX = "fitness_"
/**
 * The rel prefix.
 */

  public  static  final String    REL_PREFIX     = "rel_";
/**
 * This property group contains all control properties common to all
 * searches.
 */

  public  static  final PropertyGroup CONTROL_PROPERTIES =
    new PropertyGroup("control");
/**
 * This property group contains all fitness properties.
 */

  public  static  final PropertyGroup FITNESS_PROPERTIES =
    new PropertyGroup("fitness");
/**
 * This property group contains all individual properties.
 */

  public  static  final PropertyGroup INDIVIDUAL_PROPERTIES =
    new PropertyGroup("individual");
/**
 * This property group contains all performance properties.
 */

  public  static  final PropertyGroup PERFORMANCE_PROPERTIES =
    new PropertyGroup("performance");
/**
 * This property group contains all properties concerning the operator
 * statistics.
 */

  public  static  final PropertyGroup STATISTIC_PROPERTIES =
    new PropertyGroup("statistics");
  
/**
 * The property for the mutation statistics.
 */

  public  static  final Property<StatisticInfo>     MUTATION_STAT = 
       new Property<StatisticInfo>("mutation_stat",
                                   SearchUtils.STATISTIC_PROPERTIES,
                                   StatisticInfo.class);
    
/**
 * The property for the new-individual statistics.
 */

  public  static  final Property<StatisticInfo>     NEW_STAT =
       new Property<StatisticInfo>("new_stat",
                                   SearchUtils.STATISTIC_PROPERTIES,
                                   StatisticInfo.class);  
   /**
 * The property for the crossover statistics.
 */

  public  static  final Property<StatisticInfo>     CROSSOVER_STAT =
       new Property<StatisticInfo>("crossover_stat",
                                   SearchUtils.STATISTIC_PROPERTIES,
                                   StatisticInfo.class);
/**
 * The property for the statistics of all individuals.
 */

  public  static  final Property<StatisticInfo>     ALL_STAT =
       new Property<StatisticInfo>("all_stat",
                                   SearchUtils.STATISTIC_PROPERTIES,
                                   StatisticInfo.class);
  
/**
 * The property for the containing the start time.
 */

  public  static  final TimeProperty              START_TIME  =
                new TimeProperty( "start_time",
                                  SearchUtils.PERFORMANCE_PROPERTIES,
                                  0, Long.MAX_VALUE, false);
/**
 * The property for the count of search tasks performed.
 */

  public  static  final BoundedLongProperty       TASK_COUNT =
       new BoundedLongProperty("task_count",
                               SearchUtils.PERFORMANCE_PROPERTIES,
                               0, Long.MAX_VALUE);
/**
 * The property for the containing the total time.
 */

  public  static  final TimeProperty              TOTAL_TIME =
              new TimeProperty( "total_time",
                                 SearchUtils.PERFORMANCE_PROPERTIES,
                                 0, Long.MAX_VALUE, true);
  

  
/**
 * Prevent anyone from instantiating this class.
 */

  private SearchUtils()
    {
    Typesafe.do_not_call();    
    }
  
/**
 * Format a double by ensuring that it is a non-negative number.
 * @param p_double  The double to be formated.
 * @return  <code>p_double</code> if it is a non-negative number,
 *          <code>0.0d</code> otherwise.
 */

  public  static  final double  format_positive (final double p_double)
    {
    return  ((p_double != Double.POSITIVE_INFINITY) &&
             (p_double != Double.NEGATIVE_INFINITY) &&
             (p_double >  0.0d)) ? p_double : 0.0d;
    }

/**
 * This method returns a value proportional to how small the absolute value
 * of the input <code>p_val</code> is.
 * @param p_val   The input value.
 * @return  A value <code>v: 0.0d <= v <= 1.0d</code>, with
 *          <code>p_val == 0 => v = 1.0d</code> and
 *          <code>p_val is infinite => v = 0</code>.
 */

  public  static  final double  evaluate_min  (final double p_val)
    {
    if(Mathematics.is_number(p_val))
      {
      return (1.0d / (1.0d + Math.abs(p_val)));
      }
    return 0.0d;
    }

/**
 * This method returns a value proportional to how big the value
 * of the input <code>p_val</code> is.
 * @param p_val   The input value.
 * @return  A value <code>v: 0.0d <= v <= 1.0d</code>, with
 *          <code>p_val == 0 => v = 0</code> and
 *          <code>p_val is infinite => v = 1.0d</code>.
 */

  public  static  final double  evaluate_max  (final double p_val)
    {
    if(p_val <= 0.0d) return 0.0d;
    return evaluate_min(1.0d / p_val);
    }

/**
 * This method returns a value inverse proportional to the distance of
 * two values.
 * @param p_val_1 The first input value.
 * @param p_val_2 The second input value.
 * @return  A value <code>v: 0.0d <= v <= 1.0d</code>, with
 *          <code>p_val_1 == p_val_2 => v = 1.0d</code> and
 *          <code>p_val_1 is infinite far away from p_val_2 => v = 0</code>.
 */

  public  static  final double  evaluate_distance(final double p_val_1,
                                                  final double p_val_2)
    {
    return evaluate_min(p_val_1 - p_val_2);
    }

/**
 * Compute the simplified logarithm of a number.
 * @param p_val The value to compute the simplified logarithm of.
 * @return  The simplified logarithm of <code>p_val</code>, which is
 *          <code>0.0d for all p_val <= 0.0d, ln(1.0d + p_val) for all
 *                p_val > 0.0d</code>
 */

  public  static  final double  ln  (final double p_val)
    {
    return ((p_val <= 0.0d) ? 0.0d : Math.log(p_val + 1.0d));
    }

/**
 * Obtain the ratio of two real numbers.
 * @param p_d1  The first number.
 * @param p_d2  The second number.
 * @return  <code>p_d1/p_d2</code> if both numbers are != 0,
 *          <code>Double.XXXX_INFINITY</code> if one is == 0 and the other
 *          one not, <code>0.0d</code> otherwise.
 */

  public  static  final double  get_ratio   (final double p_d1,
                                             final double p_d2)
    {
    if(p_d1 != 0.0d)
      {
      if(p_d2 != 0.0d) return (p_d1 / p_d2);
      return Double.POSITIVE_INFINITY;
      }
    if(p_d2 != 0.0d) return Double.NEGATIVE_INFINITY;
    return 1.0d;
    }
  
/**
 * Obtain the realtive difference of two real numbers.
 * @param p_d1  The first number.
 * @param p_d2  The second number.
 * @return  The difference <code>p_d1 - p_d2</code> put in relation to
 *          <code>(p_d1+p_d2)/2</code> using <code>get_ratio</code>. 
 */

  public  static  final double  get_rel_difference(final double p_d1,
                                                   final double p_d2)
    {
    return get_ratio( p_d1 - p_d2, 0.5d * (p_d1 + p_d2) );
    }
  

/**
 * Randomly pick an index. The indexes are weighted by the normalized
 * weights in <code>p_data</code>, with <code>p_data[0]>0</code> and
 * <code>p_data[p_data.length-1]==1</code>.
 * @param p_data          The index weights.
 * @param p_random_value  The random value to use as index.
 * @return A randomly picked index.
 */

  public static final int   random_index    (final double[] p_data,
                                             final double   p_random_value)
    {
    int l_i;

    l_i = Arrays.search(p_data, p_random_value, p_data.length);
    if(l_i < 0) return (-(l_i + 1));
    return l_i;
    }
  

/**
 * Normalize an array of doubles.
 * @param p_data  The array to be normalized.
 */

  public  static  final void  normalize       (final double[] p_data)
    {
    int     l_i, l_l;
    double  l_s;

    l_s = 0.0d;
    l_l = p_data.length;

    for(l_i = 0; l_i < l_l; l_i++)
      {
      l_s        += p_data[l_i];
      p_data[l_i] = l_s;
      }

    l_s = (1.0d / l_s);
    for(l_i = (l_l-2); l_i >= 0; l_i--)
      {
      p_data[l_i] *= l_s;
      }

    p_data[l_l-1] = Double.MAX_VALUE;
    } 
  }

File Information:

file name:SearchUtils.java
package:org.dgpf.search.api
qualified name:org.dgpf.search.api.SearchUtils.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/SearchUtils.java
size:12.856 KB (13165 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-06-07 12:42:16 GMT+0000
last access: 2017-11-24 00:12:24 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:12:24 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo