1 / 21

Solving NP Complete Problems using GA

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

Download Presentation

Solving NP Complete Problems using GA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Solving NP Complete Problems using GA Mubasher Baig Asif Jamshed Umar Faiz

  2. Solving NP Complete Problems using GA • Two basic steps to solving the traveling salesman problem using a GA.

  3. 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.

  4. 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?)

  5. 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).

  6. Implementation • Take input on the number of cities from the user • Generate a (symmetric) square distance matrix representing the distance between each city.

  7. 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

  8. 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 */

  9. Implementation #include<stdlib.h> #include<iostream.h> #include<time.h> const int MAX_DISTANCE = 30; const int POP_SIZE = 100;

  10. 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*);

  11. 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);

  12. 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); }

  13. 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; }

  14. 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; }

  15. 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; }

  16. 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; }

  17. 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; }

  18. 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; }

  19. 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; }

  20. 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; }

  21. 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; }

More Related