# The Traveling Salesmen Problem

Get the entire book!

In this section you will be introduced to the traveling salesman problem (TSP). The traveling salesman problem is a common choice for algorithms such as the genetic algorithm. The TSP is popular because it is an NP-Hard problem that cannot generally be solved by traditional iterative programming.

The traveling salesman problem will be revisited in Chapter 9, "Simulated Annealing". Chapter 8 shows how find solutions for the TSP using a genetic algorithm. As we will learn in Chapter 9, this is not the only way that this can be done. Of course the ultimate goal of this book is programming neural networks in Java. Though neither chapter 8 nor chapter 9 teach you anything specifically about neural networks, the algorithms of genetic algorithms and simulated annealing will form the foundation for optimization of a neural network. Chapter 11 will show you how simulated annealing and genetic algorithms can be used to optimize the weight matrix of a neural network. Now you will be shown exactly what the traveling salesman problem is.

### Understanding the Traveling Salesman Problem

The traveling salesman problem involves a "traveling salesman" who must visit a certain number of cities. The shortest route that the salesman will travel is what is being sought. The salesman is allowed to begin and end at any city. The only requirement is that the traveling salesmen visit each city once. The salesman may not visit a city more than once.

This may seem like an easy enough task for a normal iterative program. Consider the magnitude with which the number of possible combinations grows as the number of cities increases. If there is one or two cities only one step is required. Three increases this to six. Table 8.1 shows how quickly these numbers grow.

Table 8.1: Number of steps to solve TSP with a conventional program

 Number of Cities Number of Steps 1 1 2 1 3 6 4 24 5 120 6 720 7 5,040 8 40,320 9 362,880 10 3,628,800 11 39,916,800 12 479,001,600 13 6,227,020,800 ... ... 50 3.041 * 10^64

The formula behind the above table is the factorial. The number of cities n is calculated using the factorial operator (!). The factorial of some arbitrary n value is given by n * (n-1) * (n-2) * ... * 3 * 2 * 1. As you can see from the above table, these values become incredibly large when a program must do a "brute force" search. The example program that we will examine in the next section finds a solution to a fifty city problem in a matter of minutes. This is done by using a genetic algorithm, rather than a normal brute-force search.

### Implementing the Traveling Salesmen Problem

So far we have discussed the basic principles of what genetic algorithms are and how they are used. Now it is time to examine a Java example. In this section you will be shown a complete application that is capable of finding solutions to the TSP. As this program is examined you will be shown first how the user interface is constructed, and also how the genetic algorithm itself was implemented. We will begin by introducing the example program and showing you how to use it.

### Using the Traveling Salesmen Program

The traveling salesmen program itself is very easy to use. This program displays the cities, shown as dots and the current best solution. The number of generations and mutation percent are also shown. As the program runs these values are updated. The final output from the program is shown in Figure 8.1.

Figure 8.1: The traveling salesman program

As the program is running you will see white lines change between the green cities. The path currently being displayed is the shortest path in the entire population. As the program runs you will see a path begin to emerge. Running this program on a Pentium 700mghtz machine too approximately two minutes, and found the solution after 453 generations. You may have more or less generations when you execute this program due to some of the random elements of this program, such as mutation and mating parameters.

When the program is nearly done you will notice that new patterns are not introduced, and the program seems to stabilize. Yet you will also notice that additional generations are still being calculated. This is an important part of the genetic algorithm. Knowing when its done! This is not as straightforward as it might seem. You do not know how many steps are required, nor do you know what the shortest distance actually is.

Some criteria must be specified so that the program knows when to stop. This program stops when the optimal solution does not change for 100 generations. Once this has happened the program indicates that it has found a solution after the number of generations that it actually took. Now that you have seen how the GA program works, we will examine how it was constructed. We will begin by examining the user interface.

### Overall Structure

The traveling salesman program uses three Java files. In this section we will examine how the individual classes of the traveling salesman program fits together. Subsequent sections will examine each of these classes in detail. The overall structure of this program is shown as a UML diagram in Figure 8.2.

Figure 8.2: Traveling Salesman Program Structure

It is important to understand the relationship between the individual classes that make up the traveling salesman program. These classes, and their functions, are summarized here.

• TravelingSalesman - This class implements the user interface and performs general initiliazation.
• Map - This class assists the TravelingSalesman class by drawing the map of cities.
• City - This class stores individual city coordinates. This class also contains methods that are used to calculate the distance between cities.
• Chromosome - This class implements the chromosome. This is the most complex class of the program, as it implements most of the functionality of the genetic algorithm.

We will now examine each of these classes in detail. We will begin with the user interface.

### The User Interface

The first part of the program that we will examine is the TravelingSalesman.java file. This file implements the user interface and is responsible for setting up the city locations. This file will be reused considerably in the next chapter. The traveling salesman problem also applies to simulated annealing.

The TravelingSalesman class extends the Java class JFrame. This means that it is a fully functional window. The TravelingSalesman class implements the main application window that the user interacts with. The complete listing for this class is shown in Listing 8.1.

Listing 8.1: The User Interface (TravelingSalesman.java)

```import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class TravelingSalesman
extends JFrame
implements Runnable,ComponentListener {

public static final int CITY_COUNT = 50;
public static final int POPULATION_SIZE = 1000;
public static final double MUTATION_PERCENT = 0.10;
protected int matingPopulationSize = POPULATION_SIZE/2;
protected int favoredPopulationSize = matingPopulationSize/2;
protected Map map = null;
protected JLabel status;
protected int cutLength = CITY_COUNT/5;
protected int generation;

protected boolean started = false;
protected City [] cities;
protected Chromosome [] chromosomes;

public TravelingSalesman() {
setSize(300,300);
setTitle("Traveling Salesman Problem");
}

/**
* Used to add necessary components.
*
* @param e The event.
*/
public void componentShown(ComponentEvent e)
{
getContentPane().setLayout(new BorderLayout());

if ( map == null ) {
map = new Map(this);
status = new JLabel("Starting up");
}
start();

}

/**
*/
public void start() {
// create a random list of cities

cities = new City[TravelingSalesman.CITY_COUNT];
for ( int i=0;i<TravelingSalesman.CITY_COUNT;i++ ) {
cities[i] = new City(
(int)(Math.random()*(getBounds().width-10)),
(int)(Math.random()*(getBounds().height-60)));
}

// create the initial chromosomes

chromosomes = new Chromosome[TravelingSalesman.POPULATION_SIZE];
for ( int i=0;i<TravelingSalesman.POPULATION_SIZE;i++ ) {
chromosomes[i] = new Chromosome(cities);
chromosomes[i].setCut(cutLength);
chromosomes[i].setMutation(TravelingSalesman.MUTATION_PERCENT);
}
Chromosome.sortChromosomes(chromosomes,TravelingSalesman.POPULATION_SIZE);

// start up the background thread
started = true;
map.update(map.getGraphics());

generation = 0;

if ( worker != null )
worker = null;
worker.start();
}

/**
* The main loop for the background thread.
* It is here that most of the work os orchestrated.
*/
public void run() {

double thisCost = 500.0;
double oldCost = 0.0;
double dcost = 500.0;
int countSame = 0;

map.update(map.getGraphics());

while(countSame<100) {

generation++;

int ioffset = matingPopulationSize;
int mutated = 0;

// Mate the chromosomes in the favored population
// with all in the mating population
for ( int i=0;i<favoredPopulationSize;i++ ) {
Chromosome cmother = chromosomes[i];
// Select partner from the mating population
int father = (int) ( 0.999999*Math.random()*(double)matingPopulationSize);
Chromosome cfather = chromosomes[father];

mutated += cmother.mate(cfather,chromosomes[ioffset],chromosomes[ioffset+1]);
ioffset += 2;
}

// The new generation is in the matingPopulation area
// move them to the correct area for sort.
for ( int i=0;i<matingPopulationSize;i++ ) {
chromosomes[i] = chromosomes[i+matingPopulationSize];
chromosomes[i].calculateCost(cities);
}

// Now sort the new mating population
Chromosome.sortChromosomes(chromosomes,matingPopulationSize);

double cost = chromosomes[0].getCost();
dcost = Math.abs(cost-thisCost);
thisCost = cost;
double mutationRate = 100.0 * (double) mutated / (double) matingPopulationSize;

NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMinimumFractionDigits(2);

status.setText("Generation "+generation+" Cost "+(int)thisCost+
" Mutated "+nf.format(mutationRate)+"%");

if ( (int)thisCost == (int)oldCost ) {
countSame++;
} else {
countSame = 0;
oldCost = thisCost;
}
map.update(map.getGraphics());

}
status.setText("Solution found after "+generation+" generations.");
}

/**
* The main method.
*
* @param args Not used
*/
public static void main(String args[])
{
(new TravelingSalesman()).show();
}

}
```

There are two very important methods inside of the TravelingSalesman class. The first is the start method which initializes the cities and other data structures and begins the background thread. Once the background thread begins the run method is called to actually execute the background thread. As is consistent with Java programs the run method makes up the background thread, and once the run method returns the thread ends. The background thread will remain running until a solution is found. We will begin by examining how the start method is constructed.

The first thing that the start method does is creates the list of cities. These cities are assigned random x and y coordinates so that the cities are placed differently each time the program is run. The City class, which will be described later in this section, is used to hold the x and y coordinates of each city. The following lines of code create an array of random cities.

```  cities = new City[TravelingSalesman.CITY_COUNT];
for ( int i=0;i<TravelingSalesman.CITY_COUNT;i++ ) {
cities[i] = new City(
(int)(Math.random()*(getBounds().width-10)),
(int)(Math.random()*(getBounds().height-60)));
}
```

Once the cities have been created the organisms or chromosomes must be created. The POPULATION_SIZE variable defines how many organisms will be created. Each of these chromosomes will be assigned a random path through the cities. Therefore, each of these chromosomes represents a potential path, or solution. Each leg in this journey can be thought of as a gene. Each chromosome is also given a mutation percentage. This defines the percent of the offspring of this chromosome that will undergo mutation. The cut-length determines how many of the genes of a chromosome will be mutated.

```  chromosomes = new Chromosome[TravelingSalesman.POPULATION_SIZE];
for ( int i=0;i<TravelingSalesman.POPULATION_SIZE;i++ ) {
chromosomes[i] = new Chromosome(cities);
chromosomes[i].setCut(cutLength);
chromosomes[i].setMutation(TravelingSalesman.MUTATION_PERCENT);
}
```

Finally, the chromosomes must be sorted. This allows natural selection to occur. Calling the following line of code will sort each of the chromosomes according to their suitability. In the case of the traveling salesman problem, the suitability refers to the length of the path through the cities. The shorter the distance, the more suited the chromosome.

```Chromosome.sortChromosomes(chromosomes,TravelingSalesman.POPULATION_SIZE);
```

Next the program starts up the background thread. This background thread will run until a solution is found.

```// start up the background thread
started = true;
map.update(map.getGraphics());

generation = 0;

if ( worker != null )
worker = null;
worker.start();
}
```

The run method of the TravelingSalesman class implements the background thread. The run method begins by updating the map. The map is what is displayed to the user. This class will be discussed in a subsequent section. The program then begins a while loop that will continue so until the same solution has been found for 100 generations in a row.

```  map.update(map.getGraphics());

while(countSame<100) {

generation++;

int ioffset = matingPopulationSize;
int mutated = 0;
```

The run method begins by mating all of the chromosomes that are in the favored population with partners in the mating population. The favored population is the top 25% and the mating population is the top 50%. A mother and father are selected. The mother comes from the favored population and may chose a father from any of the mating population. The terms mother and father are purely arbitrary. Any organism can play any role.

```   for ( int i=0;i<favoredPopulationSize;i++ ) {
Chromosome cmother = chromosomes[i];
// Select partner from the mating population
int father = (int) ( 0.999999*Math.random()*(double)matingPopulationSize);
Chromosome cfather = chromosomes[father];
```

The actual mating occurs by calling the mate method of the mother organism. This method will be explained in a later section.

```    mutated += cmother.mate(cfather,chromosomes[ioffset],chromosomes[ioffset+1]);
ioffset += 2;
}
```

The newly created children are moved to the bottom 50% of the population. This effectively kills the lower 50% of organisms that are not well suited.

```   for ( int i=0;i<matingPopulationSize;i++ ) {
chromosomes[i] = chromosomes[i+matingPopulationSize];
chromosomes[i].calculateCost(cities);
}
```

Now the new children, which are likely superior to many of the parents, are at the bottom. We must resort the entire list of organisms so that new "classes" may be formed. This is done by using the sortChromosomes method of the Chromosome class.

```   Chromosome.sortChromosomes(chromosomes,matingPopulationSize);
```

Finally, the statistics are calculated and displayed to the user.

```   double cost = chromosomes[0].getCost();
dcost = Math.abs(cost-thisCost);
thisCost = cost;
double mutationRate = 100.0 * (double) mutated / (double) matingPopulationSize;

NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMinimumFractionDigits(2);

status.setText("Generation "+generation+" Cost "+(int)thisCost+
" Mutated "+nf.format(mutationRate)+"%");

if ( (int)thisCost == (int)oldCost ) {
countSame++;
} else {
countSame = 0;
oldCost = thisCost;
}
map.update(map.getGraphics());

}
```

Once the main loop exits, the final solution is displayed.

```  status.setText("Solution found after "+generation+" generations.");
}
```

The TravelingSalesman class makes up most of the user interface. Next we will examine the City class, which stores the coordinates of cities and also calculates the distance between cities.

### Handling Cities

The City class is used to hold the x and y coordinates for each city. The City class is shown in Listing 8.2.

Listing 8.2: Cities (City.java)

```class City {

int xpos;
int ypos;

/**
* Constructor.
*
* @param x The city's x position
* @param y The city's y position.
*/
City(int x, int y) {
xpos = x;
ypos = y;
}

/**
* Return's the city's x position.
*
* @return The city's x position.
*/
int getx() {
return xpos;
}

/**
* Returns the city's y position.
*
* @return The city's y position.
*/
int gety() {
return ypos;
}

/**
* Returns how close the city is to another city.
*
* @param cother The other city.
* @return A distance.
*/
int proximity(City cother) {
return proximity(cother.getx(),cother.gety());
}

/**
* Returns how far this city is from a specific point.
* This method uses the pythagorean theorum to calculate
* the distance.
*
* @param x The x coordinate
* @param y The y coordinate
* @return The distance.
*/
int proximity(int x, int y) {
int xdiff = xpos - x;
int ydiff = ypos - y;
return(int)Math.sqrt( xdiff*xdiff + ydiff*ydiff );
}
}
```

The City class serves mainly as a holder for the x and y coordinates of a city. Collections of these cities will make up the paths that will be represented by the chromosomes. Another useful function of the City class is the proximity method. This method is used to calculate how far the city is from another x and y coordinate or another city. The proximity method works by using the Pythagorean theorem. The Pythagorean thermo specifies that for a right triangle, the sum of the squares of the two legs is equal to the square of the hypotenuse. Now that you have seen how individual cities are stored, we will examine how the output from the program is displayed.

### Display the Map

The Map class is a simple component that can be placed onto a Java window. This class displays the path of cities contained in a chromosome. This class is used to display the path used by the current best chromosome. The Map class is shown in Listing 8.3.

Listing 8.3: Display the Map (Map.java)

```import javax.swing.*;
import java.awt.*;

public class Map extends JPanel {

/**
* The TravelingSalesman object that owns this object.
*/
protected TravelingSalesman owner;

/**
* Constructor.
*
* @param owner The TravelingSalesman object that owns
* this object.
*/
Map(TravelingSalesman owner)
{
this.owner = owner;
}

/**
* Update the graphical display of the map.
*
* @param g The graphics object to use.
*/
public void paint(Graphics g) {
update(g);
}

/**
* Update the graphical display of the map.
*
* @param g The graphics object to use.
*/
public void update(Graphics g) {
int width = getBounds().width;
int height = getBounds().height;

g.setColor(Color.black);
g.fillRect(0,0,width,height);

if ( !owner.started ) return;

g.setColor(Color.green);
for ( int i=0;i<TravelingSalesman.CITY_COUNT;i++ ) {
int xpos = owner.cities[i].getx();
int ypos = owner.cities[i].gety();
g.fillOval(xpos-5,ypos-5,10,10);
}

g.setColor(Color.white);
for ( int i=0;i<TravelingSalesman.CITY_COUNT;i++ ) {
int icity = owner.chromosomes[0].getCity(i);
if ( i!=0 ) {
int last = owner.chromosomes[0].getCity(i-1);
g.drawLine(
owner.cities[icity].getx(),
owner.cities[icity].gety(),
owner.cities[last].getx(),
owner.cities[last].gety());
}
}
}
}
```

Most of the work performed by this class is done by the paint method. This method is called by the TravelingSalesman class to display the list in a graphical form. The paint method is fairly simple. The paint method loops through the list of cities for the best chromosome and displays each city as a dot, and paths as lines connecting the dots.

Now that you understand how the user interface works with this program we will examine the actual genetic algorithm. The genetic algorithm is implemented primarily in the Chromosome class. The Chromosome class will be examined in the next section.

### Implementing the Chromosomes

?

The Chromosome class implements most of the genetic algorithm. The this class is responsible for mating the chromosomes, performing mutations and sorting a list of chromosomes, by their suitability. Listing 8.3 shows the Chromosome class.

Listing 8.3: Display the Chromosomes (Chromosome.java)

```class Chromosome {

protected int [] cityList;
protected double cost;
protected double mutationPercent;
protected int cutLength;

Chromosome(City [] cities) {
boolean taken[] = new boolean[cities.length];
cityList = new int[cities.length];
cost = 0.0;
for ( int i=0;i<cityList.length;i++ ) taken[i] = false;
for ( int i=0;i<cityList.length-1;i++ ) {
int icandidate;
do {
icandidate = (int) ( 0.999999* Math.random() *
(double) cityList.length );
} while ( taken[icandidate] );
cityList[i] = icandidate;
taken[icandidate] = true;
if ( i == cityList.length-2 ) {
icandidate = 0;
while ( taken[icandidate] ) icandidate++;
cityList[i+1] = icandidate;
}
}
calculateCost(cities);
cutLength = 1;
}

void calculateCost(City [] cities) {
cost=0;
for ( int i=0;i<cityList.length-1;i++ ) {
double dist = cities[cityList[i]].proximity(cities[cityList[i+1]]);
cost += dist;
}
}

double getCost() {
return cost;
}

int getCity(int i) {
return cityList[i];
}

void setCities(int [] list) {
for ( int i=0;i<cityList.length;i++ ) {
cityList[i] = list[i];
}
}

void setCity(int index, int value) {
cityList[index] = value;
}

void setCut(int cut) {
cutLength = cut;
}

void setMutation(double prob) {
mutationPercent = prob;
}

int mate(Chromosome father, Chromosome offspring1, Chromosome offspring2) {
int cutpoint1 = (int) (0.999999*Math.random()*(double)(cityList.length-cutLength));
int cutpoint2 = cutpoint1 + cutLength;

boolean taken1 [] = new boolean[cityList.length];
boolean taken2 [] = new boolean[cityList.length];
int off1 [] = new int[cityList.length];
int off2 [] = new int[cityList.length];

for ( int i=0;i<cityList.length;i++ ) {
taken1[i] = false;
taken2[i] = false;
}

for ( int i=0;i<cityList.length;i++ ) {
if ( i<cutpoint1 || i>= cutpoint2 ) {
off1[i] = -1;
off2[i] = -1;
} else {
int imother = cityList[i];
int ifather = father.getCity(i);
off1[i] = ifather;
off2[i] = imother;
taken1[ifather] = true;
taken2[imother] = true;
}
}

for ( int i=0;i<cutpoint1;i++ ) {
if ( off1[i] == -1 ) {
for ( int j=0;j<cityList.length;j++ ) {
int imother = cityList[j];
if ( !taken1[imother] ) {
off1[i] = imother;
taken1[imother] = true;
break;
}
}
}
if ( off2[i] == -1 ) {
for ( int j=0;j<cityList.length;j++ ) {
int ifather = father.getCity(j);
if ( !taken2[ifather] ) {
off2[i] = ifather;
taken2[ifather] = true;
break;
}
}
}
}
for ( int i=cityList.length-1;i>=cutpoint2;i-- ) {
if ( off1[i] == -1 ) {
for ( int j=cityList.length-1;j>=0;j-- ) {
int imother = cityList[j];
if ( !taken1[imother] ) {
off1[i] = imother;
taken1[imother] = true;
break;
}
}
}
if ( off2[i] == -1 ) {
for ( int j=cityList.length-1;j>=0;j-- ) {
int ifather = father.getCity(j);
if ( !taken2[ifather] ) {
off2[i] = ifather;
taken2[ifather] = true;
break;
}
}
}
}

offspring1.setCities(off1);
offspring2.setCities(off2);

int mutate = 0;
if ( Math.random() < mutationPercent ) {
int iswap1 = (int) (0.999999*Math.random()*(double)(cityList.length));
int iswap2 = (int) (0.999999*Math.random()*(double)cityList.length);
int i = off1[iswap1];
off1[iswap1] = off1[iswap2];
off1[iswap2] = i;
mutate++;
}
if ( Math.random() < mutationPercent ) {
int iswap1 = (int) (0.999999*Math.random()*(double)(cityList.length));
int iswap2 = (int) (0.999999*Math.random()*(double)cityList.length);
int i = off2[iswap1];
off2[iswap1] = off2[iswap2];
off2[iswap2] = i;
mutate++;
}
return mutate;
}

public static void sortChromosomes(Chromosome chromosomes[],int num) {
Chromosome ctemp;
boolean swapped = true;
while ( swapped ) {
swapped = false;
for ( int i=0;i<num-1;i++ ) {
if ( chromosomes[i].getCost() > chromosomes[i+1].getCost() ) {
ctemp = chromosomes[i];
chromosomes[i] = chromosomes[i+1];
chromosomes[i+1] = ctemp;
swapped = true;
}
}
}
}
}
```

The most important method provided by the Chromosome class is the mate method. This method will cause a father, which is passed in as a parameter, to mate with the mother class. We will now examine this method and see the algorithm used to combine the genetic material from two chromosomes to produce two offspring.

First a random cut point is chosen that will determine what percent of the genes, which are legs in the journey from city to city, will be taken from each parent.

```  int cutpoint1 = (int) (0.999999*Math.random()*(double)(cityList.length-cutLength));
int cutpoint2 = cutpoint1 + cutLength;
```

Next two boolean arrays are setup so that we can remember which genes were taken from each parent. This array is initialized to false.

```  boolean taken1 [] = new boolean[cityList.length];
boolean taken2 [] = new boolean[cityList.length];
int off1 [] = new int[cityList.length];
int off2 [] = new int[cityList.length];

for ( int i=0;i<cityList.length;i++ ) {
taken1[i] = false;
taken2[i] = false;
}
```

The mating process is a relatively straightforward process. The mother and father chromosomes are both "cut" at two points. This divides each into three sections. These three sections are then selectively spliced back together to form two offspring. First we will handle the middle component. The following lines of code do this.

```  for ( int i=0;i<cityList.length;i++ ) {
if ( i<cutpoint1 || i>= cutpoint2 ) {
off1[i] = -1;
off2[i] = -1;
} else {
int imother = cityList[i];
int ifather = father.getCity(i);
off1[i] = ifather;
off2[i] = imother;
taken1[ifather] = true;
taken2[imother] = true;
}
}
```

The middle component will be taken from the father for child 1 and the mother for child two. Each of the genes used for this section are marked as taken. Next we will loop through the left section of the chromosome. The following lines of code do this.

```  for ( int i=0;i<cutpoint1;i++ ) {
if ( off1[i] == -1 ) {
for ( int j=0;j<cityList.length;j++ ) {
int imother = cityList[j];
if ( !taken1[imother] ) {
off1[i] = imother;
taken1[imother] = true;
break;
}
}
}
if ( off2[i] == -1 ) {
for ( int j=0;j<cityList.length;j++ ) {
int ifather = father.getCity(j);
if ( !taken2[ifather] ) {
off2[i] = ifather;
taken2[ifather] = true;
break;
}
}
}
}
```

For the left section child 1 will take available genes from the mother and child 2 will take available genes from the father. Finally, the program must handle the right section. The right section works similar to the left section. The main difference is child 1 will take available genes from the father and child 2 will take available genes from the mother. The following lines of code do this.

```  for ( int i=cityList.length-1;i>=cutpoint2;i-- ) {
if ( off1[i] == -1 ) {
for ( int j=cityList.length-1;j>=0;j-- ) {
int imother = cityList[j];
if ( !taken1[imother] ) {
off1[i] = imother;
taken1[imother] = true;
break;
}
}
}
if ( off2[i] == -1 ) {
for ( int j=cityList.length-1;j>=0;j-- ) {
int ifather = father.getCity(j);
if ( !taken2[ifather] ) {
off2[i] = ifather;
taken2[ifather] = true;
break;
}
}
}
}
```

The two new paths are copied to the new children. The following lines of code does this.

```  offspring1.setCities(off1);
offspring2.setCities(off2);
```

Now that the parents have successfully created two offspring we may want to mutate these children slightly. The following lines of code handles the mutation process.

```
int mutate = 0;
if ( Math.random() < mutationPercent ) {
int iswap1 = (int) (0.999999*Math.random()*(double)(cityList.length));
int iswap2 = (int) (0.999999*Math.random()*(double)cityList.length);
int i = off1[iswap1];
off1[iswap1] = off1[iswap2];
off1[iswap2] = i;
mutate++;
}
if ( Math.random() < mutationPercent ) {
int iswap1 = (int) (0.999999*Math.random()*(double)(cityList.length));
int iswap2 = (int) (0.999999*Math.random()*(double)cityList.length);
int i = off2[iswap1];
off2[iswap1] = off2[iswap2];
off2[iswap2] = i;
mutate++;
}
```

The mutation process is completely random. Two random genes are chosen, and then swapped as part of the mutation process. The randomness is mutation is useful to counter the very methodical mating process that is used.

The other major component of the Chromosome class is the sortChromosomes method. This method is used to sort the chromosomes according to their suitability to the problem. This allows the less desirables to be kept to the bottom of the list. The top of this list will make up the mating and preferred classes.

You have now seen a simple application of genetic algorithms. If you consider the traveling salesmen problem you will see that it is actually very similar to a neural network. There are just too many combinations to try every path in the Traveling Salesman Problem. Likewise, in a neural network, there are just too many synaptic connections to track all of them. To optimize the neural network weight matrix, we can also apply a genetic algorithm.

Calais Document Category:
Position:
Technology:

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer