210 likes | 380 Views
Solving NP Complete Problems using GA. Mubasher Baig Asif Jamshed Umar Faiz. Solving NP Complete Problems using GA. Two basic steps to solving the traveling salesman problem using a GA. Solving NP Complete Problems using GA and FT. First step
E N D
Solving NP Complete Problems using GA Mubasher Baig Asif Jamshed Umar Faiz
Solving NP Complete Problems using GA • Two basic steps to solving the traveling salesman problem using a GA.
Solving NP Complete Problems using GA and FT • First step • Create a group of many random tours in what is called a population. • These tours are stored as a sequence of numbers.
Solving NP Complete Problems using GA and FT • Second step • Randomly select tours for crossover • Crossover creates 2 new solutions children in the hope that they create an even better solution. (single point crossover?)
Implementation • 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 • generating random permutations is a difficult since we have to be careful that an older number is not repeated. • 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; od */
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 generation srand(time(NULL)); short int cities; cout << "Enter the number of cities: "; cin >> cities; //randomly assign distances between cities short 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 half for(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 0 for(i = 0; i < cities; i++) matrix[i][i] = 0 return 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 formatting if(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 permutations for(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 used for(i = 0; i < n; i++) node[i] = true; int pointer = 0; int jump = 0; for(i = 0; i < n; i++) { //jump size determined randomly jump = (rand() % (n - 1)); //find the number not yet used which is at //a distance 'jump' with wrap around while(jump >= 0) { pointer = (pointer + 1) % n; if(node[pointer]) jump--; } //assign that number to the position //and mark it 'used' in then odes array permutation[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 used for(i = 0; i < n; i++) node[i] = true; int pointer = 0; int jump = 0; for(i = 0; i < n; i++) { //jump size determined randomly jump = (rand() % (n - 1)); //find the number not yet used which is at //a distance 'jump' with wrap around while(jump >= 0) { pointer = (pointer + 1) % n; if(node[pointer]) jump--; } //assign that number to the position //and mark it 'used' in then odes array permutation[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 formatting if(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 one short int* best = pop[0]; //then iterate through all of the population //to fnd the shotest tour for(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 void displayTour(short int* tour, short int n) { for(int i = 0; i < n; i++) { //for formatting if(tour[i] < 10) cout << " "; cout << tour[i] << " "; } cout << "\b"; return; }