• We just launched and are currently in beta. Join us as we build and grow the community.

Genetic Algorithm: Solving the Function Optimization Problem using Java

marcus2039

Freemium Model Innovator
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
56
Likes
192
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this article you will find a description of basic steps of the genetic Algorithm and an example of function's optimization in Java.

There is a variety of problems that can not be solved with a simple algorithms. This problems needs a lot of time and resources to find a solution. Often we don't have a to find an exact solution : we can find a solution that is enough good for the goal.
An example of such a kind of algorithm is Genetic Algorithm. Genetic algorithm is inspired by the process of the evolution in nature. The process of evolution shows that the nature created perfect things from the basic particles. The some thing is done by the genetic algorithm: a good solution can be found without having a lot of knowledge in the problem's area.
The Genetic algorithm consists of these steps:
  1. Creating initial population
  2. Selection of individuals from the population
  3. "Mating"
  4. Crossover
  5. Checking if the solution is found

One of the basic example of the use of the Genetic algorithm is soving the problem of function optimization - finding the minimum or the maximum value of a function on a interval.
I'll explain the Algorithm on this example.
First of all, a population is a set of "individuals", so we need to create individual

class:
  1. package

    optimisation_GA

    ;

  2. public

    class

    individual {
  3. int

    decimal;
  4. String

    binary;
  5. int

    fitnessFunction;
  6. int

    length;

    //length of binary representation
  7. individual(

    int

    value, int

    _length)

    {
  8. decimal =

    value;
  9. length =

    _length;
  10. binary =

    ""

    ;
  11. decToBin(

    decimal)

    ;
  12. addZero(

    )

    ;
  13. }
  14. //makes binary representation of each
  15. //decimal of equal length
  16. private

    void

    addZero(

    )

    {
  17. // TODO Auto-generated method stub
  18. for

    (

    int

    i =

    binary.length

    (

    )

    ;

    i !=

    length;

    ++

    i)
  19. binary =

    "0"

    +

    binary;

  20. }
  21. //convert from decimal number
  22. //to binary number
  23. private

    Object

    decToBin(

    int

    dec)

    {
  24. // TODO Auto-generated method stub
  25. int

    num;

  26. if

    (

    dec <=

    1

    )

    {
  27. binary +=

    dec;
  28. return

    null

    ;

    // KICK OUT OF THE RECURSION
  29. }

  30. num=

    dec %

    2;
  31. decToBin(

    dec >>

    1

    )

    ;
  32. binary +=

    num;
  33. return

    null

    ;
  34. }
  35. public

    individual crossover(

    individual pair)

    {
  36. individual child;
  37. int

    crossoverPoint =

    (

    int

    )

    Math

    .random

    (

    )

    *

    length;
  38. String

    newBinValue =

    ""

    ;
  39. for

    (

    int

    i =

    0

    ;

    i !=

    crossoverPoint;

    ++

    i)
  40. newBinValue +=

    binary.charAt

    (

    i)

    ;
  41. for

    (

    int

    i =

    crossoverPoint;

    i !=

    length;

    ++

    i)
  42. newBinValue +=

    binary.charAt

    (

    i)

    ;
  43. child =

    new

    individual(

    Integer

    .parseInt

    (

    newBinValue,2

    )

    ,length)

    ;
  44. return

    child;
  45. }

  46. }

This has a representation of an integer in decimal and binary form. Binary form is used for crossover operation. The crossover operation consists of dividing an individual's set of chromosomes (binary representation) and changing the part of genetic information between to individuals.
Population is a set of individuals. It can be implemented in next way:
  1. package

    optimisation_GA

    ;

  2. import

    java.util.ArrayList

    ;
  3. import

    java.util.Collections

    ;
  4. import

    java.util.Comparator

    ;

  5. import

    javax.script.ScriptEngine

    ;
  6. import

    javax.script.ScriptEngineManager

    ;
  7. import

    javax.script.ScriptException

    ;
  8. import

    javax.swing.JOptionPane

    ;

  9. public

    class

    population {
  10. int

    a;
  11. int

    b;
  12. int

    numberOfPopulation;
  13. int

    size;
  14. int

    timeLimit;
  15. int

    length;
  16. String

    function;

  17. ArrayList<

    individual>

    individuals;
  18. population(

    String

    _function,int

    _a,int

    _b,int

    _numberOfPopulation,int

    _size,int

    _timeLimit,int

    _length)

    {
  19. individuals =

    new

    ArrayList<

    individual>

    (

    )

    ;
  20. function =

    _function;
  21. a =

    _a;
  22. b =

    _b;
  23. numberOfPopulation =

    _numberOfPopulation;
  24. size =

    _size;
  25. timeLimit =

    _timeLimit;
  26. length =

    _length;
  27. initialPopulation(

    )

    ;
  28. }
  29. private

    void

    initialPopulation(

    )

    {
  30. // TODO Auto-generated method stub
  31. for

    (

    int

    i =

    0

    ;

    i !=

    size;

    ++

    i)

    {
  32. int

    rand =

    a +

    (

    int

    )

    (

    Math

    .random

    (

    )

    *

    (

    (

    b -

    a)

    +

    1

    )

    )

    ;
  33. individuals.add

    (

    new

    individual(

    rand,length)

    )

    ;
  34. }
  35. }
  36. public

    void

    calculateFitnessFunction(

    )

    {
  37. for

    (

    int

    i =

    0

    ;

    i !=

    individuals.size

    (

    )

    ;++

    i)

    {
  38. int

    x =

    individuals.get

    (

    i)

    .decimal

    ;
  39. String

    tempValue =

    function;
  40. String

    replaceSt =

    Integer

    .toString

    (

    x)

    ;

  41. String

    testValue =

    tempValue.replace

    (

    "x"

    , Integer

    .toString

    (

    x)

    )

    ;
  42. ScriptEngineManager mgr =

    new

    ScriptEngineManager(

    )

    ;
  43. ScriptEngine engine =

    mgr.getEngineByName

    (

    "JavaScript"

    )

    ;
  44. try

    {
  45. int

    y =

    (

    (

    Double

    )

    engine.eval

    (

    testValue)

    )

    .intValue

    (

    )

    ;
  46. individuals.get

    (

    i)

    .fitnessFunction

    =

    y;
  47. }

    catch

    (

    ScriptException e1)

    {
  48. // TODO Auto-generated catch block

  49. }
  50. }
  51. }
  52. public

    void

    range(

    )

    {
  53. Collections

    .sort

    (

    individuals, new

    Comparator<

    individual>

    (

    )

    {
  54. public

    int

    compare(

    individual left, individual right)

    {
  55. return

    right.fitnessFunction

    -

    left.fitnessFunction

    ;
  56. }
  57. }

    )

    ;
  58. for

    (

    int

    i =

    0

    ;

    i !=

    individuals.size

    (

    )

    ;++

    i)
  59. System

    .out

    .println

    (

    individuals.get

    (

    i)

    .decimal

    +

    " -- "

    +

    individuals.get

    (

    i)

    .fitnessFunction

    )

    ;
  60. }
  61. public

    void

    selection(

    )

    {
  62. // TODO Auto-generated method stub
  63. if

    (

    individuals.size

    (

    )

    ==

    size)
  64. return

    ;
  65. while

    (

    individuals.size

    (

    )

    >

    size)
  66. individuals.remove

    (

    individuals.size

    (

    )

    -

    1

    )

    ;


  67. }
  68. public

    void

    crossover(

    )

    {
  69. // TODO Auto-generated method stub
  70. for

    (

    int

    i =

    0

    ;

    i !=

    size;++

    i)

    {
  71. individual child =

    individuals.get

    (

    i)

    .crossover

    (

    getRandomIndividual(

    )

    )

    ;
  72. individuals.add

    (

    child)

    ;
  73. }


  74. }
  75. private

    individual getRandomIndividual(

    )

    {
  76. return

    individuals.get

    (

    (

    int

    )

    Math

    .random

    (

    )

    *

    size)

    ;
  77. }
  78. public

    void

    printP(

    )

    {
  79. // TODO Auto-generated method stub
  80. for

    (

    int

    i =

    0

    ;

    i !=

    individuals.size

    (

    )

    ;++

    i)
  81. System

    .out

    .println

    (

    i +

    " "

    +

    individuals.get

    (

    i)

    .decimal

    +

    " -- "

    +

    individuals.get

    (

    i)

    .fitnessFunction

    )

    ;
  82. }
  83. }

These class performs next functions:
  • creates initial population. It's done by generating a number of random individuals in interval of function definition.
  • calculates fitness function. Fitness function is a value, that shows, how is the individual is "accommodated" to the environmental conditions. In this example fitness function is the function that needs to be optimazed
  • ranges individuals of population according to the fitness function value
  • selects the best individuals from population after ranging them by the fitness function.

Now we have all data to implement the Genetic Algorithm. The basic cycle of the algorithm is:
  1. workingPopulation =

    new

    population(

    functionString,a,b,nPopulation,size,time,length)

    ;
  2. int

    count =

    0

    ;
  3. long

    startTime,
  4. endTime,
  5. workTime;
  6. startTime =

    System

    .currentTimeMillis

    (

    )

    /

    1000

    ;
  7. workingPopulation.calculateFitnessFunction

    (

    )

    ;

  8. do

    {
  9. workingPopulation.selection

    (

    )

    ;
  10. System

    .out

    .println

    (

    "Selection passed"

    )

    ;
  11. workingPopulation.crossover

    (

    )

    ;
  12. System

    .out

    .println

    (

    "crossover passed"

    )

    ;
  13. workingPopulation.calculateFitnessFunction

    (

    )

    ;

  14. workingPopulation.range

    (

    )

    ;
  15. count++;
  16. endTime =

    System

    .currentTimeMillis

    (

    )

    /

    1000

    ;
  17. workTime =

    endTime -

    startTime;
  18. System

    .out

    .println

    (

    "-------------------generation #"

    +

    count+

    1

    )

    ;
  19. workingPopulation.printP

    (

    )

    ;

  20. }

    while

    (

    (

    workTime <

    time)

    &&

    (

    count <

    nPopulation)

    )

    ;
  21. workingPopulation.printP

    (

    )

    ;

nPopulation is number of populations, set by user and the workTime is the limit on execution time.
These is a basic example of the usage of Genetic Algorithm, but this algorithm can be applied to the range of problems from ,bioinformatics, , computational science, engineering, economics, chemistry, mathematics, physics and a lot of other areas.
You can download the example of this program and try it to find maximum of any function.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top