Post on 20-Dec-2015
• A problem is called NP (nondeterministic polynomial) if its solution (if one exists) can be guessed and verified in polynomial time
• Nondeterministic means that no particular rule is followed to make the guess.
Solving NP Complete Problems using GA
• If a problem is NP and all other NP problems are polynomial-time reducible to it, the problem is NP-complete.
• Finding an efficient algorithm for any NP-complete problem implies that an efficient algorithm can be found for all such problems.
Solving NP Complete Problems using GA
• If you could find a way to solve an NP-complete problem quickly, then you could use that algorithm to solve all NP problems quickly.
Solving NP Complete Problems using GA
• Traveling Salesman Problem– A traveling salesman is to visit a number of
cities; how to plan the trip so every city is visited once and just once and the whole trip is as short as possible ?
• Testing every possibility for an N city tour would be N! math additions.
Solving NP Complete Problems using GA
• Implementation Strategy– Two basic steps to solving the traveling
salesman problem using a GA.
Solving NP Complete Problems using GA
• First step– Create a group of many random tours in what
is called a population. • These tours are stored as a sequence of numbers.
• Second step– Randomly select tours for crossover– Crossover creates 2 new solutions (children)
in the hope that they create an even better solution.
Solving NP Complete Problems using GA and FT
• Size of the population may be changed by changing CONST variable (POP_SIZE) in the code
• Maximum distance between two cities may be controlled by changing the value of associated CONST (MAX_DIST).
Implementation
• Take input on the number of cities from the user
• Generate a (symmetric) square distance matrix representing the distance between each city.
Implementation
• Generate the initial population
• Both the distance matrix and the initial population is displayed to the user.
• Run the genetic algorithm
Implementation
/*The general form of a genetic algorithm:t := 0;initialize(P(0));evaluate(P(0));while not done do P'(t) := propotional_selection(P(t)); P'(t) := crossover(P'(t)); P'(t) := mutation(P'(t)); evaluate(P'(t)); P(t + 1) := P'(t); t := t + 1;*/
Implementation
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
const int MAX_DISTANCE = 30;
const int POP_SIZE = 100;
Implementation
– //initiate a symmetric matrix with random distances– short int** generateDistanceMatrix(short int);
– //display the distance matrix– void display(short int** matrix, short int size);
– //free the allocated memory– void deleteMatrix(short int** matrix, short int size);
– //returns a permutation of [1,n]– short int* generateRandomPermutation(short int n,
bool*);
Implementation
– //initialize the population with random tours– short int** initialize(short int cities);
– void displayPopulation(short int**, short int);
– //evaluation function returns the shortest permutation– short int* evaluate(short int** pop, short int**
distances, int n);
– //calculates the length of the supplied tour– int tourLength(short int* tour, short int** distances,
short int n);
Implementation
void displayTour(short int* tour, short int n);
int main(){
//give a seed for random number generationsrand(time(NULL));short int cities;cout << "Enter the number of cities: ";cin >> cities;
//randomly assign distances between citiesshort int** distanceMatrix = generateDistanceMatrix(cities);
cout << "\nThe Distance Matrix:" << endl;display(distanceMatrix, cities);}
Implementation
short int** generateDistanceMatrix(short int cities){
int i, j;
short int** matrix = new short int* [cities];for(i = 0; i < cities; i++)
matrix[i] = new short int[cities];
//assign random weights to upper half and copy to lower halffor(i = 0; i < cities; i++){
for(j = i; j < cities; j++){
matrix[i][j] = rand() % MAX_DISTANCE + 1;matrix[j][i] = matrix[i][j];
}}//distance to itself is 0for(i = 0; i < cities; i++)
matrix[i][i] = 0return matrix;
}
Implementation
void display(short int** matrix, short int size){
int i, j;
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
//for formattingif(matrix[i][j] < 10)
cout << " ";cout << matrix[i][j] << " ";
}
cout << endl;}return;
}
Implementation
void deleteMatrix(short int** matrix, short int size){
for(int i = 0; i < size; i++)delete matrix[i];
delete matrix;
return;}short int** initialize(short int cities){
short int** tour = new short int* [POP_SIZE];bool* node = new bool[cities];
//assign random permutationsfor(int i = 0; i < POP_SIZE; i++)
tour[i] = generateRandomPermutation(cities, node);
delete node;return tour;
}
Implementation
short int* generateRandomPermutation(short int n, bool* node){
short int* permutation = new short int[n];int i;
//flags for the numbers already usedfor(i = 0; i < n; i++)
node[i] = true;
int pointer = 0;int jump = 0;for(i = 0; i < n; i++){
//jump size determined randomlyjump = (rand() % (n - 1));
//find the number not yet used which is at//a distance 'jump' with wrap aroundwhile(jump >= 0){
pointer = (pointer + 1) % n;if(node[pointer])
jump--;
}//assign that number to the position//and mark it 'used' in then odes arraypermutation[i] = pointer + 1;node[pointer] = false;
}return permutation;
}
Implementation
short int* generateRandomPermutation(short int n, bool* node){
short int* permutation = new short int[n];int i;
//flags for the numbers already usedfor(i = 0; i < n; i++)
node[i] = true;
int pointer = 0;int jump = 0;for(i = 0; i < n; i++){
//jump size determined randomlyjump = (rand() % (n - 1));
//find the number not yet used which is at//a distance 'jump' with wrap aroundwhile(jump >= 0){
pointer = (pointer + 1) % n;if(node[pointer])
jump--;
}//assign that number to the position//and mark it 'used' in then odes arraypermutation[i] = pointer + 1;node[pointer] = false;
}return permutation;
}
Implementation
void displayPopulation(short int** tours, short int n){
for(int i = 0; i < POP_SIZE; i++){
for(int j = 0; j < n; j++){
//for formattingif(tours[i][j] < 10)
cout << " ";cout << tours[i][j] << " ";
}cout << endl;
}return;
}
Implementation
short int* evaluate(short int** pop, short int** distances, int n){
//set the best permutation to the first oneshort int* best = pop[0];//then iterate through all of the population//to fnd the shotest tourfor(int i = 0; i < POP_SIZE; i++){
if(tourLength(pop[i], distances, n) < tourLength(best, distances, n))
best = pop[i];}return best;
}
Implementation
//calculate the tour length
int tourLength(short int* best, short int** distances, short int n)
{
int length = 0;
//find the length of the shortest cycle
for(int i = 0; i < n; i++)
length += distances[i][(i+1) % n];
return length;
}
Implementation