-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDM.cpp
More file actions
165 lines (153 loc) · 3.54 KB
/
Copy pathDM.cpp
File metadata and controls
165 lines (153 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<limits.h>
#include <utility>
#include <algorithm>
#include<iostream>
#include <vector>
#include <map>
#include <set>
#include<fstream>
//#define size 10
#define min(a,b) a>b?b:a
#define sizePOW 1024
using namespace std;
ofstream GApath;
ofstream SApath;
ofstream DMpath;
ofstream GAdistance;
ofstream SAdistance;
ofstream DMdistance;
ofstream GAtime;
ofstream SAtime;
ofstream DMtime;
ifstream nvalue;
int npow,g[1000][1024],p[1000][1024];
int originalGraph[1][2];
int points[10000][2],n;
int curr_path[10000];
int new_path[10000];
int min_path[10000];
int visited[10000],cost=0;
int generateRandomNumber()
{
return (rand()%(n-1))+1;
}
int generateRandomNumber1()
{
return (rand()%(100-1))+1;
}
int calculateDistance(int x1,int y1,int x2,int y2)
{
return int(sqrt(pow(x1-x2,2)+pow(y1-y2,2))+0.5);
}
void get()
{
printf("No of cities=");
scanf("%d",&n);
printf("\nEnter Cost Matrix\n");
for(int i=0;i<n;i++)
{ points[i][0]=generateRandomNumber1();
points[i][1]=generateRandomNumber1();
printf("\nEnter Elements of Row # : %d\n",i+1);
printf("%d %d",points[i][0],points[i][1]);
}
for(int i=0;i<n;i++)
{
originalGraph[i][i]=0;
for(int j=i+1;j<n;j++)
{
originalGraph[i][j]=calculateDistance(points[i][0],points[i][1],points[j][0],points[j][1]);
originalGraph[j][i]=originalGraph[i][j];
}
}
printf("\n\nThe cost list is:\n\n");
for(int k=0;k<n;k++)
{
printf("\n\n");
for(int p=0;p<n;p++)
{
printf("\t%d",originalGraph[k][p]);
}
}
printf("\n");
}
//dynamic algorithm
int compute(int start,int set)
{ int masked,mask,result=INT_MAX,temp,i;
if(g[start][set]!=-1)
return g[start][set];
for(i=0;i<n;i++)
{
mask=(npow-1)-(1<<i);
masked=set&mask;
if(masked!=set)
{
temp=originalGraph[start][i]+compute(i,masked);
if(temp<result)
result=temp,p[start][set]=i;
}
}
return g[start][set]=result;
}
void getpath(int start,int set)
{
if(p[start][set]==-1) return;
int x=p[start][set];
int mask=(npow-1)-(1<<x);
int masked=set&mask;//remove p from set
printf("%d--> ",x+1);
DMpath << x+1<<",";
getpath(x,masked);
}
void TSP1()
{ int i,j;
for(i=0;i<n;i++)
for(j=0;j<npow;j++)
g[i][j]=p[i][j]=-1;
for(i=0;i<n;i++)g[i][0]=originalGraph[i][0];
int result=compute(0,npow-2);//npow-2 to exclude our "home" vertex
// printf("Tour cost:%d\n",result);
DMdistance << " Minimum Cost: " << result;
DMdistance.close();
// printf("Tour path:\n0 ");
getpath(0,npow-2);
printf("1\n");
DMpath << 1;
DMpath.close();
}
//Genetic algorithm
int main()
{
srand(time(NULL));
get();
int r1,r2;
clock_t start_t,end_t, total_t;
clock_t start_t1,end_t1, total_t3;
clock_t start_t2,end_t2, total_t4;
//Minimum distance using dynamic prog.
// get();
start_t1=clock();
cout<<"\n\nstart time for Dynalic Programming="<<start_t1<<endl;
// printf("Tour sequence For Dynamic programming=");
// mincost(0);
DMpath.open("DMpath.txt");
DMtime.open("DMtime.txt");
DMdistance.open("DMdistance.txt");
DMpath<<1<<",";
printf("1-->");
npow=(int)pow(2,n);
// put();
TSP1();
end_t1=clock();
cout<<"\nend time for Dynalic Programming="<<end_t1<<endl;
//put();
double total2 = ((double)(end_t1 - start_t1))/CLOCKS_PER_SEC;
printf("Total time taken by CPU For Dynamic Programming: %.20f\n", total2 );
DMtime<<"Time="<<total2;
DMtime.close();
return 0;
}