Guest User

Untitled

a guest
Aug 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. % Read the file wharf.csv which is a 30x30 matrix of 1's and 0's
  2. wharf_locations = csvread('wharf.csv');
  3.  
  4. % Store wharf_locations in variable t
  5. t = wharf_locations;
  6.  
  7. % Store the size of the matrix into variables
  8. [m,n] = size(wharf_locations);
  9.  
  10. % Asks user for x and y coordinates of start and goal locations
  11. x = input('Please enter x coordinate for start location:');
  12. y = input('Please enter y coordinate for start location:');
  13. c = input('Please enter x coordinate for goal location:');
  14. d = input('Please enter y coordinate for goal location:');
  15.  
  16. % If the starting location is forbidden (outside of walls or in an
  17. % obstacle, display a message and ask for new input
  18. while x<1 || x > m || y < 1 || y > m || wharf_locations(y,x) == 1
  19. disp('Starting location is forbidden, enter new coordinates:');
  20. x = input('Please enter x coordinate for start location:');
  21. y = input('Please enter y coordinate for start location:');
  22. end
  23.  
  24. % If the goal location is forbidden (outside of walls or in an obstacle,
  25. % display a message and ask for new input)
  26. while c<1 || c>m || d<1 || d>m || wharf_locations(d,c) == 1
  27. disp('Goal location is forbidden, enter new coordaintes:');
  28. c = input('Please enter x coordinate for goal location:');
  29. d = input('Please enter y coordinate for goal location:');
  30. end
  31.  
  32. % Set variable steps originally = to 0
  33. steps = 0;
  34.  
  35. % Ask the user for random or smart strategy
  36. strat = input('Please type "1" for random strategy or "5" for smart strategy:');
  37.  
  38. % If the user did not enter 1 or s, get them to try again
  39. while strat ~= 1 && strat ~= 5
  40. disp('You must enter either 1 or 5, try again');
  41. strat = input('Please type "1" for random strategy or "5" for smart strategy:');
  42. end
  43.  
  44. % Create a movie matrix
  45. movieMatrix = t;
  46.  
  47. % Change all containers originally 1 to -10
  48. movieMatrix(movieMatrix > 0)= -10;
  49.  
  50. % 10 represents the robot which is originally at start position
  51. movieMatrix(y,x) = 10;
  52.  
  53. % -3 represents the coordinates of the goal
  54. movieMatrix(d,c) = - 3;
  55.  
  56. % Set the count for number of steps on a tile to 0
  57. count1 = 0;
  58. count2 = 0;
  59. count3 = 0;
  60. count4 = 0;
  61.  
  62.  
  63. % Asks the user for the maximum number of steps the strad takes
  64. maxSteps = input('Enter maximum number of steps strad should take:');
  65.  
  66. % If user typed 1, proceed with random strategy, if user typed s, use smart
  67. % strategy
  68. if strat == 1
  69.  
  70. % Set up while loop that allows the strad to move to a random adjacent tile
  71. % without entering forbidden territory
  72. while steps <= maxSteps
  73.  
  74. % Do not allow strad to move to forbidden territory if in corner of
  75. % matrix boundaries
  76. if x == 1 && y == 1
  77. a = randi(2);
  78. elseif x == 1 && y == n
  79. a = randi(2);
  80. if a == 1
  81. a = 4;
  82. end
  83. elseif x == m && y == 1
  84. a = randi(2);
  85. if a == 2
  86. a = 3;
  87. end
  88. elseif x == m && y == m
  89. a = randi(2);
  90. if a == 1
  91. a = 3;
  92. else a = 4;
  93. end
  94.  
  95. % Do not allow strad to move to forbidden territory if on edge of
  96. % matrix boundaries
  97. elseif x == 1
  98. a = randi(3);
  99. if a == 3
  100. a = 4;
  101. end
  102. elseif x == m
  103. a = randi(3);
  104. if a == 2
  105. a = 4;
  106. end
  107. elseif y == 1
  108. a = randi(3);
  109. elseif y == m
  110. a = randi(3);
  111. if a == 1
  112. a = 4;
  113. end
  114.  
  115. % Do not allow strad to move to forbidden territory if next to
  116. % obstacle surrounding 3 sides of strad
  117. elseif t(y+1,x) == 1 && t(y-1,x) == 1 && t(y,x+1) == 1
  118. a = 3;
  119. elseif t(y+1,x) == 1 && t(y-1,x) == 1 && t(y,x-1) == 1
  120. a = 2;
  121. elseif t(y+1,x) == 1 && t(y,x-1) == 1 && t(y,x+1) == 1
  122. a = 4;
  123. elseif t(y-1,x) == 1 && t(y,x-1) == 1 && t(y,x+1) == 1
  124. a = 1;
  125.  
  126. % Do not allow strad to move to forbidden territory if next to obstacle
  127. % surrounding 2 sides of strad
  128. elseif t(y+1,x) == 1 && t(y-1,x) == 1
  129. a = randi(2,3);
  130. elseif t(y+1,x) == 1 && t(y,x-1) == 1
  131. a = randi(2);
  132. if a == 1
  133. a = 4;
  134. end
  135. elseif t(y+1,x) == 1 && t(y,x+1) == 1
  136. a = randi(2);
  137. if a == 1
  138. a = 3;
  139. elseif a == 2
  140. a = 4;
  141. end
  142. elseif t(y-1,x) == 1 && t(y,x-1) == 1
  143. a = randi(2);
  144. elseif t(y-1,x) == 1 && t(y,x+1) == 1
  145. a = randi(2);
  146. if a == 2
  147. a = 3;
  148. end
  149. elseif t(y,x+1) == 1 && t(y,x-1) == 1
  150. a = randi(2);
  151. if a == 2
  152. a = 4;
  153. end
  154.  
  155. % Do not allow strad to move to forbidden territory if next to obstacle
  156. % on one side of strad
  157. elseif t(y,x-1) == 1
  158. a = randi(3);
  159. if a == 3
  160. a = 4;
  161. disp(a);
  162. end
  163. elseif t(y-1,x) == 1
  164. a = randi(3);
  165. elseif t(y,x+1) == 1
  166. a = randi(3);
  167. if a == 2
  168. a = 4;
  169. end
  170. elseif t(y+1,x) == 1
  171. a = randi(3);
  172. if a == 1
  173. a = 4;
  174. end
  175. else a = randi(4);
  176. end
  177.  
  178. % Strad moves right if a = 1, down if a = 2, up if a = 3 and left if
  179. % a = 4
  180. if a == 1
  181. y = y + 1;
  182. elseif a == 2
  183. x = x + 1;
  184. elseif a == 3
  185. x = x - 1;
  186. elseif a == 4
  187. y = y - 1;
  188. end
  189.  
  190.  
  191. if a == 1
  192. movieMatrix(y-1,x)= movieMatrix(y-1,x)+ 1;
  193. elseif a == 2
  194. movieMatrix(y,x-1)= movieMatrix(y,x-1)+1;
  195. elseif a == 3;
  196. movieMatrix(y,x+1)= movieMatrix(y,x+1)+1;
  197. else movieMatrix(y+1,x)= movieMatrix(y+1,x) +1;
  198. end
  199.  
  200. % Display each step in x and y coordinates
  201. s = [x,y];
  202. r = [c,d];
  203. disp(s)
  204. movieMatrix(y,x) = 1;
  205.  
  206. colormap jet;
  207. colorbar;
  208. imagesc(movieMatrix);
  209.  
  210. % Increase the count on the number of steps
  211. steps = steps + 1;
  212.  
  213. frames = getframe();
  214. if r == s;
  215. break
  216. end
  217. end
  218.  
  219. % If the maximum steps is reached, display "Maximum steps reached", if a
  220. % valid path is found, display "Valid path found in x steps"
  221. if steps >= maxSteps
  222. disp('Maximum steps reached');
  223. else disp(['Valid path found in ', num2str(steps),' ', 'steps']);
  224. end
  225.  
  226. elseif strat == 5
  227. disp('There is currently no smart strategy available')
  228. end
Add Comment
Please, Sign In to add comment