Implement a genetic algorithm to find a solution to the Traveling Salesman problem for the following distance matrix: In the above distance matrix, each city is labeled 1, 2, 3, ..., 9 along the top...

1 answer below »
AI - Genetic Algorithm for the Traveling Salesman Problem


Implement a genetic algorithm to find a solution to the Traveling Salesman problem for the following distance matrix: In the above distance matrix, each city is labeled 1, 2, 3, ..., 9 along the top row and the left column. The distance between city #4 and city #2 is 10 units. Your solution should specify the order of the cities and the distance of the trip. For example, you might get something like: Starting at city 9: Order: 9 -> 2 -> 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 9 to 2 is 17 units 2 to 1 is 2 units 1 to 3 is 11 units 3 to 4 is 5 units https://en.wikipedia.org/wiki/Travelling_salesman_problem https://en.wikipedia.org/wiki/Distance_matrix 4 to 5 is 6 units 5 to 6 is 12 units 6 to 7 is 19 units 7 to 8 is 21 units 8 to 9 is 6 units For a total distance of 99 units. Which is not an optimal solution. A better solution would be something like: Starting at city 1: Order: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 1 1 to 2 is 2 units 2 to 3 is 13 units 3 to 4 is 5 units 4 to 5 is 6 units 5 to 6 is 12 units 6 to 7 is 19 units 7 to 8 is 21 units 8 to 9 is 6 units 9 to 1 is 5 units for a total distance of 89 units. You want your algorithm to find the best combination of city orders to give you the lowest distance traveled possible. In comments in your program, identify your fitness function, mutation function, encoding scheme. It must have some manifestation of crossover, mutation, fitness test, and generational reproduction. To make it easy to identify and organize, I'd place the functionality for each of these in their own functions. Run your algorithm at least ten times, experiment with a different initial population size each time, different number of generations, and report your best solution given your chosen population size and number of generations. Hints: • Here is a tutorial that you may find helpful: tutorial.ppt . • Here is a paper written on the subject. • Don't forget, you are judging fitness by distance and trying to get to a global minimum. • An example crossover function could pick a subset of cities and copy that over, filling in the rest of the set in the same order, filling in the gaps. Example: Parents: 8 6 7 5 3 0 9 1 2 4 ( the subset is underlined ) 0 1 2 3 4 5 6 7 8 9 Child: 1 2 4 5 3 0 9 6 8 7 https://bb.csueastbay.edu/bbcswebdav/pid-7053183-dt-content-rid-70001720_1/xid-70001720_1 https://www.hindawi.com/journals/cin/2017/7430125/
Answered 1 days AfterMar 31, 2021

Answer To: Implement a genetic algorithm to find a solution to the Traveling Salesman problem for the following...

Shashi Kant answered on Apr 01 2021
149 Votes
import java.util.List;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class GeneticAlgorithm extends JFrame {
Random rnd = new Random(1);
int n = rnd.nextInt(300) + 250;
int generation;
double[] x = new double[n];
double[] y = new double[n];
int[] bestCity;
{
for (int i = 0; i < n; i++) {
x[i] = rnd.nextDouble();
y[i] = rnd.nextDouble();
}
}
public void geneticAlgorithm() {
bestCity = new int[n];
for (int i = 0; i < n; i++)
bestCity[i] = i;
final int populationlim = 100;
final Population population = new Population(populationlim);
final int n = x.length;
for (int i = 0; i < populationlim; i++)
population.chromosomes.add(new Chromosome(optimize(getRandomPermutation(n))));
final double mutationRate = 0.3;
final int generations = 10_000;
for (generation = 0; generation < generations; generation++) {
int i = 0;
while (population.chromosomes.size() < population.populationlim) {
int i1 = rnd.nextInt(population.chromosomes.size());
int i2 = (i1 + 1 + rnd.nextInt(population.chromosomes.size() - 1)) % population.chromosomes.size();
Chromosome parent1 = population.chromosomes.get(i1);
Chromosome parent2 = population.chromosomes.get(i2);
int[][] pair = crossOver(parent1.p, parent2.p);
if (rnd.nextDouble() < mutationRate) {
mutate(pair[0]);
mutate(pair[1]);
}
population.chromosomes.add(new Chromosome(optimize(pair[0])));
population.chromosomes.add(new Chromosome(optimize(pair[1])));
}
population.nextGeneration();
bestCity = population.chromosomes.get(0).p;
repaint();
}
}
int[][] crossOver(int[] p1, int[] p2) {
int n = p1.length;
int i1 = rnd.nextInt(n);
int i2 = (i1 + 1 + rnd.nextInt(n - 1)) % n;
int[] n1 = p1.clone();
int[] n2 = p2.clone();
boolean[] used1 = new boolean[n];
boolean[] used2 =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here