-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c.save-failed
244 lines (182 loc) · 3.76 KB
/
main.c.save-failed
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <stdlib.h>
int greedy_travers_agent(int l);
int a_star_travers_agent(int l);
int greedy_best_node(int l);
int a_star_best_node(int l);
//structure
struct STATE
{
char name[50];
int straight_cost;
} states[20];
struct a_star_memory
{
int node;
int value;
}memory;
int neighbour_nodes[21],path[21],visited[21],a[21][21],n=20,g[20];
int c=0,x=0;
int main()
{
int i,j;
for(int k=1;k<=n;k++)
{
path[k]=0;
visited[k]=0;
g[k]=0;
}
FILE *st;
st=fopen("state.txt","r");
if(st==NULL) exit(1);
for(i=1;i<=n;i++)
{
fscanf(st,"%s",&states[i].name);
fscanf(st,"%d",&states[i].straight_cost);
}
//print
printf("\n heuristic cost : \n");
for(i=1;i<=n;i++)
{
printf("\n %s \n",states[i].name);
printf("\n %d \n",states[i].straight_cost);
}
// taking graph from the file.
FILE *fp;
fp=fopen("file.txt","r");
if(fp==NULL) exit(1);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
fscanf(fp,"%d",&a[i][j]);
}
}
//print
printf("\n\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
// call function
printf(" \n # greedy search visiting path : \n\n");
int start=1;
greedy_travers_agent(start);
printf("\n\n ok");
for(int k=1;k<=n;k++)
{
visited[k]=0;
}
printf(" \n # a star search visiting path : \n\n");
g[start]=0;
memory.value=99999;
a_star_travers_agent(start);
printf("\n\n hmm");
return 0;
}
//greedy search
int greedy_travers_agent(int l)
{
if(c==0)
{
printf(" --> %s ",states[l].name);
visited[l]=1;
// give the sates for reaching goal
if(states[l].straight_cost==0)
{
c=1;
}
if(c!=1)
{
int i=greedy_best_node(l);
greedy_travers_agent(i);
}
}
}
// find best node of greedy search
int greedy_best_node(int l)
{
int min=99999;
int best;
for(int i=1;i<=n;i++)
{
if((a[l][i]!=0)&&(visited[i]==0))
{
if(states[i].straight_cost<min)
{
min=states[i].straight_cost;
best=i;
}
}
}
return best;
}
// a star
int a_star_travers_agent(int l)
{
if(x==0)
{
int v=g[l]+states[l].straight_cost;
printf(" --> %s (%d) ",states[l].name,v);
visited[l]=1;
// give the sates for reaching goal
if(states[l].straight_cost==0)
{
x=1;
}
if(x!=1)
{
int i=a_star_best_node(l);
a_star_travers_agent(i);
}
}
}
// find best node of a star
int a_star_best_node(int l)
{
int min=99999;
int second_min=999999;
int best=0,second_best=0,m,v,i;
// first best
for(i=1;i<=n;i++)
{
if((a[l][i]!=0)&&(visited[i]==0))
{
v=g[l]+a[l][i];
m=states[i].straight_cost+v;
if(m<min)
{
min=m;
best=i;
}
}
}
if(memory.value<min)
{
memory.node=best;
memory.value=min;
}
// second best
for(i=1;i<=n;i++)
{
if((a[l][i]!=0)&&(visited[i]==0))
{
v=g[l]+a[l][i];
m=states[i].straight_cost+v;
if((m<second_min)&&(m>min))
{
second_min=m;
second_best=i;
}
}
}
g[best]=g[l]+a[l][best];
memory.node=second_best;
memory.value=second_min;
printf(" [memory %s (%d)]",states[memory.node].name,memory.value);
return best;
}