1 Introduction

The Chinese Postman Problem (CPP), for the first time proposed by the mathematician Kwan Mei-Ko in 1962 [1], is a problem that consists of finding the shortest route to deliver mail, traveling along every road and coming back to the post office [2].

From its first formulation onward, the CPP has been the subject of many real-life routing contexts, especially in urban applications such as garbage collection service, street sweeping, snow removal, sprinkling of salt on roads, network maintenance, security patrolling, and postal delivery service (mail carrier, newspaper boy) [3, 4].

To provide practical illustrations, real estate agents must traverse on foot through the streets of their area of interest within the city. They must navigate house by house to identify properties whose owners are interested in renting or selling as efficiently as possible. They aspire to accomplish this task in the shortest amount of time. In certain cities or specific sections, the need arises for pedestrian delivery to all residents, akin to the classical “newspaper boy problem". This involves manually sweeping streets and, in some cases, collecting waste and depositing it into carts. Such scenarios are particularly prevalent in historic city centers with narrow, winding streets. Not to mention street tourism, exemplified by a photographer who wishes to explore every thoroughfare in a quaint ancient village before deciding on subjects for photography. The underlying challenge across all these instances is solving the Chinese Postman Problem (CPP).

The CPP belongs to the Vehicle Routing Problem (VRP) class. However, many applications are out of this category, for example, robot exploration, interactive system analysis, and website usability [5].

Considering graph theory, given a connected undirected graph G(NE) with N nodes and E edges with non-negative weights, which can be seen, for example, as the modeling of a road network where nodes are the crossroads and edges the roads, the CPP problem consists in finding a minimum-length path that starts at any node and traverses the network edges at least once before returning to the starting point [3].

One of the methodologies that can be used to solve this problem is to exploit the concept that a connected undirected graph G(NE) has at least one circuit that traverses every edge exactly once if and only if G contains exactly zero nodes of odd-degrees, i.e., if every node has an even degree, i.e., if it is an Euler graph [6].

A connected undirected graph G(NE) always has a pair number of odd-degree nodes, so it can be turned into an augmented graph \(\bar{G}(N, E \cup E' )\) by adding duplicated edges that transform G(NE) into an Euler graph [6].

In the CPP literature, several works use the Euler graph methodology for solving efficiently the CPP on directed and undirected graphs [3, 6,7,8,9,10,11,12]. However, among them, very few are the methods that belong to the class of meta-heuristics, and within this group, we can mention Genetic Algorithms (GA) [5] and DNA computing [13].

Meta-heuristics are a kind of algorithmic concepts that can be used to define heuristic methods applicable to a broad set of different problems, i.e., in other words, general-purpose algorithmic frameworks that can be applied to various optimization problems with relatively few modifications [4, 14,15,16].

Within the domain of meta-heuristic optimization techniques, there exists a diverse array of methodologies encompassing approaches like Ant Colony Optimization (ACO) [17,18,19,20,21,22], Genetic Algorithms (GA) [17, 19,20,21,22,23,24], Particle Swarm Optimization (PSO) [17, 18, 22, 23, 25, 26], Simulated Annealing (SA) [24, 26], Artificial Bee Colony (ABC) [18, 25], Tabu Search (TS) [19, 24], Bat Algorithm (BA) [26], Cuckoo Search (CS) [25], Firefly Algorithm (FA) [25], Grey Wolf Optimization (GWO) [25], Harmony search (HS) [25], Whale Optimization (WOA) [27], Brain Storm Optimization (BSO) [17], among others (Table 1).

Although, over the years, new meta-heuristics have been proposed in the literature, ACO, GA, and PSO have consistently demonstrated their remarkable adaptability to diverse problem structures. Consequently, these three methods find frequent application in many practical optimization scenarios (Table 1).

Among these three meta-heuristics, ACO appears to be better suited to handle the CPP problem than the others. This preference stems from its extensive history of successful applications closely aligned with the distinctive characteristics inherent to CPP. Notably, ACO’s inaugural deployment transpired in the Traveling Salesman Problem (TSP) context, and subsequently, it was used successfully for a wide array of vehicle routing problems (VRP). Furthermore, it has been harnessed to tackle over one hundred problems classified within the NP-hard category. Comprehensive analysis reveals that ACO possesses two pivotal strengths: remarkable efficiency attributed to its adequate local search capabilities and the capacity to facilitate a robust probabilistic and adaptive solution generation process across the search space [4].

Table 1 Most recent articles related to the following two search modes: “Comparisons between meta-heuristics” and “meta-heuristic approaches for solving the vehicle routing problem, Chinese postman problem, and traveling salesman problem”

ACO is part of the family of meta-heuristic algorithms. It is a Swarm Intelligence (SI) method that takes inspiration from the behavior of some ant species depositing pheromones on the ground to mark favorable paths that can be followed by other members of the colony from the nest to a source of food [4].

ACO algorithms have been generally applied for solving optimization problems, generating several artificial ants that build paths that converge to exact or approximate solutions by exchanging quality information via a communication scheme inspired by ant colony working characteristics [4].

In literature, ACO was used successfully to tackle VRP problems such as shortest path problem (SPP) [28, 29] and traveling salesman problem (TSP) [4], but, to the best of our knowledge, no ACO applications are present for solving CPP.

This paper tries to fill this gap by presenting a new algorithm for CPP called ACO-CPP; the algorithm has been tested and compared with a Genetic Algorithm (GA) and a recursive algorithm that explores all possible solutions. The results show ACO-CPP proves to be efficient and consistent. Moreover, it performs equally or better than GAs.

The present work is structured as follows: Sect. 2 presents problem definitions and notations about CPP; Sect. 3 shows the general ACO framework and Sect. 4 shows its real-word applications and most notable variants; Sect. 5 describes the proposed ACO-CPP algorithm; Sect. 6 presents the experimental results; Sect. 7 presents the conclusions drawn.

2 Problem definition

The following is a list of definitions useful to present the notation used and enhance the understanding of this paper (Fig. 1).

  • Undirected graph G(NE) \(\longrightarrow\) consists of a set of N of nodes and a set E of undirected edges where each edge \(e_{u} = (n_i, n_j)\) in E connects two nodes \(n_i\) and \(n_j\) in N, and has a positive weight \(w(e_{u})\) [2].

  • Connected undirected graph \(\longrightarrow\) represents a graph with a path for every pair of nodes \(n_i, n_j \in N\), i.e., from each node \(n_i\) to every node \(n_j\) [3].

  • Path (or chain) \(\longrightarrow\) in an undirected graph G is a sequence of adjacent edges connected through a node each other [3].

  • Circuit (or cycle) \(\longrightarrow\) consists of a closed path whose initial and final nodes coincide, i.e., a path where the starting point corresponds to the ending point [3].

  • Degree of a node \(d(n_i)\) \(\longrightarrow\) in an undirected graph G, is the number of edges which is incident to the node [2].

  • Eulerian path (or Eulerian trail) \(\longrightarrow\) is a path that visits every edge exactly once (allowing for revisiting vertices more times).

  • Euler’s Circuit (or tour, or cycle) \(\longrightarrow\) is an Eulerian path that starts and ends on the same node, i.e., a circuit traverses every edge exactly once. A connected undirected graph G where at least an Eulerian circuit exists is defined as Euler’s graph.

Fig. 1
figure 1

Graphs representation and its characteristics

In terms of Graph Theory, in a connected undirected graph G(NE), the CPP final objective is finding a closed path (or circuit) that visits every edge at least once in such a way as to obtain a tour with the lowest total cost [5, 2].

An essential and well-known result in the literature as part of Euler’s Theorem claims that a connected undirected graph G has a circuit that traverses every edge exactly once if and only if precisely zero nodes of odd-degrees are contained in it (i.e., if every node has an even degree) [3].

This means that when every node in a graph G has an even degree, at least one Eulerian tour exists, and all possible Eulerian tours represent optimal solutions to the CPP. Thanks to this result, a possible approach for addressing the CPP, given a graph G comprising both even and odd-degree nodes, involves the conversion of odd-degree nodes into even-degree nodes. This transformation renders the graph even, and an even graph inherently possesses Euler’s tours. One possible method for solving the CPP, when provided with a graph, is to add edges \(e_u'\in E'\) to G to find the minimum-cost augmentation \(G'\) of G that satisfies the even-degree property for all nodes, i.e., \(d(n_i) = 2k \ \ \forall n_i \in N\).

Suppose the degree of each node of a graph G is even. In that case, at least an Eulerian tour exists. Therefore, a possible CPP solution consists of finding the minimum-cost augmentation \(G'\) of G by adding edges in such a way as to satisfy the even nodes’ property. In this way, an augmented Euler’s graph \(\bar{G} = G \cup G'\) will be generated, and the solution will be finally found by identifying a route over it [5, 2]. The problem of adding edges in such a way to satisfy the even nodes property can be mathematically formalized as follows:

Find the set:

$$\begin{aligned} E'\subset E \end{aligned}$$
(1)

Minimize:

$$\begin{aligned} W =\sum w(e_u'),\ \ \ e_u'\in E' \end{aligned}$$
(2)

Subject to:

$$\begin{aligned} \forall n_i \in N \,\, \exists k \in \mathbb {N} \mid d(n_i) = 2k \ \ \end{aligned}$$
(3)

\(\bar{G} = G \cup G' = (N, E \cup E')\) is the Euler’s graph where there is the solution of the CPP, \(G'\) is a minimum-cost augmentation of the graph, and \(d(n_i)\) is the degree of the generic node \(n_i\). In this case, it is required that each node becomes even [2].

The Handshaking Lemma states that in every finite undirected graph, the number of nodes with odd degrees is always even [30,31,32]. Therefore, a widespread method used to satisfy the even nodes property consists of connecting pairings of odd nodes through shortest paths (SPs). Solving the CPP, therefore, means finding the minimum-cost augmentation composed of the sum of SPs between couples of nodes with odd degrees.

As long as a method of solution for the CPP consists of finding a minimum-cost augmentation, an algorithmic method that wants to explore all the possible solutions to turn a non-Euler graph G into an Euler graph \(\bar{G}\) and find in it an Euler tour would be written in the following steps:

  1. 1.

    Find all the odd nodes in the graph G. The total number is always even [3, 5].

  2. 2.

    Determine each possible pairing of odd nodes and find all the possible pairwise matching of SPs [3].

  3. 3.

    Explore all the possible combinations of pairings of odd node couples.

  4. 4.

    Select the one with the shortest total length.

  5. 5.

    Build a graph \(\bar{G} = G \cup G'\).

  6. 6.

    Find an Euler tour on \(\bar{G}\).

The final tour is a circuit that is a CPP optimal solution on the original graph, and its length is equal to the total length of the edges in E plus the total length of the edges \(E'\) due to the minimum-length matching [3]. An Euler graph makes it easy to find the Euler tour using well-known algorithms such as Fleury’s or Hierholzer’s algorithm (description in Sect. 5)[33, 3].

Exploring all the possible combinations of pairings of odd node couples is an exact method. However, it is unfeasible for problems with significant amounts of odd nodes as, given the number of odd nodes \(N_{odds}\) of a graph G, the number of possible combinations of pairings of odd node couples c is the semi-factorial of the number \(n^* = N_{odds}-1\) which corresponds to the product of the number \(n^*\) with all the odd integers preceding it, that is:

$$\begin{aligned} c = n^*!! = n^* \cdot (n^*-2) \cdot (\ldots ) \cdot 3 \cdot 1 = \prod _{m=0}^{\frac{n^*-1}{2}} 2m+1, \ \ n^* = 2k+1, \ \ k \in \mathbb {N} \end{aligned}$$
(4)

3 Evolving the solution: the ACO strategy

In numerous ant species, ants release a substance known as pheromone onto the ground as they travel to and from a food source. This pheromone presence is detected by fellow ants, who then choose routes with higher concentrations. This mechanism enables ants to transport food to their nest efficiently.

Ant colony optimization is a meta-heuristic iterative algorithm formalized for the first time by Dorigo et al. [34]. Within the framework of ACO, artificial ants work collectively to construct solutions for an optimization problem. They share information about solution quality using a communication scheme (pheromone updating) that resembles the natural communication methods employed by real ants.

The objective of the pheromone update process is to enhance the pheromone levels associated with favorable or promising solutions while reducing those connected with unfavorable ones. Typically, this is accomplished through two main steps: first, a reduction in all pheromone values through pheromone evaporation, and second, an increase in the pheromone levels related to a selected group of advantageous solutions [4].

This algorithm framework generates several artificial ants at each iteration, individually building a solution by walking from node to node along a graph that encodes the problem [4]. At the moment of each choice, an ant selects one node rather than another according to a stochastic mechanism biased by a parameter \(\tau\) called pheromone, a variable associated with each edge that can be read and modified by ants and whose aim is to be higher where the solution results more convenient.

More specifically, let us consider a generic node \(n_{i}\) connected to a certain number of edges in which an artificial ant is located; each edge has a probability of being crossed that is a combination of a stochastic mechanism value with a proportional-to-pheromone value associated with it.

At the end of each iteration, based on the quality of the solutions constructed by the ants, pheromone values are updated of an amount \(\Delta \tau\) in such a way as to bias the next iteration ants in constructing solutions similar to the best ones obtained in the previous iteration. In other words, after the pheromone is deposited, subsequent artificial ants are brought to use pheromone information as a guide toward best quality search space regions [4].

Inside the iteration, the ACO meta-heuristic algorithmic general framework can be traced back to three main phases as shown in Algorithm 1:

  1. 1.

    AntsGeneration: a set of \(N_\text {ant}\) artificial ants is generated and constructs solutions traveling through feasible steps without violating the constraints. Given a graph G, the process can be imagined as a walk on it where the choice of each edge is guided by a stochastic mechanism biased by the pheromone, respectively, associated. The rule of the stochastic-pheromone choice varies across the different ACO algorithms proposed in literature [4].

  2. 2.

    LocalSearch: included in state-of-the-art ACO algorithms [4] consists of improving the solutions obtained through a local search.

  3. 3.

    PheromoneUpdating: the pheromone is updated according to pheromone values that are associated with good quality artificial ant solutions, and it is decreased through pheromone evaporation [4].

Algorithm 1
figure a

ACO general framework [4]

4 ACO: real-word applications and most notable variants

ACO has widely applied to various real-world scenarios. Throughout its history, the typology of real-life applications where ACO has found greater diffusion are routing, assignment, scheduling, and subsetting problems. However, it is essential to note that this list is not exhaustive, as ACO and its variants have demonstrated their effectiveness in an extensive range of applications [4]. Recent examples of ACO applications include its utilization in solving the Waste Collection Routing Problem (WCRP). This problem involves designing an optimal route to serve all customers (represented as nodes) with minimal total travel time or distance while employing the fewest number of vehicles, considering specific constraints such as vehicle capacity [35]. ACO has also been employed to address the Dynamic Traveling Salesman Problem (DTSP). In this scenario, a supplier must deliver parcels to customers, with parcels arriving at the depot during distribution. The time a parcel arrives at the depot is referred to as its release date [36]. In E-logistics, ACO has been instrumental in tackling the VRP for service providers, where vehicles must strategically visit pick-up nodes, such as warehouses, before making customer deliveries. This multifaceted problem entails minimizing total travel costs while accommodating real-world constraints like heterogeneous vehicles, capacity limits, time windows, and driver working durations [37]. Additionally, ACO has been effectively employed in optimizing trajectory tracking for nonlinear three-rigid-link maneuvers (RLM) [38] and minimizing non-productive tool travel time in drilling processes [39]. ACO’s versatility also extends into the realm of optimizing neural network algorithms [40]. In the context of cloud computing, where service providers grapple with resource allocation, security, privacy, and virtual machine migration, ACO has proven successful in scheduling heterogeneous tasks, thus enhancing Quality of Service (QoS) for clients as the volume of client requests escalates within cloud environments [41]. Lastly, ACO has recently been employed to generate optimal trajectories for industrial robots in machining and additive manufacturing applications [42]. In summary, ACO’s versatility and effectiveness have led to its adoption in various applications, showcasing its adaptability and value in addressing complex real-world problems.

Numerous ACO algorithms have been introduced in the academic literature. In this context, we outline the primary characteristics of the three most prominent variants, namely Ant System (AS), MAX-MIN Ant System (MMAS), and Ant Colony System (ACS).

  • Ant System (AS): AS is primarily characterized by the simultaneous pheromone value updates performed by all k ants that have constructed a solution during each iteration [43]. In this scenario, the pheromone update process is executed as follows:

    $$\begin{aligned} \tau (i,j) \leftarrow (1-\rho ) \tau (i,j)+\sum _{k=1}^{N_\text {ant}} \Delta \tau _k(i,j) \end{aligned}$$
    (5)

    where \(\rho\) represents the evaporation rate, \(N_\text {ant}\) denotes the total number of ants, and \(\Delta \tau _k(i,j)\) is the amount of pheromone deposited on the edge (ij) by ant k. Ant System is the first ACO algorithm proposed in the literature [43] and is generally used for solving routing problems.

  • MAX-MIN Ant System (MMAS): distinctive features of MMAS include the exclusive pheromone trail updates performed solely by the best ant and the imposition of a pheromone value boundary [44]. The pheromone update process is executed in the following manner:

    $$\begin{aligned} \tau (i,j) \leftarrow [(1-\rho ) \tau (i,j)+\Delta \tau _\text {Best}(i,j)]_{\tau _{min}}^{\tau _\text {max}} \end{aligned}$$
    (6)

    here, \(\tau _\text {max}\) and \(\tau _{min}\) represent the upper and lower bounds set for pheromone levels, often determined empirically. The operator \([x]_{\tau _{min}}^{\tau _\text {max}}\) is defined as follows:

    $$\begin{aligned} \small [x]_{\tau _{min}}^{\tau _\text {max}}= {\left\{ \begin{array}{ll} \tau_{\text {max}}\ \ {} &{}\ \ x>\tau_{\text {max}}\\ \tau_{\text {max}}\ \ {} &{}\ \ x<\tau_{\text{min}}\\ x\ \ {} &{}\ \ \text {otherwise}\\ \end{array}\right. } \end{aligned}$$
    (7)

    and \(\Delta \tau _\text {Best}(i,j)\) is as follows:

    $$\begin{aligned} \small \Delta \tau _\text {Best}(i,j)= {\left\{ \begin{array}{ll} 1/L_\text {Best}\ \ {} &{}\ \ e_u \in \text {Tour}\\ 0\ \ {} &{}\ \ \text {otherwise}\\ \end{array}\right. } \end{aligned}$$
    (8)

    where \(L_{\text {Best}}\) represents the length of the tour undertaken by the best ant. The determination of the best tour length depends on the choices made by the designer, whether it is based on the best ant of the current iteration (iteration-best) or the best ant observed since the beginning of the algorithm (best-so-far).

  • Ant Colony System (ACS): the most interesting contributions of ACS [45, 46] is the introduction of a local pheromone update in addition to the pheromone update performed at the end of the construction process (offline pheromone update). The local pheromone update is performed by all the ants after each construction step. Each ant applies it only to the last edge traversed by using:

    $$\begin{aligned} \tau (i,j) = (1-\phi ) \tau (i,j)+ \phi \tau _0 \end{aligned}$$
    (9)

    where \(\phi \in (0,1]\) is the pheromone decay coefficient, and \(\tau _0\) is the initial value of the pheromone. The primary aim of the local update is to introduce diversity into the search conducted by subsequent ants within an iteration. By decreasing the pheromone concentration on traversed edges, ants encourage the selection of different edges by subsequent ants, generating distinct solutions. This reduces the likelihood of multiple ants producing identical solutions during a single iteration.

5 ACO-CPP: ant colony optimization for CPP

5.1 General algorithm framework

In the context of ACO, an algorithm for solving the CPP can be conceptualized as follows: The CPP involves identifying the optimal pairings of odd nodes through SPs to minimize the total cost. In our ACO-CPP approach, each ant consists of a set of pairings of odd nodes, where each pair represents an SP. The quality of an ant’s path is determined by the total length, or cost, which corresponds to the sum of its SPs.

Over several iterations, a group of ants is generated. During the initial iteration, ants are created using a heuristic scheme. Specifically, ants select random odd nodes to move between, following a rule prioritizing higher likelihood pairings resulting in shorter SPs. This initial iteration serves as a heuristic exploration phase to find better solutions. At the end of each iteration, ants that have discovered superior solutions deposit a significant amount of pheromone along their paths, while those with inferior solutions contribute less. Throughout the paths marked by pheromones, the evaporation chemical agent works to decrease pheromone levels across the solution space uniformly.

Starting from the second iteration, the pheromone parameter exerts influence. During the construction of each path, the ant is influenced by the heuristic factor and the pheromone level, which represents the collective experience of previous ants in the search. As iterations progress, the pheromone component increasingly affects the choices of the ants, leading to more ants following a common path, which indicates algorithm convergence toward a solution (Fig. 2). If the search space has been sufficiently well explored and the pheromone has guided ants to converge along the best path, the global optimum of the CPP can be attained.

Fig. 2
figure 2

Example of a convergence graph obtained by using ACO-CPP with 50 ants for 100 iterations on a graph with 48 nodes, 24 of which have odd degrees, and with edge weights ranging from 1 to 1000

In light of this operational framework, we introduce the Ant Colony Optimization for CPP (ACO-CPP). The algorithm begins by constructing a matrix denoted as D, representing the SPs between pairs of odd vertices within the initial graph G. Subsequently, the ants aim to identify the optimal combination of SPs whose total length yields the minimum-cost augmentation, denoted as \(G'\). This entails minimizing \(\sum {w(e_u)'}\). Finally, Hierholzer’s algorithm obtains the Eulerian graph \(\bar{G}\) by finding a possible Euler tour. The general ACO-CPP framework is shown in Fig. 3.

Fig. 3
figure 3

The ACO-CPP general framework

More in detail, the ACO-CPP algorithm starts from the undirected graph \(G=(N,E)\) and gives as output an undirected graph \(\bar{G}=(N,E \cup E')\) running the following procedure:

  1. 1.

    Get odd nodes: All the \(N_{odds}\) odd-degree nodes are identified in G; \(N_{odds}\) is always even [3].

  2. 2.

    Matrix of odd-pairs-SPs construction: a square anti-diagonal matrix of distances D of order \(N_{odds}\) is built, i.e., a matrix which contains in its cells the SPs between odd nodes. Each index represents an odd node, and each cell is the SP cost between the corresponding odd-degree nodes, i.e., the minimum-length matching. In other words, in such a matrix, for each pairing, we find the edges that connect the odd vertices with the shortest possible path. Dijkstra’s algorithm calculates the shortest route between each pair.

  3. 3.

    ACO search: The core of the ACO-CPP algorithm presented in this paper consists of finding the best combination of SPs between odd nodes (pairwise matching), which minimizes the total cost of the augmentation through an ACO search. According to this, two matrices, \(\eta\) and \(\tau\), are generated starting from the matrix D described as follow:

    • Attractiveness matrix(\(\eta\)): matrix of order \(N_{odds}\) computed as the reciprocal of D changing the diagonal values as 0 as in Equation (10). This matrix represents the contribution, during the selection of the shortest path (SP) by an ant, of choosing one SP over another with a higher probability based on the length of the SP:

      $$\begin{aligned} \eta (i,j)=\frac{1}{D(i,j)}, \, \, i\ne j;\,\, \eta (i,i)=0. \end{aligned}$$
      (10)
    • Pheromone matrix(\(\tau\)): matrix of order \(N_{odds}\) that at the starting point has for each cell the user-selected value \(\tau _0\) as in Equation (11). As the iterations progress, this matrix will see its values change due to the contributions of the ants from past iterations. Specifically, its cell values will change positively the more past ants have found particular SPs more convenient, considering their entire path. Simultaneously, all the cell values will change negatively due to the contribution of pheromone evaporation. This matrix represents the contribution, during an ant’s selection of an SP, of choosing one SP over another with a higher probability based on what the experience of the past ants suggests as being more convenient.

      $$\begin{aligned} \tau = \begin{pmatrix}\tau _0 &{} \dots &{} \tau _0\\ \vdots &{} \ddots &{} \vdots \\ \tau _0 &{} \dots &{} \tau _0 \end{pmatrix} \end{aligned}$$
      (11)

    At this point, it is necessary to find the best combination of odd pairings whose SPs sum is the least possible. Each combination represents an ant and signifies the additional edges needed to solve CPP.

    The ants generation consists in the generation of \(N_\text {ant}\) ants \({{{{\textbf {{\texttt {for}}}}}}}\) \(it_\text {max}\) iterations. Each ant is associated with a probability matrix P, defined as a Hadamard product (element-wise product) between \(\eta\) and \(\tau\). At the beginning of the ants construction, this matrix appears the same for all ants in the current iteration. However, its values are subsequently modified differently based on the choices made by the ants during their construction process. Each cell represents a potential choice (SP) made by an ant as it traverses its path. Following this scheme, when an ant selects one cell over another, it implies that the ant chooses an SP between two nodes rather than an alternative one. Specifically, matrix P is described as follows:

    • Probability matrix (P): matrix defined as the Hadamard product of Equation (12) with \(\alpha\) and \(\beta\), two parameters user-selected called pheromone and heuristic weights, which aim to control the relative importance of the pheromone versus the heuristic information [4]:

      $$\begin{aligned} P=(\tau \alpha )\circ (\eta \beta ). \end{aligned}$$
      (12)

      Such a matrix has the aim to outline the pairings \({{{{\textbf {{\texttt {for}}}}}}}\) \(N_{odds}/2\) times. At first, a normalization of matrix P is generated:

      $$\begin{aligned} P(i,j) \leftarrow \frac{P(i,j)}{\sum _{i,j} P(i,j)}, \end{aligned}$$
      (13)

      then, through a selection criterion, a couple of odd nodes are selected; lastly, all the elements present in the rows and columns corresponding to the chosen nodes are set to 0. These three steps are repeated \({{{{\textbf {{\texttt {for}}}}}}}\) \(N_{odds}/2\) times. Our ACO-CPP used the Roulette selection method as a selection criterion. Roulette selection is a kind of selection in which the probability that one solution is selected is directly proportional to the pheromone value owned by the solution itself [47]. To establish a connection between this definition and the current problem, let us consider a scenario in which we have a probability matrix. From this matrix, we must select a single cell representing a potential SP between two odd nodes. This process involves two main steps. Firstly, we normalize all the values within the matrix by dividing them by the sum of all the values in matrix P. This normalization ensures that the values are appropriately scaled. Secondly, we compute the cumulative sum of all the elements in the matrix, resulting in a set of values falling within the range [0, 1]. At this stage, a random number in the range [0, 1] is generated, and the cumulative sum determines the boundaries within which this number falls. These boundaries indicate the selected indexes corresponding to the choice.

      After the construction of ants during an iteration, a pheromone update is applied, which has two components:

    • Pheromone reinforcement: \({{{{\textbf {{\texttt {for}}}}}}}\) each ant, pheromone is updated thought the formula of Equation (14), where \(\Delta \tau (i,j)\) is the quantity of pheromone laid on the edge \((n_i, n_j)\) by each ant, L is the length of the tour constructed, and Q is a constant user-selected:

      $$\begin{aligned} \tau (i,j) \leftarrow \tau (i,j)+\Delta \tau (i,j), \ \ with \ \ \Delta \tau (i,j)=\frac{Q}{L}, \end{aligned}$$
      (14)
    • Pheromone evaporation: \({{{{\textbf {{\texttt {for}}}}}}}\) each iteration, pheromone evaporates thought the formula of Equation (15) where \(\rho\) is a constant user-selected called Evaporation Rate:

      $$\begin{aligned} \tau (i,j) \leftarrow (1-\rho )\tau (i,j), \end{aligned}$$
      (15)

    Similarly to many other ACO algorithms in the literature [4], the ACO-CPP algorithm has features such as the pheromone updating in both its forms pheromone (reinforcement and evaporation) and the probability matrix. However, the latter is used as a matrix of SPs between odd nodes and presents the consequent manipulations described in “Probability matrix (P)” point to adapt it to the CPP structure problem. The graphic representation of the ACO search is shown in Fig. 5.

  4. 4.

    Augmented Graph construction: an Ant is a possible solution to the CPP problem (combination of odd pairings), and the WinnerAnts are the best solutions the ACO-CPP algorithm has found. The best-found combination of extra edges \(G'\) is used to generate a new augmented graph \(\bar{G} = G \cup G' = (N, E \cup E')\) that contains no nodes of odd-degree; such a graph, known as Euler Graph, has the property of containing at least one possible route that may be a solution for the CPP [2], and, such a possible route, known as Euler Tour, can be found through algorithms like Hierholzer’s one [33, 3].

  5. 5.

    Hierholzer Euler Tour: An Euler tour is found using Hierholzer’s algorithm. The general framework of Hierholzer’s algorithm is the following: choose any starting node \(n_i\), and follow a trail of edges from that node until returning to \(n_i\). It is impossible to get stuck at any node other than \(n_i\), because the even degree of all vertices ensures that, when the trail enters another node \(n_j\), an unused edge must be left \(n_j\). The tour formed this way is a closed tour but may not cover all the vertices and edges of the initial graph. As long as there exists a node \(n_k\) that belongs to the current tour but that has adjacent edges not part of the tour, start another trail from \(n_k\), following unused edges until returning to \(n_k\), and join the tour formed in this way to the previous tour. Since we assume the original graph is connected, repeating the previous step will exhaust all edges of the graph [48]. Such a circuit represents a CPP optimal solution whose length is equal to the total length of the edges in G plus the total length of the edges in the minimum-length matching [3].

Fig. 4
figure 4

Example of constructing a Hierholzer's Euler Tour using the previously augmented graph as an illustration

Fig. 5
figure 5

Workflow of the core of the ACO-CPP. Red boxes represent the nested \({{{{\textbf {{\texttt {for}}}}}}}\) iterations inside the main \({{{{\textbf {{\texttt {for}}}}}}}\) cycle (green boxes). Besides, in one \({{{{\textbf {{\texttt {for}}}}}}}\) cycle, there is a sub-nesting \({{{{\textbf {{\texttt {for}}}}}}}\) cycle (orange boxes)

5.2 Encoding

ACO-CPP algorithm is written using PYTHON, and graphs are represented using a weighted adjacency matrix and node table encoding. In this context, the graph is represented as a square weighted matrix which has the names of the nodes of the graph as row and column indexes so that, if there exists an edge from node \(n_i\) to node \(n_j\), in the place \((n_i, n_j)\) there is a number corresponding to its weight. Otherwise, it is 0. The weighted node table encoding is a matrix \(N_{edges}\)x3 where the three columns indicate the starting node, the ending node, and the weight of an edge of G.

Weighted adjacency matrix encoding was beneficial in getting the odd nodes from the graph, and weighted node table encoding showed its advantage for the random generation of weights during the graph construction.

We used the PYTHON object class for artificial ants representation, characterized by two properties: Tour and Cost. The Tour is a list of lists where each sub-list is an odd vertices pairing. Cost is a float64 variable that contains the SPs sum of all the odd pairings. The ant object structure is defined as in Algorithm 2.

Algorithm 2
figure b

Ant encoding

6 Experimental analysis

To evaluate the ACO-CPP performances, we developed an algorithm to execute various test cases. In each test case, we create a connected graph and then identify the best solution using different combinations of:

  • Recursive algorithm proposed by Araz SharmaFootnote 1

  • Genetic Algorithms of Jiang Hua and Kang Li-shan[5].

  • ACO-CPP algorithm.

The following subsections will describe the various experiment components, the experimental setup and the experiment results.

6.1 Graph generation

Our graph generation algorithm can generate a connected undirected graph with a desired number of nodes that are randomly connected and with each weight value ranging within the integer values [1; 1000]. Such a function was designed to manage the number of nodes and the count of odd nodes. Specifically, in the first phase, an undirected graph with random connections where it is guaranteed the property of connection \(G_{FC}\) is generated by considering as an object the number of nodes provided (\(N_{nodes}\)). Then, considering the number of odd nodes (\(N_{odds}\)) user-required, an augmented graph \(G_{A}\) is built by randomly connecting the nodes. The edges of both graphs are considered, and if there are present self-loops or extra-edges, they are deleted. New edges are added until the number of odd nodes user-desired is obtained. Lastly, the two graphs are merged, getting G, a not-weighed graph, and as the last step, each edge is assigned a random weight ranging from 1 to \(W_\text {max} = 1000\). The pseudo-code of graph construction is provided in Algorithm 3.

Algorithm 3
figure c

Undirected connected graph generation pseudo-code

6.2 Recursive algorithm

In the recursive algorithm, starting from the identified odd nodes, all possible combinations of pairings are computed, along with the corresponding subsequent cost of graph augmentation. Among all the costs, the lowest value is selected and stored.

6.3 Genetic algorithm

Genetic Algorithms have an object coding identical to ACO-CPP. There is a matrix of SPs, and solutions are coded as Genotypes, which are objects made by a list of pairing of odd nodes (Tour) and a relative cost graph augmentation (Cost).

In such an algorithm, an initial population is considered \(\texttt{for}\) a certain number of iterations. Then, using selection criteria, a portion \(p_m\) and \(p_c\) of it is chosen to produce a new generation of child solutions through genetic operators like mutation and crossover. Finally, from all the solutions in the current iteration, a subset is selected to form the next generation for the subsequent iteration. Roulette wheel selection is always applied as a selection criterion, and the best solution is selected at the end of the algorithm.

During the mutation process, for each \(p_{m,i}\) genotype, two random integers are generated within the range \([1, N_{odds}]\), which will represent the indices in the Genotype Tour to be exchanged. To provide an example, let us assume \(N_{odds} = 8\). When the mutation operator is applied to a genotype, it randomly generates two indices, for instance, \(i=3\) and \(j=7\). The operator then exchanges the genotype values at positions i and j. As a result:

$$\begin{aligned} Gentype(i)_{new} = Genotype(j)_{old} \ \ \ \ Gentype(j)_{new} = Genotype(i)_{old} \end{aligned}$$
(16)

During crossover, a pair of genotypes is selected. A pair of indexes corresponding to the position of a pair of odd nodes in genotypes tour is individuated. Odd node values are exchanged along those positions, and consequently, the remainder of the genotype list tours are adjusted. To provide an example, let us assume \(N_{odds} = 8\). When the crossover operator is applied to a genotype, it randomly generates two adjacent indices, denoted as i and j, both starting from an odd number. For instance, let us consider \(i=3\) and \(j=4\). Next, two genotypes, labeled a and b, are randomly selected from the population, and the odd nodes between them are exchanged based on the specified indices, with adjustments made to the remaining elements. This process can be described as follows:

$$\begin{aligned} Gentype(i)_{a,new}= & {} Genotype(i)_{b,old} \ \ \ \ Gentype(j)_{a,new} = Genotype(j)_{b,old} \end{aligned}$$
(17)
$$\begin{aligned} Gentype(i)_{b,new}= & {} Genotype(i)_{a,old} \ \ \ \ Gentype(j)_{b,new} = Genotype(j)_{a,old} \end{aligned}$$
(18)

6.4 ACO-CPP algorithm

The ACO-CPP algorithm starts from the odd nodes information, generates the matrix of SPs D, and then \(\texttt{for}\) \(it_\text {max}\) iterations develop \(N_\text {ant}\). Each ant is built in this way: at the beginning are constructed the initial pheromone matrix \(\tau\) and the heuristic matrix \(\eta\), then the matrix \(P=(\tau \alpha )\circ (\eta \beta )\). Such a matrix \(\texttt{for}\) the number of odd pairings divided 2 times, \(\texttt{for}\) \(N_\text {ant}\) ants, in a first moment, is updated in the matrix \(P(i,j)=\frac{P(i,j)}{\sum P(i,j)}\), then, using the function Roulette wheel selection, finds the two odd nodes to connect and sets all the elements of both rows and columns of the odd nodes indexes to 0.

After each iteration, pheromone updating is applied as illustrated in Equation (14) and Equation (15). Specifically, at the end of each iteration, every ant contributes to updating the matrix \(\tau\). This contribution, the pheromone reinforcement, is more significant the shorter the total path the ant has traversed along the cells of \(\tau\), which corresponds to the tour undertaken by the ant itself (Equation (14)). Following the reinforcement phase, the evaporation phase is implemented, involving a uniform reduction of all \(\tau\) values by an amount specified in Equation (15). The underlying idea is to utilize pheromone reinforcement to emphasize more favorable paths while employing pheromone evaporation to diminish less favorable paths.

The ACO-CPP algorithm is built in such a way as to store the artificial ant, which shows the best solution of all (Winner Ant). During the experiment, its hyperparameter settings are those of Algorithm 4.

Algorithm 4
figure d

ACO-CPP parameters set in the experiment

6.5 Experimental setup

To evaluate the ACO-CPP performance, three different test cases were carried out. We conducted the experiments 300 times to measure the effectiveness and the consistency of ACO-CPP, setting the ACO-CPP parameters as in Algorithm 4. For both meta-heuristics, we varied the choice of the number of solutions explored (ants for ACO, and \(p_m\) and \(p_c\) for GA) and the number of iterations within the range of 10 to 40. A more comprehensive description of the experiment is as follows:

  • Experiment 1: Recursive-GA-ACO comparison conducted to measure the meta-heuristics ability to obtain the global optimum (Algorithm 5). We considered graphs with varying numbers of odd nodes (ranging from 4 to 12) and various combinations of hyperparameters designed to maintain a similar number of solutions explored by the meta-heuristics. We performed 300 measurements for different combinations of graphs and hyperparameters to assess the augmentation brought about by ACO and GA. This allowed us to determine how often GA and ACO achieved the global optimum during their respective runs.

  • Experiment 2: GA-ACO statistics comparison for measuring consistency and reproducibility (Algorithm 6). Five graphs with varying numbers of odd nodes (ranging from 4 to 12) are generated (see Fig. 6 in Sect. 1). Using different combinations of hyperparameters designed to maintain a similar number of solutions explored by the meta-heuristics, we conducted 300 measurements for each graph and hyperparameter combination to calculate the graph augmentation produced by ACO and GA. From these measurements, we computed the average and standard deviation. The lower the average and standard deviation, the more consistent the algorithm can find the best solution.

  • Experiment 3: Recursive-GA-ACO comparison conducted to measure the ability of meta-heuristics to achieve the global optimum when varying the ACO parameters \(\alpha\) and \(\beta\) (Algorithm 7). This test case is identical to the first one but explores different combinations of hyperparameters \(\alpha\) and \(\beta\). The aim was to investigate whether the proposed experimental setup inherently exhibits characteristics that result in different modes of exploring the search space to find the best solution, depending on the chosen graph parameters. The goal was to find the best combination of hyperparameters for solving it most effectively.

To conduct the experiments, we employed a computer equipped with 12 GB of RAM and an i5 processor (2.30 GHz). With these specifications, the computer could solve problems using the recursive algorithm and store all possible solutions in memory for graphs with up to 12 odd nodes within a relatively short time frame. It required several minutes to solve problems with 14 nodes, but it could not handle problems with 16 nodes or more. This description emphasizes as in this case, it is possible to find exact solutions for the CPP, but in general, as problems become more complex, finding exact solutions becomes impractical or exceedingly time-consuming, and the meta-heuristics advantages start to show up.

The primary challenge in meta-heuristics is to obtain the global optimum without getting trapped in a local minimum. To assess the ability of ACO-CPP to attain the global optimum with a certain level of accuracy, it is necessary to compare it with an exact algorithm, such as the recursive algorithm. Additionally, it is crucial to determine whether the inherent working scheme of ACO-CPP is better suited for solving problems like CPP compared to other meta-heuristics, therefore requiring a comparison with GA.

Algorithm 5
figure e

Experiment 1

Algorithm 6
figure f

Experiment 2

Algorithm 7
figure g

Experiment 3

6.6 Experimental results

Tables 2, 3, and 4 show the results of experiments 1, 2, and 3, respectively. In Table 2, the number of tests where GA and ACO-CPP found the best solution is represented. In Table 3, the graph augmentations’ minimum, maximum, average, and standard deviation. Finally, in Table 4, the number of tests where the best solution was found by GA and ACO-CPP while changing \(\alpha\) and \(\beta\).

Experiment 1: test results demonstrate that ACO-CPP exhibits effectiveness and can converge toward the best solution with success rates of \(91\%\) when the parameters \(N_\text {ant}\) and \(it_\text {max}\) are optimized according to the computational complexity of the problem. Furthermore, comparing meta-heuristics, it is noteworthy that GA excels at finding the global optimum when the number of solutions to explore is limited. In contrast, ACO-CPP consistently performs well in finding the global optimum, especially in scenarios involving a wide range of potential solutions. Experiment 1 reveals that ACO-CPP may not achieve the global optimum as effectively as GA when dealing with a relatively small number of possible solutions. However, it demonstrates high effectiveness when the number of solutions increases significantly.

Experiment 2: test results show that ACO-CPP is more efficient and consistent than GA when it comes to obtaining the best solutions in different runs. It consistently demonstrates an equal or significantly lower average best solution for all combinations and an equal or lower standard deviation.

Experiment 3: test results seem to suggest that the proposed experimental setup is more easily solvable with pheromone-based influence when the number of odd nodes is less than 10. Conversely, it appears more easily solvable with heuristic influence or a balanced combination of heuristics and pheromones when the number of odd nodes equals 12. The case with 10 nodes does not seem to exhibit a clear preference.

In our interpretation, the first experiment shows that when the hyperparameter settings are not optimized, ACO-CPP exhibits advantages for complex problems but also weaknesses when dealing with problems involving a limited number of solutions. The second experiment demonstrates that ACO-CPP is a consistent algorithm, a desirable meta-heuristic characteristic. Lastly, the third experiment reveals that ACO-CPP is suitable when hyperparameters are well set, even for problems with limited solutions. Additionally, it unveils some other very interesting characteristics: when an ant is at a point and has to decide which point to move to (a couple of nodes), it has several choices to consider. The ant’s decision is influenced by two forces, each contributing to the choice. The first force that can be seen as a "convenience force" is related to the proximity of the nodes (the shortest path), and the closer two nodes are, the higher the likelihood of selecting them (heuristic force). The second force that can be seen as a "past experience force" is associated with the number of ants that have previously passed through a particular couple of nodes and found it to be a good choice; the higher this count, the greater the probability of selecting that node (pheromone force).

In the experiment, assigning a higher value to \(\alpha\) instead of \(\beta\) is meant to give more importance to the pheromone force in the first case and more importance to the heuristic force in the second one. The results of the ACO experiments reveal a certain pattern in the quality of solutions. This pattern suggests that, in general, for problems with a limited number of solutions to explore, giving more importance to the pheromone force is advantageous. When the number of odd nodes is 10, there is not one force that outperforms the others. Ultimately, when the number of possible solutions becomes significantly larger, it appears that a heuristic or a balance between heuristic and pheromone forces is more favorable to choose. So, when the ants give more weight to the pheromone force, graphs with fewer than 10 nodes are better solved (relying on past ant experiences). When more weight is given to the heuristic force or a balance between the two is struck, more complex graphs are solved more effectively.

GA finds solutions using three main operators: mutation, crossover, and reproduction, each with its distinct way of exploring the search space. Mutation can be seen as a “random exploration force”, capable of exploring different regions of the search space without a specific logic. Reproduction can differently be seen as a “past experience force”, favoring solutions that have proven beneficial in the past. Finally, crossover, similarly to reproduction, employs the “past experience force” to conduct local searches and explore the solution space.

A meta-heuristic can be more valuable than others in different contexts, depending on how well its exploration scheme aligns with the structure of the problem’s solutions. Therefore, ACO-CPP appears to perform well, as GA, in problems with a limited number of solutions when pheromone and heuristic parameters are properly configured. Above all, ACO seems to be well-suited to solving CPP due to its intrinsic nature for problems with a high number of nodes generating a large number of potential solutions.

Table 2 Number of tests where the best solution is found by varying the parameters \(P_{m}\), \(P_{c}\) and \(it_\text {max}\) for GA, while \(N_\text {ant}\) and \(it_\text {max}\) for ACO (ACO best results over GA in bold)
Table 3 Minimum, maximum, average, and standard deviation of graph augmentations when the best solution is found in 5 graphs (ranging from 4 to 12 odd nodes) over 300 runs
Table 4 Number of tests where the best solutions is found by varying the parameters \(P_{m}\), \(P_{c}\) and \(it_\text {max}\) for GA, while \(N_\text {ant}\), \(it_\text {max}\), \(\alpha\), and \(\beta\) for ACO (best results in bold)

7 Conclusions

This paper proposes a solution to the Chinese Postman Problem using an Ant Colony Optimization Algorithm (ACO-CPP). ACO has demonstrated successful applications in both academic and real-world contexts. While it has proven to be a highly effective method for addressing route problems and other NP-hard category problems over time [4], to the best of our knowledge, it has never been tested on the CPP problem.

In this work, our ACO-CPP is evaluated through experiments conducted on connected undirected graphs with randomly generated connections between nodes and edge weights randomly ranging from 1 to 1000. To assess its consistency and effectiveness, ACO-CPP is compared with a Genetic Algorithm (GA) and a recursive algorithm in three separate experiments: (1) recursive-ACO-GA comparisons involving randomly generated graphs to find the global optimum; (2) statistic comparisons between ACO and GA using specifically generated graphs; (3) recursive-ACO(\(\alpha\)/\(\beta\))-GA hyperparameter comparisons over randomly generated graphs for the attainment of the global optimum.

The first and third experiments show the algorithm is efficient. With the correct choice of hyperparameters, it can match the performance of GA when dealing with problems with a limited number of solutions to explore (less than 10 odd nodes). However, it surpasses GA when addressing problems with a significantly larger number of solutions to explore, achieving the global optimum with a high likelihood (greater than \(91\%\)). Additionally, the second experiment demonstrates that ACO-CPP exhibits better consistency than GA across different runs.

In the context of the experiments, both GA and ACO appear to perform well when the number of solutions is relatively low, likely because there is no meta-heuristic search method that outperforms significantly. However, as the number of odd nodes increases, GA’s performance seems to decline, leading solutions toward local minima. On the contrary, ACO appears more adaptable, possibly due to its pheromone-heuristic approach, making it better suited to tackle the context.

As indicated in Equation (4), the number of solutions to explore increases drastically with the increasing number of pairs of odd nodes in the graph. The real novelty of our ACO-CPP is its ability to address problems with many solutions to explore and to obtain the global optimum by exploring only a reduced portion of the search space, especially in a context like that of CPP. This is achieved while demonstrating superior performance and consistency compared to the other method proposed (GA).

The presented ACO-CPP highlights all the meta-heuristic strengths because, thanks to the pheromone updating strategy, the artificial ants converge toward finding the global optimal solution for a high percentage of attempts while keeping the number of generated solutions low. At the same time, it contains the typical meta-heuristic weaknesses such as the risk of falling into local optima or the difficulty in deciding which are the best parameters to choose [49].

Due to its algorithmic structure, ACO-CPP seems suitable for solving problems such as Directed Chinese Postman Problem (DCPP) and Mixed Chinese Postman Problem (MCPP). Future works will be aimed at designing new experiments to measure its performance in this context. This will involve adapting the recursive algorithm to these contexts and proposing new meta-heuristics to solve such problems for highly complex graphs.