run-experiments.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/bash
  2. # cd into the directory containing this script (from the bash faq 028)
  3. if [[ $BASH_SOURCE = */* ]]; then
  4. cd -- "${BASH_SOURCE%/*}/" || exit 1
  5. fi
  6. # Check for the python dependencies we need at the end
  7. ./scripts/check-dependencies.py
  8. if [ $? != 0 ]; then
  9. echo "Please make sure all dependencies are installed before running this script."
  10. exit 1
  11. fi
  12. # Number of simulation runs to do in parallel
  13. parallel=""
  14. # Number of simulation runs in each configuration
  15. n=""
  16. # Number of configurations in experiment 1
  17. exp1=""
  18. # Experiment 1 beginning
  19. e1b=1
  20. # Experiment 1 end
  21. e1e=33
  22. # Number of configurations in experiment 2
  23. exp2=""
  24. # Experiment 2 beginning
  25. e2b=1
  26. # Experiment 2 end
  27. e2e=8
  28. # Max observed memory use for 1 trial
  29. mem_use=688
  30. # Proceed without confirmation
  31. noninteractive=false
  32. # By default, run experiments and process results
  33. run_experiments=true
  34. process_results=true
  35. # Get parameters
  36. while getopts ":p:n:1:2:yer" opt; do
  37. case ${opt} in
  38. p)
  39. parallel="${OPTARG}"
  40. ;;
  41. n)
  42. n="${OPTARG}"
  43. ;;
  44. 1)
  45. exp1="${OPTARG}"
  46. if [ "$exp1" != "" ]; then
  47. # If the user specified a range, use that range
  48. if [[ "$exp1" == *"-"* ]]; then
  49. e1b="${exp1%%-*}"
  50. e1e="${exp1##*-}"
  51. exp1=$((e1e - e1b + 1))
  52. else
  53. e1b=1
  54. e1e="$exp1"
  55. fi
  56. fi
  57. ;;
  58. 2)
  59. exp2="${OPTARG}"
  60. if [ "$exp2" != "" ]; then
  61. # If the user specified a range, use that range
  62. if [[ "$exp2" == *"-"* ]]; then
  63. e2b="${exp2%%-*}"
  64. e2e="${exp2##*-}"
  65. exp2=$((e2e - e2b + 1))
  66. else
  67. e2b=1
  68. e2e="$exp2"
  69. fi
  70. fi
  71. ;;
  72. y)
  73. noninteractive=true
  74. ;;
  75. e)
  76. run_experiments=true
  77. process_results=false
  78. ;;
  79. r)
  80. run_experiments=false
  81. process_results=true
  82. ;;
  83. esac
  84. done
  85. # Run experiments unless -r flag was used
  86. if [ "$run_experiments" == true ]; then
  87. # Ask user for values they didn't already specify
  88. if [ "$parallel" == "" ]; then
  89. read -e -p "How many simulation runs should we perform in parallel? (We suggest the number of CPU cores you have.) " parallel
  90. echo ""
  91. fi
  92. if [ "$n" == "" ]; then
  93. read -e -p "How many trials should we do in each configuration? [5] " n
  94. # Default to 5
  95. if [ "$n" == "" ]; then
  96. n=5
  97. fi
  98. echo ""
  99. fi
  100. if [ "$exp1" == "" ]; then
  101. read -e -p "How many configurations should we use in the first experiment? [33] " exp1
  102. # Default to 33 and max at 33
  103. if [[ "$exp1" == "" || "$exp1" -gt 33 ]]; then
  104. exp1=33
  105. # Min 0
  106. elif [[ "$exp1" -lt 0 ]]; then
  107. exp1=0
  108. fi
  109. echo ""
  110. # Begining and end
  111. e1b=1
  112. e1e="$exp1"
  113. fi
  114. if [ "$exp2" == "" ]; then
  115. read -e -p "How many configurations should we use in the second experiment? [8] " exp2
  116. # Default to 10 and max at 10
  117. if [[ "$exp2" == "" || "$exp2" -gt 8 ]]; then
  118. exp2=8
  119. # Min 0
  120. elif [[ "$exp2" -lt 0 ]]; then
  121. exp2=0
  122. fi
  123. echo ""
  124. # Beginning and end
  125. e2b=1
  126. e2e="$exp2"
  127. fi
  128. num_configs=$((exp1 + exp2))
  129. num_trials=$((num_configs * n))
  130. batches=$(( (num_trials + parallel - 1) / parallel))
  131. if [[ "$parallel" -gt "$num_trials" ]]; then
  132. parallel=$num_trials
  133. fi
  134. echo "We will test Troll Patrol in ${num_configs} configurations."
  135. echo "We will run the simulation ${n} times in each configuration."
  136. echo "This results in a total of ${num_trials} simulation runs."
  137. echo "We will do ${parallel} runs in parallel, so this is ${batches} batches."
  138. echo "It is recommended that you have at least ${parallel} CPU cores and $((parallel * mem_use)) MB of RAM."
  139. echo "If you don't have enough cores or RAM, try reducing the number of parallel simulation runs."
  140. echo "It is anticipated that this will take from $((batches + batches / 2)) to $((batches * 2)) days to complete."
  141. if [ "$noninteractive" == false ]; then
  142. read -e -p "Is this okay? (y/N) " res
  143. if [[ "$res" != "Y" && "$res" != "y" ]]; then
  144. exit 1
  145. fi
  146. fi
  147. ./scripts/run-experiments.sh "$parallel" "$n" "$e1b" "$e1e" "$e2b" "$e2e"
  148. fi
  149. # Process results unless -e flag was used
  150. if [ "$process_results" == true ]; then
  151. # Parse out bridge info we want to plot
  152. for i in results/*/*-simulation; do
  153. sed -n '/^Full stats per bridge:$/,/^End full stats per bridge$/{//!p;}' \
  154. "$i" > "${i%-simulation}-bridges.csv"
  155. sim_begin=$(grep -Po '(?<=Simulation began on day )(.*)(?=$)' "$i")
  156. censor_begin=$(grep -Po '(?<=Censor began on day )(.*)(?=$)' "$i")
  157. echo "$sim_begin,$censor_begin" > "${i%-simulation}-start.csv"
  158. done
  159. if [ "$n" == "" ]; then
  160. read -e -p "How many trials did we do in each configuration? [5] " n
  161. # Default to 5
  162. if [ "$n" == "" ]; then
  163. n=5
  164. fi
  165. echo ""
  166. fi
  167. ./scripts/plot-results.py 1 $n results/1/*-bridges.csv
  168. ./scripts/plot-results.py 2 $n results/2/*-bridges.csv
  169. fi
  170. echo "Done. See the results directory for the output."