Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active February 16, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/dabb0a5c6f594be5ababecf10a1abb2b to your computer and use it in GitHub Desktop.
Save primaryobjects/dabb0a5c6f594be5ababecf10a1abb2b to your computer and use it in GitHub Desktop.
library(ggplot2)
simulate <- function(numMales, numFemales, goal) {
result <- data.frame()
# Create rabbits.
males <- c(0, numMales)
females <- c(0, numFemales)
month <- 0
# Loop entire goal is met.
while (sum(males, females) < goal && sum(females) > 0) {
# Each female of at least 4 months is fertile.
fertile <- sum(females[5:length(females)])
babiesm <- ifelse(is.na(fertile), 0, fertile * 5)
babiesf <- ifelse(is.na(fertile), 0, fertile * 9)
deceased <- sum(males[!is.na(males[95:length(males)])], females[!is.na(females[95:length(females)])])
# Rabbits 96 months or older die.
males <- males[!is.na(males[1:95])]
females <- females[!is.na(females[1:95])]
# Add babies
males <- c(babiesm, males)
females <- c(babiesf, females)
# Print status.
result <- rbind(result, data.frame(month=month, babies=sum(babiesm, babiesf), deceased=deceased, total=sum(males, females)))
month <- month + 1
}
result
}
# Set our inputs.
input <- list(list(males=2, females=4, goal=1000000000),
list(males=2, females=4, goal=15000000000))
# Get reports.
result <- sapply(input, function(test) {
list(simulate(test$males, test$females, test$goal))
})
# Plot result for last report.
g <- ggplot(result[[2]], aes(x = month, y = total))
g <- g + geom_line()
g <- g + geom_line(aes(y = deceased), color = 'red')
g <- g + ggtitle('Rabbit Population by Month')
g <- g + theme_bw()
g <- g + theme(plot.title = element_text(size=18, face="bold", hjust=0.5), axis.text.x = element_text(angle = 45, hjust = 1), legend.position='none')
g <- g + xlab('Month')
g <- g + ylab('# Rabbits')
g <- g + scale_y_continuous(labels = scales::comma)
g <- g + scale_x_continuous(breaks=seq(from=1, to=nrow(result[[2]]), by=2))
print(g)
2 4 1000000000
[1] "Month 0 6 total rabbits"
[1] "0 bunnies born."
[1] "Month 1 6 total rabbits"
[1] "0 bunnies born."
[1] "Month 2 6 total rabbits"
[1] "0 bunnies born."
[1] "Month 3 62 total rabbits"
[1] "56 bunnies born."
[1] "Month 4 118 total rabbits"
[1] "56 bunnies born."
[1] "Month 5 174 total rabbits"
[1] "56 bunnies born."
[1] "Month 6 230 total rabbits"
[1] "56 bunnies born."
[1] "Month 7 286 total rabbits"
[1] "56 bunnies born."
[1] "Month 8 846 total rabbits"
[1] "560 bunnies born."
[1] "Month 9 1910 total rabbits"
[1] "1064 bunnies born."
[1] "Month 10 3478 total rabbits"
[1] "1568 bunnies born."
[1] "Month 11 5550 total rabbits"
[1] "2072 bunnies born."
[1] "Month 12 8126 total rabbits"
[1] "2576 bunnies born."
[1] "Month 13 15742 total rabbits"
[1] "7616 bunnies born."
[1] "Month 14 32934 total rabbits"
[1] "17192 bunnies born."
[1] "Month 15 64238 total rabbits"
[1] "31304 bunnies born."
[1] "Month 16 114190 total rabbits"
[1] "49952 bunnies born."
[1] "Month 17 187326 total rabbits"
[1] "73136 bunnies born."
[1] "Month 18 329006 total rabbits"
[1] "141680 bunnies born."
[1] "Month 19 625414 total rabbits"
[1] "296408 bunnies born."
[1] "Month 20 1203558 total rabbits"
[1] "578144 bunnies born."
[1] "Month 21 2231270 total rabbits"
[1] "1027712 bunnies born."
[1] "Month 22 3917206 total rabbits"
[1] "1685936 bunnies born."
[1] "Month 23 6878262 total rabbits"
[1] "2961056 bunnies born."
[1] "Month 24 12506990 total rabbits"
[1] "5628728 bunnies born."
[1] "Month 25 23339014 total rabbits"
[1] "10832024 bunnies born."
[1] "Month 26 43420446 total rabbits"
[1] "20081432 bunnies born."
[1] "Month 27 78675302 total rabbits"
[1] "35254856 bunnies born."
[1] "Month 28 140579662 total rabbits"
[1] "61904360 bunnies born."
[1] "Month 29 253142574 total rabbits"
[1] "112562912 bunnies born."
[1] "Month 30 463193702 total rabbits"
[1] "210051128 bunnies born."
[1] "Month 31 853977718 total rabbits"
[1] "390784016 bunnies born."
[1] "Month 32 1562055438 total rabbits"
[1] "708077720 bunnies born."
2 4 15000000000
[1] "Month 0 6 total rabbits"
[1] "0 bunnies born."
[1] "Month 1 6 total rabbits"
[1] "0 bunnies born."
[1] "Month 2 6 total rabbits"
[1] "0 bunnies born."
[1] "Month 3 62 total rabbits"
[1] "56 bunnies born."
[1] "Month 4 118 total rabbits"
[1] "56 bunnies born."
[1] "Month 5 174 total rabbits"
[1] "56 bunnies born."
[1] "Month 6 230 total rabbits"
[1] "56 bunnies born."
[1] "Month 7 286 total rabbits"
[1] "56 bunnies born."
[1] "Month 8 846 total rabbits"
[1] "560 bunnies born."
[1] "Month 9 1910 total rabbits"
[1] "1064 bunnies born."
[1] "Month 10 3478 total rabbits"
[1] "1568 bunnies born."
[1] "Month 11 5550 total rabbits"
[1] "2072 bunnies born."
[1] "Month 12 8126 total rabbits"
[1] "2576 bunnies born."
[1] "Month 13 15742 total rabbits"
[1] "7616 bunnies born."
[1] "Month 14 32934 total rabbits"
[1] "17192 bunnies born."
[1] "Month 15 64238 total rabbits"
[1] "31304 bunnies born."
[1] "Month 16 114190 total rabbits"
[1] "49952 bunnies born."
[1] "Month 17 187326 total rabbits"
[1] "73136 bunnies born."
[1] "Month 18 329006 total rabbits"
[1] "141680 bunnies born."
[1] "Month 19 625414 total rabbits"
[1] "296408 bunnies born."
[1] "Month 20 1203558 total rabbits"
[1] "578144 bunnies born."
[1] "Month 21 2231270 total rabbits"
[1] "1027712 bunnies born."
[1] "Month 22 3917206 total rabbits"
[1] "1685936 bunnies born."
[1] "Month 23 6878262 total rabbits"
[1] "2961056 bunnies born."
[1] "Month 24 12506990 total rabbits"
[1] "5628728 bunnies born."
[1] "Month 25 23339014 total rabbits"
[1] "10832024 bunnies born."
[1] "Month 26 43420446 total rabbits"
[1] "20081432 bunnies born."
[1] "Month 27 78675302 total rabbits"
[1] "35254856 bunnies born."
[1] "Month 28 140579662 total rabbits"
[1] "61904360 bunnies born."
[1] "Month 29 253142574 total rabbits"
[1] "112562912 bunnies born."
[1] "Month 30 463193702 total rabbits"
[1] "210051128 bunnies born."
[1] "Month 31 853977718 total rabbits"
[1] "390784016 bunnies born."
[1] "Month 32 1562055438 total rabbits"
[1] "708077720 bunnies born."
[1] "Month 33 2827272398 total rabbits"
[1] "1265216960 bunnies born."
[1] "Month 34 5105555566 total rabbits"
[1] "2278283168 bunnies born."
[1] "Month 35 9274298886 total rabbits"
[1] "4168743320 bunnies born."
[1] "Month 36 16960098350 total rabbits"
[1] "7685799464 bunnies born."
month babies deceased total
1 0 0 NA 6
2 1 0 NA 6
3 2 0 NA 6
4 3 56 NA 62
5 4 56 NA 118
6 5 56 NA 174
7 6 56 NA 230
8 7 56 NA 286
9 8 560 NA 846
10 9 1064 NA 1910
11 10 1568 NA 3478
12 11 2072 NA 5550
13 12 2576 NA 8126
14 13 7616 NA 15742
15 14 17192 NA 32934
16 15 31304 NA 64238
17 16 49952 NA 114190
18 17 73136 NA 187326
19 18 141680 NA 329006
20 19 296408 NA 625414
21 20 578144 NA 1203558
22 21 1027712 NA 2231270
23 22 1685936 NA 3917206
24 23 2961056 NA 6878262
25 24 5628728 NA 12506990
26 25 10832024 NA 23339014
27 26 20081432 NA 43420446
28 27 35254856 NA 78675302
29 28 61904360 NA 140579662
30 29 112562912 NA 253142574
31 30 210051128 NA 463193702
32 31 390784016 NA 853977718
33 32 708077720 NA 1562055438
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment