1 / 67

Evolutionary Algorithms

JavaOne [BOF2913 ]. The key to solving complex Java puzzles. Evolutionary Algorithms. # javaone # EvolutionaryAlgorithms @ BWKnopper. Let me introduce myself…. Bas W. Knopper Dutch Java Developer NCIM (IT Services Company) 9 years Java exp AI enthousiast

soo
Download Presentation

Evolutionary Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaOne [BOF2913] The key to solving complex Java puzzles Evolutionary Algorithms #javaone #EvolutionaryAlgorithms @BWKnopper

  2. Let me introduce myself… • Bas W. Knopper • Dutch • Java Developer • NCIM (IT Services Company) • 9 years Java exp • AI enthousiast • Soft spot for Evolutionary Algorithms

  3. Questions • A lot of groundto cover in 45 mins. • Askquestionsanytime! • But don’t take it personal if I have to move on… • We’lldiscussit over a <insertbeverage of choice>! • If time permits it • Question round at the end as well

  4. What I would like to accomplish… • Interest • Understanding • How & when • Add to toolbox

  5. NASA • Space Technology 5 mission • launched March 22, 2006, and completed June 20, 2006 • Three full service 25-kilogram-class spacecraft

  6. NASA Continued • Needs even smaller antenna • Thatstillfunctionsaccordingtospec • Needfor solution that’snot easy to engineer • Sotheyusedan EA that made these:

  7. Recap • Powerfulexample • Evolving object • First evolved object tofly in space • How?

  8. Evolution - “Survival of the fittest”

  9. Mutation Recombination

  10. Mutation Recombination

  11. From evolution to problem solving Environment Problem Individual Candidate Solution

  12. Puzzle solving time! • More down to earth example • Travelling Salesman Problem

  13. Travelling Salesman Problem • Given n cities • n = number of cities to visit • Find (optimal) route for visiting all cities • Visit every city only once • Return to origin city • Search space is huge • For 30 cities there are 30!  10^32 possible routes • That’s 100.000.000.000.000.000.000.000.000.000.000possible routes! Brute force mightnotbe the best solution…

  14. Puzzle solving time! • More down to earth example • Travelling Salesman Problem • Use case to show you • Evolutionary Algorithm Design • Plain Java Code • Demo

  15. Different EA’s • Genetic Algorithms (GA’s) • Evolution Strategies (ES’s) • Evolutionary Programming (EP) • Genetic Programming (GP) • All have the same basis

  16. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  17. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  18. Candidate Solution - Representation • n = 6 • And base city 1 • Label cities 1,2,3,4,5,6 • Candidate Solution • Signifying the route • forn = 10

  19. Mutation Recombination

  20. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  21. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  22. Evaluation Function (Fitness Function) • Summeddistance: • d1,2 + d2,4 + … + d5,1 • Minimize!

  23. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  24. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  25. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  26. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  27. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  28. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  29. /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ privatevoidcalculateFitness() { /* initializetotaldistance */ doubletotalDistance = 0; /* * For all Cities in the route (except the last one) get the distance between this * City and the next and add it to the totalDistance */ for (int i = 0; i < route.size() - 1; i++) { City city = route.get(i); City nextCity = route.get(i + 1); totalDistance += city.calculateDistance(nextCity); } /* store totalDistance as this candidate solution's fitness */ this.fitness = totalDistance; }

  30. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  31. Termination Condition • EA’s are stochastic • May never find optimum • Combination • Max number of runs (generations) • Suretoterminate • Either: • Fitness threshold • Fitness improvementremainsunder a threshold over runs • Populationdiversitydropsunder a certainthreshold

  32. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  33. Parent Selection • Wherex is the number of parents: • Pickx best • Random x • Best x out of random y • In ourexample: • Best x out of random y

  34. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  35. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  36. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  37. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  38. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  39. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  40. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  41. private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = newArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = newArrayList<CandidateSolution>(); /* createparent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ intrandomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolutionrandomSelection = tempPopulation.get(randomlySelectedIndex); randomCandidates.add(randomSelection); /* delete the candidate from the temp population, so we can't pick it again */ tempPopulation.remove(randomlySelectedIndex); } /* Sort the population so that the best candidates are up front */ Collections.sort(randomCandidates); /* return a list with size parentSelectionSize with the best CandidateSolutions*/ returnrandomCandidates.subList(0, parentSelectionSize); }

  42. EA Algorithm (Pseudocode) INITIALISE population with random candidate solutions; EVALUATE each candidate; WHILE ( TERMINATION CONDITION is not satisfied ) { 1 SELECT parents; 2 RECOMBINE pairs of parents; 3 MUTATE the resulting offspring; 4 EVALUATE new candidates; 5 SELECT individuals for the next generation; }

  43. Recombination • In ourexample: • half-half does notwork • “Cut-and-crossfill”

  44. public List<CandidateSolution> recombine(CandidateSolutionotherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = newArrayList<City>(); List<City> childRoute2 = newArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ intcutIndex = new Random().nextInt(parentRoute1.size()); /* copy the first part of the parents cut into the children */ childRoute1.addAll(parentRoute1.subList(0, cutIndex)); childRoute2.addAll(parentRoute2.subList(0, cutIndex)); /* performcrossfillforbothchildren */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = newCandidateSolution(childRoute1); CandidateSolution child2 = newCandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ }

  45. public List<CandidateSolution> recombine(CandidateSolutionotherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = newArrayList<City>(); List<City> childRoute2 = newArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ intcutIndex = new Random().nextInt(parentRoute1.size()); /* copy the first part of the parents cut into the children */ childRoute1.addAll(parentRoute1.subList(0, cutIndex)); childRoute2.addAll(parentRoute2.subList(0, cutIndex)); /* performcrossfillforbothchildren */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = newCandidateSolution(childRoute1); CandidateSolution child2 = newCandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ }

  46. public List<CandidateSolution> recombine(CandidateSolutionotherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = newArrayList<City>(); List<City> childRoute2 = newArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ intcutIndex = new Random().nextInt(parentRoute1.size()); /* copy the first part of the parents cut into the children */ childRoute1.addAll(parentRoute1.subList(0, cutIndex)); childRoute2.addAll(parentRoute2.subList(0, cutIndex)); /* performcrossfillforbothchildren */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = newCandidateSolution(childRoute1); CandidateSolution child2 = newCandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ }

  47. public List<CandidateSolution> recombine(CandidateSolutionotherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = newArrayList<City>(); List<City> childRoute2 = newArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ intcutIndex = new Random().nextInt(parentRoute1.size()); /* copy the first part of the parents cut into the children */ childRoute1.addAll(parentRoute1.subList(0, cutIndex)); childRoute2.addAll(parentRoute2.subList(0, cutIndex)); /* performcrossfillforbothchildren */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = newCandidateSolution(childRoute1); CandidateSolution child2 = newCandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ }

  48. public List<CandidateSolution> recombine(CandidateSolutionotherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = newArrayList<City>(); List<City> childRoute2 = newArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ intcutIndex = new Random().nextInt(parentRoute1.size()); /* copy the first part of the parents cut into the children */ childRoute1.addAll(parentRoute1.subList(0, cutIndex)); childRoute2.addAll(parentRoute2.subList(0, cutIndex)); /* performcrossfillforbothchildren */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = newCandidateSolution(childRoute1); CandidateSolution child2 = newCandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ }

  49. public List<CandidateSolution> recombine(CandidateSolutionotherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = newArrayList<City>(); List<City> childRoute2 = newArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ intcutIndex = new Random().nextInt(parentRoute1.size()); /* copy the first part of the parents cut into the children */ childRoute1.addAll(parentRoute1.subList(0, cutIndex)); childRoute2.addAll(parentRoute2.subList(0, cutIndex)); /* performcrossfillforbothchildren */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = newCandidateSolution(childRoute1); CandidateSolution child2 = newCandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ }

More Related