{'fraction_non_alphanumeric': 0.04213027352804821, 'fraction_numerical': 0.013444598980064905, 'mean_word_length': 4.689746125947907, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
The Transformer Network for the Traveling Salesman Problem

The Traveling Salesman Problem (TSP) is the most popular and most studied combinatorial problem, starting with von Neumann in 1951. It has driven the discovery of several optimization techniques such as cutting planes, branch-and-bound, local search, Lagrangian relaxation, and simulated annealing. The last five years have seen the emergence of promising techniques where (graph) neural networks have been capable to learn new combinatorial algorithms. The main question is whether deep learning can learn better heuristics from data, i.e. replacing human-engineered heuristics? This is appealing because developing algorithms to tackle efficiently NP-hard problems may require years of research, and many industry problems are combinatorial by nature. In this work, we propose to adapt the recent successful Transformer architecture originally developed for natural language processing to the combinatorial TSP. Training is done by reinforcement learning, hence without TSP training solutions, and decoding uses beam search. We report improved performances over recent learned heuristics with an optimal gap of 0.004% for TSP50 and 0.39% for TSP100.


Traditional TSP Solvers
The TSP was first formulated by William Hamilton in the 19th century. The problem states as follows; given a list of cities and the distances between each pair of cities, what is the shortest possible path that visits each city exactly once and returns to the origin city? TSP belongs to the class of routing problems which are used every day in industry such as warehouse, transportation, supply chain, hardware design, manufacturing, etc. TSP is an NP-hard problem with an exhaustive search of complexity O(n!). TSP is also the most studied combinatorial problem. It has motivated the development of important optimization methods including Cutting Planes [10], Branch-and-Bound [4,16], Local Search [8], Lagrangian Relaxation [12], Simulated Annealing [24].
There exist two traditional approaches to tackle combinatorial problems; exact algorithms and approximate/heuristic algorithms. Exact algorithms are guaranteed to find optimal solutions, but they become intractable when n grows. Approximate algorithms trade optimality for computational efficiency. They are problem-specific, often designed by iteratively applying a simple man-crafted rule, known as heuristic. Their complexity is polynomial and their quality depends on an approximate ratio that characterizes the worst/average-case error w.r.t the optimal solution.
Exact algorithms for TSP are given by exhaustive search, Dynamic or Integer Programming. A Dynamic Programming algorithm was proposed for TSP in [16] with O(n 2 2 n ) complexity, which becomes intractable for n > 40. A general purpose Integer Programming (IP) solver with Cutting Planes (CP) and Branch-and-Bound (BB) called Gurobi was introduced in [15]. Finally, a highly specialized linear IP+CP+BB, namely Concorde, was designed in [2]. Concorde is widely regarded as the fastest exact TSP solver, for large instances, currently in existence.
Several approximate/heuristic algorithms have been introduced. Christofides algorithm [7] approximates TSP with Minimum Spanning Trees. The algorithm has a polynomial-time complexity with O(n 2 log n), and is guaranteed to find a solution within a factor 3/2 of the optimal solution. Farthest/nearest/greedy insertion algorithms [20] have complexity O(n 2 ), and farthest insertion (the best insertion in practice) has an approximation ratio of 2.43. Google OR-Tools [14] is a highly optimized program that solves TSP and a larger set of vehicle routing problems. This program applies different heuristics s.a. Simulated Annealing, Greedy Descent, Tabu Search, to navigate in the search space, and refines the solution by Local Search techniques. 2-Opt algorithm [27,21] proposes an heuristic based on a move that replaces two edges to reduce the tour length. The complexity is O(n 2 m(n)), where n 2 is the number of node pairs and m(n) is the number of times all pairs must be tested to reach a local minimum (with worst-case being O(2 n/2 )). The approximation ratio is 4/ √ n. Extension to 3-Opt move (replacing 3 edges) and more have been proposed in [6]. Finally, LKH-3 algorithm [18] introduces the best heuristic for solving TSP. It is an extension of the original LKH [28] and LKH-2 [17] based on 2-Opt/3-Opt where edge candidates are estimated with a Minimum Spanning Tree [17]. LKH-3 can tackle various TSP-type problems.

Neural Network Solvers
In the last decade, Deep learning (DL) has significantly improved Computer Vision, Natural Language Processing and Speech Recognition by replacing hand-crafted visual/text/speech features by features learned from data [26]. For combinatorial problems, the main question is whether DL can learn better heuristics from data than hand-crafted heuristics? This is attractive because developing algorithms to tackle efficiently NP-hard problems require years of research (TSP has been actively studied for seventy years). Besides, many industry problems are combinatorial. The last five years have seen the emergence of promising techniques where (graph) neural networks have been capable to learn new combinatorial algorithms with supervised or reinforcement learning. We briefly summarize this line of work below.
• PointerNets [39]: A pioneer work using modern DL to tackle TSP and combinatorial optimization problems. This work combines recurrent networks to encode the cities and decode the sequence of nodes in the tour, with the attention mechanism. The network structure is similar to [3], which was applied to NLP with great success. The decoding is auto-regressive and the network parameters are learned by supervised learning with approximate TSP solutions.
• PointerNets+RL [5]: The authors improve [39] with Reinforcement Learning (RL) which eliminates the requirement of generating TSP solutions as supervised training data. The tour length is used as reward. Two RL approaches are studied; a standard unbiased reinforce algorithm [40], and an active search algorithm that can explore more candidates.
• Order-invariant PointerNets+RL [33]: The original network [39] is not invariant by permutations of the order of the input cities (which is important for NLP but not for TSP). This requires [39] to randomly permute the input order to let the network learn this invariance. The work [33] solves this issue by making the encoder permutation-invariant.
• S2V-DQN [9]: This model is a graph network that takes a graph and a partial tour as input, and outputs a state-valued function Q to estimate the next node in the tour. Training is done by RL and memory replay [31], which allows intermediate rewards that encourage farthest node insertion heuristic.
• Quadratic Assignment Problem [34]: TSP can be formulated as a QAP, which is NP-hard and also hard to approximate. A graph network based on the powers of adjacency matrix of node distances is trained in supervised manner. The loss is the KL distance between the adjacency matrix of the ground truth cycle and its network prediction. A feasible tour is computed with beam search.
• Permutation-invariant Pooling Network [23]: This work solves a variant of TSP with multiple salesmen. The network is trained by supervised learning and outputs a fractional solution, which is transformed into a feasible integer solution by beam search. The approach is non-autoregressive, i.e. single pass.
• Tranformer-encoder+2-Opt heuristic [11]: The authors use a standard transformer to encode the cities and they decode sequentially with a query composed of the last three cities in the partial tour. The network is trained with Actor-Critic RL, and the solution is refined with a standard 2-Opt heuristic.
• Tranformer-encoder+Attention-decoder [25]: This work also uses a standard transformer to encode the cities and the decoding is sequential with a query composed of the first city, the last city in the partial tour and a global representation of all cities. Training is carried out with reinforce and a deterministic baseline.
• GraphConvNet [22]: This work learns a deep graph network by supervision to predict the probabilities of an edge to be in the TSP tour. A feasible tour is generated by beam search. The approach uses a single pass.
• 2-Opt Learning [41]: The authors design a transformer-based network to learn to select nodes for the 2-Opt heuristics (original 2-Opt may require O(2 n/2 ) moves before stopping). Learning is performed by RL and actor-critic.
• GNNs with Monte Carlo Tree Search [42]: A recent work based on AlphaGo [35] which augments a graph network with MCTS to improve the search exploration of tours by evaluating multiple next node candidates in the tour. This improves the search exploration of auto-regressive methods, which cannot go back once the selection of the nodes is made.

Proposed Architecture
We cast TSP as a "translation" problem where the source "language" is a set of 2D points and the target "language" is a tour (sequence of indices) with minimal length, and adapt the original Transformers [37] to solve this problem. We train by reinforcement learning, with the same setting as [25]. The reward is the tour length and the baseline is simply updated if the train network improves the baseline on a set of random TSPs. See Figure 1 for a description of the proposed architecture.
Encoder. It is a standard Transformer encoder with multi-head attention and residual connection. The only difference is the use of batch normalization, instead of layer normalization. The memory/speed complexity is O(n 2 ). Formally, the encoder equations are (when considering a single head for an easier description) where z is a start token, initialized at random. See Figure 5 for an illustration of the encoder. Decoder. The decoding is auto-regressive, one city at a time. Suppose we have decoded the first t cities in the tour, and we want to predict the next city. The decoding process is composed of four steps detailed below and illustrated on Figure 3. Decoder -Part 1. The decoding starts with the encoding of the previously selected i t city : where PE t ∈ R d is the traditional positional encoding in [37] to order the nodes in the tour: Decoder -Part 2. This step prepares the query using self-attention over the partial tour. The selfattention layer is standard and uses multi-head attention, residual connection, and layer normalization. The memory/speed complexity is O(t) at the decoding step t. The equations for this step are (when again considering a single head for an easier description) Decoder -Part 3. This stage queries the next possible city among the non-visited cities using a query-attention layer. Multi-head attention, residual connection, and layer normalization are used. The memory/speed complexity is O(n) at each recursive step.
with M t is the mask if the visited cities and is the Hadamard product.
Decoder -Part 4. This is the final step that performs a final query using a single-head attention to get a distribution over the non-visited cities. Eventually, the next node i t+1 is sampled from the distribution using Bernoulli during training and greedy (index with maximum probability) at inference time to evaluate the baseline. The memory/speed complexity is O(n). The final equation is where C = 10.

Architecture Comparison
Comparing Transformers for NLP (translation) vs. TSP (combinatorial optimization), the order of the input sequence is irrelevant for TSP but the order of the output sequence is coded with PEs for both TSP and NLP. TSP-Encoder benefits from Batch Normalization as we consider all cities during the encoding stage. TSP-Decoder works better with Layer Normalization since one vector is decoded at a time (auto-regressive decoding as in NLP). The TSP Transformer is learned by Reinforcement Learning, hence no TSP solutions/approximations required. Both transformers for NLP and TSP have quadratic complexity O(n 2 L).
Comparing with the closed neural network models of [25] and [11], we use the same transformerencoder (with BN) but our decoding architecture is different. We construct the query using all cities in the partial tour with a self-attention module. [25] use the first and last cities with a global representation of all cities as the query for the next city. [11] define the query with the last three cities in the partial tour. Besides, our decoding process starts differently. We add a token city z ∈ R 2 . This city does not exist and aims at starting the decoding at the best possible location by querying all cities with a self-attention module. [25] starts the decoding with the mean representation of the encoding cities and a random token of the first and current cities. [11] starts the decoding with a random token of the last three cities.

Decoding Technique
Given a set X ∈ R n×2 of 2-D cities, a tour is represented as an ordered sequence of city indices : seq n = {i 1 , i 2 , . . . , i n } and TSP can be cast as a sequence optimization problem: max seq n ={i1,...,in} P TSP (seq n |X) = P TSP (i 1 , ..., i n |X).
For auto-regressive decoding, i.e. selecting a city one at a time, P TSP can be factorized with the chain rule: P TSP (i 1 , ..., i n |X) = P (i 1 |X) · P (i 2 |i 1 , X) · P (i 3 |i 2 , i 1 , X) · ... · P (i n |i n−1 , i n−2 , ..., X). (26) Hence the decoding problem aims at finding the sequence i 1 , i 2 , . . . , i n that maximizes the objective: Finding exactly the optimal sequence by exhaustive search is intractable given the O(n!) complexity, and approximations are necessary. The simplest approximate search is the greedy search; at each time step, the next city is selected with the highest probability: The complexity is linear O(n).
Better sampling techniques such as beam search or Monte Carlo Tree Search (MTCS) are known to improve results over greedy search in NLP [36] and TSP [34,23,25,22,41,42]. Their complexity is O(Bn), where B is the number of beams or explored paths. Beam search [29] is a breadth-first search (BFS) technique where the breath has a limited size B. the beam search decoding problem is as follows: For B = 1, the solution is given by greedy decoding. For B > 1, the solution at t is determined by considering all possible extensions of B beams, and only keeping the Top-B probabilities : or equivalently (for better numerical stabilities) :

Numerical Experiments
We compare the proposed architecture with existing methods in Table 1

Discussion
In this work, we essentially focused on the architecture. We observe that the Transformer architecture can be successful to solve the TSP Combinatorial Optimization problem, expanding the success of Transformer for NLP and CV. It also improves recent learned heuristics with an optimal gap of 0.004% for TSP50 and 0.39% for TSP100.
Further developments can be considered with better sampling techniques such as group beam-search [38,30] or MCTS [42] which are known to improve results. Besides, the use of heuristics like 2-Opt to get intermediate rewards has also shown improvements [41] (the tour length as global reward requires to wait the end of the tour construction).
However, traditional solvers like Concorde/LKH-3 still outperform learning solvers in terms of performance and generalization, although neural network solvers offer faster inference time, O(n 2 L) vs. O(n 2.5 b(n)), where O(b(n)) is the number of branches to explore in BB.
What's next? The natural next step is to scale to larger TSP sizes for n > 100 but it is challenging as GPU memories are limited, and Transformer architectures and auto-regressive decoding are in O(n 2 ). We could consider "harder" TSP/routing problems where traditional solvers like Gurobi/LKH-3 can only provide weaker solutions or would take very long to solve. We could also work on "harder" combinatorial problems where traditional solvers s.a. Gurobi cannot be used.
Another attractive research direction is to leverage learning techniques to improve traditional solvers. For example, traditional solvers leverage Branch-and-Bound technique [4,16]. Selecting the variables to branch is critical for search efficiency, and relies on human-engineered heuristics s.a. Strong Branching [1] which is a high-quality but expensive branching rule. Recent works [13,32] have shown that neural networks can be successfully used to imitate expert heuristics and speed-up the BB computational time. Future work may focus on going beyond imitation of human-based heuristics, and learning novel heuristics for faster Branch-and-Bound technique.

Conclusion
The field of Combinatorial Optimization is pushing the limit of deep learning. Traditional solvers still provide better solutions than learning models. However, traditional solvers have been studied since the 1950s and the interest of applying deep learning to combinatorial optimization has just started. This topic of research will naturally expend in the coming years as combinatorial problems problems s.a. assignment, routing, planning, scheduling are used every day by companies. Novel software may also be developed that combine continuous, discrete optimization and learning techniques.