Learning Several Languages from Labeled Strings: State Merging and Evolutionary Approaches

The problem of learning pairwise disjoint deterministic finite automata (DFA) from positive examples has been recently addressed. In this paper, we address the problem of identifying a set of DFAs from labeled strings and come up with two methods. The first is based on state merging and a heuristic related to the size of each state merging iteration. State merging operations involving a large number of states are extracted, to provide sub-DFAs. The second method is based on a multi-objective evolutionary algorithm whose fitness function takes into account the accuracy of the DFA w.r.t. the learning sample, as well as the desired number of DFAs. We evaluate our methods on a dataset originated from industry.


Introduction
The problem of identifying a set of DFAs from examples that belong to different unknown languages is a recent task in grammatical inference, explored in Linard et al. (2017) for the first time. The setting considered was the observation of positive examples from multiple disjoint regular languages. Considering, for example, the following set of strings S = {aa, aaa, aaaa, abab, ababab, abba, abbba, abbbba}, the claim is that gathering a single automaton might be less informative than gathering several DFAs respectively encoding the languages a * , (ab) * and ab * a. There is a trade-off between the number of languages and how specific each language should be. That is, covering all words through a single language may not be the desired result, but having a language for each word may also not be desired. The approaches explored so far are based on clustering, either using clustering by compression as pre-processing, then applying DFA inference algorithms; or identifying tandem repeats in the strings and then generalizing the found patterns.
In this paper, we consider a setting where one can observe labeled (i.e. positive and negative) examples from multiple languages. Hence, classical DFA inference algorithms, such as Regular Positive Negative Inference (RPNI) in Oncina and Garcia (1992) or Evidence-Driven State Merging (EDSM) in Lang et al. (1998) can be extended. These algorithms consist of computing a Prefix-Tree Acceptor (PTA) for all the input strings and marking the states as rejecting or accepting depending on the input words. Then, state merging is iteratively applied, to finally obtain an automaton consistent with the dataset. Many modifications of these seminal state merging algorithms have been explored, such as adding merge constraints to state merging (Lambeau et al., 2008). Extensions to Probabilistic Deterministic Finite Automata (Carrasco and Oncina, 1994), and grammatical inference Linard through SAT (Heule and Verwer, 2010) or SMT solvers (Neider, 2012;Smetsers, 2017) have been explored. Nonetheless, none of these state merging algorithms have been modified to output more than one DFA, as in the spirit of Linard et al. (2017).
We propose here a modification of state merging algorithms, in particular RPNI, which outputs a set of DFAs. We also propose an evolutionary algorithm inspired by Dupont (1994) whose fitness function takes into account the accuracy of the DFA w.r.t. the learning sample -that is to say ensuring that all positive strings are accepted and all negative strings rejected -while in addition taking into account the desired number of DFAs.
This paper is organized as follows. In Section 2 we recall preliminary definitions. We present then, in Sections 3 and 4 our techniques to infer several languages from a set of labeled strings. In Section 5 we show the results we achieved on an industrial case study. Finally, we discuss and conclude about the proposed methods.

Definitions
In this section, we present the different concepts and definitions related to the learning of a set of DFAs from labeled strings.
Strings Let Σ denote a finite alphabet of symbols. A string x = a 0 . . . a n is a finite sequence of symbols. The empty string is denoted . We denote by Σ * the set of all strings over Σ, and by Σ + all nonempty strings over Σ (i.e. Σ * = Σ + ∪ { }). Similarly, we denote by Σ i the set of all strings over Σ of length i. Given a set of strings S, we denote by prefix(S) the set of prefixes of strings in S.
Labeled examples A set of labeled strings is a finite set of strings divided into positive strings (belonging to the language to learn) and negative strings (not belonging to the language to learn). We refer to S + and S − as the set of positive and negative examples.
Languages A regular language can be described by a deterministic finite automaton (DFA), which is a tuple A = (Σ, Q, q 0 , δ, F ), where Q is a finite set of states, q 0 ∈ Q is the starting state, δ : Q × Σ → Q is a partial transition function from states and symbols to states, and F ⊆ Q is a set of accepting states. Note that the transition function can also be represented as a transition matrix M , being a two-dimensional array of size |Q| × |Σ| where M i,j = δ(q i , a j ) with q i and M i,j ∈ Q and a j ∈ Σ; and the set of accepting states as an output array O of size |Q| where the i-th value of the array would be 1 if q i is accepting, 0 otherwise. Let q be a state, a be a symbol and u be a string. Then we extend δ to δ * : Q × Σ * → Q by δ * (q, ) = q and δ * (q, au) = δ * (δ(q, a), u). The set of tails described by a state q is the set {x ∈ Σ * : δ * (q, x) ∈ F }. Hence, the language L(A) recognized by DFA A is the set of tails of the starting state of A. A Prefix Tree Acceptor (PTA) of an non empty set S + is a tree-like DFA A = (Σ, Q, , δ, S + ) such that Q = prefix(S + ) and ∀ua ∈ prefix(S + ) : δ(u, a) = ua.

State-merging technique
Previous work (Linard et al., 2017) highlighted the interest of gathering more than one DFA as result of learning algorithms. In the setting we consider, we deal with both positive and negative examples. Hence our focus on a modification of RPNI.

RPNI
In RPNI, a PTA is built from the learning sample. Then, the task is to try to generalize this prefix tree by merging states, always ensuring that the resulting automaton is consistent with both negative and positive samples. At any moment of the iterative process of state merging, states are categorized, i.e. marked as either red (states that have been analyzed/merged, and that will be part of the final automaton) or blue (candidate states, that need to be analyzed to see if merging with a red state is possible). RPNI can be seen as based on this "red/blue" framework (de la Higuera, 2010), which is an iterative process with the following operations: compatibility: operation ensuring that a blue and a red state are either both accepting or rejecting; merging: operation iteratively performed, consisting of merging one blue and one red state that are compatible. Merging is possible iff the resulting DFA is consistent with S + and S − ; promoting: operation turning a blue state into red in case the blue state has been fully analyzed, and it cannot be merged with any of the red states; and folding: operation enabling to reconnect a possibly disconnected part of the DFA after merging two states

RPNI-Splitting
While performing state merging, it appears that some merges are involving either a considerable number of states (relative to the overall size of the DFA at the current iteration), or a considerable number of accepting states (in comparison with the size of S + ). The approach assumes the following: in case a merging operation involves many states, that is to say, that the size of the DFA drastically changes, then the part being merged has a remarkably distinct meaning with regards to the rest of the DFA. That is, in case a significant number of accepting states collapse all at once, it might be interesting to look into the related strings belonging to S + by deriving a separate solution from this sub-sample. We say that we split the DFA at this iteration. The notion of big merge can be defined as a function of an input splitting parameter k, the larger the more sub-DFAs would be returned.
In Algorithm 1, we present a modification of RPNI to output a set of DFAs. The definitions for buildPTA, rpniCompatible, merge and promote are extensively described in the literature (see for instance de la Higuera (2010)). The function choose returns the first blue state in alphabetical order. We refer to RPNI without splitting as standardRPNI. |Q m | stands for the number of accepting states being merged during an iteration of state merging, |S + | the size of the positive sample, |Q| − |Q m | for the difference of size of the DFA before and after an iteration of state merging. As soon as a consequent merge is detected, we extract all the strings s ∈ F \F m to derive a fine-grained subsidiary solution. The idea is to build a sub-PTA accepting all strings s ∈ F \F m and rejecting S − . Note here all the DFAs reject S − . Then, 1. usual state merging is executed, to gather a proper sub-DFA from the particular set of strings originated from the big merge extraction and 2. RPNI-splitting is recursively called with S + \F m as positive sample and S − ∪ F m as negative sample, which means using the strings in S + which have not been merged as positive sample, and adding the merged ones to the negative dataset. At the end of the overall process, we gather as many sub-DFAs as detected big merges. The main advantage of the application of this heuristic is its genericity: it is applicable to other state merging algorithms such as EDSM. We present an example of RPNI-Splitting in Appendix A.
Algorithm 1: RPNI-splitting. Input: S + , S − : set of labeled strings, k: splitting parameter Output: set of at most k DFAs accepting all strings s ∈ S + and rejecting all strings

Evolutionary algorithm
Evolutionary algorithms (EAs) for learning DFAs are an interesting solution for the problem of grammatical inference. By construction, they ensure that the returned DFA is consistent with the input dataset since the proportion of strings correctly classified is part of the fitness function, with the best possible fitness value is attained as stopping criterion. We propose here to base the selection not only on the accuracy of the DFA, but also on the number of desired DFAs k. Hence the multi-objectiveness of our algorithm.

DFA learning through Evolution
Evolutionary Algorithms for DFA learning have been investigated in Dupont (1994) and consist of the following steps: 1. the generation of an initial population containing a single randomly generated DFA; 2. the evaluation of the fitness of each DFA in the population. Often, the accuracy of the DFA w.r.t. S + and S − is a measure of choice for fitness; 3. performing genetic operations on the individuals; 4. the selection of the best individuals in the population. Steps 2, 3 and 4 are repeated until at least one individual in the population achieves perfect fitness or a maximum number of allowed iterations (stopping criterion) is reached: then, the best DFA in the population is returned.
Whenever more than one objective has to be met (accuracy, size of the DFA), multiobjective evolutionary algorithms such as NSGA-II (Deb et al., 2002) can be used and consist in taking into account the fitnesses of the different objectives, then making a trade-off of these in the selection of the best individuals. Multi-objective optimization for DFA learning has already been used by Wever et al. (2017) in an active learning setting.

Evolutionary algorithm for multiple DFA learning
Initial Population The initial operation is to define the seed population. We chose to start with an initial population consisting of one DFA A per string in S + such that ∀s ∈ S + , A = buildPTA(s).
Genetic Operators There are two main genetic operators that, given parent(s), generate new individuals using the parent(s) genome as a basis. Mutation consists of, given a parent DFA p 1 , randomly mutating an entry in the transition matrix M p 1 to a random value in Q. The same operation can be applied to the output array O. Crossover consists of taking 2 parents p 1 and p 2 , mixing their genomes, and producing children solutions out of them. Concretely, a random index (single-point) is chosen in the transition matrices M p 1 , M p 2 of the parents from which the two sub-parts will either go to one child or to the other.

Fitness Functions
After a new generation is created, two scores are assigned to each individual: f 1 based on the accuracy a of the DFA (proportion of strings in S + and S − which are correctly classified) and f 2 based on the number of sub-DFAs n (see paragraph on transitions clustering) that are calculated out of the individual and given S + .
Note here that the optimal fitnesses we want to reach are 0, so that the evolutionary algorithm will tend to discard individuals having the greatest scores.
Transitions Clustering The number of sub-DFAs that can be rendered out of a single DFA is calculated by looking at the number of different paths taken by the strings in S + when parsing them in the DFA (Algorithm 2). Our goal is then to cluster the transitions as in the spirit of Zhang et al. (2017). In their work, they perform state sequence clustering to figure out different driving behaviors, represented as timed automata. That is why, given: 1. a DFA and 2. a positive dataset S + , we first look which transitions are used by the strings in S + , and then return the number of different paths taken by the strings. Note here that the accepting state of the string is part of the solution. We can then, at the termination of the evolutionary algorithm once the best individual is found, return a solution containing k sub-DFAs using the k set of transitions, as explained in Algorithm 2. An example of transitions clustering is shown in Appendix B.
Note here that a candidate DFA accepting none of the strings in S + contains no set of transitions from its initial state to an accepting state. As a consequence, the result returned by Algorithm 2 will be the empty set. From a practical perspective, the way to use the evolutionary algorithm is to apply Algorithm 2 to the best individual returned by the EA, to gather k DFAs accepting strings in S + and rejecting all strings in S − .

Experiments
We evaluated our methods on a dataset inspired by the industrial case study described in Linard et al. (2017). We consider here a set of 6 languages I = {L(a + ), L((ab) ≥2 ), L((abc) + ), L(ab + a), L(a + b + ), L(a(bc) + a)} from which 100 strings are generated. The strings are divided into a training set and a test set T . The proportion of strings in the training set in comparison to the strings in the test set (density) varies from 0.02 to 0.20. We consider 4 methods: the 2 methods presented in this paper, RPNI-splitting (RP) and the evolutionary algorithm (EA), as well as the 2 methods of Linard et al. (2017), clustering by compression (CC) and the identification of tandem repeats (TR). We denote by O the set of DFAs returned by the methods. The results are stated in terms of purity. This represents to what extent the learned DFAs recognize a single target language. Note here that this measure is relevant in the case of disjoint languages. For each learned DFA, we count the number of strings issued from the most common of input the languages, such that The negative dataset S − necessary for RP and EA is composed of Σ 1 ∪ Σ 2 with Σ = {a, b, c}. CC and TR do not require any negative set. We evaluated our methods on each subset of k distinct languages for k from 2 to 5. The number of input languages k has been given as parameter to the learning algorithms, to return k output languages. We show in Figure 1a to 1d the purity as a function of the density, depending on the number of languages to learn k.
The method CC performs remarkably well even with small training sets, whereas TR fails to generalize repeated patterns only having a cardinality of at least 2. For RP, we see that a dataset size of at least 5 strings improves the results. Concerning EA, we see that it underperforms whenever the number of DFAs to learn exceeds 4. Surprisingly, it seems that approaches which learn from positive examples achieve better results than those needing positive and negative examples, especially when the number of DFAs to learn is too large.

Discussion
We modified state-of-the-art algorithms and applied it to a set of simple languages. We wonder on what class(es) of language(s) our methods work the best, and also would like to explore some properties of the learning algorithms.
Class of languages It is easy to see that CC, TR and RP will fail to distinguish {w ∈ {a} * | |w| mod 2 = 0} from {w ∈ {a} * | |w| mod 2 = 1} when they both are in S + . Indeed, by construction of these algorithms, nothing ensures that odd or even repetitions of a pattern will be differentiated. However, they will properly recognize regular languages for which there is an integer n such that for all words x, y, z and integers m ≥ n then xy m z ∈ L iff xy n z ∈ L. In other words, star-free languages can be easily recognized by our methods. However, EA offers more liberty and can possibly recognize some of the non-star-free languages (such as odd/even number of a's) since one can imagine a small dataset and a high number of clusters to find. The identification of different numbers of repetitions does not seem impossible.
Properties of RPNI-Splitting We reflect on 2 properties of RPNI-splitting.
Example 1 Let us consider A as the canonical automaton returned by standard RPNI with S + and S − as learning sample. Let us consider the set of strings s + ⊂ S + . Let B the automaton returned by standard RPNI with s + and S − as learning sample. Then Two explanations : 1. By example in Appendix A. We gather 3 DFAs (Figure 2b) by RPNI-splitting, and 1 DFA (Figure 2a) by standardRPNI. Let us denote A the DFA in Figure 2a and B the third DFA in Figure 2b. L(A) is gathered from S + = {aa, aaaa, aaaaaa, aaaaaaa, abba, abbba, abbbba, abab, ababab, abababab} and L(B) is gathered from s + = {abab, ababab, abababab}. We see that {λ, bba} ∈ L 1 whereas {λ, bba} / ∈ L 0 .
2. During state merging between a blue state b and a red state r, we know that a sub-DFA can be derived from the set of all reachable accepting states from b as positive sample, and S − as negative sample. This set of states is a subset of S + and does not offer any guarantees about the language inclusion of the learned automata. Indeed, one could do more generalization than the other, depending on the sampling.
Example 2 Let us consider O as the set of automata returned by Algorithm 1 (RPNIsplitting). Let us consider A as the canonical automaton returned by standard RPNI. Finally, let L(X ) be the language accepted by automaton X . Then By construction, RPNI-splitting extracts sub-solutions such that each sub-DFA is computed from a subset of the positive sample. A first intuition would be to say that the union of all sub-DFAs would be equal to the unique automaton returned by standard RPNI. Nevertheless, each sub-solution undergoes its own merging operations, that can lead to more or less generalizations in comparison to the unique solution.
Bound on the number of DFAs with RPNI-Splitting By construction, RPNIsplitting cannot render more DFAs than the number of state merging operations standard RPNI would perform. One could easily imagine that even with a large k splitting factor, the number of returned DFAs will be bounded.

Conclusion
In this paper, we describe two innovative methods to learn several languages from labeled strings. The first one consists in performing PTA-splitting, that is to say, a heuristic-based derivation of sub-solutions from the original state merging algorithms. The later consists in an evolutionary algorithm which fitness function combines the accuracy of the DFA and the number of sub-DFAs that can be derived from it, by parsing the positive strings in the DFA and counting how many different transition sequences exist. From a practical perspective, we see that the gathering of more than one automaton can be meaningful since a too generalized solution is not always the best. This is notably the case in some industrial settings. We also evaluated these methods as well as the methods described in Linard et al. (2017), and conclude that the different methods provide comparable results when the number of DFAs to learn is small. However, in case the number of DFAs to learn is great, the inference of multiple languages is of better quality from positive examples only. We will then investigate to what extent compression can be useful for grammatical inference.
As further works, we will investigate the definition of a metric that would compare the different DFAs provided by the different methods. We highlight the necessity of such a measure since its absence prevents us from a proper evaluation of our methods. We think that the sole use of the predictive factor of the different automata is not a good enough measure. The finding of relevant heuristics or statistics to that end promises to be complicated since it is difficult to determine whether a solution is better than another because it generalizes too much/not enough, gives too many/too few sub-DFAs, etc. We probably face here a philosophical question from a theoretical perspective, but depending on the applications, expert knowledge can decide which heuristic is relevant. We would also like to adapt automata learning as an SMT problem (Smetsers, 2017) to the learning of several languages. Finally, we think that we can develop active learning techniques in order to learn several systems by querying a teacher and learning more than one System Under Test (SUT). Of course, this would imply to redefine Minimally Adequate Teachers (MAT), and also the different queries that the learner could send to the teacher.