Genetic Algorithms in Java

Document Sample
Genetic Algorithms in Java Powered By Docstoc
					               Application
Genetic Algorithms in Java




  Théorie et pratique de la programmation
     Prof. Pascal Fua, Michael Calonder
              TSP - a difficult problem
●   TSP: Traveling Salesman
    Problem
●   Given a list of cities
    and their pairwise
    distances, the task is to
    find a shortest possible tour
    that visits each city exactly once
                   Example: TSP
●   Approach 1: Brute force. Evaluate every possible
    solution and pick the best. A solution is defined by a
    permutation of cities, e.g. x={3,8,5,1,7,2,4,6} (N=8).
●   Problem with approach 1: TSP is NP-complete
    N cities → (N‒1)!/2 possible tours
    N=5 → 60 possible tours... OK
    N=10 → 106 possible tours... still OK
    N=20 → 1018 possible tours... ~317'000 years
    N=50 → 1064 possible tours... ~1051 years
●   In practice, heuristics for N up to 105 work ok.
    But: requires a lot of problem knowledge. Similiar/better
    results with Genetic Algorithms: black box optimization.

     (difficult)            Genetic            (sub)optimal
    Optimization           Algorithm             Solution
      Problem
         Genetic Algorithms (GAs)
●   GAs are inspired by Darwinian evolution
●   Idea: Simulate evolution by continously updating a
    population of solutions.
●   To this end, we need to define
     • what “fit” or “capable of survival” means
     • how a solution is encoded
     • how two solutions are recombined
     • how a solution is mutated

●   Then let the population “live” in this pot for many
    generations.
●   Observed effect: The population becomes better and
    better as time goes by. The solutions of the final  
    population are much better than the initial one.
●   Main application: Optimization problems.
                            4
           Genetic Algorithms (GAs)
●   Every solution in the population can be attributed a
    score, or fitness value fi, by some fitness function F.

                                                   Solutions
                                                   xi

Fitness evaluation
 fi = F( ) = F(xi)
                                                   Population




●   Recombination: Two high scoring parent solutions
    are combined into two new offspring solutions.
●   Mutation: Every offspring solution is mutated
    according to some probability.
class TSPParam {
   public static final int N=20;             TSP in Java (Outline)
   private TSPParam() {}
}
                                                  solution = permutation
class TSPSolution {
   private int[] perm;                                       of indices
   public TSPSolution() {}
}
                                                  Functioninterface
class TSPFitFunc implements Function {
                                                  from before
    private TSPFitFunc() {;}

    // returns the travelled distance for the solution
    public static double value(TSPSolution x){
       double sum=0.;
       // sum distances of path defined by x
       return sum;
    }                                             fitness := trajectory
}
                                                             length
public class TSP {
   public static void main(String[] args){
      // generate a set of random solutions
      // repeat
      //    test each solution in the population and rank them
      //    remove some fraction of bad solutions from the population
      //    recombine good solutions to fill gaps
      //    make small changes to some of the solutions
      // until best solution is good enough
   }
}
                                         6
              Applications of GA
Numerous, to name but a few:
 ● Genetic Programming “Programs writing programs”
 ● Multi-objective optimization problems
 ● Code-breaking
 ● RNA structure prediction, Protein folding
 ● Game Theory Equilibrium Resolution
 ● Finding hardware bugs
 ● Timetabling problems
 ● Container loading optimization

Keep in mind: Genetic Algorithms
 ● are still theoretically poorly understood and hence
   there are no guarantees on the quality of the solution
 ● are often less efficient than hand-crafted algorithms
   for a specific problem
 ● often yield surprisingly good results
                           7