Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "list.h"
  5. #include "queue.h"
  6.  
  7. #define MAX_FILENAME 100
  8.  
  9. int main(void)
  10. {
  11. //variables
  12. int selectionComplete = 0, numBranches;
  13.  
  14. char graphFile[MAX_FILENAME], heurFile[MAX_FILENAME];
  15. char* startC;
  16. char* goalC;
  17.  
  18. //prompt the user for graph and heuristic file names
  19. printf("Enter the graph information filename: \n");
  20. fgets(graphFile, MAX_FILENAME, stdin);
  21.  
  22. printf("Enter the graph heuristic filename: \n");
  23. fgets(heurFile, MAX_FILENAME, stdin);
  24.  
  25. //prompt the user for the goal nodes
  26. printf("Enter the start node: \n");
  27. startC = fgetc(stdin);
  28.  
  29. printf("Please enter the goal node: \n");
  30. goalC = fgetc(stdin);
  31.  
  32. //prompt the user for K branch search value
  33. printf("Please enter the amount of branches to search: \n");
  34. while (selectionComplete != 1)
  35. {
  36. scanf("%d", &numBranches);
  37. if (numBranches < 1 || numBranches > 3)
  38. {
  39. printf("Please enter a valid number between 1 and 3 inclusive...\n");
  40. }
  41. else
  42. {
  43. printf("Branches to Search: %d\n", numBranches);
  44. selectionComplete = 1;
  45. }
  46. }
  47.  
  48. printf("Beginning search...\n");
  49.  
  50. //DEBUG
  51. printf("%s %s %c %c %d\n", graphFile, heurFile, startC, goalC, numBranches);
  52.  
  53. FILE* graphFP = fopen(graphFile, "r");
  54. if (graphFP == NULL)
  55. {
  56. perror("Error opening graph file");
  57. }
  58. else
  59. {
  60. FILE* heurFP = fopen(heurFile, "r");
  61. if (heurFP == NULL)
  62. {
  63. fclose(graphFP);
  64. perror("Error opening heuristic file");
  65. }
  66. else
  67. {
  68. printf("getting there...\n");
  69. }
  70. }
  71.  
  72. //create the graph linked list
  73. Head* graphListPtr = (Head*)malloc(sizeof(Head));
  74. graphListPtr->first = NULL;
  75.  
  76. //variables
  77. int readCnt, EndOfFile = 1;
  78. Node *graphCurrentPtr, *heurCurrentPtr;
  79. char *graphA, *graphB, *heurA;
  80.  
  81. //populate the graph list with data from the file
  82. do
  83. {
  84. //create a data array
  85. GraphData* graphDataArray = (GraphData*)malloc(sizeof(GraphData));
  86. readCnt = fscanf(graphFP, "%c %c %d", graphA, graphB, &(graphDataArray->graphArray[2]));
  87. if (readCnt != 3)
  88. {
  89. EndOfFile = 0;
  90. }
  91. else
  92. {
  93. //convert the characters to integers
  94. &(graphDataArray->graphArray[0]) = atoi(graphA);
  95. &(graphDataArray->graphArray[1]) = atoi(graphB);
  96.  
  97. //create a new node and move across all the data
  98. Node* newNode = (Node*)malloc(sizeof(Node*));
  99. newNode->data = graphDataArray;
  100. newNode->nextNode = listPtr->first;
  101. listPtr->first = newNode;
  102. }
  103. } while(EndOfFile != 0);
  104.  
  105. //do the same again for the heuristic file
  106. //create the heuristic linked list
  107. Head* heurListPtr = (Head*)malloc(sizeof(Head));
  108. heurListPtr->first = NULL;
  109.  
  110. //populate the heuristic list with data from the file
  111. do
  112. {
  113. //create a data array
  114. HeurData* heurDataArray = (HeurData*)malloc(sizeof(HeurData));
  115. readCnt = fscanf(heurFP, "%c %d", heurA, &(heurDataArray->heurArray[1]));
  116. if (readCnt != 2)
  117. {
  118. EndOfFile = 0;
  119. }
  120. else
  121. {
  122. //convert the char to an integer
  123. &(heurDataArray->heurArray[0]) = atoi(heurA);
  124. //create a new node and copy across all the data
  125. Node* newNode = (Node*)malloc(sizeof(Node*));
  126. newNode->data = heurDataArray;
  127. newNode->nextNode = listPtr->first;
  128. listPtr->first = newNode;
  129. }
  130. } while(EndOfFile != 0);
  131.  
  132. //DEBUG: traverse the list and print all the values to the terminal
  133. graphCurrentPtr = graphListPtr->first;
  134. while (graphCurrentPtr->nextNode != NULL)
  135. {
  136. printf("%d %d %d\n", graphCurrentPtr->nodeData->graphArray[0], graphCurrentPtr->nodeData->graphArray[1], graphCurrentPtr->nodeData->graphArray[2]);
  137. graphCurrentPtr = graphCurrentPtr->nextNode;
  138. }
  139.  
  140. heurCurrentPtr = heurListPtr->first;
  141. while (heurCurrentPtr->nextNode != NULL)
  142. {
  143. printf("%d %d\n", heurCurrentPtr->nodeData->heurArray[0], heurCurrentPtr->nodeData->heurArray[1]);
  144. heurCurrentPtr = heurCurrentPtr->nextNode;
  145. }
  146.  
  147. //search goes here
  148. //greedy search with additional branches in memory
  149. //find starting node
  150. int start, goal, i, goalFound = 0;
  151.  
  152. graphCurrentPtr = graphListPtr->first;
  153. while (graphCurrentPtr->nodeData->graphArray[0] != start) //check that the starting char is equal to the starting node
  154. {
  155. graphCurrentPtr = graphCurrentPtr->nextNode;
  156. }
  157.  
  158. //save start into the traversed queue
  159. add_data(start);
  160.  
  161. //from starting node, depending on size of K load best (smallest heuristic) K amount of branches into queue
  162. //continue down tree, adding the best K nodes into the queue at each node expanded
  163. //loop till goal found
  164. int currentSmallestBranch, possibleSmallestBranch, currentTerminalNode;
  165.  
  166. //create a checked list for the path
  167. Head* checkedListPtr = (Head*)malloc(sizeof(Head));
  168. checkedListPtr->first = NULL;
  169.  
  170. Node* checkedCurrentPtr;
  171.  
  172. while(goalFound != 1)
  173. {
  174. currentTerminalNode = check_front(); //pull the current deepest node
  175. graphCurrentPtr = graphListPtr->first;
  176. heurCurrentPtr = heurListPtr->first;
  177.  
  178. //reset the current smallest branch value to something really big
  179. currentSmallestBranch = 10000;
  180. i=0;
  181.  
  182. if (currentTerminalNode != goal) //if this current pulled node is not the goal
  183. {
  184. do
  185. {
  186. //loop through the graph list and find the terminal node K number of branches, make note of the heuristic value for the branches.
  187. //loop graph list, find same value in 1st column
  188. //
  189. do
  190. {
  191. if (graphCurrentPtr->nodeData->graphArray[0] == currentTerminalNode) //we have found a branch
  192. {
  193. do
  194. {
  195. //find the second value in the heuristic list
  196. if (heurCurrentPtr->nodeData->heurArray[0] == graphCurrentPtr->nodeData->graphArray[1])
  197. {
  198. //update the possible smallest heursitic
  199. possibleSmallestBranch == heurCurrentPtr->nodeData->heurArray[1];//save that branch length
  200. if (possibleSmallestBranch < currentSmallestBranch)
  201. {
  202. currentSmallestBranch = possibleSmallestBranch //update the current smallest branch value if the new one is smaller
  203. }
  204. }
  205. heurCurrentPtr = heurCurrentPtr->nextNode;
  206. } while (heurCurrentPtr->nextNode != NULL)
  207. }
  208. graphCurrentPtr = graphCurrentPtr->nextNode;
  209. } while (graphCurrentPtr->nextNode != NULL) //finish at the end of the graph list
  210.  
  211. //load the smallest offshoot node into the queue
  212. add_data(currentSmallestBranch);
  213. i++
  214. } while (i != 3) //keep looping until the branch counter is exhausted
  215.  
  216. //load the checked node into the checked list
  217. CheckedData* checkedDataVal = (CheckedData*)malloc(sizeof(CheckedData));
  218. &(checkedDataVal->checkedVal) = currentTerminalNode;
  219. }
  220. else
  221. {
  222. goalFound = 1;
  223. }
  224. }
  225.  
  226. //once a goal is found, traverse the checked node list and interatively find the shortest path
  227. printf("Shortest Path: \n");
  228. checkedCurrentPtr = checkedListPtr->first;
  229. while (checkedCurrentPtr->nextNode != NULL)
  230. {
  231. printf("%d - ", checkedCurrentPtr->nodeData->checkedVal);
  232. checkedCurrentPtr = checkedCurrentPtr->nextNode;
  233. }
  234. printf("\n");
  235.  
  236. //free all memory
  237. fclose(graphFP);
  238. graphCurrentPtr = graphListPtr->first;
  239. Node* temp; /*create a temporary node pointer*/
  240. while (temp != NULL)
  241. {
  242. temp = currentPtr->nextNode;
  243. free(currentPtr->nodeData);
  244. free(currentPtr);
  245. currentPtr = temp;
  246. }
  247. free(graphListPtr);
  248.  
  249. fclose(heurFP);
  250. heurCurrentPtr = heurListPtr->first;
  251. while (temp != NULL)
  252. {
  253. temp = currentPtr->nextNode;
  254. free(currentPtr->nodeData);
  255. free(currentPtr);
  256. currentPtr = temp;
  257. }
  258. free(heurListPtr);
  259.  
  260. checkedCurrentPtr = ckeckedListPtr->first;
  261. while (temp != NULL)
  262. {
  263. temp = checkedCurrentPtr->nextNode;
  264. free(checkedCurrentPtr->nodeData);
  265. free(checkedCurrentPtr);
  266. checkedCurrentPtr = temp;
  267. }
  268. free(checkedListPtr);
  269.  
  270. return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement