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

Solving NP Complete Problems using GA

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Solving NP Complete Problems  using GA

Mubasher Baig

Asif Jamshed

Umar Faiz

Solving NP Complete Problems using GA

Page 2: Solving NP Complete Problems  using GA

• Two basic steps to solving the traveling salesman problem using a GA.

Solving NP Complete Problems using GA

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

Solving NP Complete Problems using GA and FT

Page 4: Solving NP Complete Problems  using GA

• 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?)

Solving NP Complete Problems using GA and FT

Page 5: Solving NP Complete Problems  using GA

• 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

Page 6: Solving NP Complete Problems  using GA

• Take input on the number of cities from the user

• Generate a (symmetric) square distance matrix representing the distance between each city.

Implementation

Page 7: Solving NP Complete Problems  using GA

• 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

Page 8: Solving NP Complete Problems  using GA

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

Page 9: Solving NP Complete Problems  using GA

#include<stdlib.h>

#include<iostream.h>

#include<time.h>

const int MAX_DISTANCE = 30;

const int POP_SIZE = 100;

Implementation

Page 10: Solving NP Complete Problems  using GA

– //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

Page 11: Solving NP Complete Problems  using GA

– //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

Page 12: Solving NP Complete Problems  using GA

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

Page 13: Solving NP Complete Problems  using GA

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

Page 14: Solving NP Complete Problems  using GA

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

Page 15: Solving NP Complete Problems  using GA

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

Page 16: Solving NP Complete Problems  using GA

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

Page 17: Solving NP Complete Problems  using GA

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

Page 18: Solving NP Complete Problems  using GA

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

Page 19: Solving NP Complete Problems  using GA

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

Page 20: Solving NP Complete Problems  using GA

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

Page 21: Solving NP Complete Problems  using GA

void displayTour(short int* tour, short int n){

for(int i = 0; i < n; i++){

//for formattingif(tour[i] < 10)

cout << " ";cout << tour[i] << " ";

}cout << "\b";return;

}

Implementation