Temperature = Init Temp; (Init Temp=10000)

Total Page:16

File Type:pdf, Size:1020Kb

Temperature = Init Temp; (Init Temp=10000)

PROJECT # 3

Submitted By:

Priyank Tiwari Problem statement

A traveling salesman must travel to 20 cities (at least once each) of a country whose boarders form a rectangle of length (east-west) 3000 miles and width 1500 (south- north) miles. First, generate 20 such cities (randomly, uniformly) and then start traveling from the most northeast city (defined as the city closest to the northeast corner of the country) to the remaining 19 cities. Find a path that has the shortest total distance. You may use a brute force method to do it but you are encouraged to use one of the stochastic optimization methods such as simulated annealing. Please do the following:

(1) Compute the CPU time necessary to find such shortest path by one processor. (2) Use P processors to compute the same path or a different path with the same distance (accurate to machine precision if the new distance is different.) a P = 2 b. P = 4 c P = 8 (3) Plot the speedup curve and discuss your timing results.

ALGORITHM TEMPERATURE = INIT_TEMP; (INIT TEMP=10000) CITY=INIT CONFIG OF CITIES(set the initial config) CITY(0)=Northernmost city(sort out the northern most city and put as 1’st element) While (TEMPERATURE > 0.1) { While ( INNER_LOOP_CRITERION = TRUE) (run for 100 times) { mindistance(OLD COST) = CALC PATH( ); RANDOMOIZE ; ( not to randomize position of cities but their arrangement in City array) Distancecalc(NEW COST)= CALC PATH( ); If ( Distancecalc< mindistance) { mindistance= Distancecalc; [OldCost = NewCost;] SaveCity=City (DistCalc); [Save the configuration at the least distance] } ------Accept the good move

ELSE

{ r = RANDOM(0,1); Delta Cost=NEW COST-OLD COST; y = exp( -DeltaCost / TEMPERATURE); if(r < y) { mindistance= Distancecalc; [OldCost = NewCost;] SaveCity=City (DistCalc); [Save the configuration at the least distance] } ------Accept the bad move

}

ELSE { reject bad move

( don’t change mindistance)

}

} ------INNER LOOP boundary Mindistnace1Proc=mindistance for(J=0;J

} for(J=0;J

{

IF myrank !=0

RECIEVE (sortedmyrank )

IF myrank = sortedmyrank ------( means the rank sent from 0) SEND( savecity config) } for(J=0;J

TEMP= TEMP * ALPHA (TEMP= TEMP* .8) }------OUTER LOOP BOUNDARY PROJECT CODE: //Declaration of Header Files #include #include #include #include #define MAX 20

//declaring the function prototypes double calc_shortestpath(); double calc_path(); void randomize(); void calc_parallel();

//defining the city struct city_structure { int x,y; }; struct city_structure city[MAX],saved_city[MAX]; //definition of global variables int t=10000; double alpha=0.8; double min_distance=100000000000; int p,my_rank,rankfrom0; long int smallest; int flag=0;

MPI_Status status;

//START of Main Function int main(int argc, char *argv[]) { int i,j,tag,temp_x,temp_y; double temp,distance[MAX]; double minimum; double starttime=0, endtime=0;

//variables usedfor calculating execution time

double time_taken=0,timeelapsed=0,totaltime=0; //variables used for calculating execution time

//MPI code MPI_Init(&argc, &argv); //Initiate MPI MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); //Set COMM and my_rank MPI_Comm_size(MPI_COMM_WORLD, &p); //find COMM's size

//Assigning the locations of the cities

for(i=0;i

//Doing this to find the north east city

temp=3354; for(i=0;i<20;i++)

{ distance[i]= sqrt(pow(city[i].x -3000,2)+ pow(city[i].y - 1500,2)); if(distance[i]

//1st city swapped with most northeast city temp_x=city[0].x; temp_y=city[0].y;

city[0].x=city[tag].x; city[0].y=city[tag].y;

city[tag].x= temp_x; city[tag].y= temp_y;

starttime = MPI_Wtime(); //starting timer

calc_parallel();// This is the crux of the project

endtime = MPI_Wtime();//stopping the timer time_taken=endtime-starttime;//computing time taken

//Calculating the time taken by each processor and then sending //it to processor with Rank 0 for(j=0;j

if(my_rank==0) { totaltime=time_taken; for(j=1;j

MPI_Recv(&timeelapsed,1,MPI_DOUBLE,j,j*3,MPI_COMM_WORLD,&status); totaltime+=timeelapsed; }

minimum=smallest; printf("The Travelling Salesman problem for calculating the shortest path between 20 cities using Simulated Annealing\n"); printf("\n\nminimum distance calculated is %lf",minimum); printf("\n\nYou get the shorterst path by traversing thru the following cities back to the first city\n"); for(i=0;i<20;i++) printf(" (%d,%d)\n-",city[i].x,city[i].y);

printf(" (%d,%d)\n-",city[0].x,city[0].y); printf("\n\nTime taken for execution of the program is %f seconds\n",totaltime/p); }

MPI_Finalize(); return 0; }

//Function for calculating the shortest path by each processor double calc_shortestpath() { int count,i; double dist=0,distance_calc,minimum,delta,c,a;

for(count=0;count<100;count++) { randomize(); distance_calc=calc_path(); if(distance_calc

return(min_distance); }

//Function for calculating the path for 1 iteration double calc_path() { int i; double dist=0; for(i=1;i<19;i++) dist+=sqrt(pow(city[i].x -city[i+1].x,2)+ pow(city[i].y - city[i+1].y,2));

dist+=sqrt(pow(city[0].x -city[1].x,2)+ pow(city[0].y - city[1].y,2)); dist+=sqrt(pow(city[0].x -city[19].x,2)+ pow(city[0].y - city[19].y,2)); return(dist); }

//Function for changing the cities selected for calculating the path void randomize() { int i,number,temp_x,temp_y; for(i=1;i<20;i++) { number=rand()%19+1; temp_x=city[i].x; temp_y=city[i].y;

city[i].x=city[number].x; city[i].y=city[number].y;

city[number].x= temp_x; city[number].y= temp_y; } }

/*This is a function which each processor will execute for finding the shortest path,then send it to rank 0 processor for determining which processor calculated the shortest path and then broadcast this information to all other processors so that it can improve upon this path using the

Simulated Annealing Technique */ void calc_parallel() { int i,j,k,count,ranking,min_rank;

double min_dist1proc=100000000,minimumbyprocs[8];

while (t>0.1) { min_dist1proc=calc_shortestpath();

//Send smallest distance calculated by each processor to rank 0 processor for(j=0;j

MPI_Send(&min_dist1proc,1,MPI_DOUBLE,0,j*10,MPI_COMM_WORLD);

MPI_Send(&j,1,MPI_INT,0,j*10,MPI_COMM_WORLD); }

/*Rank 0 processor determines which processor calculated the shortest distance and then sends this information to other processors*/ if(my_rank==0) { minimumbyprocs[0]=min_dist1proc; for(j=1;j

MPI_Recv(&minimumbyprocs[j],1,MPI_DOUBLE,j,j*10,MPI_COMM_WORLD,&stat us);

MPI_Recv(&ranking,1,MPI_INT,j,j*10,MPI_COMM_WORLD,&status); }

smallest=1000000; for(count=0;count

for(j=1;j

MPI_Send(&min_rank,1,MPI_INT,j,j*20,MPI_COMM_WORLD); } rankfrom0=min_rank; }

/*Each processor checks the rank sent by rank 0 processor to check whether it itself is the one which calculated the shortest distance*/ for(j=1;j

MPI_Recv(&rankfrom0,1,MPI_INT,0,j*20,MPI_COMM_WORLD,&status); // printf("\nminimum rank=%d",rankfrom0); } }

for(j=0;j

MPI_Send(&saved_city[i].x,1,MPI_INT,k,k*30,MPI_COMM_WORLD);

MPI_Send(&saved_city[i].y,1,MPI_INT,k,k*30,MPI_COMM_WORLD); } } } /*All processors receive the best path computed during 1 iteration by 1 processor and then improves upon this path*/ if(p>1) for(k=0;k

MPI_Recv(&city[i].x,1,MPI_INT,rankfrom0,k*30,MPI_COMM_WORLD,&status );

MPI_Recv(&city[i].y,1,MPI_INT,rankfrom0,k*30,MPI_COMM_WORLD,&status ); } } t=0.8*t;

} }

Project CODE for Brute force:

//Declaration of Header Files #include #include #include #include #define MAX 20

//declaring the function prototypes double calc_shortestpath(); double calc_path(); void randomize(); void calc_parallel();

//defining the city struct city_structure { int x,y; }; struct city_structure city[MAX],saved_city[MAX]; //definition of global variables int t=10000; double alpha=0.8; double min_distance=100000000000; int p,my_rank,rankfrom0; long int smallest; int flag=0;

MPI_Status status;

//START of Main Function int main(int argc, char *argv[]) { int i,j,tag,temp_x,temp_y; double temp,distance[MAX]; double minimum; double starttime=0, endtime=0; //variables used for calculating execution time double time_taken=0,timeelapsed=0,totaltime=0; //variables used for calculating execution time

//MPI code MPI_Init(&argc, &argv); //Initiate MPI MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); //Set COMM and my_rank MPI_Comm_size(MPI_COMM_WORLD, &p); //find COMM's size

//Assigning the locations of the cities for(i=0;i

//Doing this to find the north east city temp=3354; for(i=0;i<20;i++) { distance[i]= sqrt(pow(city[i].x -3000,2)+ pow(city[i].y - 1500,2)); if(distance[i]

//1st city swapped with most northeast city temp_x=city[0].x; temp_y=city[0].y;

city[0].x=city[tag].x; city[0].y=city[tag].y; city[tag].x= temp_x; city[tag].y= temp_y;

starttime = MPI_Wtime(); //starting timer

calc_parallel();// This is the crux of the project

endtime = MPI_Wtime();//stopping the timer time_taken=endtime-starttime;//computing time taken

//Calculating the time taken by each processor and then sending it to processor with Rank 0 for(j=0;j

if(my_rank==0) { totaltime=time_taken; for(j=1;j

MPI_Recv(&timeelapsed,1,MPI_DOUBLE,j,j*3,MPI_COMM_WORLD,&status); totaltime+=timeelapsed; }

minimum=smallest; printf("The Travelling Salesman problem for calculating the shortest path between 20 cities using Simulated Annealing\n"); printf("\n\nminimum distance calculated is %lf",minimum); printf("\n\nYou get the shorterst path by traversing thru the following cities back to the first city\n"); for(i=0;i<20;i++) printf(" (%d,%d)\n-",city[i].x,city[i].y);

printf(" (%d,%d)\n-",city[0].x,city[0].y); printf("\n\nTime taken for execution of the program is %f seconds\n",totaltime/p); }

MPI_Finalize(); return 0; }

//Function for calculating the shortest path by each processor double calc_shortestpath() { int count,i; double dist=0,distance_calc,minimum,delta,c,a;

for(count=0;count<100;count++) { randomize(); distance_calc=calc_path(); if(distance_calc

}

return(min_distance); }

//Function for calculating the path for 1 iteration double calc_path() { int i; double dist=0; for(i=1;i<19;i++) dist+=sqrt(pow(city[i].x -city[i+1].x,2)+ pow(city[i].y - city[i+1].y,2));

dist+=sqrt(pow(city[0].x -city[1].x,2)+ pow(city[0].y - city[1].y,2)); dist+=sqrt(pow(city[0].x -city[19].x,2)+ pow(city[0].y - city[19].y,2)); return(dist); }

//Function for changing the cities selected for calculating the path void randomize() { int i,number,temp_x,temp_y; srand(my_rank*1000); for(i=1;i<20;i++) { number=rand()%19+1; temp_x=city[i].x; temp_y=city[i].y;

city[i].x=city[number].x; city[i].y=city[number].y;

city[number].x= temp_x; city[number].y= temp_y; } }

/*This is a function which each processor will execute for finding the shortest path,then send it to rank 0 processor for determining which processor calculated the shortest path and then broadcast this information to all other processors so that it can improve upon this path using the

Simulated Annealing Technique */ void calc_parallel() { int i,j,k,count,ranking,min_rank; double min_dist1proc=100000000,minimumbyprocs[8];

while (t>0.1)//&&(flag==0)) { min_dist1proc=calc_shortestpath();

//Send smallest distance calculated by each processor to rank 0 processor for(j=0;j

MPI_Send(&min_dist1proc,1,MPI_DOUBLE,0,j*10,MPI_COMM_WORLD);

MPI_Send(&j,1,MPI_INT,0,j*10,MPI_COMM_WORLD); }

/*Rank 0 processor determines which processor calculated the shortest distance and then sends this information to other processors*/ if(my_rank==0) { minimumbyprocs[0]=min_dist1proc; for(j=1;j

MPI_Recv(&minimumbyprocs[j],1,MPI_DOUBLE,j,j*10,MPI_COMM_WORLD,&stat us);

MPI_Recv(&ranking,1,MPI_INT,j,j*10,MPI_COMM_WORLD,&status); }

smallest=1000000; for(count=0;count

rankfrom0=min_rank;

/* if(smallest<13984) { flag=1; for(j=1;j

MPI_Send(&flag,1,MPI_INT,j,j*50,MPI_COMM_WORLD); } }*/ }

/*for(j=1;j

MPI_Recv(&flag,1,MPI_INT,0,j*50,MPI_COMM_WORLD,&status); // if(flag==1) return; } }*/

t=0.8*t; } }

RESULTS FOR SIMULATED ANNEALING:

TIMING(Simulated annealing)

0.19

0.17 0.1649 s

d 0.15 n o

c 0.13 e s

Series1 n

i 0.11

e 0.0976

m 0.09 i

T 0.079321 0.07 0.06417 0.05 1 2 4 8 Number of procs speed up (Simulated annealing)

1.7 1.5 1.5209 1.3 1.23 p u

1.1 d 1 Series1 e

e 0.9 p s 0.7 0.5918 0.5 0.3 1 2 4 8 time in secs

RESULTS FOR BRUTE FORCE TIMING(brute force)

0.19 0.18375 0.18 s d

n 0.17 0.1702 o c

e 0.16 s

0.155195 Series1

n 0.15 0.15012 i

e 0.14 m i T 0.13 0.12 1 2 4 8 Number of procs

speed up (brute force)

1.25 1.224 1.2 1.18 1.15

p 1.1 1.08 u 1.05 d Series1 e

e 1 1 p

s 0.95 0.9 0.85 0.8 1 2 4 8 time in secs

ANALYSIS: The method employed here for finding a optimum solution for the traveling salesman problem is SIMULATED ANNEALING.The methodology we have used here for calculating the speed up is that what distance we found out in the case of 1 processor ,using that as a reference we have evaluated the performance in case of more processors. Generally this method involves a lot of message passing, so it is useful mostly for the problems with large computation parameters .Since our algorithm is based on the calculation for the distances between the cities and then adding them up for getting the total path length, which involves a very less time for 20 cities. So computation time is very less as compared to that of the message passing involved.

Moreover the results reveal that since all the processors are running the whole process individually on each of them separately, so it might be the case that 2 or more processors may not be able to generate the minimum distance in less time as compared to that of 1 processor case. Because the results for more processors depends on just the random distribution for the cities, so this scenario is possible. This case is observed in case of 2 processors ,where the same distance path (13,984 miles)as that of 1 processor case was found by 2 processor case in more time. But fortunately when 4 or more processors are used, the random combinations for the path selected are more and thereafter the same distance path can be discovered in a relatively less time. But since down the line each of the processor is calculating the whole algorithm on it’s own so a very large speed up should not be expected as is evident from the result.

Where as the results obtained in case of brute force are as expected and the speed up curve is also obtained as is expected. In this all the possible combinations are tried for all the city paths.

------

Recommended publications