Laga Tools

Inheritance Hierarchy
  • Laga.GeneticAlgorithm
    • static class LagaTools
      • BinaryChromosomeToInteger()
      • ExtractDNA()
      • ReversePopulation()
      • Prefix()
      • Parse()
      • Reverse()
      • Fisher_Yates()
      • Fisher_YatesPercent()
      • GetRandomNumber()
      • RandomCharBinary()
      • RandomChar()
      • RandomInt()
      • Mom_Dad()


Methods

BinaryChromosomeToInteger(this T[] chromosome)

  • Description: Convert a binary chromosome to an integer, base of 2.
  • Parameters:
    • T[]: Your chromosome
  • Return: int

Example

    //11111000000
    char[] chr = new char[] {'1','1','1','1','1','0','0','0','0','0','0'};
    int r = LagaTools.BinaryChromosomeToInteger(chr);

    //result
    1984

ExtractDNA(this T[] chromosome, int index, int length)

  • Description: Extract part of the DNA from a chromosome.
  • Parameters:
    • T[]: Any chromosome type
    • index: index number to start the extraction
    • length: length of the extraction
  • Return: T[] A DNA segment from the original chromosome

Example

    char[] chr = new char[] {'a','e','i', 'o', 'u'};
    char[] charSegment = LagaTools.ExtractDNA(chr, 1, 3);

    float[] flt = new float[] { 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.7f};
    float[] fltSegment = LagaTools.ExtractDNA(flt, 0, 3);

    //result
    e, i, o
    0.1, 0.2, 0.3

ReversePopulation(char[][] charPop)

  • Description: Reverse the original Population
  • Parameters:
    • char[][]: Population in char[][] format. (I haven’t implement the interface, because so far, I haven’t need it. but I will do it.)
  • Return: void

Example

    char[][] chrPop = new char[3][];
    chrPop[0] = new char[] { 'a', 'e' };
    chrPop[1] = new char[] { 'i', 'o' };
    chrPop[2] = new char[] { 'u' };

    //results before reverse
    chrPop is: [a, e], [i, o], [u]

    LagaTools.ReversePopulation(chrPop);

    //results after reverse - the same population is reversed!
    chrPop is: [u], [i, o], [a, e]

Prefix(string prefix, int size)

  • Description: Generate prefixs
  • Parameters:
    • string: string prefix
    • int: size the length of the prefix to generate
  • Return: string[]

Example

    string[] strPrefix = LagaTools.Prefix("_Hi: ", 5);

    //result
    "_Hi: 0", "_Hi: 1", "_Hi: 2", "_Hi: 3", "_Hi: 4"

Parse(point[] points, int round)

  • Description: Round the location of a laga point, based on the round number
  • Parameters:
    • point[]: the points to round coordinates
    • int: round number coordinates
  • Return: string[]

Example

    point[] points = new point[3];
    points[0] = new point(0.17452, 0.12315, 0.43423);
    points[1] = new point(0.56752, 0.48579, 0.56273);
    points[2] = new point(0.34745, 0.08343, 0.13459);

    string[] strPtParse = LagaTools.Parse(points, 2);

    //result
    "0.17, 0.12, 0.43", "0.57, 0.49, 0.56", "0.35, 0.08, 0.13"


Parse(any)

  • Description: Parse any Chromosome type to string Array
  • Parameters:
    • double, int, float, char: the list to parse.
  • Return: string[]

Example

    float[] any = new float[] {3.5f, 5.6f, 3.14f, 4.9f};
    string[] strPtParse = LagaTools.Parse(any);

    //result
    "3.5", "5.6", "3.14", "4.9"


Reverse(any)

  • Description: Reverse the original array.
  • Parameters:
    • double, int, float: the array to reverse.
  • Return: void

Example

    float[] any = new float[] {3.5f, 5.6f, 3.14f, 4.9f};
    LagaTools.Reverse(any);

    //result after reverse:
    4.9f, 3.14f, 5.6f, 3.5f
    

I’m thinking to move the following methods to a different class and add another mathematical functions, specially for random numbers, distributions and some useful tools for pre processing data (very useful in Machine Learning). I will keep the documentation updated.

Fisher_Yates(int[] arrInt)

  • Description: Fisher-Yates Shuffle Algorithm for array of integers.
  • Parameters:
    • int[]: The array of integers to shuffle.
  • Return: int[]

Example

    int[] arrSortInt = new int[] {1,2,3,4,5,6,7,8,9,0};
    int[] arrUnsortInt = LagaTools.Fisher_Yates(arrSortInt);

    //result
    8, 0, 4, 6, 7, 5, 9, 3, 2, 1
    

Fisher_Yates(this T[] arrData)

  • Description: Experimental Fisher_Yates algorithm to shuffle the original array.
  • Parameters:
    • T[]: Any array
  • Return: void

Example

    char[] arrChar = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'};

    //result before the Fisher_Yates implementation
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'

    LagaTools.Fisher_Yates(arrChar);

    //result after Fisher_Yates implementation, for the same arrChar.
    'i', 'd', 'f', 'b', 'c', 'j', 'e', 'h', 'a', 'k', 'g'
    


Revised documentation: 2019/01/21