-
Notifications
You must be signed in to change notification settings - Fork 1
/
misscan_id.c
333 lines (262 loc) · 11.7 KB
/
misscan_id.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include<stdio.h>
#include<stdlib.h>
#define CANNIBAL_CHARGE 20
#define MISSIONARY_CHARGE 10
#define CAPACITY 2
struct state{
int cannibalCountLeft;
int missionaryCountLeft;
int missionaryCountRight;
int cannibalCountRight;
int side; // 1 if boat is present on right side else 0
};
struct Node{
struct state * thisState;
struct Node * next;
struct Node * parentNode;
int costFromSourceToNode;
int depthFromRoot;
};
struct List{
struct Node * thisNode;
struct List * next;
};
//Adding list items to front inDFS
struct List * addToList(struct List * headList,struct Node * thisNode){
struct List * newListItem = (struct List *)malloc(sizeof(struct List));
newListItem->thisNode = thisNode;
newListItem->next = NULL;
if(headList == NULL) headList = newListItem;
else{
newListItem->next = headList;
headList = newListItem;
}
return headList;
}
struct List * removeFromList(struct List * headList){
if(headList == NULL) return NULL;
struct List * savedListItem = headList;
headList = headList->next;
free(savedListItem);
return headList;
}
struct state * createState(int missionaryCountLeft, int missionaryCountRight, int cannibalCountLeft, int cannibalCountRight,int side){
struct state * newState = (struct state *) malloc(sizeof(struct state));
newState->cannibalCountLeft = cannibalCountLeft;
newState->cannibalCountRight = cannibalCountRight;
newState->missionaryCountLeft = missionaryCountLeft;
newState->missionaryCountRight = missionaryCountRight;
newState->side = side;
return newState;
}
struct Node * createNode(struct Node * parentNode,struct state * thisState,int arcCost){
struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->parentNode = parentNode;
newNode->thisState = thisState;
newNode->next = NULL;
if(parentNode == NULL) {
newNode->costFromSourceToNode = 0;
newNode->depthFromRoot = 0;
}
else {
newNode->costFromSourceToNode = parentNode->costFromSourceToNode + arcCost;
newNode->depthFromRoot = parentNode->depthFromRoot+1;
}
}
struct Node * addSuccessorsForBoatOnLeft(struct Node * head, struct Node * currentNode,int missionaryCountOnBoat, int cannibalCountOnBoat){
int missionaryCountLeft = currentNode->thisState->missionaryCountLeft;
int missionaryCountRight = currentNode->thisState->missionaryCountRight;
int cannibalCountLeft = currentNode->thisState->cannibalCountLeft;
int cannibalCountRight = currentNode->thisState->cannibalCountRight;
int missionaryRemOnLeft = missionaryCountLeft-missionaryCountOnBoat;
int cannibalRemOnLeft = cannibalCountLeft-cannibalCountOnBoat;
int cannibalAddedOnRight = cannibalCountRight + cannibalCountOnBoat;
int missionaryAddedOnRight = missionaryCountRight + missionaryCountOnBoat;
int newSide = 1;
int chargeToTravel = (missionaryCountOnBoat*MISSIONARY_CHARGE) + (cannibalCountOnBoat*CANNIBAL_CHARGE);
if(((missionaryRemOnLeft>=cannibalRemOnLeft) || (missionaryRemOnLeft == 0)) && ((missionaryAddedOnRight>=cannibalAddedOnRight) || (missionaryAddedOnRight == 0))){
struct state * successorState = createState(missionaryRemOnLeft,missionaryAddedOnRight,cannibalRemOnLeft,cannibalAddedOnRight,newSide);
struct Node * newNode = createNode(currentNode,successorState,chargeToTravel);
if(head == NULL){
head = newNode;
}else{
struct Node * temp = head;
newNode->next = temp;
head = newNode;
}
}
return head;
}
struct Node * addSuccessorsForBoatOnRight(struct Node * head, struct Node * currentNode,int missionaryCountOnBoat, int cannibalCountOnBoat){
int missionaryCountLeft = currentNode->thisState->missionaryCountLeft;
int missionaryCountRight = currentNode->thisState->missionaryCountRight;
int cannibalCountLeft = currentNode->thisState->cannibalCountLeft;
int cannibalCountRight = currentNode->thisState->cannibalCountRight;
int missionaryRemOnRight = missionaryCountRight-missionaryCountOnBoat;
int cannibalRemOnRight = cannibalCountRight-cannibalCountOnBoat;
int cannibalAddedOnLeft = cannibalCountLeft + cannibalCountOnBoat;
int missionaryAddedOnLeft = missionaryCountLeft + missionaryCountOnBoat;
int newSide = 0;
int chargeToTravel = (missionaryCountOnBoat*MISSIONARY_CHARGE) + (cannibalCountOnBoat*CANNIBAL_CHARGE);
if(((missionaryRemOnRight>=cannibalRemOnRight) || (missionaryRemOnRight == 0)) && ((missionaryAddedOnLeft>=cannibalAddedOnLeft) || (missionaryAddedOnLeft == 0))){
struct state * successorState = createState(missionaryAddedOnLeft,missionaryRemOnRight,cannibalAddedOnLeft,cannibalRemOnRight,newSide);
struct Node * newNode = createNode(currentNode,successorState,chargeToTravel);
if(head == NULL){
head = newNode;
}else{
struct Node * temp = head;
newNode->next = temp;
head = newNode;
}
}
return head;
}
struct Node * successorFunction(struct Node * currentNode){
int missionaryCountLeft = currentNode->thisState->missionaryCountLeft;
int missionaryCountRight = currentNode->thisState->missionaryCountRight;
int cannibalCountLeft = currentNode->thisState->cannibalCountLeft;
int cannibalCountRight = currentNode->thisState->cannibalCountRight;
int missionaryCountOnBoat = 0;
int cannibalCountOnBoat = 0;
int side = currentNode->thisState->side;
struct Node * head = NULL;
//(1M,1C),(2C,0M),(2C,0M),(1M,0C),(1C,0M)
if(side == 0){
for(int i=1;i<=CAPACITY;i++){
for(int j=0;j<=i;j++){
missionaryCountOnBoat = j;
cannibalCountOnBoat = i-j;
if((missionaryCountOnBoat<=missionaryCountLeft) && (cannibalCountOnBoat<=cannibalCountLeft)){
head = addSuccessorsForBoatOnLeft(head,currentNode,missionaryCountOnBoat,cannibalCountOnBoat);
}
}
}
}else if(side == 1){
for(int i=1;i<=CAPACITY;i++){
for(int j=0;j<=i;j++){
missionaryCountOnBoat = j;
cannibalCountOnBoat = i-j;
if((missionaryCountOnBoat<=missionaryCountRight) && (cannibalCountOnBoat<=cannibalCountRight)){
head = addSuccessorsForBoatOnRight(head,currentNode,missionaryCountOnBoat,cannibalCountOnBoat);
}
}
}
}
return head;
}
//Goal Test
int isGoal(struct state * thisState, struct state * goalState){
if((thisState->cannibalCountLeft == goalState->cannibalCountLeft) &&
(thisState->cannibalCountRight == goalState->cannibalCountRight) &&
(thisState->missionaryCountLeft == goalState->missionaryCountLeft) &&
(thisState->missionaryCountRight == goalState->missionaryCountRight) &&
(thisState->side == goalState->side))
return 1;
else return 0;
}
//Search Algorithm
//Print states
void printState(struct state * thisState){
int left = 0,right = 0;
if(thisState->side==0) left =1;
else right = 1;
printf("[left: {%dM, %dC, %d} right: {%dM, %dC, %d}]\n",thisState->missionaryCountLeft,thisState->cannibalCountLeft,left,thisState->missionaryCountRight,thisState->cannibalCountRight,right);
}
void printPath(struct Node * goalNode){
if(goalNode == NULL) return;
printPath(goalNode->parentNode);
printState(goalNode->thisState);
}
int isSameState(struct state * stateA, struct state * stateB){
if((stateA->cannibalCountLeft == stateB->cannibalCountLeft) &&
(stateA->cannibalCountRight == stateB->cannibalCountRight) &&
(stateA->missionaryCountLeft == stateB->missionaryCountLeft) &&
(stateA->missionaryCountRight == stateB->missionaryCountRight) &&
(stateA->side == stateB->side))
return 1;
else return 0;
}
int findNodeInList(struct List * head,struct Node * curNode){
struct List * temp = head;
while(temp!=NULL){
struct state * stateA = temp->thisNode->thisState;
struct state * stateB = curNode->thisState;
if(isSameState(stateA,stateB) == 1) return 1;
temp = temp->next;
}
return 0;
}
void printListItems(struct List * head){
struct List * temp = head;
while(temp!=NULL){
printf(".......................................\n");
printState(temp->thisNode->thisState);
printf("Path Cost from root: %d\n",temp->thisNode->costFromSourceToNode);
printf(".......................................\n");
temp = temp->next;
}
}
int main(int argc, char * argv[]){
struct state * initialState = createState(3,0,3,0,0);
struct state * goalState = createState(0,3,0,3,1);
struct Node * startNode = createNode(NULL, initialState,0);
struct Node * goalNode = NULL;
int numberOfNodesExpanded = 0;
int numberOfNodesGenerated = 1;
for(int k=0;goalNode == NULL;k++){
struct List * openList = addToList(NULL,startNode);
struct List * closedList = NULL;
printf("[For K = %d ] ##########################################################################################################\n",k);
printf("******************************************************************************\n");
printf("OPEN_LIST\n");
printListItems(openList);
printf("-------------------------------------------------------------------------------\n");
printf("CLOSE_LIST\n");
printListItems(closedList);
printf("*******************************************************************************\n\n");
while(openList!=NULL){
struct Node * curNode = openList->thisNode;
if(curNode->depthFromRoot == (k)){
openList = removeFromList(openList);
continue;
}
struct state * curState = curNode->thisState;
numberOfNodesExpanded++;
struct Node * successorsList = successorFunction(curNode);
openList = removeFromList(openList);
printf("*******************************************************************************\n");
printf("Expand Node: ");
printState(curState);
printf("-------------------------------------------------------------------------------\n");
printf("Nodes generated: \n");
while(successorsList!=NULL){
struct Node * succNode = successorsList;
successorsList = successorsList->next;
struct state * succState = succNode->thisState;
numberOfNodesGenerated++;
printState(succState);
if(isGoal(succState,goalState)!=1){
if(findNodeInList(openList,succNode) == 0 && findNodeInList(closedList,succNode)==0)
openList = addToList(openList,succNode);
}else{
goalNode = succNode;
break;
}
}
closedList = addToList(closedList,curNode);
printf("-------------------------------------------------------------------------------\n");
printf("OPEN_LIST\n");
printListItems(openList);
printf("-------------------------------------------------------------------------------\n");
printf("CLOSE_LIST\n");
printListItems(closedList);
printf("*******************************************************************************\n\n");
if(goalNode!=NULL) break;
}
}
printf("Number of Expanded Nodes: %d\n",numberOfNodesExpanded);
printf("Number of Generated Nodes: %d\n",numberOfNodesGenerated);
printf("Solution Path Cost: %d\n",goalNode->costFromSourceToNode);
printf("Solution path\n");
printPath(goalNode);
}