lio.util
Class Utils

java.lang.Object
  extended by lio.util.Utils

public class Utils
extends java.lang.Object

This class contain some useful methods


Field Summary
static double log2
          The natural logarithm of 2.
 
Constructor Summary
Utils()
           
 
Method Summary
static double binaryToDouble(int[] binChain, double lowerLimit, double upperLimit)
          Returns the double value encoding by the chain of bits passed as parametter given a range of values.
static int[] binaryToGray(int[] binary)
          Converts a number in binary code to gray
static boolean comparePopulations(Population first, Population second)
          Compares two populations.
static double[] generateVector(int pSize, double[] lowerVal, double[] higherVal)
          Generates a vector of doubles.
static double[] generateVector(int pSize, double lowerVal, double higherVal)
          Generates a vector of doubles.
static boolean getFlag(char flag, java.lang.String[] options)
          Checks if the given flag is in the String array.
static java.lang.String getOption(java.lang.String flag, java.lang.String[] options)
          Gets an option indicated by the flag from the given array of strings.
static int[] grayToBinary(int[] gray)
          Converts a number in gray code to binary
static double grayToDouble(int[] grayChain, double lowerLimit, double upperLimit)
          Returns the double value encoding in gray by the chain of bits passed as parametter given a range of values.
static int hammingDistance(BitChain firstInd, BitChain secondInd)
          Returns the hamming distance between two binary individuals.
static double log2(double a)
          Returns the logarithm of a for base 2.
static int maxIndex(Population population)
          Returns index of maximum element in a given array of doubles.
static int minIndex(double[] doubles)
          Returns index of minimum element in a given array of doubles.
static int minIndex(Population population)
          Returns index of minimum element in a given array of doubles.
static void nextBinConfiguration(int[] values)
           
static int[] randomPermutation(int size)
          Returns a random permutation of the specified size.
static int[] sort(double[] array)
          Sorts a given array of doubles in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array.
static int[] sort(int[] array)
          Sorts a given array of integers in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log2

public static double log2
The natural logarithm of 2.

Constructor Detail

Utils

public Utils()
Method Detail

getFlag

public static boolean getFlag(char flag,
                              java.lang.String[] options)
Checks if the given flag is in the String array. If the flag is found, it is replaced with the empty string.

Parameters:
flag - the character indicating the flag.
strings - the array of strings containing all the options.
Returns:
true if the flag was found

getOption

public static java.lang.String getOption(java.lang.String flag,
                                         java.lang.String[] options)
                                  throws java.lang.Exception
Gets an option indicated by the flag from the given array of strings. Replaces flag and option with empty strings.

Parameters:
flag - the string indicating the option.
options - the array of strings containing all the options.
Returns:
the indicated option or an empty string
Throws:
java.lang.Exception - if the option indicated by the flag can't be found

sort

public static int[] sort(double[] array)
Sorts a given array of doubles in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. NOTE THESE CHANGES: the sort is no longer stable and it doesn't use safe floating-point comparisons anymore. Occurrences of Double.NaN are treated as Double.MAX_VALUE

Parameters:
array - this array is not changed by the method!
Returns:
an array of integers with the positions in the sorted array.

sort

public static int[] sort(int[] array)
Sorts a given array of integers in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. The sort is stable. (Equal elements remain in their original order.)

Parameters:
array - this array is not changed by the method!
Returns:
an array of integers with the positions in the sorted array.

minIndex

public static int minIndex(double[] doubles)
Returns index of minimum element in a given array of doubles. First minimum is returned.

Parameters:
doubles - the array of doubles
Returns:
the index of the minimum element

maxIndex

public static int maxIndex(Population population)
Returns index of maximum element in a given array of doubles. First minimum is returned.

Parameters:
doubles - the array of doubles
Returns:
the index of the maximum element

minIndex

public static int minIndex(Population population)
Returns index of minimum element in a given array of doubles. First minimum is returned.

Parameters:
doubles - the array of doubles
Returns:
the index of the minimum element

binaryToDouble

public static double binaryToDouble(int[] binChain,
                                    double lowerLimit,
                                    double upperLimit)
Returns the double value encoding by the chain of bits passed as parametter given a range of values.

Parameters:
binChain - chain of bits.
lowerLimit - lower value that can take the double (represented by all 0's).
upperLimit - highest value that can take the double (represented by all 1's).
Returns:
The double number represented by the chain of bits.

grayToDouble

public static double grayToDouble(int[] grayChain,
                                  double lowerLimit,
                                  double upperLimit)
Returns the double value encoding in gray by the chain of bits passed as parametter given a range of values.

Parameters:
grayChain - chain of bits.
lowerLimit - lower value that can take the double (represented by all 0's).
upperLimit - highest value that can take the double (represented by all 1's).
Returns:
The double number represented by the chain of bits.

grayToBinary

public static int[] grayToBinary(int[] gray)
Converts a number in gray code to binary

Parameters:
gray - Array of bits containing the number in gray.
Returns:
a binary representation of the number

binaryToGray

public static int[] binaryToGray(int[] binary)
Converts a number in binary code to gray

Parameters:
gray - Array of bits containing the binary number.
Returns:
a gray representation of the number

randomPermutation

public static int[] randomPermutation(int size)
Returns a random permutation of the specified size.

Parameters:
size - Size of the permutation.
Returns:
an array of integer

hammingDistance

public static int hammingDistance(BitChain firstInd,
                                  BitChain secondInd)
Returns the hamming distance between two binary individuals.

Parameters:
firstInd - First individual.
secondInd - Second individual.
Returns:
Hamming distance between the two individuals.

comparePopulations

public static boolean comparePopulations(Population first,
                                         Population second)
Compares two populations. They are equal if their sizes and the fitness of the individuals are the same.


generateVector

public static double[] generateVector(int pSize,
                                      double lowerVal,
                                      double higherVal)
Generates a vector of doubles.

Parameters:
pSize - Size of the vector;
lowerVal - Minimum value that can be generated.
higherVal - Maximum value that can be generated. *
Returns:
A vector with size pSize whose values are in [lowerVal,higherVal].

generateVector

public static double[] generateVector(int pSize,
                                      double[] lowerVal,
                                      double[] higherVal)
Generates a vector of doubles.

Parameters:
pSize - Size of the vector;
lowerVal - Minimum value that can be generated.
higherVal - Maximum value that can be generated. *
Returns:
A vector with size pSize whose values are in [lowerVal,higherVal].

nextBinConfiguration

public static void nextBinConfiguration(int[] values)

log2

public static double log2(double a)
Returns the logarithm of a for base 2.

Parameters:
a - a double