# Predicting the Magnitude of Earthquakes Using Grammatical Evolution
## 2 Materials and Methods
### Constructed Neural Networks  
Another method used in the conducted experiments was the neural network construction method [51]. This method can be used to discover the optimal architecture of artificial neural networks, as well as to estimate the parameters of the network by using the Grammatical Evolution technique. This method has been applied in a series of problems, such as in chemistry [52], education problems [53], autism screening [54], etc. The BNF grammar incorporated in the neural construction procedure is depicted in Figure 5. This grammar can produce artificial neural networks in the following form:  
\[N\big{(}\overrightarrow{x},\overrightarrow{w}\big{)}=\sum_{i=1}^{H}w_{(d+2)i-( d+1)}\sigma\Bigg{(}\sum_{j=1}^{d}x_{j}w_{(d+2)i-(d+1)+j}+w_{(d+2)i}\Bigg{)} \tag{1}\]  
S:=<Sigval> (0) <Sigval>:::=<Node> (0) | <Node> + <Sigval> (1) <Node>::=<Number>*sig(<Sum>++Number>) (0) <Sum>::= <Number>*CKlist> (0) | <Sum>+<Sum> (1) <List>::= x1 (0) | x2 (1)................................ | xd (d-1) <Number>::= (<Dist>.<Dist>) (0) | (-<Dist>.<Dist>) (1) <Dist>::= <Digt> (0) | <Digt><Dist> (1) <Digt>::= 0 (0) | 1 (1)................................ | 9 (9)  
The value \(H\) denotes the number of processing units (weights) of the neural network. Also, the function \(\sigma(x)\) stands for the sigmoid function. Following the previous equation, it is deducted that the total number of parameters for this network can be computed as follows:  
\[n=(d+2)H\]  
Figure 4: An example of the application of the one point crossover method in the Grammatical Evolution. The green color is used for the parts of the first chromosome and the blue color is used for the parts of the second chromosome.  
Figure 5: The proposed grammar for the construction of artificial neural networks through Grammatical Evolution.  
As an example, consider the following neural network:  
\[N(x)=1.9\text{sig}(10.5x_{1}+3.2x_{3}+1.4)+2.1\text{sig}(2.2x_{2}-3.3x_{3}+3.2) \tag{3}\]  
This expression represents a neural network used in a problem with 3 inputs \((x_{1},x_{2},x_{3})\). The number of processing nodes is \(H=2\). This neural network is outlined graphically in Figure 6.  
The steps of the method used to construct artificial neural networks are shown below:  
* **Step 1--Initialization step**. 1. **Define** as \(N_{c}\) the number of chromosomes in the genetic population and as \(N_{g}\) the total number of allowed generations. 2. **Set** as \(p_{s}\) the used selection rate and as \(p_{m}\) the used mutation rate. 3. **Initialize** the chromosomes \(c_{i},\ i=1,\ldots,N_{c}\). Each chromosome is considered as a set of randomly selected positive integers. 4. **Set \(k=0\)** as the generation counter.
* **Step 2--Fitness calculation step**. 1. **For**\(i=1,\ldots,N_{c}\)**perform the following:** 1. **Obtain** the chromosome \(c_{i}\). 2. **Create** the corresponding neural network \(N_{i}\big{(}\overrightarrow{x},\overrightarrow{w}\big{)}\) for this chromosome using the grammar in Figure 5. 3. **Calculate** the associated fitness value \(f_{i}\) as the training error of network \(N_{i}\big{(}\overrightarrow{x},\overrightarrow{w}\big{)}\), defined as follows: \[f_{i}=\sum_{j=1}^{M}\big{(}N_{i}\big{(}\overrightarrow{x},\overrightarrow{ w}\big{)}-y_{j}\big{)}^{2}\]