# Enhancing Energy Efficiency in IoT-NDN via Parameter Optimization
## 5 Implementation
### Hyperparameter Optimization
#### 5.5.2 Grid Search Implementation  
The grid search algorithm in this framework is performed in two places: for all fixed parameter combinations (in function outerGridSearch) and for all variable combinations (in function innerGridSearch).  
The idea is that the map of keys to lists of values is difficult to iterate, so we introduce a counter list. This list contains an index for each key corresponding to the value list's current position. For example, assume the fixed parameter mappings are GRAPH_SIZE \(\rightarrow\{10,20,30\}\) and GRAPH_TOPOLOGY \(\rightarrow\{\text{GRID},\text{STAR}\}\). If the current iteration has assigned 30 to the graph size and gid to the topology, then the counter list would be \([2,0]\), indicating the values' indices in order. Furthermore, the grid search distinguishes between constMap for mappings of keys to values and ListMap for key-to-list-of-values mappings. The code for the outer grid search function is shown in Listing 1. First, the counter list is initialized with 0s; then, the list elements take on each possible index successively until the counters are all 0s again. In each iteration, a new run folder is created, which is appended with the current iteration number, i.e., run-1, run-2, etc. After that, the function innerGridSearch is run by transforming the current fixed map combination into a ConstMap.  
The innerGridSearch then also performs grid search, but this time on the variable parameters. The fixed parameters are now stored in a ConstMap, like the constant parameters, so only the variable parameters are iterated in this function. innerGridSearch has the same control flow as outerGridSearch, apart from some details. The difference is inside the isRelevant sccope: While outerGridSearch creates new run folders for each fixed combination, innerGridSearch creates a run folder within the folder for the currently considered fixed combination. Inside this inner folder, the set of parameters is written into a config file named input.cnf. The created graph graph.txt is also located in this folder. Finally, the actual simulator simulator.cc is repeatedly executed with the input.cnf files from each subfolder as input. Depending on the defined number of batches per iteration, this number of folders is created inside the inner run folder. The result of each batch is then written into the corresponding batch folder as a.csv file. For example, for fixed iteration 2, variable iteration 6, and the third batch (all zero-indexed), that would be run-2/run-6/batch-2/results.csv.  
```
Listing 1 Main control flow of outerGridSearch on fixed parameters. counters represents a list of indices for the mapping of fixed keys to a list of values. Every combination of counters will be generated until it contains only 0s again, like in the beginning. This grid search algorithm is executed for variable parameters again (with the name innerGridSearch), but with the fixedMap being a ConstMap.
``` voidouterGridSearch(constConstMap&constMap,constListMap&fixedMap,constListMap&variableMap){
3//Initializelocalvariables
4
5 do{
6 if(isRelevant(keysToIndex,paramsToValues,counters)){
7 staticuint32_tfixedCount=1;
8//CreaterunfolderforcurrentsetoffixedparametersandcurrentfixedCount
9 innerGridSearch(constMap,getNewConstMap(keysToIndex,paramsToValues,counters),variableMap,fixedFolder);
10 fixedCount++;
11}
12 counterIndex=counters.size()-1;
13
14 while(counterIndex>=0&&counters.at(counterIndex)==maxCounters.at(counterIndex)){
15 counters.at(counterIndex)=0;
16 counterIndex--;
17}
18
19 if(counterIndex>=0)
20 counters.at(counterIndex)+=1;
21
22}while(!allZeros(counters));
23} ```