Beruflich Dokumente
Kultur Dokumente
6/
examples.salesman
Class TravellingSalesman
java.lang.Object org.jgap.impl.salesman.Salesman examples.salesman.TravellingSalesman
Explains how to use JGAP extensions, needed to solve the task group, known as the Problem of the travelling salesman. The extensions are defined in org.jgap.impl.salesman
The traveling salesman problem is the following: given a finite number of 'cities' along with the cost of travel between each pair of them, find the cheapest way of visiting all the cities and returning to your starting point.
Also see
J. Grefenstette, R. Gopal, R. Rosmaita, and D. Gucht. Genetic algorithms for the traveling salesman problem. In Proceedings of the Second International Conference on Genetice Algorithms. Lawrence Eribaum Associates, Mahwah, NJ, 1985. Sushil J. Louis & Gong Li (very clear explanatory material) Travelling salesman web site
This simple test and example shows how to use the Salesman class. The distance between the cities is assumed to be equal to the difference of the assigned numbers. So, the optimal solution is obviously 1 2 3 4 ... n or reverse, but the system does not know this. To get the useful application, you need to override at least the distance function. Also, it is recommended to define a new type of gene, corresponding the data about your "city". For example, it can contain the city X and Y co-ordinates, used by the distance function. Since: 2.0 See Also: Serialized Form
Field Summary
static int static int[][]
Constructor Summary
TravellingSalesman()
Method Summary
IChromosome createSampleChromosome(java.lang.Object a_initial_data) Create an array of the given number of integer genes.
double
distance(Gene a_from, Gene a_to) Distance is equal to the difference between city numbers, except the distance between the last and first cities that is equal to 1. main(java.lang.String[] args) Solve a sample task with the number of cities, defined in a CITIES constant.
static void
Methods inherited from class org.jgap.impl.salesman.Salesman createConfiguration, createFitnessFunction, findOptimalPath, getConfiguration, getMaxEvolution, getPopulationSize, getStartOffset, setMaxEvolution, setPopulationSize, setStartOffset Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Field Detail
CITIES
public static final int CITIES
CITYARRAY
public static final int[][] CITYARRAY
Constructor Detail
TravellingSalesman
public TravellingSalesman()
Method Detail
createSampleChromosome
public IChromosome createSampleChromosome(java.lang.Object a_initial_data)
Create an array of the given number of integer genes. The first gene is always 0, this is the city where the salesman starts the journey. Specified by: createSampleChromosome in class Salesman Parameters: a_initial_data - ignored Returns: Chromosome Since: 2.0
distance
public double distance(Gene a_from, Gene a_to)
Distance is equal to the difference between city numbers, except the distance between the last and first cities that is equal to 1. In this way, we ensure that the optimal solution is 0 1 2 3 .. n easy to check. Specified by: distance in class Salesman Parameters: a_from - first gene, representing a city a_to - second gene, representing a city Returns: the distance between two cities represented as genes Since: 2.0
main
public static void main(java.lang.String[] args)
Solve a sample task with the number of cities, defined in a CITIES constant. Print the known optimal way, sample chromosome and found solution. Parameters: args - not relevant here Since: 2.0
++++++++++++++++++++++++
org.jgap.impl.salesman
Class Salesman
java.lang.Object org.jgap.impl.salesman.Salesman
The class solves the travelling salesman problem. The traveling salesman problem, or TSP for short, is this: given a finite number of 'cities' along with the cost of travel between each pair of them, find the cheapest way of visiting all the cities and returning to your starting point.) Since: 2.0 See Also:
J. Grefenstette, R. Gopal, R. Rosmaita, and D. Gucht. Genetic algorithms for the traveling salesman problem. In Proceedings of the Second International Conference on Genetic Algorithms. Lawrence Eribaum Associates, Mahwah, NJ, 1985. Sushil J. Louis & Gong Li (explanatory material) TPS web site
, Serialized Form
Constructor Summary
Salesman()
Method Summary
Configuration createConfiguration(java.lang.Object a_initial_data) Create a configuration. FitnessFunction createFitnessFunction(java.lang.Object a_initial_data) Return the fitness function to use.
abstract
IChromosome createSampleChromosome(java.lang.Object a_initial_data) Override this method to create a single sample chromosome, representing a list of "cities".
abstract
double
distance(Gene a_from, Gene a_to) Override this method to compute the distance between "cities", indicated by these two given genes.
IChromosome findOptimalPath(java.lang.Object a_initial_data) Executes the genetic algorithm to determine the optimal path between the cities. Configuration getConfiguration()
int int int
getMaxEvolution() getPopulationSize() getStartOffset() Gets a number of genes at the start of chromosome, that are excluded from the swapping. setMaxEvolution(int a_maxEvolution) Set the maximal number of iterations for population to evolve (default 512). setPopulationSize(int a_populationSize) Set an population size for this solution (default 512) setStartOffset(int a_offset) Sets a number of genes at the start of chromosome, that are excluded from the swapping.
void
void void
Constructor Detail
Salesman
public Salesman()
Method Detail
distance
public abstract double distance(Gene a_from, Gene a_to)
Override this method to compute the distance between "cities", indicated by these two given genes. The algorithm is not dependent on the used type of genes. Parameters:
a_from - first gene, representing a city a_to - second gene, representing a city
Returns: the distance between two cities represented as genes Since: 2.0
createSampleChromosome
public abstract IChromosome createSampleChromosome(java.lang.Object a_initial_data)
Override this method to create a single sample chromosome, representing a list of "cities". Each gene corresponds a single "city" and can appear only once. By default, the first gene corresponds a "city" where the salesman starts the journey. It never changes its position. This can be changed by setting other start offset with setStartOffset( ). Other genes will be shuffled to create the initial random population. Parameters: a_initial_data - the same object as was passed to findOptimalPath. It can be used to specify the task more precisely if the class is used for solving multiple tasks Returns: a sample chromosome Since: 2.0
createFitnessFunction
public FitnessFunction createFitnessFunction(java.lang.Object a_initial_data)
Return the fitness function to use. Parameters: a_initial_data - the same object as was passed to findOptimalPath. It can be used to specify the task more precisely if the class is used for solving multiple tasks Returns: an applicable fitness function Since: 2.0
createConfiguration
public Configuration createConfiguration(java.lang.Object a_initial_data) throws InvalidConfigurationException
Create a configuration. The configuration should not contain operators for odrinary crossover and mutations, as they make chromosoms invalid in this task. The special operators SwappingMutationOperator and GreedyCrossober should be used instead. Parameters: a_initial_data - the same object as was passed to findOptimalPath. It can be used to specify the task more precisely if the class is used for solving multiple tasks Returns:
getMaxEvolution
public int getMaxEvolution()
setMaxEvolution
public void setMaxEvolution(int a_maxEvolution)
Set the maximal number of iterations for population to evolve (default 512). Parameters: a_maxEvolution - sic Since: 2.0
getPopulationSize
public int getPopulationSize()
setPopulationSize
public void setPopulationSize(int a_populationSize)
Set an population size for this solution (default 512) Parameters: a_populationSize - sic Since: 2.0
findOptimalPath
public IChromosome findOptimalPath(java.lang.Object a_initial_data) throws java.lang.Exception
Executes the genetic algorithm to determine the optimal path between the cities. Parameters: a_initial_data - can be a record with fields, specifying the task more precisely if the class is used to solve multiple tasks. It is passed to createFitnessFunction, createSampleChromosome and createConfiguration Returns: chromosome representing the optimal path between cities Throws:
java.lang.Exception
Since: 2.0
setStartOffset
public void setStartOffset(int a_offset)
Sets a number of genes at the start of chromosome, that are excluded from the swapping. In the Salesman task, the first city in the list should (where the salesman leaves from) probably should not change as it is part of the list. The default value is 1. Parameters: a_offset - start offset for chromosome Since: 2.0
getStartOffset
public int getStartOffset()
Gets a number of genes at the start of chromosome, that are excluded from the swapping. In the Salesman task, the first city in the list should (where the salesman leaves from) probably should not change as it is part of the list. The default value is 1. Returns: start offset for chromosome Since: 2.0
getConfiguration
public Configuration getConfiguration()