
Custom Data Type Optimization Using
the Genetic Algorithm
This example shows how to use the genetic algorithm to minimize a function using a
custom data type. The genetic algorithm is customized to solve the traveling salesman
problem.
Traveling Salesman Problem.................................................................................................................1
Customizing the Genetic Algorithm for a Custom Data Type....................................................3
Required Functions for the Traveling Salesman Problem............................................................3
Genetic Algorithm Options Setup .......................................................................................................9
Traveling Salesman Problem
The traveling salesman problem is an optimization problem where there is a finite number
of cities, and the cost of travel between each city is known. The goal is to find an ordered
set of all the cities for the salesman to visit such that the cost is minimized. To solve the
traveling salesman problem, we need a list of city locations and distances, or cost, between
each of them.
Our salesman is visiting cities in the United States. The file usborder.mat contains a map
of the United States in the variables x and y, and a geometrically simplified version of the
same map in the variables xx and yy.
load('usborder.mat','x','y','xx','yy');
% plot(x,y,'Color','red'); hold on;
We will generate random locations of cities inside the border of the United States. We can
use the inpolygon function to make sure that all the cities are inside or very close to the
US boundary.
cities = 40;
locations = zeros(cities,2);
n = 1;
while (n <= cities)
xp = rand*1.5;
yp = rand;
if inpolygon(xp,yp,xx,yy)