# Readability Formulas for Elementary School Texts in Mexican Spanish
## 3 Methodology
### Automatic Generation of a Readability Formula Using GP
```
Input: Labeled dataset \(\mathcal{D}\), parameters: population size \(N\), number of generations \(G\), protected crossover probability \(p_{c}\), protected mutation probability \(p_{m}\),  numeric mutation probability \(p_{n}\), maximum tree depth \(d_{\max}\) Output: Best readability formula \(f^{*}\) \(\mathcal{P}_{0}\leftarrow\textsc{InitPopulation}(N,d_{\max})\) \(\textsc{Eval}(\mathcal{P}_{0},\mathcal{D})\) for\(g\gets 1\)to\(G\)do \(\mathcal{M}\leftarrow\textsc{BinaryTournament}(\mathcal{P}_{g-1})\) \(\mathcal{O}_{g}\leftarrow\textsc{ApplyGeneticOperators}(\mathcal{M},p_{c},p_{m},p_{n},d_{ \max})\) \(\textsc{Eval}(\mathcal{O}_{g},\mathcal{D})\) \(\mathcal{P}_{g}\leftarrow\textsc{SelectNextGen}(\mathcal{P}_{g-1},\mathcal{O}_{ g},N)\) \(f^{*}\leftarrow\textsc{BestIndividual}(\mathcal{P}_{G})\) return\(f^{*}\)
```  
**Algorithm 1**Genetic Programming for Readability Formula Discovery  
In Algorithm 1, \(\mathcal{D}\) denotes a labeled dataset containing text samples and associated grade-level labels. Function \(\textsc{InitPopulation}(N,d_{\max})\) generates the initial population \(\mathcal{P}_{0}\) by randomly constructing \(N\) individuals (syntax trees) using the variable-depth grow method, with maximum depth \(d_{\max}\). This method selects functions or terminals for each node at random, with the probability of selecting a terminal increasing with the node's depth. Considering random trees of size \(O(d_{\max})\), function \(\textsc{InitPopulation}\) requires \(O(Nd_{\max})\) time. The function \(\textsc{Eval}(\mathcal{P},\mathcal{D})\) computes the fitness of each individual in population \(\mathcal{P}\) by comparing predicted grade levels to true labels in \(\mathcal{D}\), using the MSE as the fitness criterion. Computing the MSE for \(n\) observations requires \(O(n)\) time, so evaluating the fitness of all \(N\) individuals, each with size \(d_{\max}\), takes \(O(nNd_{\max})\) time. The function \(\textsc{BinaryTournament}(\mathcal{P})\) constructs a mating pool \(\mathcal{M}\) by repeatedly selecting the fitter individual from randomly sampled pairs in \(\mathcal{P}\). Implementing function \(\textsc{BinaryTournament}\) requires \(O(N)\) time. The pool \(\mathcal{O}_{g}\) of offspring is created via \(\textsc{ApplyGeneticOperators}(\mathcal{M},p_{c},p_{m},p_{n},d_{\max})\), which applies protected crossover, protected mutation, and numeric mutation (according to their respective probabilities) while respecting the maximum tree depth. Function \(\textsc{ApplyGeneticOperators}\) take \(O(Nd_{\max})\) time. The resulting offspring \(\mathcal{O}_{g}\) are evaluated using \(\textsc{Eval}\). Then, \(\textsc{SelectNextGen}(\mathcal{P},\mathcal{O},N)\) forms the next generation by selecting the top \(N\) individuals from the union \(\mathcal{P}\cup\mathcal{O}\) (by using a sorting algorithm), keeping half from the current population and half from the offspring. The execution time of function \(\textsc{SelectNextGen}\) is \(O(N\log N)\). Finally, \(\textsc{BestIndividual}(\mathcal{P})\) returns the highest-fitness individual in population \(\mathcal{P}\)--i.e., the best-evolved readability formula. The overall execution time of Algorithm 1 is \(O(GN(d_{\max}n+\log N))\). Algorithm 1 largely follows a standard Genetic Programming framework, with two critical modifications: the use of 'protected' genetic operations and the inclusion of 'numeric mutation'. Protected genetic operations enforce structural constraints by ensuring that all offspring remain within the predefined maximum tree depth. This control preserves the syntactic validity and interpretability of evolved individuals, addressing a common limitation in standard crossover and mutation, which can lead to uncontrolled growth. In parallel, numeric mutation, introduced by [PERSON] and [PERSON] [33], perturbs the numericconstants found in terminal nodes. This operator aims to fine-tune real-valued parameters within individuals, enhancing the expressiveness and precision of evolved formulas.