Advertisement
Guest User

semut

a guest
Dec 14th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.59 KB | None | 0 0
  1. clear();clc();close()
  2. A = zeros(200,20);
  3. A_up = 6*ones(1,20);
  4. A_down = zeros(1,20);
  5. A_left = 6*ones(202,1);
  6. A_right = A_left;
  7. A = [A_up; A; A_down];
  8. A = [A_left A A_right]
  9. A(202,11) = 10000;
  10.  
  11. M = zeros(200,20);
  12. M_up = (-6)*ones(1,20);
  13. M_left = (-6)*ones(202,1);
  14. M = [M_up; M; M_up];
  15. M = [M_left M M_left]
  16.  
  17. max_ant = 20
  18. deposit = 3;
  19. maximum = 20;
  20. level1 = 5;
  21. level2 = 10;
  22. level3 = 15;
  23. level4 = 20;
  24. total_ants = 10000;
  25. N = total_ants
  26. SEMUT(:,:,1) = A;
  27. PHERO(:,:,1) = M;
  28. t = 2;
  29. for asdf = 1:300
  30. for i = 2:202
  31. for j = 2:21
  32. if A(i,j)>0
  33. for k = 1:A(i,j)
  34. NWp = M(i-1,j-1);
  35. NEp = M(i-1,j+1);
  36. lst = [NWp NEp];
  37. mx = max(lst);
  38. plst = find(lst == mx);
  39. post = plst(randi([1,length(plst)],1));
  40. pher = lst(1,plst(randi([1,length(plst)],1)));
  41. if pher == 0
  42. if i > 2
  43. if j == 2
  44. n = 2;
  45. elseif j == 21
  46. n = 1;
  47. else
  48. n = randi(2);
  49. end
  50. if n == 1
  51. if A(i-1,j-1) < max_ant || i == 3
  52. A(i,j) = A(i,j) - 1;
  53. A(i-1,j-1) = A(i-1,j-1) + 1;
  54. if i == 202 && N>0
  55. N = N-1;
  56. A(i,j) = A(i,j) + 1;
  57. end
  58. if M(i,j) < maximum
  59. M(i,j) = M(i,j) + deposit;
  60. else
  61. M(i,j) = M(i,j);
  62. end
  63. end
  64. else
  65. if A(i-1,j+1) < max_ant || i == 3
  66. A(i-1,j+1) = A(i-1,j+1) + 1;
  67. A(i,j) = A(i,j) - 1;
  68. if i == 202 && N>0
  69. N = N-1;
  70. A(i,j) = A(i,j) + 1;
  71. end
  72. if M(i,j) < maximum
  73. M(i,j) = M(i,j) + deposit;
  74. else
  75. M(i,j) = M(i,j);
  76. end
  77. end
  78. end
  79. end
  80. elseif pher < level1
  81. kecepatan = 1;
  82. x = i;
  83. y = j;
  84. for m = 1: kecepatan
  85. if x-1 > 1
  86. NWp = M(x-1,y-1);
  87. NEp = M(x-1,y+1);
  88. if y == 2
  89. NWp = -1;
  90. elseif y == 21
  91. NEp = -1;
  92. end
  93. lst = [NWp NEp];
  94. mx = max(lst);
  95. plst = find(lst == mx);
  96. post = plst(randi([1,length(plst)],1));
  97. if post == 1
  98. if A(x-1,y-1) < max_ant || x == 3
  99. A(x,y) = A(x,y) - 1;
  100. A(x-1,y-1) = A(x-1,y-1) + 1;
  101. if x == 202 && N>0
  102. N = N-1;
  103. A(x,y) = A(x,y) + 1;
  104. end
  105. if M(x,y) < maximum
  106. M(x,y) = M(x,y) + deposit;
  107. else
  108. M(x,y) = M(x,y);
  109. end
  110. x = x-1;
  111. y = y-1;
  112. end
  113. else
  114. if A(x-1,y+1) < max_ant || x == 3
  115. A(x-1,y+1) = A(x-1,y+1) + 1;
  116. A(x,y) = A(x,y) - 1;
  117. if x == 202 && N>0
  118. N = N-1;
  119. A(x,y) = A(x,y) + 1;
  120. end
  121. if M(x,y) < maximum
  122. M(x,y) = M(x,y) + deposit;
  123. else
  124. M(x,y) = M(x,y);
  125. end
  126. x = x-1;
  127. y = y+1;
  128. end
  129. end
  130. end
  131. end
  132. elseif pher < level2
  133. kecepatan = 2;
  134. x = i;
  135. y = j;
  136. for m = 1: kecepatan
  137. if x-1 > 1
  138. NWp = M(x-1,y-1);
  139. NEp = M(x-1,y+1);
  140. if y == 2
  141. NWp = -1;
  142. elseif y == 21
  143. NEp = -1;
  144. end
  145. lst = [NWp NEp];
  146. mx = max(lst);
  147. plst = find(lst == mx);
  148. post = plst(randi([1,length(plst)],1));
  149. if post == 1
  150. if A(x-1,y-1) < max_ant || x == 3
  151. A(x,y) = A(x,y) - 1;
  152. A(x-1,y-1) = A(x-1,y-1) + 1;
  153. if x == 202 && N>0
  154. N = N-1;
  155. A(x,y) = A(x,y) + 1;
  156. end
  157. if M(x,y) < maximum
  158. M(x,y) = M(x,y) + deposit;
  159. else
  160. M(x,y) = M(x,y);
  161. end
  162. x = x-1;
  163. y = y-1;
  164. end
  165. else
  166. if A(x-1,y+1) < max_ant || x == 3
  167. A(x-1,y+1) = A(x-1,y+1) + 1;
  168. A(x,y) = A(x,y) - 1;
  169. if x == 202 && N>0
  170. N = N-1;
  171. A(x,y) = A(x,y) + 1;
  172. end
  173. if M(x,y) < maximum
  174. M(x,y) = M(x,y) + deposit;
  175. else
  176. M(x,y) = M(x,y);
  177. end
  178. x = x-1;
  179. y = y+1;
  180. end
  181. end
  182. end
  183. end
  184. elseif pher < level3
  185. kecepatan = 3;
  186. x = i;
  187. y = j;
  188. for m = 1: kecepatan
  189. if x-1 > 1
  190. NWp = M(x-1,y-1);
  191. NEp = M(x-1,y+1);
  192. if y == 2
  193. NWp = -1;
  194. elseif y == 21
  195. NEp = -1;
  196. end
  197. lst = [NWp NEp];
  198. mx = max(lst);
  199. plst = find(lst == mx);
  200. post = plst(randi([1,length(plst)],1));
  201. if post == 1
  202. if A(x-1,y-1) < max_ant || x == 3
  203. A(x,y) = A(x,y) - 1;
  204. A(x-1,y-1) = A(x-1,y-1) + 1;
  205. if x == 202 && N>0
  206. N = N-1;
  207. A(x,y) = A(x,y) + 1;
  208. end
  209. if M(x,y) < maximum
  210. M(x,y) = M(x,y) + deposit;
  211. else
  212. M(x,y) = M(x,y);
  213. end
  214. x = x-1;
  215. y = y-1;
  216. end
  217. else
  218. if A(x-1,y+1) < max_ant || x == 3
  219. A(x-1,y+1) = A(x-1,y+1) + 1;
  220. A(x,y) = A(x,y) - 1;
  221. if x == 202 && N>0
  222. N = N-1;
  223. A(x,y) = A(x,y) + 1;
  224. end
  225. if M(x,y) < maximum
  226. M(x,y) = M(x,y) + deposit;
  227. else
  228. M(x,y) = M(x,y);
  229. end
  230. x = x-1;
  231. y = y+1;
  232. end
  233. end
  234. end
  235. end
  236. else
  237. kecepatan = 4;
  238. x = i;
  239. y = j;
  240. for m = 1: kecepatan
  241. if x-1 > 1
  242. NWp = M(x-1,y-1);
  243. NEp = M(x-1,y+1);
  244. if y == 2
  245. NWp = -1;
  246. elseif y == 21
  247. NEp = -1;
  248. end
  249. lst = [NWp NEp];
  250. mx = max(lst);
  251. plst = find(lst == mx);
  252. post = plst(randi([1,length(plst)],1));
  253. if post == 1
  254. if A(x-1,y-1) < max_ant || x == 3
  255. A(x,y) = A(x,y) - 1;
  256. A(x-1,y-1) = A(x-1,y-1) + 1;
  257. if x == 202 && N>0
  258. N = N-1;
  259. A(x,y) = A(x,y) + 1;
  260. end
  261. if M(x,y) < maximum
  262. M(x,y) = M(x,y) + deposit;
  263. else
  264. M(x,y) = M(x,y);
  265. end
  266. x = x-1;
  267. y = y-1;
  268. end
  269. else
  270. if A(x-1,y+1) < max_ant || x == 3
  271. A(x-1,y+1) = A(x-1,y+1) + 1;
  272. A(x,y) = A(x,y) - 1;
  273. if x == 202 && N>0
  274. N = N-1;
  275. A(x,y) = A(x,y) + 1;
  276. end
  277. if M(x,y) < maximum
  278. M(x,y) = M(x,y) + deposit;
  279. else
  280. M(x,y) = M(x,y);
  281. end
  282. x = x-1;
  283. y = y+1;
  284. end
  285. end
  286. end
  287. end
  288. end
  289. end
  290. end
  291. end
  292. end
  293. SEMUT(:,:,t) = A;
  294. PHERO(:,:,t) = M;
  295. t = t + 1;
  296. end
  297.  
  298. for i = 1:t-1
  299. colormap([1 1 1;
  300. 0 0 0])
  301. image(SEMUT(:,:,i)+1)
  302. KIN(i) = getframe()
  303. end
  304.  
  305. for i = 1:t-1
  306. colormap(gray(30))
  307. image(PHERO(:,:,i)+7)
  308. KAN(i) = getframe()
  309. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement