SlideShare a Scribd company logo
Passion for Technologyb.knopper@ncim.nl @BWKnopper
EVOLUTIONARY ALGORITHMS
The key to solving complex Java puzzles
JavaOne [BOF2913]
#javaone #EvolutionaryAlgorithms @BWKnopper
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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
Slide 2 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Questions
• A lot of ground to cover in 45 mins.
• Ask questions anytime!
• But don’t take it personal if I have to move on…
• We’ll discuss it over a <insert beverage of choice>!
• If time permits it
• Question round at the end as well
Slide 3 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
What I would like to accomplish…
• Interest
• Understanding
• How & when
• Add to toolbox
Slide 4 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
NASA
• Space Technology 5 mission
• launched March 22, 2006, and completed June 20, 2006
• Three full service 25-kilogram-class spacecraft
Slide 5 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
NASA Continued
• Needs even smaller antenna
• That still functions according to spec
• Need for solution that’s not easy to engineer
• So they used an EA that made these:
Slide 6 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Slide 7 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Recap
• Powerful example
• Evolving object
• First evolved object to fly in space
• How?
Slide 8 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Evolution - “Survival of the fittest”
Finite
Resources
Lifeforms
with a basic
instinct
towards
Reproduction
Natural
Selection
Slide 9 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Recombination
Mutation
Slide 10 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Recombination
Mutation
Slide 11 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
From evolution to problem solvingEnvironment Problem
Individual Candidate Solution
Slide 12 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Puzzle solving time!
• More down to earth example
• Travelling Salesman Problem
Slide 13 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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.000 possible routes!
Brute force might not be the best solution…
Slide 14 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Puzzle solving time!
• More down to earth example
• Travelling Salesman Problem
• Use case to show you
• Evolutionary Algorithm Design
• Plain Java Code
• Demo
Slide 15 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Different EA’s
• Genetic Algorithms (GA’s)
• Evolution Strategies (ES’s)
• Evolutionary Programming (EP)
• Genetic Programming (GP)
• All have the same basis
Slide 16 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 17 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 18 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Candidate Solution - Representation
• n = 6
• And base city 1
• Label cities 1,2,3,4,5,6
• Candidate Solution
• Signifying the route
• for n = 10
1563421
110947356821
Slide 19 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Recombination
Mutation
1653421 1653421
1653421
1653421
1653421
Slide 20 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 21 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 22 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Evaluation Function (Fitness Function)
• Summed distance:
• d1,2 + d2,4 + … + d5,1
• Minimize!
1563421
Slide 23 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 24 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 25 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 26 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 27 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 28 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 29 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Calculates the total distance of the whole route and stores it as this
* candidate solution's fitness
*/
private void calculateFitness() {
/* initialize total distance */
double totalDistance = 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;
}
Slide 30 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 31 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Termination Condition
• EA’s are stochastic
• May never find optimum
• Combination
• Max number of runs (generations)
• Sure to terminate
• Either:
• Fitness threshold
• Fitness improvement remains under a threshold over runs
• Population diversity drops under a certain threshold
Slide 32 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 33 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Parent Selection
• Where x is the number of parents:
• Pick x best
• Random x
• Best x out of random y
• In our example:
• Best x out of random y
Slide 34 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 35 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 36 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 37 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 38 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 39 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 40 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 41 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
private List<CandidateSolution> parentSelection() {
List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population);
List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>();
/* create parent pool */
for(int i = 0; i < parentPoolSize; i++)
{
/* select a random candidate solution from the temp population */
int randomlySelectedIndex = random.nextInt(tempPopulation.size());
CandidateSolution randomSelection = 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 */
return randomCandidates.subList(0, parentSelectionSize);
}
Slide 42 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 43 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Recombination
• In our example:
• half-half does not work
• “Cut-and-crossfill”
1653421
1421
1245631
15421 135421 1635421 1425631
1245631 1245631 1245631
Slide 44 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
public List<CandidateSolution> recombine(CandidateSolution otherParent) {
/* get routes of both parents */
List<City> parentRoute1 = getRoute();
List<City> parentRoute2 = otherParent.getRoute();
/* initialize the routes for the children */
List<City> childRoute1 = new ArrayList<City>();
List<City> childRoute2 = new ArrayList<City>();
/* randomize cutIndex for "cross-and-fill point" */
int cutIndex = 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));
/* perform crossfill for both children */
crossFill(childRoute1, parentRoute2, cutIndex);
crossFill(childRoute2, parentRoute1, cutIndex);
/* create new children using the new children routes */
CandidateSolution child1 = new CandidateSolution(childRoute1);
CandidateSolution child2 = new CandidateSolution(childRoute2);
/* put the children in a list and return it (omitted for layout reasons) */
}
Slide 45 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
public List<CandidateSolution> recombine(CandidateSolution otherParent) {
/* get routes of both parents */
List<City> parentRoute1 = getRoute();
List<City> parentRoute2 = otherParent.getRoute();
/* initialize the routes for the children */
List<City> childRoute1 = new ArrayList<City>();
List<City> childRoute2 = new ArrayList<City>();
/* randomize cutIndex for "cross-and-fill point" */
int cutIndex = 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));
/* perform crossfill for both children */
crossFill(childRoute1, parentRoute2, cutIndex);
crossFill(childRoute2, parentRoute1, cutIndex);
/* create new children using the new children routes */
CandidateSolution child1 = new CandidateSolution(childRoute1);
CandidateSolution child2 = new CandidateSolution(childRoute2);
/* put the children in a list and return it (omitted for layout reasons) */
}
Slide 46 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
public List<CandidateSolution> recombine(CandidateSolution otherParent) {
/* get routes of both parents */
List<City> parentRoute1 = getRoute();
List<City> parentRoute2 = otherParent.getRoute();
/* initialize the routes for the children */
List<City> childRoute1 = new ArrayList<City>();
List<City> childRoute2 = new ArrayList<City>();
/* randomize cutIndex for "cross-and-fill point" */
int cutIndex = 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));
/* perform crossfill for both children */
crossFill(childRoute1, parentRoute2, cutIndex);
crossFill(childRoute2, parentRoute1, cutIndex);
/* create new children using the new children routes */
CandidateSolution child1 = new CandidateSolution(childRoute1);
CandidateSolution child2 = new CandidateSolution(childRoute2);
/* put the children in a list and return it (omitted for layout reasons) */
}
Slide 47 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
public List<CandidateSolution> recombine(CandidateSolution otherParent) {
/* get routes of both parents */
List<City> parentRoute1 = getRoute();
List<City> parentRoute2 = otherParent.getRoute();
/* initialize the routes for the children */
List<City> childRoute1 = new ArrayList<City>();
List<City> childRoute2 = new ArrayList<City>();
/* randomize cutIndex for "cross-and-fill point" */
int cutIndex = 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));
/* perform crossfill for both children */
crossFill(childRoute1, parentRoute2, cutIndex);
crossFill(childRoute2, parentRoute1, cutIndex);
/* create new children using the new children routes */
CandidateSolution child1 = new CandidateSolution(childRoute1);
CandidateSolution child2 = new CandidateSolution(childRoute2);
/* put the children in a list and return it (omitted for layout reasons) */
}
Slide 48 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
public List<CandidateSolution> recombine(CandidateSolution otherParent) {
/* get routes of both parents */
List<City> parentRoute1 = getRoute();
List<City> parentRoute2 = otherParent.getRoute();
/* initialize the routes for the children */
List<City> childRoute1 = new ArrayList<City>();
List<City> childRoute2 = new ArrayList<City>();
/* randomize cutIndex for "cross-and-fill point" */
int cutIndex = 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));
/* perform crossfill for both children */
crossFill(childRoute1, parentRoute2, cutIndex);
crossFill(childRoute2, parentRoute1, cutIndex);
/* create new children using the new children routes */
CandidateSolution child1 = new CandidateSolution(childRoute1);
CandidateSolution child2 = new CandidateSolution(childRoute2);
/* put the children in a list and return it (omitted for layout reasons) */
}
Slide 49 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
public List<CandidateSolution> recombine(CandidateSolution otherParent) {
/* get routes of both parents */
List<City> parentRoute1 = getRoute();
List<City> parentRoute2 = otherParent.getRoute();
/* initialize the routes for the children */
List<City> childRoute1 = new ArrayList<City>();
List<City> childRoute2 = new ArrayList<City>();
/* randomize cutIndex for "cross-and-fill point" */
int cutIndex = 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));
/* perform crossfill for both children */
crossFill(childRoute1, parentRoute2, cutIndex);
crossFill(childRoute2, parentRoute1, cutIndex);
/* create new children using the new children routes */
CandidateSolution child1 = new CandidateSolution(childRoute1);
CandidateSolution child2 = new CandidateSolution(childRoute2);
/* put the children in a list and return it (omitted for layout reasons) */
}
Slide 50 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Check the rest of the route in the crossing parent and add the cities
* that are not yet in the child (in the order of the route of the crossing
* parent)
*/
private void crossFill(List<City> childRoute, List<City> parentRoute,
int cutIndex) {
/* traverse the parent route from the cut index on and add every city not yet in the child to the child */
for (int i = cutIndex; i < parentRoute.size(); i++) {
City nextCityOnRoute = parentRoute.get(i);
if (!childRoute.contains(nextCityOnRoute)) {
childRoute.add(nextCityOnRoute);
}
}
/* traverse the parent route from the start of the route and add every city not yet in the child to the child */
for (int i = 0; i < cutIndex; i++) {
City nextCityOnRoute = parentRoute.get(i);
if (!childRoute.contains(nextCityOnRoute)) {
childRoute.add(nextCityOnRoute);
}
}
}
1425631
1425631
Slide 51 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 52 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
1653421
Mutation
• Change of nr won’t work
• Infeasible candidate solution
• Swap to the rescue!
1653421 1253461
Slide 53 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Mutates the current individual by swapping two random cities in its
* route.
*/
public void mutate() { 1653421
1653421
1253461
Random random = new Random();
/* randomly select two indices in the route */
int indexFirstCity = random.nextInt(route.size());
int indexSecondCity = random.nextInt(route.size());
/* Make sure they are different */
while (indexFirstCity == indexSecondCity) {
indexSecondCity = random.nextInt(route.size());
}
/* retrieve the Cities on the given indices */
City firstCity = route.get(indexFirstCity);
City secondCity = route.get(indexSecondCity);
/* Changer! */
route.set(indexFirstCity, secondCity);
route.set(indexSecondCity, firstCity);
}
Slide 54 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 55 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
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;
}
Slide 56 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Survivor Selection
• Replacement Strategy
• Replace Worst
Slide 57 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
/**
* Selects the survivors by removing the worst candidate
* solutions from the list, so we have the original
* population size again
*/
private void selectSurvivors() {
Collections.sort(population);
population = population.subList(0, populationSize);
}
Slide 58 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Tuning…
• Mutation probability
• Population size
• Nr of offspring
• Termination condition (# runs or fitness)
• Parent selection
• Survival selection
• Initialisation
• Random
Slide 59 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
EA Behavior
Figures from “Introduction to Evolutionary Computing” by A.E. Eiben & J.E. Smith (Springer)
Slide 60 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Demo!
• Used NASA World Wind to map route on 
• https://github.com/ncim-tsp/JavaOneTSP.git
Slide 61 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Java Frameworks & API’s
http://jgap.sourceforge.net/http://watchmaker.uncommons.org
Slide 62 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Java Frameworks & API’s
• ECJ
• http://cs.gmu.edu/~eclab/projects/ecj/
• MOEA Framework
• http://www.moeaframework.org
• JEAF
• https://github.com/GII/JEAF
• …
Slide 63 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
With great power comes great responsibility
• Can I find a solution using a brute-force approach?
• (within a reasonable amount of time)
• Am I facing an optimization or search problem?
• Can I encode a candidate solution to the problem?
• Representation possible?
• Can I determine the fitness of a candidate solution?
Slide 64 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
than a fool can learn from a wise answer”
– Bruce Lee
“A wise man can learn more from a foolish question
Slide 65 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Additional questions?
• Contact me on @BWKnopper
• Google it!
• There’s lots to find…
• Papers
• Demo’s
• Aforementioned Frameworks/API’s
Slide 66 of 68
Passion for Technologyb.knopper@ncim.nl @BWKnopper
Have a great week @JavaOne
That’s all folks!
Thank you.
Slide 67 of 68

More Related Content

Similar to Evolutionary Algorithms: The Key to Solving Complex Java Puzzles

Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-caseToronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
Alexandra N. Martinez
 
Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)
Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)
Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)
Bas W. Knopper
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Brendan Eich
 
AI_Session 3 Problem Solving Agent and searching for solutions.pptx
AI_Session 3 Problem Solving Agent and searching for solutions.pptxAI_Session 3 Problem Solving Agent and searching for solutions.pptx
AI_Session 3 Problem Solving Agent and searching for solutions.pptx
Asst.prof M.Gokilavani
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
robertledwes38
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
D. j Vicky
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
D. j Vicky
 
Greedy Algorithm by Jerin
Greedy Algorithm by JerinGreedy Algorithm by Jerin
Greedy Algorithm by Jerin
Jubaida Quader
 
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScriptBig Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
Igalia
 
Computational thinking with Swift Playgrounds (Future Schools 2017)
Computational thinking with Swift Playgrounds (Future Schools 2017)Computational thinking with Swift Playgrounds (Future Schools 2017)
Computational thinking with Swift Playgrounds (Future Schools 2017)
Daniel Budd
 
Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...
Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...
Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...
Sri Ambati
 
Final Presentation - Edan&Itzik
Final Presentation - Edan&ItzikFinal Presentation - Edan&Itzik
Final Presentation - Edan&Itzikitzik cohen
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
contact2kazi
 
The DEBS Grand Challenge 2018
The DEBS Grand Challenge 2018The DEBS Grand Challenge 2018
The DEBS Grand Challenge 2018
Holistic Benchmarking of Big Linked Data
 
“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...
“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...
“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...
DevClub_lv
 
Lecture1
Lecture1Lecture1
Lecture1
Amisha Dalal
 
SP18 Generative Design - Week 8 - Optimization
SP18 Generative Design - Week 8 - OptimizationSP18 Generative Design - Week 8 - Optimization
SP18 Generative Design - Week 8 - Optimization
Danil Nagy
 
mini project_shortest path visualizer.pptx
mini project_shortest path visualizer.pptxmini project_shortest path visualizer.pptx
mini project_shortest path visualizer.pptx
tusharpawar803067
 
Algorithms - Future Decoded 2016
Algorithms - Future Decoded 2016Algorithms - Future Decoded 2016
Algorithms - Future Decoded 2016
Frank Krueger
 
Report Card making BY Mitul Patel
Report Card making BY Mitul PatelReport Card making BY Mitul Patel
Report Card making BY Mitul Patel
Mitul Patel
 

Similar to Evolutionary Algorithms: The Key to Solving Complex Java Puzzles (20)

Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-caseToronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
 
Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)
Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)
Making darwin proud: Coding Evolutionary Algorithms in Java (JFokus)
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
AI_Session 3 Problem Solving Agent and searching for solutions.pptx
AI_Session 3 Problem Solving Agent and searching for solutions.pptxAI_Session 3 Problem Solving Agent and searching for solutions.pptx
AI_Session 3 Problem Solving Agent and searching for solutions.pptx
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
 
Greedy Algorithm by Jerin
Greedy Algorithm by JerinGreedy Algorithm by Jerin
Greedy Algorithm by Jerin
 
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScriptBig Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
 
Computational thinking with Swift Playgrounds (Future Schools 2017)
Computational thinking with Swift Playgrounds (Future Schools 2017)Computational thinking with Swift Playgrounds (Future Schools 2017)
Computational thinking with Swift Playgrounds (Future Schools 2017)
 
Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...
Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...
Driver vs Driverless AI - Mark Landry, Competitive Data Scientist and Product...
 
Final Presentation - Edan&Itzik
Final Presentation - Edan&ItzikFinal Presentation - Edan&Itzik
Final Presentation - Edan&Itzik
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
The DEBS Grand Challenge 2018
The DEBS Grand Challenge 2018The DEBS Grand Challenge 2018
The DEBS Grand Challenge 2018
 
“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...
“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...
“Accident Reconstruction” by Aleksis Liekna from Scope Technologies at Auto f...
 
Lecture1
Lecture1Lecture1
Lecture1
 
SP18 Generative Design - Week 8 - Optimization
SP18 Generative Design - Week 8 - OptimizationSP18 Generative Design - Week 8 - Optimization
SP18 Generative Design - Week 8 - Optimization
 
mini project_shortest path visualizer.pptx
mini project_shortest path visualizer.pptxmini project_shortest path visualizer.pptx
mini project_shortest path visualizer.pptx
 
Algorithms - Future Decoded 2016
Algorithms - Future Decoded 2016Algorithms - Future Decoded 2016
Algorithms - Future Decoded 2016
 
Report Card making BY Mitul Patel
Report Card making BY Mitul PatelReport Card making BY Mitul Patel
Report Card making BY Mitul Patel
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 

Evolutionary Algorithms: The Key to Solving Complex Java Puzzles

  • 1. Passion for Technologyb.knopper@ncim.nl @BWKnopper EVOLUTIONARY ALGORITHMS The key to solving complex Java puzzles JavaOne [BOF2913] #javaone #EvolutionaryAlgorithms @BWKnopper
  • 2. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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 Slide 2 of 68
  • 3. Passion for Technologyb.knopper@ncim.nl @BWKnopper Questions • A lot of ground to cover in 45 mins. • Ask questions anytime! • But don’t take it personal if I have to move on… • We’ll discuss it over a <insert beverage of choice>! • If time permits it • Question round at the end as well Slide 3 of 68
  • 4. Passion for Technologyb.knopper@ncim.nl @BWKnopper What I would like to accomplish… • Interest • Understanding • How & when • Add to toolbox Slide 4 of 68
  • 5. Passion for Technologyb.knopper@ncim.nl @BWKnopper NASA • Space Technology 5 mission • launched March 22, 2006, and completed June 20, 2006 • Three full service 25-kilogram-class spacecraft Slide 5 of 68
  • 6. Passion for Technologyb.knopper@ncim.nl @BWKnopper NASA Continued • Needs even smaller antenna • That still functions according to spec • Need for solution that’s not easy to engineer • So they used an EA that made these: Slide 6 of 68
  • 7. Passion for Technologyb.knopper@ncim.nl @BWKnopper Slide 7 of 68
  • 8. Passion for Technologyb.knopper@ncim.nl @BWKnopper Recap • Powerful example • Evolving object • First evolved object to fly in space • How? Slide 8 of 68
  • 9. Passion for Technologyb.knopper@ncim.nl @BWKnopper Evolution - “Survival of the fittest” Finite Resources Lifeforms with a basic instinct towards Reproduction Natural Selection Slide 9 of 68
  • 10. Passion for Technologyb.knopper@ncim.nl @BWKnopper Recombination Mutation Slide 10 of 68
  • 11. Passion for Technologyb.knopper@ncim.nl @BWKnopper Recombination Mutation Slide 11 of 68
  • 12. Passion for Technologyb.knopper@ncim.nl @BWKnopper From evolution to problem solvingEnvironment Problem Individual Candidate Solution Slide 12 of 68
  • 13. Passion for Technologyb.knopper@ncim.nl @BWKnopper Puzzle solving time! • More down to earth example • Travelling Salesman Problem Slide 13 of 68
  • 14. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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.000 possible routes! Brute force might not be the best solution… Slide 14 of 68
  • 15. Passion for Technologyb.knopper@ncim.nl @BWKnopper Puzzle solving time! • More down to earth example • Travelling Salesman Problem • Use case to show you • Evolutionary Algorithm Design • Plain Java Code • Demo Slide 15 of 68
  • 16. Passion for Technologyb.knopper@ncim.nl @BWKnopper Different EA’s • Genetic Algorithms (GA’s) • Evolution Strategies (ES’s) • Evolutionary Programming (EP) • Genetic Programming (GP) • All have the same basis Slide 16 of 68
  • 17. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 17 of 68
  • 18. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 18 of 68
  • 19. Passion for Technologyb.knopper@ncim.nl @BWKnopper Candidate Solution - Representation • n = 6 • And base city 1 • Label cities 1,2,3,4,5,6 • Candidate Solution • Signifying the route • for n = 10 1563421 110947356821 Slide 19 of 68
  • 20. Passion for Technologyb.knopper@ncim.nl @BWKnopper Recombination Mutation 1653421 1653421 1653421 1653421 1653421 Slide 20 of 68
  • 21. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 21 of 68
  • 22. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 22 of 68
  • 23. Passion for Technologyb.knopper@ncim.nl @BWKnopper Evaluation Function (Fitness Function) • Summed distance: • d1,2 + d2,4 + … + d5,1 • Minimize! 1563421 Slide 23 of 68
  • 24. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 24 of 68
  • 25. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 25 of 68
  • 26. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 26 of 68
  • 27. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 27 of 68
  • 28. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 28 of 68
  • 29. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 29 of 68
  • 30. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Calculates the total distance of the whole route and stores it as this * candidate solution's fitness */ private void calculateFitness() { /* initialize total distance */ double totalDistance = 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; } Slide 30 of 68
  • 31. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 31 of 68
  • 32. Passion for Technologyb.knopper@ncim.nl @BWKnopper Termination Condition • EA’s are stochastic • May never find optimum • Combination • Max number of runs (generations) • Sure to terminate • Either: • Fitness threshold • Fitness improvement remains under a threshold over runs • Population diversity drops under a certain threshold Slide 32 of 68
  • 33. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 33 of 68
  • 34. Passion for Technologyb.knopper@ncim.nl @BWKnopper Parent Selection • Where x is the number of parents: • Pick x best • Random x • Best x out of random y • In our example: • Best x out of random y Slide 34 of 68
  • 35. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 35 of 68
  • 36. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 36 of 68
  • 37. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 37 of 68
  • 38. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 38 of 68
  • 39. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 39 of 68
  • 40. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 40 of 68
  • 41. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 41 of 68
  • 42. Passion for Technologyb.knopper@ncim.nl @BWKnopper private List<CandidateSolution> parentSelection() { List<CandidateSolution> tempPopulation = new ArrayList<CandidateSolution>(population); List<CandidateSolution> randomCandidates = new ArrayList<CandidateSolution>(); /* create parent pool */ for(int i = 0; i < parentPoolSize; i++) { /* select a random candidate solution from the temp population */ int randomlySelectedIndex = random.nextInt(tempPopulation.size()); CandidateSolution randomSelection = 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 */ return randomCandidates.subList(0, parentSelectionSize); } Slide 42 of 68
  • 43. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 43 of 68
  • 44. Passion for Technologyb.knopper@ncim.nl @BWKnopper Recombination • In our example: • half-half does not work • “Cut-and-crossfill” 1653421 1421 1245631 15421 135421 1635421 1425631 1245631 1245631 1245631 Slide 44 of 68
  • 45. Passion for Technologyb.knopper@ncim.nl @BWKnopper public List<CandidateSolution> recombine(CandidateSolution otherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = new ArrayList<City>(); List<City> childRoute2 = new ArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ int cutIndex = 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)); /* perform crossfill for both children */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = new CandidateSolution(childRoute1); CandidateSolution child2 = new CandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ } Slide 45 of 68
  • 46. Passion for Technologyb.knopper@ncim.nl @BWKnopper public List<CandidateSolution> recombine(CandidateSolution otherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = new ArrayList<City>(); List<City> childRoute2 = new ArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ int cutIndex = 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)); /* perform crossfill for both children */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = new CandidateSolution(childRoute1); CandidateSolution child2 = new CandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ } Slide 46 of 68
  • 47. Passion for Technologyb.knopper@ncim.nl @BWKnopper public List<CandidateSolution> recombine(CandidateSolution otherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = new ArrayList<City>(); List<City> childRoute2 = new ArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ int cutIndex = 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)); /* perform crossfill for both children */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = new CandidateSolution(childRoute1); CandidateSolution child2 = new CandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ } Slide 47 of 68
  • 48. Passion for Technologyb.knopper@ncim.nl @BWKnopper public List<CandidateSolution> recombine(CandidateSolution otherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = new ArrayList<City>(); List<City> childRoute2 = new ArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ int cutIndex = 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)); /* perform crossfill for both children */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = new CandidateSolution(childRoute1); CandidateSolution child2 = new CandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ } Slide 48 of 68
  • 49. Passion for Technologyb.knopper@ncim.nl @BWKnopper public List<CandidateSolution> recombine(CandidateSolution otherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = new ArrayList<City>(); List<City> childRoute2 = new ArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ int cutIndex = 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)); /* perform crossfill for both children */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = new CandidateSolution(childRoute1); CandidateSolution child2 = new CandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ } Slide 49 of 68
  • 50. Passion for Technologyb.knopper@ncim.nl @BWKnopper public List<CandidateSolution> recombine(CandidateSolution otherParent) { /* get routes of both parents */ List<City> parentRoute1 = getRoute(); List<City> parentRoute2 = otherParent.getRoute(); /* initialize the routes for the children */ List<City> childRoute1 = new ArrayList<City>(); List<City> childRoute2 = new ArrayList<City>(); /* randomize cutIndex for "cross-and-fill point" */ int cutIndex = 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)); /* perform crossfill for both children */ crossFill(childRoute1, parentRoute2, cutIndex); crossFill(childRoute2, parentRoute1, cutIndex); /* create new children using the new children routes */ CandidateSolution child1 = new CandidateSolution(childRoute1); CandidateSolution child2 = new CandidateSolution(childRoute2); /* put the children in a list and return it (omitted for layout reasons) */ } Slide 50 of 68
  • 51. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Check the rest of the route in the crossing parent and add the cities * that are not yet in the child (in the order of the route of the crossing * parent) */ private void crossFill(List<City> childRoute, List<City> parentRoute, int cutIndex) { /* traverse the parent route from the cut index on and add every city not yet in the child to the child */ for (int i = cutIndex; i < parentRoute.size(); i++) { City nextCityOnRoute = parentRoute.get(i); if (!childRoute.contains(nextCityOnRoute)) { childRoute.add(nextCityOnRoute); } } /* traverse the parent route from the start of the route and add every city not yet in the child to the child */ for (int i = 0; i < cutIndex; i++) { City nextCityOnRoute = parentRoute.get(i); if (!childRoute.contains(nextCityOnRoute)) { childRoute.add(nextCityOnRoute); } } } 1425631 1425631 Slide 51 of 68
  • 52. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 52 of 68
  • 53. Passion for Technologyb.knopper@ncim.nl @BWKnopper 1653421 Mutation • Change of nr won’t work • Infeasible candidate solution • Swap to the rescue! 1653421 1253461 Slide 53 of 68
  • 54. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Mutates the current individual by swapping two random cities in its * route. */ public void mutate() { 1653421 1653421 1253461 Random random = new Random(); /* randomly select two indices in the route */ int indexFirstCity = random.nextInt(route.size()); int indexSecondCity = random.nextInt(route.size()); /* Make sure they are different */ while (indexFirstCity == indexSecondCity) { indexSecondCity = random.nextInt(route.size()); } /* retrieve the Cities on the given indices */ City firstCity = route.get(indexFirstCity); City secondCity = route.get(indexSecondCity); /* Changer! */ route.set(indexFirstCity, secondCity); route.set(indexSecondCity, firstCity); } Slide 54 of 68
  • 55. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 55 of 68
  • 56. Passion for Technologyb.knopper@ncim.nl @BWKnopper 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; } Slide 56 of 68
  • 57. Passion for Technologyb.knopper@ncim.nl @BWKnopper Survivor Selection • Replacement Strategy • Replace Worst Slide 57 of 68
  • 58. Passion for Technologyb.knopper@ncim.nl @BWKnopper /** * Selects the survivors by removing the worst candidate * solutions from the list, so we have the original * population size again */ private void selectSurvivors() { Collections.sort(population); population = population.subList(0, populationSize); } Slide 58 of 68
  • 59. Passion for Technologyb.knopper@ncim.nl @BWKnopper Tuning… • Mutation probability • Population size • Nr of offspring • Termination condition (# runs or fitness) • Parent selection • Survival selection • Initialisation • Random Slide 59 of 68
  • 60. Passion for Technologyb.knopper@ncim.nl @BWKnopper EA Behavior Figures from “Introduction to Evolutionary Computing” by A.E. Eiben & J.E. Smith (Springer) Slide 60 of 68
  • 61. Passion for Technologyb.knopper@ncim.nl @BWKnopper Demo! • Used NASA World Wind to map route on  • https://github.com/ncim-tsp/JavaOneTSP.git Slide 61 of 68
  • 62. Passion for Technologyb.knopper@ncim.nl @BWKnopper Java Frameworks & API’s http://jgap.sourceforge.net/http://watchmaker.uncommons.org Slide 62 of 68
  • 63. Passion for Technologyb.knopper@ncim.nl @BWKnopper Java Frameworks & API’s • ECJ • http://cs.gmu.edu/~eclab/projects/ecj/ • MOEA Framework • http://www.moeaframework.org • JEAF • https://github.com/GII/JEAF • … Slide 63 of 68
  • 64. Passion for Technologyb.knopper@ncim.nl @BWKnopper With great power comes great responsibility • Can I find a solution using a brute-force approach? • (within a reasonable amount of time) • Am I facing an optimization or search problem? • Can I encode a candidate solution to the problem? • Representation possible? • Can I determine the fitness of a candidate solution? Slide 64 of 68
  • 65. Passion for Technologyb.knopper@ncim.nl @BWKnopper than a fool can learn from a wise answer” – Bruce Lee “A wise man can learn more from a foolish question Slide 65 of 68
  • 66. Passion for Technologyb.knopper@ncim.nl @BWKnopper Additional questions? • Contact me on @BWKnopper • Google it! • There’s lots to find… • Papers • Demo’s • Aforementioned Frameworks/API’s Slide 66 of 68
  • 67. Passion for Technologyb.knopper@ncim.nl @BWKnopper Have a great week @JavaOne That’s all folks! Thank you. Slide 67 of 68