# Simulation Tutorial
The template is at /home/FTP/tmp/Simulation_Template.
:::info
The Problem in this simulation template is as follow:
Minimize the number of charging periods required by the stationary chargers to charge the sensors in the wireless sensor network
:::
## How to get Template
```bash=
cd ~
cp -r /home/FTP/tmp/Simulation_Template .
```
And the content of template is as below:
```
Simulation_Template
├── Average.cpp
├── Average.out
├── Balanced_CCSP.cpp
├── Balanced_CCSP.out
├── Draw_result_figure.py
├── Draw_topo_figure.py
├── Gen_topo_figure.sh
├── Gen_topo.sh
├── main.out
├── rand2.cpp
├── Run_Balanced_CCSP.sh
├── Runiform_rand.cpp
├── Runiform_rand.h
├── setup.txt
└── .vscode
```
the purpose of each file in template is as below:
| type | File Name | Purpose | Remarks |
|:---------:|:---------------------:|:------------------------------------------------:|:-------:|
| file | Average.cpp | the source code of Average.out | |
| file | Average.out | calculate average of output which is from method | |
| file | Balanced_CCSP.cpp | source code of method | |
| file | Balanced_CCSP.out | execution file of method | |
| file | Draw_result_figure.py | draw the figure of result | |
| file | Draw_topo_figure.py | generate the figure of topology | |
| file | Gen_topo_figure.sh | shell script of generate the figure of topology | |
| file | Gen_topo.sh | shell script of generate topology | |
| file | main.out | generate topology | |
| file | rand2.cpp | the Source code of main.out | |
| file | Run_Balanced_CCSP.sh | shell script of source code of method | |
| file | Runiform_rand.cpp | the source code of Uniform random | |
| file | Runiform_rand.h | the header file of Uniform random | |
| file | setup.txt | some parameters of sensor | |
| directory | Topo_houhou | the txt file of topology | |
| directory | .vscode | environment setting file of vscode | |
## Perform Process
In General, the simulation will have the following process:
```flow
st=>start: Start
set=>operation: Setting the parameters
gen_topo=>operation: Generate topology
get_topo=>inputoutput: the txt file of topology
run_method=>operation: Run method
get_result=>inputoutput: the result of method
process_data=>operation: Data Processing
data_processed=>inputoutput: Processed data
draw_figure=>operation: Draw figure of result
e=>end: End
st->set->gen_topo->get_topo->run_method->get_result->process_data->data_processed->draw_figure->e
```
### 1.Generate topology
We execute command to generate 50 nodes and 12 chargers, and place them in a 50x50 network, as shown below:
```bash=
sh Gen_topo.sh 50 12 50
Date: 0723
```

When Shell script is done, we will find a new directory named "Topo_houhou", the txt file of topology will be placed into this directory.
So, the structure of our template will like as below:
```
Simulation_Template
├── Average.cpp
├── Average.out
├── Balanced_CCSP.cpp
├── Balanced_CCSP.out
├── Draw_result_figure.py
├── Draw_topo_figure.py
├── Gen_topo_figure.sh
├── Gen_topo.sh
├── main.out
├── rand2.cpp
├── Run_Balanced_CCSP.sh
├── Runiform_rand.cpp
├── Runiform_rand.h
├── setup.txt
├── **Topo_houhou** <=== New Directory
└── .vscode
```
Finally, the content which in the directory named "Topo_houhou" will like as below:
```
Topo_houhou
└── 0723
└── 12sinks
└── 50_12
├── 1
│ ├── Node_Topolist.txt
│ ├── setup.txt
│ └── Sink_Topolist.txt
.
.
.
├── 100
│ ├── Node_Topolist.txt
│ ├── setup.txt
│ └── Sink_Topolist.txt
```
### 2.Run method
We execute command to perform our method with 50 nodes and 12 chargers, and place them in a 50x50 network, as shown below:
```bash=
sh Run_Balanced_CCSP.sh 50 12
Date: 0723
```
When Shell script is done, we will find a new directory named "Result", the txt file of topology will be placed into this directory.
So, the structure of our template will like as below:
```
Simulation_Template
├── Average.cpp
├── Average.out
├── Balanced_CCSP.cpp
├── Balanced_CCSP.out
├── Draw_result_figure.py
├── Draw_topo_figure.py
├── Gen_topo_figure.sh
├── Gen_topo.sh
├── main.out
├── rand2.cpp
├── Run_Balanced_CCSP.sh
├── Runiform_rand.cpp
├── Runiform_rand.h
├── setup.txt
├── Topo_houhou
├── **Result** <=== New Directory
└── .vscode
```
Finally, the content which in the directory named "Result" will like as below:
```
Result
└── 0723
└── 50_12.txt
```
Because in our simulation, the network size is not considered as a variable factor, and the location of the node or charger already contains the network size information, so it will not be input as a parameter to the method. In addition, the Result folder does not have any folder or file name classification related to the network size, it is only related to the number of nodes, the number of sensors, or the date.
### 3.Data Processing
Average.cpp is used to calculate the average value of the method results, and it has been included in the 20th line of the script used in the section named "Run method", so when you finish executing the script, the data processing is finished.
Here is the content of shell script ,you can also open the file in vscode.
```shell=
#!/bin/bash
node_num=$1
sink_num=$2
read -p "Date: " mydate
# create_date = $3
mkdir Result
mkdir Result/${mydate}
# mkdir Result/$3
for j in `seq 100`
do
echo run $j
./Balanced_CCSP.out ./Topo_houhou/${mydate}/$2sinks/$1_$2/$j/Sink_Topolist.txt ./Topo_houhou/${mydate}/$2sinks/$1_$2/$j/Node_Topolist.txt >> ./Result/${mydate}/$1_$2.txt
# ./Balanced_CCSP.out ./Topo_houhou/$3/$2sinks/$1_$2/$j/Sink_Topolist.txt ./Topo_houhou/$3/$2sinks/$1_$2/$j/Node_Topolist.txt >> ./Result/$3/$1_$2.txt
wait
done
./Average.out ./Result/${mydate}/$1_$2.txt >> ./Result/${mydate}/$1_$2.txt
# ./Average.out ./Result/$3/$1_$2.txt >> ./Result/$3/$1_$2.txt
```
### 4.Draw figure of result
Suppose today we want to know the change in the number of charging periods scheduled by our method when the number of chargers in the network changes from 8 to 12.
First, we need to continue to generate the topology for the number of chargers, and input the generated topology into the method to obtain the result when the number of chargers is 8-12.
So, repeat the operations described in section 1-3 above.
Finally,we execute the command to draw the variable parameter as the number of chargers, and increase by 1 from 8 to 12 chargers, as follows:
```bash=
python3 Draw_result_figure.py 0 12 8 1 0723
```
This is the result figure as follow:
