PageRenderTime 132ms CodeModel.GetById 32ms RepoModel.GetById 43ms app.codeStats 0ms

/automation_osm.sh

https://gitlab.com/Raizan/ns2-aodv-pnt-simulation-automation
Shell | 296 lines | 188 code | 54 blank | 54 comment | 4 complexity | 7f0e64895d13a63fc4cca3d33e3638c6 MD5 | raw file
  1. #!/bin/bash
  2. # Author: Raizan (https://github.com/Raizan)
  3. #------------------------- ENV VARIABLES ---------------------------------------
  4. TA=/home/reisuke/TugasAkhir
  5. NS=$TA/ns-allinone-2.35/ns-2.35
  6. SUMO_HOME=$TA/sumo-0.25.0
  7. SCENARIOS=/mnt/local_disk_e/TugasAkhir/scenarios
  8. DATABASE=$SCENARIOS/simulation.db
  9. AODV_ORIGINAL_DIR=$NS/aodv_original
  10. AODV_PNT_DIR=$NS/aodv_pnt
  11. #------------------------- DB CREATION ----------------------------------------
  12. # Check if DB exists
  13. if [[ -e $SCENARIOS/simulation.db ]]; then
  14. echo "Database already exist"
  15. else
  16. echo "Database does not exist"
  17. echo "Creating simulation.db..."
  18. sqlite3 $DATABASE "create table grid_results \
  19. (id INTEGER PRIMARY KEY, folder_name TEXT, category INTEGER, \
  20. aodv_pdr REAL, pnt_pdr REAL, \
  21. aodv_e2ed REAL, pnt_e2ed REAL, \
  22. aodv_ro INTEGER, pnt_ro INTEGER, \
  23. aodv_rreq_count INTEGER, pnt_rreq_count INTEGER \
  24. );"
  25. sqlite3 $DATABASE "create table osm_results \
  26. (id INTEGER PRIMARY KEY, folder_name TEXT, category TEXT, \
  27. aodv_pdr REAL, pnt_pdr REAL, \
  28. aodv_e2ed REAL, pnt_e2ed REAL, \
  29. aodv_ro INTEGER, pnt_ro INTEGER, \
  30. aodv_rreq_count INTEGER, pnt_rreq_count INTEGER \
  31. );"
  32. sqlite3 $DATABASE "create table sequences \
  33. ( grid_last_sequence INTEGER, osm_last_sequence INTEGER \
  34. );"
  35. sqlite3 $DATABASE "insert into sequences values \
  36. ( 0, 0 );"
  37. echo "Database created at $DATABASE"
  38. fi
  39. # Revert placeholders
  40. # Replace aodv.cc with aodv.cc.bak
  41. echo "Resetting parameters..."
  42. yes | cp -rf $AODV_PNT_DIR/aodv.cc.bak $AODV_PNT_DIR/aodv.cc
  43. # Replace template.tcl with template.tcl.bak
  44. yes | cp -rf $TA/wizards/template.tcl.bak $TA/wizards/template.tcl
  45. echo "Welcome to NS2 AODV & AODV-PNT SIMULATION AUTOMATION SCRIPT"
  46. read -p "How many scenarios? " num_scenarios
  47. read -p "How many nodes? " num_nodes
  48. # ------------------------------ OSM ------------------------------------------
  49. if [[ $num_nodes -eq 50 ]]; then
  50. SAVEDIRBASE=$SCENARIOS/osm/50nodes
  51. elif [[ $num_nodes -eq 100 ]]; then
  52. SAVEDIRBASE=$SCENARIOS/osm/100nodes
  53. elif [[ $num_nodes -eq 150 ]]; then
  54. SAVEDIRBASE=$SCENARIOS/osm/150nodes
  55. elif [[ $num_nodes -eq 200 ]]; then
  56. SAVEDIRBASE=$SCENARIOS/osm/200nodes
  57. fi
  58. TYPE_FILE=$TA/wizards/specification.typ.xml
  59. OSM_FILE=$TA/wizards/osm_maps/polisi_istimewa.osm
  60. end_time=360 # simulation end time
  61. cbrstart=200.0
  62. cbrstop=350.0
  63. cbrsize=512 # 512 Bytes
  64. cbrrate=2KB # 2 KB
  65. cbrinterval=1 # 1 packet per second
  66. # Map = polisi_istimewa
  67. # Sender pos
  68. SRC_X=515.89
  69. SRC_Y=522.96
  70. # Receiver pos,
  71. DST_X=906.46
  72. DST_Y=737.20
  73. # Set sender and receiver pos in template.tcl
  74. sed -i "s/\${SRC_X}/$SRC_X/" $TA/wizards/template.tcl;
  75. sed -i "s/\${SRC_Y}/$SRC_Y/" $TA/wizards/template.tcl;
  76. sed -i "s/\${DST_X}/$DST_X/" $TA/wizards/template.tcl;
  77. sed -i "s/\${DST_Y}/$DST_Y/" $TA/wizards/template.tcl;
  78. # Set other simulation parameters in template.tcl
  79. sed -i "s/\${num_nodes}/$num_nodes/" $TA/wizards/template.tcl;
  80. sed -i "s/\${cbrsize}/$cbrsize/" $TA/wizards/template.tcl;
  81. sed -i "s/\${cbrrate}/$cbrrate/" $TA/wizards/template.tcl;
  82. sed -i "s/\${cbrinterval}/$cbrinterval/" $TA/wizards/template.tcl;
  83. sed -i "s/\${end_time}/$end_time/" $TA/wizards/template.tcl;
  84. # Set DST_X and DST_Y in aodv_pnt/aodv.cc
  85. sed -i "s/\${DST_X}/$DST_X/" $AODV_PNT_DIR/aodv.cc;
  86. sed -i "s/\${DST_Y}/$DST_Y/" $AODV_PNT_DIR/aodv.cc;
  87. osm_last_sequence=$(sqlite3 $DATABASE "select osm_last_sequence from sequences;");
  88. for i in $(seq $(($osm_last_sequence + 1)) $(($osm_last_sequence + $num_scenarios)));
  89. do
  90. # Increment osm sequence table
  91. sqlite3 $DATABASE "update sequences set osm_last_sequence = $i";
  92. # Define directory and create
  93. SAVEDIR="$SAVEDIRBASE/$i";
  94. mkdir $SAVEDIR;
  95. # Convert OSM map to SUMO
  96. netconvert --remove-edges.by-vclass pedestrian,rail \
  97. --try-join-tls --osm-files $OSM_FILE --output-file $SAVEDIR/map.net.xml \
  98. --remove-edges.isolated;
  99. python $SUMO_HOME/tools/randomTrips.py -n $SAVEDIR/map.net.xml -e $num_nodes \
  100. --seed=$RANDOM --fringe-factor 5.0 --intermediate=$RANDOM \
  101. --trip-attributes='departLane="best" departPos="random_free"' -o $SAVEDIR/map.passenger.trips.xml;
  102. # # # # Repairing connectivity problems in existing route files
  103. $SUMO_HOME/bin/duarouter -n $SAVEDIR/map.net.xml -t $SAVEDIR/map.passenger.trips.xml -o $SAVEDIR/route.rou.xml \
  104. --ignore-errors --repair;
  105. # Set those placeholders in osm-template.sumocfg
  106. cp $TA/wizards/osm-template.sumocfg $SAVEDIR/sumocfg.sumocfg;
  107. sed -i "s/\${net_filename}/map\.net\.xml/" $SAVEDIR/sumocfg.sumocfg;
  108. sed -i "s/\${route_filename}/route\.rou\.xml/" $SAVEDIR/sumocfg.sumocfg;
  109. sed -i "s/\${end_time}/$end_time/" $SAVEDIR/sumocfg.sumocfg;
  110. SCENARIO_TCL_FILENAME=scenario-$num_nodes-$i.tcl;
  111. ACTIVITY_TCL_FILENAME=activity-$num_nodes-$i.tcl;
  112. MOBILITY_TCL_FILENAME=mobility-$num_nodes-$i.tcl;
  113. CONFIG_TCL_FILENAME=config-$num_nodes-$i.tcl;
  114. # Set those placeholders in scenario tcl
  115. cp $TA/wizards/template.tcl $SAVEDIR/$SCENARIO_TCL_FILENAME;
  116. MOBILITY_PLACEHOLDER="\${mobility_filename}";
  117. ACTIVITY_PLACEHOLDER="\${activity_filename}";
  118. MOBILITY_TCL_FILENAME_EXT="$SAVEDIR/$MOBILITY_TCL_FILENAME";
  119. ACTIVITY_TCL_FILENAME_EXT="$SAVEDIR/$ACTIVITY_TCL_FILENAME";
  120. sed -i "s%$MOBILITY_PLACEHOLDER%$MOBILITY_TCL_FILENAME_EXT%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  121. sed -i "s%$ACTIVITY_PLACEHOLDER%$ACTIVITY_TCL_FILENAME_EXT%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  122. CBRSTART_PLACEHOLDER="\${cbr_start}";
  123. CBRSTOP_PLACEHOLDER="\${cbr_stop}";
  124. sed -i "s%$CBRSTART_PLACEHOLDER%$cbrstart%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  125. sed -i "s%$CBRSTOP_PLACEHOLDER%$cbrstop%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  126. # Generate NS2 Mobility file
  127. sumo -c $SAVEDIR/sumocfg.sumocfg --fcd-output $SAVEDIR/sumo.xml --verbose --summary $SAVEDIR/sumo-running-summary.txt;
  128. python $SUMO_HOME/tools/traceExporter.py --fcd-input $SAVEDIR/sumo.xml \
  129. --ns2mobility-output $SAVEDIR/$MOBILITY_TCL_FILENAME \
  130. --ns2config-output $SAVEDIR/$CONFIG_TCL_FILENAME \
  131. --ns2activity-output $SAVEDIR/$ACTIVITY_TCL_FILENAME;
  132. # Comment out lines containing negative numbers in NS2 mobility file
  133. sed -i '/-/s/^/#/' $SAVEDIR/$MOBILITY_TCL_FILENAME;
  134. # Comment out lines containing setdest (0.0) (0.0)
  135. sed -i '/setdest [0-9\.]* 0\.0 /s/^/#/' $SAVEDIR/$MOBILITY_TCL_FILENAME;
  136. sed -i '/setdest 0\.0 [0-9\.]* /s/^/#/' $SAVEDIR/$MOBILITY_TCL_FILENAME;
  137. # Change $g to $node_ in activity tcl
  138. sed -i "s/\$g/\$node_/" $SAVEDIR/$ACTIVITY_TCL_FILENAME;
  139. # Change stop to reset in activity tcl
  140. sed -i "s/stop/reset/" $SAVEDIR/$ACTIVITY_TCL_FILENAME;
  141. # Change load_flatgrid size in scenario tcl
  142. get_area_length=$(grep -r "opt([x,y])" $SAVEDIR/$CONFIG_TCL_FILENAME);
  143. # Split string by whitespace and \n
  144. area_length=($(echo $get_area_length | tr " " "\n"));
  145. # Set x and y length to load_flatgrid
  146. LENGTH_X_PLACEHOLDER="\${length_x}";
  147. LENGTH_Y_PLACEHOLDER="\${length_y}";
  148. LENGTH_X=$(bc -l <<< "${area_length[2]}+100.0");
  149. LENGTH_Y=$(bc -l <<< "${area_length[5]}+100.0");
  150. sed -i "s%$LENGTH_X_PLACEHOLDER%$LENGTH_X%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  151. sed -i "s%$LENGTH_Y_PLACEHOLDER%$LENGTH_Y%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  152. #------------------------------ SIMULATION -----------------------------------
  153. # Flow: cp AODV -> Make -> Simulate -> cp AODV-PNT -> Make -> Simulate
  154. # Duplicate scenario tcl to be a "non-sed-ed trace placeholder" copy
  155. cp $SAVEDIR/$SCENARIO_TCL_FILENAME $SAVEDIR/$SCENARIO_TCL_FILENAME.bak;
  156. # Simulate original
  157. # trace filename for AODV (original)
  158. TRACE_PLACEHOLDER="\${trace_filename}";
  159. TRACE_FILENAME="$SAVEDIR/trace-$num_nodes-$i.tr";
  160. sed -i "s%$TRACE_PLACEHOLDER%$TRACE_FILENAME%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  161. yes | cp -rf $AODV_ORIGINAL_DIR/* $NS/aodv; # Copy and replace current aodv
  162. rm $NS/aodv/*.o; # Delete compiled binary
  163. echo 'Running Make for AODV...';
  164. make -C $NS &> /dev/null;
  165. echo 'Make finished';
  166. $NS/ns $SAVEDIR/$SCENARIO_TCL_FILENAME; #2> /dev/null; # Run simulation
  167. # Replace current scenario tcl with non-sed-ed one
  168. yes | cp -rf $SAVEDIR/$SCENARIO_TCL_FILENAME.bak $SAVEDIR/$SCENARIO_TCL_FILENAME;
  169. # Simulate PNT
  170. # trace filename for AODV-PNT
  171. TRACE_PLACEHOLDER="\${trace_filename}";
  172. TRACE_FILENAME_PNT="$SAVEDIR/trace-$num_nodes-$i.tr.pnt";
  173. sed -i "s%$TRACE_PLACEHOLDER%$TRACE_FILENAME_PNT%g" $SAVEDIR/$SCENARIO_TCL_FILENAME;
  174. yes | cp -rf $AODV_PNT_DIR/* $NS/aodv;
  175. rm $NS/aodv/*.o;
  176. echo 'Running Make for AODV-PNT...';
  177. make -C $NS &> /dev/null;
  178. echo 'Make finished';
  179. $NS/ns $SAVEDIR/$SCENARIO_TCL_FILENAME; #2> /dev/null;
  180. echo " ";
  181. echo "Simulation for $SAVEDIR/$SCENARIO_TCL_FILENAME finished.";
  182. echo "Analysing trace files..."
  183. # -- Packet Delivery Ratio
  184. # AODV
  185. get_pdr="$(gawk -f $TA/awk-scripts/pdr.awk $TRACE_FILENAME)"
  186. pdr_out=($(echo $get_pdr | tr " " "\n"));
  187. pdr=${pdr_out[0]};
  188. # AODV-PNT
  189. get_pdr="$(gawk -f $TA/awk-scripts/pdr.awk $TRACE_FILENAME_PNT)"
  190. pdr_out=($(echo $get_pdr | tr " " "\n"));
  191. pdr_pnt=${pdr_out[0]};
  192. # -- Average End-to-End Delay
  193. # AODV
  194. get_e2ed="$(gawk -f $TA/awk-scripts/e2ed.awk $TRACE_FILENAME)"
  195. e2ed_out=($(echo $get_e2ed | tr " " "\n"));
  196. e2ed=${e2ed_out[0]};
  197. # AODV-PNT
  198. get_e2ed="$(gawk -f $TA/awk-scripts/e2ed.awk $TRACE_FILENAME_PNT)"
  199. e2ed_out=($(echo $get_e2ed | tr " " "\n"));
  200. e2ed_pnt=${e2ed_out[0]};
  201. # -- Routing Overhead
  202. # AODV
  203. get_ro="$(gawk -f $TA/awk-scripts/ro.awk $TRACE_FILENAME)"
  204. ro_out=($(echo $get_ro | tr " " "\n"));
  205. ro=${ro_out[0]};
  206. # AODV-PNT
  207. get_ro="$(gawk -f $TA/awk-scripts/ro.awk $TRACE_FILENAME_PNT)"
  208. ro_out=($(echo $get_ro | tr " " "\n"));
  209. ro_pnt=${ro_out[0]};
  210. # -- RREQ Count
  211. # AODV
  212. get_rreq_count="$(gawk -f $TA/awk-scripts/rreq_count.awk $TRACE_FILENAME)"
  213. rreq_count_out=($(echo $get_rreq_count | tr " " "\n"));
  214. rreq_count=${rreq_count_out[0]};
  215. # AODV-PNT
  216. get_rreq_count="$(gawk -f $TA/awk-scripts/rreq_count.awk $TRACE_FILENAME_PNT)"
  217. rreq_count_out=($(echo $get_rreq_count | tr " " "\n"));
  218. rreq_count_pnt=${rreq_count_out[0]};
  219. # Insert analysed data to DB
  220. node_category="$num_nodes";
  221. folder_name="/osm/${num_nodes}nodes/$i";
  222. sqlite3 $DATABASE "insert into osm_results ( \
  223. folder_name, category, \
  224. aodv_pdr, pnt_pdr, \
  225. aodv_e2ed, pnt_e2ed, \
  226. aodv_ro, pnt_ro, \
  227. aodv_rreq_count, pnt_rreq_count ) \
  228. values ( \
  229. \"$folder_name\", $num_nodes, \
  230. $pdr, $pdr_pnt, \
  231. $e2ed, $e2ed_pnt, \
  232. $ro, $ro_pnt, \
  233. $rreq_count, $rreq_count_pnt \
  234. );";
  235. SAVEDIR=$SAVEDIRBASE;
  236. done
  237. # Revert placeholders
  238. # Replace aodv.cc with aodv.cc.bak
  239. yes | cp -rf $AODV_PNT_DIR/aodv.cc.bak $AODV_PNT_DIR/aodv.cc
  240. # Replace template.tcl with template.tcl.bak
  241. yes | cp -rf $TA/wizards/template.tcl.bak $TA/wizards/template.tcl
  242. # ----------------------------- END OF OSM ------------------------------------