# Road Traffic Rules Synthesis Using Grammatical Evolution

- First Online:

DOI: 10.1007/978-3-319-55792-2_12

- Cite this paper as:
- Medvet E., Bartoli A., Talamini J. (2017) Road Traffic Rules Synthesis Using Grammatical Evolution. In: Squillero G., Sim K. (eds) Applications of Evolutionary Computation. EvoApplications 2017. Lecture Notes in Computer Science, vol 10200. Springer, Cham

## Abstract

We consider the problem of the automatic synthesis of road traffic rules, motivated by a future scenario in which human and machine-based drivers will coexist on the roads: in that scenario, current road rules may be either unsuitable or inefficient. We approach the problem using Grammatical Evolution (GE). To this end, we propose a road traffic model which includes concepts amenable to be regulated (e.g., lanes, intersections) and which allows drivers to temporarily evade traffic rules when there are no better alternatives. In our GE framework, each individual is a set of rules and its fitness is a weighted sum of traffic efficiency and safety, as resulting from a number of simulations where all drivers are subjected to the same rules. Experimental results show that our approach indeed generates rules leading to a safer and more efficient traffic than enforcing no rules or rules similar to those currently used.

### Keywords

Simulation Road traffic model Stochastic evolution Driverless cars## 1 Introduction and Related Work

Car driving is one of the tasks that in a not far away future will be carried out by machines, rather than by humans. In a *driverless car* scenario a machine must be able to take a number of decisions in real time, with a limited and possibly noisy perception of the environment. Such decisions must take into account the need of abiding by the rules of the road and the presence of other moving, possibly hardly predictable, agents (pedestrian, bikers, other cars, either driverless or with a human driver).

Current traffic rules have been written for a scenario where humans drive cars and may hence be suboptimal in a driverless car scenario, or even in a scenario where both machines and humans drive cars. In this work, we take a fresh look at traffic rules and investigate the possibility of devising a novel set of rules that are amenable to automation and, at the same time, able to improve *global* indexes computed over the full population of vehicles. In particular, we focus on optimizing the global *traffic efficiency* and *safety*. We believe that, broadly speaking, an approach of this kind could lead to significant advantages to the society as a whole and that the driverless car revolution could offer a unique opportunity in this respect.

We propose a framework based on Grammatical Evolution (GE) and our contribution is as follows. First, we propose and experimentally evaluate a model for road traffic including the road graph, the cars, and the rules-aware drivers who try to abide by, but can possibly evade, the rules; the model is detailed enough to include concepts such as lanes, collisions, and safety distance, which are significant to our study. Second, we propose a language to define rules which can be enforced in our model: rules are predicates and the language is given in the form of a context-free grammar. Third and finally, we propose and experimentally evaluate a method for the automatic synthesis of rules based on GE: individuals represent sets of rules (i.e., regulations) and their fitness capture the degree to which, according to the results of several simulations, traffic regulated by the set of rules is efficient and safe.

Our experimental evaluation shows that, using GE, it is possible to obtain sets of rules which result in safer (less collisions) and more efficient traffic, w.r.t. both unregulated traffic and a set of hand-written rules designed to resemble a (simplified) real world set of rules.

To the best of our knowledge, no other studies concerning the automatic synthesis of road traffic rules have been proposed before. Recent research has focused on how driverless cars should behave with respect to the existing rules: by proposing flexible control strategies which minimize the number of violated rules [14], by approaching the (highway) driverless algorithm design with rules complying as a first goal (legal safety) [15], or by formalizing rules for the sake of accountability after collisions involving driverless cars [10]. A much deeper problem in this area consists in determining which decision should be taken by a driverless car when facing a situation where only less-than-ideal outcomes are possible [4, 5]. In such a case, the decision may result in some sacrifice involving car users (passengers) or other people (e.g., pedestrians). This fundamental problem is orthogonal to our work.

Traffic is regulated not only by rules but also by the infrastructure, e.g., road markings and signs. In this area, several proposals have been made to modify the working principle of traffic lights in order to avoid congestion (e.g., [16]), also resorting to evolutionary computation [12]. More recently, motivated by the emergence of more automated vehicles, Tachet et al. even proposed the replacement of traffic lights with a novel (for road traffic) flow regulation solution build upon slot-based systems [13].

From a more general point of view, automatic synthesis of rules is a task which fits particularly well GE, since the language of the rules can be described in terms of a grammar and candidate solutions may be evaluated by means of simulated application of the corresponding set of rules: for instance, in [2] GE has been used to generate trading rules for spot foreign-exchange markets. Moreover, recent works showed that GE is suitable for addressing real problems with complex grammars, such as learning of string similarity functions [1], or designing components of vehicle routing algorithms [3].

## 2 Road Traffic Model

We consider a scenario with continuous space and discrete time in which a number of cars move according to their driving algorithms.

### 2.1 Roads and Cars

A *road graph* is a directed graph \(\mathcal {G}=(S,I)\) in which edges represent road sections, vertices represent road intersections, and where each vertex is connected to at least two edges. A *road section*\(p \in S\) is characterized by a length \(l(p) \in \mathbb {R}, l(p)>0\) and a width \(w(p) \in \mathbb {N}, w(p)>0\): the former represents the length of the section between two intersections and the latter represents the number of lanes in the section. A *road intersection*\(p \in I\) is characterized by a size \(w(p) \in \mathbb {N}, w(p) > 0\): without loss of generality, we assume that the size of an intersection is equal to the largest width among the sections connecting the intersection.

A *car* is an agent which moves on the road graph. At a given time step, the car is positioned somewhere on the road graph, i.e., its position can be determined in terms of the section/intersection, lane and distance from the section/intersection origin. The car movement is determined in terms of two speeds, i.e., along the section and along the lanes—see Fig. 1.

In detail, a car is a tuple \((p, x, y, v_x, v_y, s)\) where *p*, *x*, *y* constitute the *position*, \(v_x \in \mathbb {R}\) is the *linear speed*, \(v_y \in \{-1, 0, 1\}\) is the lane-changing speed, and \(s \in \{\text {alive},\text {dead}\}\) is the *status*. Within the position, \(p \in S \cup I\) is the section or intersection where the car is. If \(p \in S\), \(x \in [0, l(p)]\) and \(y \in \{1, \dots , w(p)\}\) are the linear and lane coordinates of the car within the road section *p*—we assume that \(x=0\) refers to the starting side of *p*. If \(p \in I\), \(x \in [0, w(p)]\) is the coordinate of the car within the intersection and *y* is not relevant.

At each time step, if the status of a car is \(s=\text {dead}\), the position is not updated. Otherwise, if the status is \(s=\text {alive}\), the position (*p*, *x*, *y*) of a car is updated as follows. If \(p \in S\) and \(0 \le x+v_x \le l(p)\), then its position at the next step is \((p, x+v_x, \min (\max (y+v_y, 0), w(p)))\). Otherwise, if \(p \in S\) and \(x+v_x < 0\) or \(x+v_x > l(p)\), then its position at the next step is \((p', 0, 0)\), where \(p' \in I\) is the appropriate intersection between the two connected by *p*. Otherwise, if \(p \in I\) and \(x + \left|v_x\right| \le w(p)\) then its position at the next step is \((p, x+\left|v_x\right|, 0)\). Otherwise and finally, if \(p \in I\) and \(x + \left|v_x\right| > w(p)\), then its position at the next step is \((p', x_0, y_0)\), where \(p' \in S\) is one of the sections connecting *p* and \(x_0=0, y_0=0\) or \(x_0=l(p'), y_0=w(p')\) depending on whether \(p'\) starts or ends in *p*, respectively—in the latter case, if \(v_x>0\), then at the next step it is set to \(-v_x\). Concerning the choice of \(p'\), let \(\{p'_1, \dots , p'_n\} \subseteq S \) be the set of sections connecting *p*, then \(p'\) is set to \(p'_j\), where *j* is chosen randomly in \(\{1, \dots , n\}\). In other words, a car moves on a section according to its speeds; whenever it reaches the end (or the beginning) of the section, it enters the connected intersection. While in an intersection, the car remains inside according to its linear speed and the intersection size. When exiting an intersection, it enters a connecting section.

*collision*may occur between a pair of cars. A collision occurs if two cars meet on the same intersection or if two cars meet on the same lane of the same road section. If a collision occurs, the status of the two cars is set to dead. In detail, let \((p^{(k)}_1, x^{(k)}_1, y^{(k)}_1)\) and \((p^{(k)}_2, x^{(k)}_2, y^{(k)}_2)\) be the positions of the two cars at time step

*k*, a collision occurs at

*k*if at least one of the following conditions is met:

\(p^{(k)}_1 = p^{(k)}_2 \in I\) and \(\left|x_1^{(k)}-x_2^{(k)}\right|<d_\text {collision}\);

\(p^{(k)}_1 = p^{(k)}_2 \in S\) and \(y^{(k)}_1 = y^{(k)}_2\) and \(\left|x_1^{(k)}-x_2^{(k)}\right|<d_\text {collision}\);

\(p^{(k)}_1 = p^{(k-1)}_1 = p^{(k)}_2 = p^{(k-1)}_2 \in I\) and \({{\mathrm{sign}}}(x_1^{(k)}-x_2^{(k)}) \ne {{\mathrm{sign}}}(x_1^{(k-1)}-x_2^{(k-1)})\);

\(p^{(k)}_1 = p^{(k-1)}_1 = p^{(k)}_2 = p^{(k-1)}_2 \in S\) and \(y^{(k)}_1 = y^{(k)}_2\) and \({{\mathrm{sign}}}(x_1^{(k)}-x_2^{(k)}) \ne {{\mathrm{sign}}}(x_1^{(k-1)}-x_2^{(k-1)})\);

\(p^{(k-1)}_1 = p^{(k-1)}_2 \in I\) and \(p^{(k)}_2 = p^{(k-1)}_2\) and \(p^{(k)}_1 \ne p^{(k-1)}_1\) and \(x^{(k-1)}_1 \le x^{(k-1)}_2\);

\(p^{(k-1)}_1 = p^{(k-1)}_2 \in S\) and \(p^{(k)}_2 = p^{(k-1)}_2\) and \(p^{(k)}_1 \ne p^{(k-1)}_1\) and \(y^{(k-1)}_1 = y^{(k-1)}_2\) and \(v_{x,1}^{(k)} > 0\) and \(x^{(k-1)}_1 \le x^{(k-1)}_2\);

\(p^{(k-1)}_1 = p^{(k-1)}_2 \in S\) and \(p^{(k)}_2 = p^{(k-1)}_2\) and \(p^{(k)}_1 \ne p^{(k-1)}_1\) and \(y^{(k-1)}_1 = y^{(k-1)}_2\) and \(v_{x,1}^{(k)} < 0\) and \(x^{(k-1)}_1 \ge x^{(k-1)}_2\);

### 2.2 Driver

A *driver* is an algorithm according to which the linear and lane-changing speeds of a car are updated. In particular, at each time step, the algorithm execution is based on the processing of (a) a set of input variables, (b) a set of state variables, (c) the driver’s car tuple, (d) a set of unmodifiable parameters and results in (a) the output of a non-empty sequence of actions and (b) the modification of the state variables.

The input variables are based on the concept of *j-lane closest car*, with \(j \in \{-1, 0, 1\}\). The *j*-lane closest car is the closest car ahead of the driver’s car on the \(y+j\) lane such that its linear distance is \(\varDelta x < d_\text {view}\), where *y* is the lane of the driver’s car and \(d_\text {view}\) is a driver’s parameter. For the sake of brevity, we omit a more formal definition, which covers also the case in which the driver’s car is in (or close to) an intersection. Note that the *j*-lane closest car could not exist for some *j*, if no cars are closer than \(d_\text {view}\) or there is no \(y+j\)-th lane.

The *input variables* are the following—see Fig. 1 for a visual interpretation of the variables.

Three

*relative movement*variables \(\delta v_{-1}\), \(\delta v_{0}\), and \(\delta v_{1}\). The value of \(\delta v_{j}\) is defined in \(\{\varnothing , \text {opposite},\)\(-1, 0, 1\}\) and is determined as follows. If there is no*j*-lane closest car, then \(\delta v_{j} = \varnothing \). Otherwise, let \((p', x', y', v'_x, v'_y, s')\) the*j*-lane closest car: if \({{\mathrm{sign}}}{v_x} \ne {{\mathrm{sign}}}{v'_x}\), then \(\delta v_{j} = \text {opposite}\), otherwise \(\delta v_{j} = {{\mathrm{sign}}}{(\left|v'_x\right|-\left|v_x\right|)}\). In other words, \(\delta v_{j}\) says if there is a*j*-lane closest car and, if any, if it moves in the opposite direction or, otherwise, is becoming closer (\(\delta v_{j} = -1\)), farther (\(\delta v_{j} = 1\)), or has the same linear speed, w.r.t. the driver’s car.Three

*closeness*variables \(\epsilon _{-1}, \epsilon _{0}, \epsilon _{1}\). The value of \(\epsilon _{j}\) is a boolean which is true if and only if there is a*j*-lane closest car and its distance \(\varDelta x\) from the driver’s car is \(\varDelta x \le d_\epsilon \), where \(d_\epsilon < d_\text {view}\) is a driver’s parameter. In other words, \(\epsilon _{j}\) is set if the*j*-lane closest car, if any, is closer than a threshold.

The *state variables* include a single variable \(d \in \mathbb {R}, d \ge 0\), which represents the distance the driver still wants to go and which is updated at each time step as \(d^{(k+1)} = d^{(k)}-|v_x^{(k)}|\). The *parameters* include \(d_\text {view}\), \(d_\epsilon \) (whose meaning was described above), a value \(v_\text {max} \in \mathbb {R}, v_\text {max} \ge 0\), and a value \(v_\varDelta \in \mathbb {R}, 0< v_\varDelta < v_\text {max}\): \(v_\text {max}\) is the maximum linear speed the driver’s car can reach and \(v_\varDelta \) represents the acceleration/deceleration of the driver’s car.

*A*of

*actions*, i.e., an ordered subset of the set \(\mathcal {A}\) of possible driver’s action, with \(\mathcal {A}=\{\uparrow , \nearrow , \rightarrow \, \searrow , \downarrow , \swarrow , \leftarrow , \nwarrow , \varnothing \}\). An action determines how \(v_x\) and \(v_y\) are updated, as shown in Table 1.

Driver’s actions.

\(a \in \mathcal {A}\) | \(v_x^{(k+1)}\) | \(v_y^{(k+1)}\) |
---|---|---|

\(\uparrow \) | \({{\mathrm{sign}}}{(v_x^{(k)}+v_\varDelta )} \min (\left|v_\text {max}\right|, \left|v_x^{(k)}\right|+v_\varDelta )\) | 0 |

\(\nearrow \) | \({{\mathrm{sign}}}{(v_x^{(k)}+v_\varDelta )} \min (\left|v_x^{(k)}\right|, \left|v_x^{(k)}\right|+v_\varDelta )\) | \(-{{\mathrm{sign}}}{v_x^{(k)}}\) |

\(\rightarrow \) | \(v_x^{(k)}\) | \(-{{\mathrm{sign}}}{v_x^{(k)}}\) |

\(\searrow \) | \({{\mathrm{sign}}}{(v_x^{(k)}+v_\varDelta )} \max (0, \left|v_x^{(k)}\right|-v_\varDelta )\) | \(-{{\mathrm{sign}}}{v_x^{(k)}}\) |

\(\downarrow \) | \({{\mathrm{sign}}}{(v_x^{(k)}+v_\varDelta )} \max (0, \left|v_x^{(k)}\right|-v_\varDelta )\) | 0 |

\(\swarrow \) | \({{\mathrm{sign}}}{(v_x^{(k)}+v_\varDelta )} \max (0, \left|v_x^{(k)}\right|-v_\varDelta )\) | \({{\mathrm{sign}}}{v_x^{(k)}}\) |

\(\leftarrow \) | \(v_x^{(k)}\) | \({{\mathrm{sign}}}{v_x^{(k)}}\) |

\(\nwarrow \) | \({{\mathrm{sign}}}{(v_x^{(k)}+v_\varDelta )} \min (\left|v_x^{(k)}\right|, \left|v_x^{(k)}\right|+v_\varDelta )\) | \({{\mathrm{sign}}}{v_x^{(k)}}\) |

\(\varnothing \) | \(v_x^{(k)}\) | 0 |

The driver’s algorithm.

\(\delta v_{1}\) | \(\delta v_{0}\) | \(\delta v_{-1}\) | \(\epsilon _{1}\) | \(\epsilon _{0}\) | \(\epsilon _{-1}\) | \(d \le x_\text {stop}\) |
| |
---|---|---|---|---|---|---|---|---|

1 | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\{\text {T}{}\}\) | \(\{\downarrow , \searrow , \swarrow \}\) |

2 | \(\{\varnothing ,1\}\) | \(\{\varnothing ,1\}\) | \(\{\varnothing ,1\}\) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\{\uparrow , \nearrow , \nwarrow , \varnothing , \downarrow \}\) |

3 | \(\forall \) | \(\{\varnothing ,1\}\) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\{\uparrow , \varnothing , \downarrow \}\) |

4 | \(\forall \) | \(\{-1,0\}\) | \(\forall \) | \(\forall \) | \(\{\text {F}\}\) | \(\forall \) | \(\forall \) | \(\{\uparrow , \varnothing , \downarrow \}\) |

5 | \(\{\varnothing ,1\}\) | \(\{-1,0\}\) | \(\forall \) | \(\{\text {F}\}\) | \(\{\text {T}\}\) | \(\forall \) | \(\forall \) | \(\{\nwarrow , \varnothing , \downarrow \}\) |

6 | \(\forall \) | \(\{-1,0\}\) | \(\{\varnothing ,1\}\) | \(\forall \) | \(\{\text {T}\}\) | \(\{\text {F}\}\) | \(\forall \) | \(\{\nearrow , \varnothing , \downarrow \}\) |

7 | \(\forall \) | \(\{0\}\) | \(\forall \) | \(\forall \) | \(\{\text {T}\}\) | \(\forall \) | \(\forall \) | \(\{\varnothing , \downarrow \}\) |

8 | \(\forall \) | \(\{-1\}\) | \(\forall \) | \(\forall \) | \(\{\text {T}\}\) | \(\forall \) | \(\forall \) | \(\{\downarrow \}\) |

9 | \(\forall \) | \(\{\text {opposite}\}\) | \(\forall \) | \(\forall \) | \(\forall \) | \(\{\text {F}\}\) | \(\forall \) | \(\{\rightarrow , \searrow , \downarrow \}\) |

10 | \(\forall \) | \(\{\text {opposite}\}\) | \(\forall \) | \(\{\text {F}\}\) | \(\forall \) | \(\forall \) | \(\forall \) | \(\{\leftarrow , \swarrow , \downarrow \}\) |

11 | \(\forall \) | \(\{\text {opposite}\}\) | \(\forall \) | \(\forall \) | \(\{\text {T}\}\) | \(\forall \) | \(\forall \) | \(\{\searrow , \swarrow , \downarrow \}\) |

12 | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\forall \) | \(\{\downarrow , \searrow , \swarrow \}\) |

In other words, an up arrow corresponds to accelerating and a down arrow corresponds to braking; a right arrow corresponds to moving on the right lane and a left arrow corresponds to moving on the left lane, and so on. The driver executes only one of the actions in *A*. The action which is actually performed is chosen after processing *A* according to a procedure that is detailed in Sect. 2.4 which takes traffic rules into account.

The driver’s algorithm is presented in the form of a multiway branch control in Table 2. The rationale for the proposed algorithm is to resemble the behavior of a “reasonable” driver who aims at traveling a distance *d* while avoiding trivial collisions: that goal is pursued by—in essence—trying to travel at the max linear speed while avoiding hitting other cars on the same lane. In detail, each row of Table 2 represents a proposition and the corresponding output *A*. The proposition is composed of the conjunction of membership checks on input variables and on the result of the comparison of the state variable *d* against \(x_\text {stop}\), which is defined as \( x_\text {stop} = k_\text {stop} v_x + \frac{1}{2}\left( k_\text {stop}^2-k_\text {stop}\right) v_\varDelta \) where \(k_\text {stop} = \left\lceil \frac{v_x}{v_\varDelta } \right\rceil \); \(x_\text {stop}\) represents the distance the driver’s car would run if it constantly decrease its speed until stopping. For instance, row 4 proposition is \(\delta v_{0} \in \{-1,0\} \wedge \epsilon _{0} \in \{\text {F}\}\), row 5 proposition is \(\delta v_{1} \in \{\varnothing ,1\} \wedge \delta v_{0} \in \{-1,0\} \wedge \epsilon _{1} \in \{\text {F}\} \wedge \epsilon _{0} \in \{\text {T}\}\). The output is determined as follows: if the row 1 proposition is true, then the output is the row 1 *A*; otherwise, if the row 2 proposition is true, then the output is the row 2 *A*, and so on—note that the last row proposition is always true, hence it is guaranteed that a non empty sequence is always output.

### 2.3 Rules

A *traffic rule* is a predicate defined on a set of variables concerning a car and its driver, its *j*-lane closest cars, and the road graph. A car *breaks* a rule at a given time step if the corresponding predicate is false.

The variables on which a traffic rule can be defined include: (a) variables concerning the car and the corresponding driver: \(\hat{v}_x\), \(v_\text {max}\), \(v_\varDelta \), \(d_\text {view}\), \(d_\epsilon \), *p*, \(\hat{x}\), and \(\hat{y}\), where \(\hat{v}_x=\left|v_x\right|\), \(\hat{x} = l(p)-x\) and \(\hat{y} = y\), if \(v_x \ge 0\), and \(\hat{x} = x\) and \(\hat{y} = w(p)-y\), otherwise; (b) variables concerning the car *j*-lane closest cars: \(\delta v_{-1}\), \(\delta v_{0}\), \(\delta v_{1}\), \(\varDelta x_{-1}\), \(\varDelta x_{0}\), and \(\varDelta x_{1}\), where \(\varDelta x_j\) is defined as in Sect. 2.2 and is set to \(+\infty \) if the corresponding \(\delta v_j = \varnothing \); (c) variables concerning the road graph section or intersection in which the car is: *l*(*p*) and *w*(*p*).

*r*is the rule) in Fig. 2.

For example, the rule stating that “the maximum speed of a car is 20” is written as \(\hat{v}_x \le 20\). The rule stating that “the car should stay on the rightmost free lane” is written as \(\hat{y} \le 0 \vee \varDelta x_{-1} \le 10\), where 10 represents a distance within which a lane is not considered free. The rule stating that “the car should proceed slowly when approaching an intersection” is written as \(\lnot \hat{x} \le 20 \vee \hat{v}_x \le 10\).

### 2.4 Rules-Aware Driver

A *rules-aware driver* is a driver that selects exactly one *winning action* out of a sequence \(A=(a_1, a_2, \dots )\) of actions, given a set *R* of traffic rules. In brief, a rules-aware driver selects the action which, if repeated for the next steps, will cause the least number of broken rules.

More in detail, the selection is performed as follows. First, for each action \(a_i\) in *A*, the sequence \(A_i\) of future actions consisting of \(a_i\) repeated \(k_\text {advance}\) times is considered. Second, the number \(n_i\) of future broken rules caused by \(A_i\) is computed as the sum of the number of rules that would be broken at each future step \(k+j\), with \(0 \le j \le |A_i|\). Third and finally, the winning action is determined as the action \(a_{i^\star }\) for which \(\frac{n_{i^\star }}{|A_{i^\star }|}\) is the lowest—in case of tie, the action with the lowest index in *A* is selected. When computing \(n_i\), the rules-aware driver predicts the future variable values assuming that: (i) the *j*-lane closest cars, if any, will maintain the same speeds of step *k*; (ii) no other *j*-lane closest cars will appear; (iii) the driver’s car will update consistently with the sequence of actions \(A_i\). If a sequence \(A_i\) is such that the future position *p* of the car changes, the sequence is truncated to the last element before that change.

## 3 Grammatical Evolution

Grammatical Evolution (GE) [11] is a form of grammar-based Genetic Programming (GP) [6] which can evolve strings belonging to a language \(\mathcal {L}(\mathcal {G})\) defined by a context-free grammar \(\mathcal {G}\). In brief, GE operates on *genotypes*, which are variable-length bit strings, maps them to *phenotypes*, which are strings of \(\mathcal {L}(\mathcal {G})\), and finally associates them with fitness values in \(\mathbb {R}\).

The genotype-phenotype mapping procedure is the distinguishing trait of GE. In this procedure, the genotype is viewed as a variable-length integer string where each *i*-th integer, called *codon* and denoted by \(g_i\), is obtained by decoding bits from the \((8(i-1))\)-th to the \((8i-1)\)-th, included, in the genotype *g*. The procedure is iterative and starts by setting the phenotype to \(p=s_0\), \(s_0\) being the grammar starting symbol, a counter *i* to 0, and a counter *w* to 0. Then, the following steps are iterated.

- 1.
The leftmost non-terminal

*s*in*p*is expanded using the*j*-th option (zero-based indexing) in the production rule \(r_s\) for*s*in \(\mathcal {G}\), with*j*being the remainder of the division between the value \(g_i\) of the*i*-th codon (zero-based indexing) and the number \(|r_s|\) of options in \(r_s\), i.e., \(j = g_i\mod |r_s|\). - 2.
The counter

*i*is incremented by 1; if*i*exceeds the number of codons, i.e., if \(i>\frac{|g|}{8}\), then*i*is set to 0 and*w*is incremented by 1—the latter operation is called wrapping and*w*represents the number of wraps performed during the mapping. - 3.
If

*w*exceeds a predefined threshold \(n_w\), the mapping is aborted, i.e., a null phenotype is returned which will be associated to the worst possible fitness. - 4.
If

*p*contains at least one non-terminal to be expanded, return to step 3, otherwise end.

The search engine of GE, i.e., the way in which the population of individuals is updated across subsequent generations, is conventionally based on Genetic Algorithms (GA). In Sect. 4.2 we provide the evolutionary parameters values which we used in our experimentation.

*fitness function*

*f*, which maps a phenotype to a number in \(\mathbb {R}\). In our case, phenotypes are sets of rules and hence we modified the grammar of Fig. 2, which describes the language for defining a single rule

*r*, by replacing the first rule in order to make it defining a rule set

*R*, as follows:

Concerning the fitness function, we aimed at defining a function which captures two desired high-level objectives of a set *R* of road traffic rules: (i) traffic flow regulated by *R* should allow car drivers to reach their destination without taking too long time, i.e., with a large average speed, and (ii) traffic flow regulated by *R* should result in no or few collisions. It can be noted that, in principle, the two objectives are conflicting: for instance (and simplifying), a set *R* imposing a very low speed limit will likely prevent many collisions, but will cause long traveling times for all drivers; on the other hand, a set *R* not imposing any speed limit will allow drivers to quickly reach their destination, but will likely result in many collisions.

*average speed ratio*(ASR) is the ratio between the actual average speed \(\frac{d_\text {tot}}{k_\text {tot}}\) a driver traveled at and the maximum theoretical average speed \(v_\text {max}\). The

*collision-per-time*(CpT) is the ratio between the number \(n_\text {collision}\) of collisions a car had during its travel and \(k_\text {tot}\), where \(n_\text {collision} \in \{0,1\}\). For the former index, the greater, the better; the opposite for the latter. Hence, instead of ASR, we considered \(1-\text {ASR}\), i.e., \(\left( 1-\frac{d_\text {tot}}{k_\text {tot}}\frac{1}{v_\text {max}}\right) \). We associate a rule set

*R*with a fitness value which is a linear combination of the two indexes averaged across all cars \(n_\text {car}\) during a number \(n_\text {sim}\) of simulations:

## 4 Experiments

We performed two experimental campaigns. The first one aimed at validating our road traffic model. The second one aimed at verifying the effectiveness of our GE-based approach for the synthesis of road traffic rules.

### 4.1 Validation of the Road Traffic Model

Model and simulation parameters.

Param | Meaning | Value |
---|---|---|

\(d_\text {collision}\) | Minimum distance between cars without collision | 1 |

\(k_\text {removal}\) | Time steps before collided cars remotion | 100 |

\(d_\text {view}\) | Driver’s view distance | 30 |

\(d_\epsilon \) | Driver’s safety distance | 10 |

\(v_\text {max}\) | Driver’s maximum speed | \(\sim U(1.5,3.5)\) |

\(v_\varDelta \) | Driver’s acceleration (deceleration) | 0.1 |

\(k_\text {advance}\) | Driver’s rules forethought time steps | 10 |

\(d^{(0)}\) | Driver’s distance to travel (i.e., initial | 2000 |

| | Number of road sections | 5 |

| | Number of road intersections | 4 |

\(w(p), p \in S\) | Number of lanes | \(\in \{2,3,4\}\) |

\(l(p), p \in S\) | Section length | \(\in \{100, 100\sqrt{2}\}\) |

\(n_\text {car}\) | Cars in the simulation | \(\in \{2,5,8,11,14,17,20\}\) |

\(k_\text {dead}\) | Dead car removal time steps | 100 |

\(k_\text {max}\) | Simulation time steps | 5000 |

We performed each simulation by maintaining constant the number \(n_\text {car}\) of cars in the graph during the whole simulation. To this end, during the simulation, we removed a car and added a new one in a random position whenever at least one of the two following conditions was met: (a) the driver’s state variable *d* (i.e., the distance the driver still wants to travel) became lower or equal to zero, or (b) exactly \(k_{\text {dead}}\) time steps passed since the car state *s* switched from alive to dead, i.e., since the step when the car was involved in a collision. Concerning the former condition, we recall that drivers do not have a specific destination; instead, their goal is to travel for a predefined distance.

Hand-written rules.

Rule | Explanation | |
---|---|---|

1 | \(\hat{y}\le 0.0\textsc {e}0 \vee \varDelta x_{-1} \le 2.0\textsc {e}1\) | Stay on the rightmost free lane |

2 | \(\hat{y}\le 1.0\textsc {e}0\) | Stay on the first or second rightmost lane |

3 | \(\lnot \hat{x}\le 3.0\textsc {e}1 \vee \hat{v}_x \le 1.5\textsc {e}0\) | When close to end of (inter)section, proceed slowly |

4 | \(\lnot \hat{x}\le 2.0\textsc {e}1 \vee \hat{v}_x \le 0.5\textsc {e}0\) | When closer to end of (inter)section, proceed more slowly |

5 | \(\lnot \hat{x}\le 3.0\textsc {e}1 \vee \hat{y}\le 0.0\textsc {e}0\) | When close to end of (inter)section, stay on the rightmost lane |

6 | \(\lnot \varDelta x_0 \le 2.0\textsc {e}1\) | Do not travel too close to the preceding car |

7 | \(\lnot \hat{v}_x \le 0 \vee \varDelta x_0 \le 2.0\textsc {e}1\) | When too close to the preceding car, stop |

8 | \(\hat{v}_x \le 2.4\textsc {e}0\) | Do not exceed a maximum speed |

Observing the plots of Fig. 3, it can be seen that the relation between the amount of traffic (\(n_\text {car}\)) and the two indexes looks sound, i.e., consistent with what happens in real world road traffic: the greater the number of traveling cars, the lower the average speed (i.e., the longer the time to destination) and the greater the overall number of collisions. From another point of view, there is a trade-off between efficiency and safety of the road traffic, like in the real world.

Moreover, by comparing the curves related to no rules and hand-written rules, it can be seen that enforcing a road traffic regulation results in a different point in the above-mentioned trade off: with the hand-written rules traffic is in general less efficient but safer. This findings suggest that our models for the road graph, the car, and the driver are sufficiently consistent with the real word and hence, in our opinion, adequate to investigate the feasibility of an automatic synthesis of road traffic rules.

### 4.2 Synthesis of Traffic Rules

GE parameters.

Population | 100 | Crossover rate | 0.9 |

Generations | 100 | Crossover operator | Two-points |

Initial genotype size | 512 | Mutation operator | Bit flip with \(p_\text {mut}=0.01\) |

Max wraps | 10 | Selection | Tournament with size 5 |

We first analyze extensively the set of rules which obtained the best fitness among the final generations of the 30 runs, which is shown in Table 6 and discussed later. We experimentally verified how this set of best GE rules affected the traffic with different amounts of injected traffic by running 10 simulations for each value of \(n_\text {car} \in \{2,5,8,11,14,17,20\}\). In other words, since GE rules were generated with \(n_\text {car}=10\), we evaluated the generalization ability of our rules synthesis approach. The results are shown in Figs. 3 and 4.

Best GE rules.

Rule | Possible explanation | |
---|---|---|

1 | \(\varDelta x_{-1} \le 4.8\textsc {e}0\) | Stay within some distance from the car on right lane |

2 | \(\lnot \hat{v}_x\le 1.1\textsc {e}1\) | Maintain at least a minimum speed |

3 | \(\lnot \delta v_0 = \text {opposite} \vee \delta v_0 = 1 \vee \)\(\lnot p \in S \vee \varDelta x_0 \le 0.3\textsc {e}1\) | When in an section, stay close to a car coming on the same lane |

4 | \(\hat{v}_x\le 1.2\textsc {e}0\) | Do not exceed a maximum speed |

Figure 4 shows the same results of Fig. 3 from another point of view, by plotting the overall number of collisions in the simulation (i.e., \(\sum n_\text {collision}\), on *y*-axis) against the overall traveled distance in the simulation (i.e., \(\sum d_\text {tot}\), on *x*-axis), averaged across simulations with the same \(n_\text {car}\), one curve for each set of rules. The figure allows to appreciate the trade-off between traffic efficiency and safety: the larger the overall distance, the larger the overall number of collisions. However, it can also be seen that the curve of GE rules strictly dominates both the other two curves (no rules and hand-written rules), hence suggesting that GE rules may be a better way of regulating road traffic regardless of the amount of cars in the graph—i.e., not only for the \(n_\text {car}\) for which the GE rules were generated. This latter finding seems to confirm the good generalization ability of our approach.

Figure 4 also shows another interesting property of our traffic model, namely it highlights the congestion condition. In facts, in both the cases where the traffic is regulated (hand-written and GE rules), there is a maximum number of cars in the graph (\(n_\text {car}=17\)) after which no further increasing in the overall distance can be obtained, while an increasing in overall number of collisions occurs. Interestingly, despite the fact that the maximum injected traffic before congestion in the two cases is the same, with GE rules the resulting overall distance is greater and the resulting overall number of collisions is smaller.

Table 6 shows in detail the best GE rules: it can be seen that the set consists of four rules, one of which (the 2nd) is clearly always broken in our simulations, since it tries to impose a minimum linear speed which cannot be reached with the parameters shown in Table 3. Rule 4 is easily understandable and its inclusion in best individual is not surprising. The role, if any, of rules 1 and 3 on the drivers’ behavior is not clear. Rule 3 is hard to understand, i.e., hard to translate in natural language: the third disjunction says that the remaining part of the rule applies only when the car is in a section (since if \(p \in I \equiv p \not \in S\), the rule is true); the first and second disjunctions can be rewritten as \(\delta v_0 \in \{\emptyset , -1, 0, 1\}\), hence resulting in the rule being writable as \((p \in S \wedge \delta v_0 = \text {opposite} ) \implies \varDelta x_0 \le 3\). However, due to the driver’s algorithm (see Table 2) and the value of the parameter \(d_\epsilon \) (see Table 3), it is unlikely that rule 3 plays an actual role in determining drivers’ behavior.

The analysis of the rules of Table 6 may suggest that some mechanism for detecting and cleaning ineffective rules may be beneficial in order to (i) increase the readability/understandability of the generated rules, and (ii) improve the evolutionary search. While we cannot make any meaningful prediction in this respect, we observe that, for the former goal, some automatic and domain-specific heuristic applied after the evolution may suffice—indeed a similar approach have been applied in [8] in the field of the automatic generation of rules for security policies.

It can be seen from Fig. 5a that GE is in general able to decrease the fitness of individuals during the evolution: however, after approximately 20 generations, fitness decreases much more slowly. We speculate that some improvement to evolutionary search might be obtained by finely tuning the GE-related parameters, or maybe by using a different variant of GE (e.g., [7, 9]). However, we also note that our scenario is characterized by a remarkably high stochasticity which could, at least, slow down the search of a good solution and, from a more practical point of view, makes experimentation and parameter tuning costly due to the long time needed to compute the fitness. In our case, running a single simulation took \(\approx \) 10 s on commodity hardware and computing the fitness of a single individual (i.e., a set of rules) consisted in running 10 simulations, precisely to mitigate the impact of the high stochasticity in the simulations.

Figure 5b shows the histogram of the best individual fitness at the end of the evolution across the 30 runs. It can be seen that most of the runs resulted in fitness values close to 1, i.e., the distribution is skewed toward bad values. We analyzed the details of the runs and found that in some of those cases the search got stuck in a local minimum corresponding to a set of rules including one or more rules which, in practice, enforce drivers to stand still. Under those rules, no collision occurs (\(\text {CpT}=0\)) and no one moves (\(\text {ASR}=0\)), which is, clearly, one extreme of the trade-off between traffic efficiency and safety.

## 5 Concluding Remarks and Future Work

We proposed and assessed experimentally, by an extensive set of simulations, a method for synthesizing automatically a set of road traffic rules with the aim of maximizing such global indexes as road efficiency (high average speed) and safety (low number of collisions). We are motivated by a future scenario in which human and machine-based drivers will coexist on the roads, making current road regulation possibly unsuitable or inefficient. We are not aware of any similar proposal.

Our method is based on GE: individuals are sets of rules written according to a context-free grammar that we designed ad hoc to express quite expressive concepts such as, e.g., “stay on the rightmost free lane” or “slow down when approaching an intersection”. The fitness of a candidate set of rules is given by a weighted sum of the traffic efficiency and safety resulting from the application of the rules set in a number of simulations, which makes this GE application highly stochastic.

Results of our experimental evaluation are promising, since generated rules result in simulated road traffic which is more efficient and safer than that regulated by hand-written rules or not regulated at all.

Our work may be extended in different ways, such as: (i) including a more fine-grained model (e.g., concerning intersections); (ii) considering a different way for expressing rules (e.g., with Linear Temporal Logic); (iii) better exploring GE parameters and/or variants. We plan to investigate some of these research lines in future works.

## Acknowledgements

The authors are grateful to Lorenzo Castelli for his insightful comments.