Guest User

Untitled

a guest
May 12th, 2020
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #extremely bad simulation
  2.  
  3.  
  4. res = NULL
  5.  
  6.  
  7. for (iter in 1:20){
  8. for (propmove in 0.05*1:20){
  9.  
  10. nindivid = 240
  11. xposition = rnorm(nindivid)
  12. yposition = rnorm(nindivid)
  13. speeds = runif(nindivid)/20
  14. speeds[1] = 0.02
  15. radius = 0.05
  16. nummove = floor(nindivid*propmove)
  17.  
  18.  
  19. xposition = xposition - floor(xposition)
  20.  
  21. yposition = yposition - floor(yposition)
  22. infectionstate = c(99, rep(0, nindivid-1))
  23.  
  24. #0 = succ, > 0 = infected, -1 = recovered
  25.  
  26. for (i in 1:100){
  27. whichmove =sample(nindivid, nummove)
  28. for (ind in whichmove){
  29. xposition[ind] = xposition[ind]+sign(rnorm(1))*speeds[ind]
  30. yposition[ind] = yposition[ind]+sign(rnorm(1))*speeds[ind]
  31. }
  32.  
  33. distmat = as.matrix(dist(cbind(xposition, yposition)))
  34.  
  35. for (ind in (1:nindivid)){
  36. if (infectionstate[ind]>0) infectionstate[ind] = infectionstate[ind]-4
  37. if (infectionstate[ind]==0){
  38. for (j in 1:nindivid){
  39. if ((distmat[ind, j]< radius) & (infectionstate[j] > 0)){
  40. infectionstate[ind] = 99
  41. }
  42. }
  43. }
  44. }
  45.  
  46.  
  47. #plot(xposition, yposition, col=sign(infectionstate) + 2, xlim=c(0,1), ylim=c(0,1), pch=19)
  48.  
  49. }
  50. print(c(propmove, tail(res,1)))
  51. #table(sign(infectionstate))
  52.  
  53. res = c(res,sum(infectionstate==0))
  54. }
  55. plot(1-rowMeans(matrix(res, 20))/250, ylim=c(0,1), type="l")
  56. }
Advertisement
Add Comment
Please, Sign In to add comment