Guest User

Untitled

a guest
Oct 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. hold_fpop = function(pop, cap) max(0, (pop - cap) / (1 - fgr))
  2. hold_lazy = function(pop, cap) max(0, ceiling((pop - cap) / 120000) * 120000)
  3.  
  4. # Strategies - functions that return freighter pop given total pop (and previous turns freighter pop)
  5. baseline = function(pop, lf) 0
  6. hold33 = function(pop, lf) hold_fpop(pop, pcap * .33)
  7. hold25 = function(pop, lf) hold_fpop(pop, pcap * .25)
  8. hold42 = function(pop, lf) hold_fpop(pop, pcap * .42)
  9. hold50 = function(pop, lf) hold_fpop(pop, pcap * .5)
  10. hold33_lazy = function(pop, lf) hold_lazy(pop, pcap*.35)
  11. hold42_lazy = function(pop, lf) hold_lazy(pop, pcap * .42)
  12. hold50_lazy = function(pop, lf) hold_lazy(pop, pcap * .50)
  13.  
  14.  
  15. hold33_50 = function(pop, lf) {
  16. if (pop >= pcap * .5) return(hold_fpop(pop, pcap * .5))
  17. if (pop >= pcap * .33) return(hold_fpop(pop, pcap * .33))
  18. return(0)
  19. }
  20.  
  21. hold33_50_lazy = function(pop, lf) {
  22. if (pop >= pcap * .5) return(hold_lazy(pop, pcap * .5))
  23. if (pop >= pcap * .33) return(hold_lazy(pop, pcap * .33))
  24. return(0)
  25. }
  26.  
  27. hold33_50_lazier = function(pop, lf) {
  28. if (pop >= pcap * .5) return(max(lf, hold_lazy(pop, pcap * .5)))
  29. if (pop >= pcap * .33) return(hold_lazy(pop, pcap * .33))
  30. return(0)
  31. }
  32.  
  33. hold33_grow_50 = function(pop, lf) {
  34. if (pop < pcap * .33) return(0)
  35. if (pop > pcap * .5) return(lf)
  36. return(hold_fpop(pop, pcap*.33))
  37. }
  38. hold33_grow_50_lazy = function(pop, lf) {
  39. if (pop < pcap * .33) return(0)
  40. if (pop > pcap * .5) return(lf)
  41. return(hold_lazy(pop, pcap*.33))
  42. }
  43.  
  44.  
  45. # Code for simulating growth
  46. strategy = "hold33_50"
  47. overflow = T
  48. pgr = .19
  49. obrm = T
  50. divisor = 1000
  51. fgr = .5 * pgr
  52.  
  53. sim = function(strategy, target) {
  54. pop = 125000
  55. cumres =0
  56. result = NULL
  57. lagfreight = 0
  58. for (y in 2400:2450) {
  59.  
  60. row = data.frame(year=y, pop=pop)
  61. row$freight=ifelse(pop >= pcap, 0, strategy(pop, lagfreight))
  62. row$planet = pop - row$freight
  63. row$fgrowth = row$freight * fgr
  64. row$after.overflow = row$planet + ifelse(overflow, row$fgrowth, 0)
  65. row$cap = row$after.overflow / pcap
  66. row$crowd = ifelse(row$cap>0.25, 16/9 * (1-row$cap)^2,1)
  67. row$pgrowth = row$after.overflow * pgr * row$crowd
  68. row$res = floor(row$after.overflow / divisor)
  69. cumres = cumres + row$res
  70. row$cumres = cumres
  71. row$newpop = row$pop + row$fgrowth + row$pgrowth
  72. pop = min(pcap, row$newpop)
  73. lagfreight = row$freight
  74. result = rbind(result, row)
  75. }
  76. result
  77. }
  78.  
  79. # Run the simulations
  80.  
  81. comparison = NULL; alldata = NULL
  82. for (hab in c(1.0, 0.5)) {
  83.  
  84. for (strategy in c("baseline", "hold25", "hold33", "hold42", "hold33_lazy", "hold42_lazy", "hold50_lazy",
  85. "hold33_50", "hold33_50_lazy", "hold33_50_lazier")) {
  86.  
  87. pcap = 1000000 * ifelse(obrm, 1.1, 1) * hab
  88. target = pcap
  89.  
  90. result = sim(get(strategy), target)
  91. fullpop = if (max(result$pop >= target)) min(result$year[result$pop >= target]) else NA
  92.  
  93. row = data.frame(hab=hab, strategy=strategy, fullpop=fullpop,
  94. res10=result$cumres[result$year == 2410],
  95. res15=result$cumres[result$year == 2415],
  96. res20=result$cumres[result$year == 2420],
  97. res30=result$cumres[result$year == 2430])
  98. comparison = rbind(comparison, row)
  99. result$strategy = strategy
  100. result$hab = hab
  101. alldata = rbind(alldata, result)
  102. }
  103. }
  104. comparison %>% arrange(hab, strategy)
  105.  
  106. # Analyses and plots
  107.  
  108. alldata = alldata %>% mutate(popgrowth=pgrowth+fgrowth)
  109.  
  110. alldata %>% filter(strategy %in% c("baseline", "hold33", "hold33_lazy")) %>% ggplot + geom_line(aes(x=year, y=pop, colour=strategy))
  111. alldata %>% filter(year < 2425, strategy %in% c("hold33", "hold33_50", "hold33_grow_50")) %>% ggplot + geom_line(aes(x=year, y=cumres, colour=strategy))
  112. alldata %>% filter(year < 2425, strategy %in% c("hold33", "hold33_50", "hold33_grow_50")) %>% ggplot + geom_line(aes(x=year, y=res, colour=strategy))
  113.  
  114. alldata %>% filter(year < 2420, strategy %in% c("hold33_lazy", "hold42_lazy", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=cumres, colour=strategy))
  115. alldata %>% filter(year < 2420, strategy %in% c("hold33_lazy", "hold42_lazy", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=res, colour=strategy))
  116. alldata %>% filter(year < 2420, strategy %in% c("hold33_lazy", "hold42_lazy", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=popgrowth, colour=strategy))
  117. alldata %>% filter(year < 2420, strategy %in% c("hold33_lazy", "hold42_lazy", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=pop, colour=strategy))
  118.  
  119. alldata %>% filter(year < 2420 & hab==0.5, strategy %in% c("hold33_lazy", "hold42_lazy", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=cumres, colour=strategy))
  120. alldata %>% filter(year < 2420 & hab==0.5, strategy %in% c("hold33_lazy", "hold42_lazy", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=res, colour=strategy))
  121.  
  122. par(mfrow=c(1,2))
  123. alldata %>% filter(hab==1.0, year < 2418, strategy %in% c("hold33", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=cumres, colour=strategy))
  124. alldata %>% filter(hab==1.0, year < 2418, strategy %in% c("hold33", "hold50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=res, colour=strategy))
  125.  
  126.  
  127. alldata %>% filter(year < 2425, strategy %in% c("hold33_lazy", "hold33_50_lazy", "hold33_50_lazier")) %>% ggplot + geom_line(aes(x=year, y=res, colour=strategy))
  128.  
  129.  
  130. alldata %>% filter(year < 2425, strategy %in% c("baseline", "hold25", "hold33", "hold42")) %>% ggplot + geom_line(aes(x=year, y=cumres, colour=strategy))
  131. alldata %>% filter(year < 2425, strategy %in% c("baseline", "hold25", "hold33", "hold42")) %>% ggplot + geom_line(aes(x=year, y=res, colour=strategy))
  132.  
  133. alldata %>% filter(strategy %in% c("baseline", "hold33", "hold33_lazy")) %>% ggplot + geom_line(aes(x=year, y=pop, colour=strategy))
  134. alldata %>% filter(strategy %in% c("baseline", "hold33_lazy")) %>% ggplot + geom_line(aes(x=year, y=pop, colour=strategy))
  135.  
  136. alldata %>% filter(strategy == "hold33_grow_50_lazy")
  137. alldata %>% filter(strategy %in% c("hold50_lazy"), hab==1.0, year < 2420)
  138. alldata %>% filter(strategy == "hold33_lazy", hab=.5)
  139. alldata %>% filter(strategy == "hold33_50")
Add Comment
Please, Sign In to add comment