## Appendix B Algorithm Creation and Applications for Problem-Solving
### Applying Random Search
From Figure 15, we can see that the user only needs to specify the path toward the initial population's trees (_path_init_pop_), the random trees that were generated throughout the evolutionary process (_path_rts_), and the path toward the table containing the historical records about all the individuals (_path_history_). Then, one has to create a reconstruction function, choose the index of the individual to reconstruct, and finally, reconstruct it. ```
1 importpandassapd
2 fromgpol.utils.inductive_programmingimportprm_reconstruct_tree,-get_tree_depth
3
4#Readshistoryfile
5 history=pd.read_excel(path+\"GSGP_history.xlsx\",index_col=0)
6#Createsareconstructionfunction
7 reconstructor=prm_reconstruct_tree(history,path_init_pop,path_rts,device)
8#Choosesthemostfitindividualtreconstruct(anyothercanbechosen)
9 start_idx=history[\"Fitness\"].idxmin()
10#Reconstructheindividualfromthechosemindex
11 tree=reconstructor(start_idx)
12#Printstheindividual'slength,depthandrepresentation
13 print(\"Bestsolution\"slength:\",len(tree))
14 print(\"Bestsolution'sdepth:\",get_tree_depth(tree))
15 print(\"Bestsolution:\",tree)
16
17>Bestsolution'slength:474178
18>Bestsolution'sdepth:44
19>Bestsolution:[add,add,add,mul,lf,add,sub,1.2,add,mul,8,9,div,5,9,-1.6,add,mul,lf,add,12,sub,3,...]
```
**Figure A9. ** An example of GSGP's reconstruction. **Appendix B.6**: _Applying Differential Evolution_

Figure A10 shows how to use DE metaheuristic to solve a continuous optimization problem. In this example, we chose the previously defined instance with [PERSON]'s function (see Figure A1), which was already explored in the context of LS and GA (see Figures A6 and A7, respectively). The example uses the so-called DE/rand/1/bin search strategy [49]: the base vector for the mutation is chosen at random (\"rand\"), there is one weighted difference of randomly selected vectors (\"1\"), and the crossover is binomial (\"bin\"). Since the number of weighted differences is 1, the mutation requires one weight parameter that is provided to the algorithm's constructor (_m_weights=torch.tensor([0.9], device=device)_). Given that this kind of DE mutation strategy requires the selection of three random individuals: the function _prm_d_ernd_selection_ receives a value of 3. ```
1 fromgpol.operators.selectorsimportprm_d_selection
2 fromgpol.operators.variatorsimportde_rand,de_binomial_xo
3 fromgpol.algorithms.differential_evolutionimportDifferentialEvolution
4
5#ChoosestheRastriginproblem
6 prob=\"Rastrigin\"
7 print(\">Problem:()\".format(prob))
8#DefinesDifferentialEvolution'=parameters
9 mheuristic=DifferentialEvolution(pi=pars[prob][\"pi=],pop_size=ps,initializer=pars[prob][\"initializer\"],selector=prm_dernd_selection(n_sols=3),mutator=de_rand,crossover=de_binomial_xo,m_weights=torch.tensor([0.9],device=device),c_rate=0.5,seed=pars[prob][\"seed\"],device=pars[prob][\"device=1])
10 mheuristic.solve(n_iter)
11 print(\">Bestfitness:{:3f}\".format(mheuristic.best_sol.fit.item()))
12 print(\">Bestsolution',mheuristic.best_sol.repr_)
13
14>Problem:Rastrigin
15>Bestfitness:0.000
16>Bestsolution:tensor([0.0010,-0.0029])
```



* [PERSON] et al. (2014) [PERSON]; [PERSON]; [PERSON] Problem definitions and evaluation criteria for the CEC 2014 special session and competition on single objective real-parameter numerical optimization. _Comput. Intell. Lab. Zhengzhou Univ. Zhengzhou China Tech. Rep. Nanyang Technol. Univ. Singap._**2014**, _635_, 490. * GEATbx-Genetic and Evolutionary Algorithms Toolbox in Matlab-Main Page. Available online: [[http://www.geatbx.com](http://www.geatbx.com)]([http://www.geatbx.com](http://www.geatbx.com)) (accessed on 16 February 2021). * [PERSON] et al. (2007) [PERSON]; [PERSON]; [PERSON]; [PERSON] _The Traveling Salesman Problem: A Computational Study (Princeton Series in Applied Mathematics)_; Princeton University Press: St. Princeton, NJ, USA, 2007. * [PERSON] (1990) [PERSON]; [PERSON] _Knapsack Problems: Algorithms and Computer Implementations_; John Wiley & Sons, Inc.: Hoboken, NJ, USA, 1990.