MechaFormer: Sequence Learning for Kinematic Mechanism Design Automation Diana Bolanos, Mohammadmehdi Ataei, Pradeep Kumar Jayaraman Abstract Designing mechanical mechanisms to trace specific paths is a classic yet notoriously difficult engineering problem, characterized by a vast and complex search space of discrete topologies and continuous parameters. We introduce MechaFormer, a Transformer-based model that tackles this challenge by treating mechanism design as a conditional sequence generation task. Our model learns to translate a target curve into a domain-specific language (DSL) string, simultaneously determining the mechanism’s topology and geometric parameters in a single, unified process. MechaFormer significantly outperforms existing baselines, achieving state-of-the-art path-matching accuracy and generating a wide diversity of novel and valid designs. We demonstrate a suite of sampling strategies that can dramatically improve solution quality and offer designers valuable flexibility. Furthermore, we show that the high-quality outputs from MechaFormer serve as excellent starting points for traditional optimizers, creating a hybrid approach that finds superior solutions with remarkable efficiency. Figure 1: Mechanism-sampling pipeline. The input curve is converted to a B-spline, encoded by a Transformer, and decoded into a DSL that specifies mechanism topology and joint parameters. Topology sampling then generates and evaluates candidates, with clusters indicating accuracy. Introduction Mechanism synthesis is a foundational problem in mechanical design that involves the systematic generation and selection of feasible kinematic topologies and configurations to achieve specific motion or functional requirements ( Hartenberg and Danavit 1964 ; Angeles 2003 ) . It plays a crucial role in various applications, from industrial robotics and manufacturing machinery to biomedical devices and consumer electronics. The overarching goal of mechanism synthesis is to determine the best arrangement of joints and links such that a mechanical system performs a desired task reliably and efficiently, often under constraints like space limitations, joint limits, or load-bearing capacities ( Norton and Han 2007 ) . A designer must select a topology (the number and arrangement of links and joints) from a discrete set of graphs while simultaneously optimizing continuous geometric parameters like joint locations. This mixed discrete-continuous space, combined with a highly non-linear and non-convex relationship between parameters and motion, is commonly encountered in mechanical design problems ( Bolanos et al. 2023 ; Martins and Ning 2021 ) , and makes it notoriously hard for analytical and optimization-based methods. Traditional mechanism synthesis follows two paths, both with unique challenges. Analytical methods rooted in kinematics, such as Burmester theory ( Hartenberg and Danavit 1964 ) , produce closed-form solutions for simple four-bar linkages and a handful of target positions but break down for continuous curves, richer topologies, or cases with no solution ( Primrose, Freudenstein, and Sandor 1964 ; Ma and Angeles 1988 ) . Numerical optimization casts synthesis as error minimization inside a chosen topology ( Ebrahimi and Payvandy 2015 ) , yet gradient methods cling to initial guesses and get trapped in local minima ( Mariappan and Krishnamurty 1996 ; Sancibrian et al. 2004 ) , while population-based search needs thousands of evaluations ( Lin 2010 ; Acharyya and Mandal 2009 ; Cabrera, Simon, and Prado 2002 ) . Both approaches usually explore only one topology, so better mechanisms remain hidden unless each alternative is optimized separately, multiplying cost. Although finding a mechanism that produces a prescribed path is exceptionally difficult, simulating the path traced by a given mechanism is straightforward. This computational asymmetry between inverse and forward kinematics makes the problem well-suited to learning-based approaches. Because forward kinematics are inexpensive, thousands of candidate mechanisms can be evaluated in seconds on modern hardware, enabling large-scale dataset generation and screening ( Heyrani Nobari et al. 2022 ; Nurizada et al. 2025 ) . Inspired by recent successes in domains like program synthesis ( Wang et al. 2021 ) , molecular design ( Mazuz et al. 2023 ) , and gear design ( Etesam et al. 2025 ; Cheong et al. 2025 ; Ataei et al. 2025 ) , we reframe kinematic synthesis as a conditional sequence-to-sequence learning problem. We introduce a domain-specific language (DSL) that serializes any mechanism into a structured string of tokens. This approach transforms the intractable design problem into a more manageable task: translating a target curve into its corresponding mechanism sentence . We present MechaFormer, a Transformer-based architecture specifically trained to perform this translation. Given a target B-spline curve, MechaFormer autoregressively generates a complete mechanism definition. Its core advantage lies in its unified approach: the model learns to output topology and geometry simultaneously, allowing it to discover the most appropriate type of mechanism for a given curve and fine-tune its parameters in a single, coherent process. A major strength of our generative approach is its capacity for interactive and exploratory design, overcoming the limitations of single-point predictions. We introduce and validate a suite of novel sampling strategies that enable a more holistic exploration of the vast and complex design space. These techniques empower designers to rapidly generate a diverse portfolio of candidate solutions, systematically probing variations in mechanism topology, geometry, and even contextual placement within a given environment. By providing a broader view of the possibilities, our method allows for the discovery of superior and non-obvious solutions. This paper makes the following contributions: 1. A unified generative framework that treats mechanism synthesis as a sequence learning problem. This approach seamlessly integrates topology selection and parameter optimization into a single, end-to-end model, allowing it to discover optimal mechanisms from a vast and complex design space without manual partitioning. 2. State-of-the-art performance in path synthesis with sampling supporting different topologies. Through comprehensive experiments, we show that MechaFormer surpasses existing learning-based baselines in path-matching accuracy, while also generating a diverse set of valid and novel mechanism designs. 3. A design framework with targeted sampling strategies. We introduce and validate sampling strategies, including Best@ k , rotational sampling, and topology sampling that substantially improve solution quality and diversity, enabling rapid exploration of candidate mechanisms. 4. A hybrid optimization workflow. We show that model outputs provide superior initializations for local optimizers, yielding elite solutions far more efficiently and robustly than direct optimization from random starts. Related Work Recent contributions in mechanical synthesis of linkage mechanisms have looked to machine learning techniques for guidance on designing complex physical systems ( Sonntag et al. 2024 ; Han et al. 2025 ) . Nurizada et al. ( Nurizada, Lyu, and Purwar 2025 ) introduced a conditional \beta -VAE model for generating diverse planar four-bar mechanisms from coupler curves. Their work is supported by a large curated dataset and a comprehensive evaluation framework based on reconstruction accuracy, novelty, and diversity. While the model is capable of producing multiple candidate mechanisms per input, its reconstruction performance shows only about 45% of generated mechanisms achieve a DTW score below 2, which indicates satisfactory curve approximation. This limits its effectiveness for high-precision synthesis tasks and suggests room for improvement in learning the mapping between input curves and mechanism parameters. Nobari et al. ( Heyrani Nobari et al. 2022 ) introduced the LINKS dataset, a large-scale collection of 100 million planar mechanisms ranging from 6 to 15 links, each paired with a motion trajectory. In follow-up work, Nobari et al. ( Nobari et al. 2024 ) developed a contrastive learning approach for retrieving mechanisms from the dataset, followed by local refinement using quasi-Newton methods. While effective in minimizing trajectory error, many of the retrieved mechanisms are topologically complex and structurally unrealistic, often involving oversized configurations or internal collisions that would be impractical or impossible to manufacture. Optimization and reinforcement learning-based methods have also been explored for mechanism synthesis. Pan et al. ( Pan et al. 2023 ) investigated a range of optimization approaches, including mixed-integer conic programming (MICP), mixed-integer nonlinear programming (MINLP), and simulated annealing (SA), to design mechanisms with up to 14 rigid links. Their hybrid MICP-MINLP approach achieved higher-quality results than SA but incurred high computational costs, often requiring over an hour to solve a single instance. Similarly, Fogelson et al. ( Fogelson, Tucker, and Cagan 2023 ) proposed a reinforcement learning framework that refines mechanism parameters through reward-based feedback while enforcing geometric constraints. Although their method demonstrates better performance than evolutionary baselines, it remains expensive and requires extensive reward engineering to balance feasibility and task performance. These approaches illustrate the promise and limitations of optimization-heavy pipelines, which struggle to scale efficiently. In contrast to prior work, our approach treats mechanism synthesis as a sequence modeling task, using a transformer-based architecture to map continuous B-spline representations of input curves to discrete DSL tokens representing mechanism topology and joint parameters. This formulation enables generalization across a diverse set of mechanism families and allows for probabilistic sampling of multiple topologies and configurations per input. Furthermore, we demonstrate how this framework can be used to discover better-performing topologies through input rotation and topology sampling, a form of post-hoc analysis that has not been explored in existing generative models. Problem Formulation The core task of kinematic path synthesis is to solve an inverse problem. Given a desired target trajectory, the goal is to discover a corresponding planar linkage mechanism that can generate it. We formalize the key components below. We represent a mechanism as \mathcal{M}=(\tau,\mathbf{J},c) , where \tau\in\mathcal{T} is a topology chosen from a finite set of mechanism types (e.g., four-bar, Watt six-bar) that fixes the number of links, the joint types, and their connectivity (revolute or prismatic). The continuous parameters are \mathbf{J}={\mathbf{j}_{1},\ldots,\mathbf{j}_{n}}\subset\mathbb{R}^{2} , the initial joint coordinates for \tau . The coupler index c selects the joint whose path must match the target. Forward kinematics is given by a deterministic simulator \Phi that maps \mathcal{M} and an actuation variable (e.g., input crank angle \theta ) to the coupler position and thus to the curve \mathcal{C}=\Phi(\mathcal{M})={\Phi(\mathcal{M},\theta)\mid\theta\in[0,2\pi)}\subset\mathbb{R}^{2}. (1) This forward process, \Phi , is computationally inexpensive and straightforward to evaluate. The inverse problem (finding an optimal mechanism \mathcal{M}^{*} that generates a trajectory closely matching a desired target curve \mathcal{C}^{*} ) can be framed as an optimization problem: \mathcal{M}^{*}=\underset{\mathcal{M}=(\tau,\mathbf{J},c)}{\arg\min}\ d(\Phi(\mathcal{M}),\mathcal{C}^{*}) (2) where d(\cdot,\cdot) is a distance metric between two curves, such as Dynamic Time Warping (DTW) ( Tavenard et al. 2020 ) . This formulation is notoriously difficult to optimize because it is both mixed-integer and non-convex: the search couples a discrete topology \tau\in\mathcal{T} with continuous joint coordinates \mathbf{J}\in\mathbb{R}^{2n} , and the objective d(\Phi(\mathcal{M}),\mathcal{C}^{*}) has many local minima in \mathbf{J} , making gradient-based and many gradient-free methods unreliable and highly sensitive to initialization. To address these challenges, we reframe kinematic synthesis as a probabilistic modeling task rather than optimization. We learn the conditional distribution p(\mathcal{M}|\mathcal{C}^{*}) from a dataset of (\mathcal{M},\mathcal{C}) pairs generated by the forward simulator \Phi , enabling direct sampling of high-quality mechanisms for any target curve. Methodology Canonical Frame Normalization for Invariance and Exploratory Sampling A fundamental challenge in learning from geometric data is handling nuisance variables, in our case a mechanism’s function is invariant to similarity transformations. Raw joint coordinates force models to waste capacity learning this invariance. We eliminate this redundancy by applying canonical normalization to every mechanism, anchoring on its two ground joints \mathbf{j}_{g_{1}} and \mathbf{j}_{g_{2}} . An affine transformation maps all joints such that \mathbf{j}^{\prime}_{g_{1}}=(0,0) and \mathbf{j}^{\prime}_{g_{2}}=(1,0) . This normalization provides two key benefits. First, it collapses infinite placements to one canonical representation, letting the model focus on mapping relative geometry to curve shape. Second, by fixing the mechanism’s frame, we enable design exploration through input curve transformation, the foundation of our rotational sampling strategy discussed later (Figure 2 ). To find optimal mechanism orientation for a given curve, we rotate the input by angles \{\alpha_{1},\dots,\alpha_{m}\} , generate mechanisms for each rotation, then apply inverse rotations to the generated mechanisms. This efficiently searches for optimal base orientation in real-world coordinates, made tractable by our representation. Figure 2: a) User-defined input curve shown by the solid line, and model-generated mechanism and coupler trajectory shown by the dashed line. b) Original curve is rotated eight times by \Delta\alpha=45^{\circ} . c) Four example mechanisms at different rotation angles. The mechanism at \alpha_{6} is selected. d) The mechanism base is rotated to retain the relative position of the user-defined input curve relative to the origin. A simple target curve is used to clearly illustrate the process. Abstracting Topology as a Single Conditional Choice We represent mechanism topology as a single categorical variable rather than having the model assemble a graph of links and joints. Each of the 24 valid, kinematically distinct topologies in our library maps to a unique token (e.g., RRRR , Watt2T1A1 , Steph1T1 ), and the model’s first task is to predict one token. This abstraction guarantees kinematic feasibility and focuses learning where it matters. Constructing valid chains directly is combinatorially hard and constrained by rules such as Grubler’s criterion ( Gogu 2005 ) and connectivity, so encoding topology as a token ensures every design starts from a valid, well-defined type. With topology fixed, the model maps curve features to the most suitable known mechanism, then infers dimensions. The detailed connectivity graph for the chosen type is stored externally and guides generation of joint coordinates. This choice does preclude novel topologies, which is an acceptable trade-off. Most practical mechanisms use a small set of foundational four-bar and six-bar linkages that our 24 types cover well. The real engineering challenge is creative dimensioning within these proven families, and that is exactly what the model optimizes. Input Representation: B-Spline Control Points To provide the model with a compact, fixed-size representation of the target curve \mathcal{C}^{*} , we parameterize it using a cubic B-spline. Any given target curve, initially represented as a set of ordered points, is fitted to a B-spline with a fixed number of K=64 control points. The resulting set of control points, \mathbf{P}=\{\mathbf{p}_{1},\dots,\mathbf{p}_{K}\} with \mathbf{p}_{k}\in\mathbb{R}^{2} , captures the essential shape of the curve. This sequence of control points \mathbf{X}\in\mathbb{R}^{K\times 2} serves as the input to our model. Output Representation: A Domain-Specific Language for Mechanisms To make the mechanism \mathcal{M} amenable to sequence generation, we developed a DSL that serializes a mechanism’s topology and continuous joint coordinates into a discrete sequence of tokens, \mathbf{y}=(y_{1},y_{2},\dots,y_{L}) . Vocabulary The vocabulary \mathcal{V} of our DSL consists of three types of tokens: • Structural Tokens: Special tokens like ‘SOS’ (Start of Sequence), ‘EOS’ (End of Sequence), and delimiters like ‘MECH_TYPE’, ‘POINTS’, ‘P_i’, ‘X:’, ‘Y:’. • Topology Tokens: A finite set of tokens representing each of the 24 mechanism topologies (including 4-bar and 6-bar linkages, with a combination of revolute or prismatic joints) in our design space, e.g., ‘RRRR’, ‘RRRP’. • Coordinate Tokens: A set of integer tokens representing quantized coordinate values. We discretize the continuous coordinate space [-\kappa,\kappa] into B=200 uniform bins (see appendix for the ablation that justified this choice). A continuous coordinate value v is mapped to a bin index b(v) via: b(v)=\left\lfloor\frac{(v+\kappa)\cdot(B-1)}{2\kappa}\right\rfloor (3) This transforms the regression of continuous joint positions into a multi-class classification problem. Sequence Structure A full mechanism sequence \mathbf{y} follows a strict, human-readable structure that first declares the topology and then lists the normalized coordinates of its free joints. A ground link is assumed to be normalized with its joints at (0,0) and (1,0) , so these are not included in the sequence. Table 1 provides a detailed example. Token Sub-Sequence Description Start of sequence token. MECH_TYPE RRRR Declares the mechanism’s topology. Here, a four-bar linkage with four revolute joints. POINTS Structural token indicating the start of the joint coordinate block. P_1 X: BIN_125 Y: BIN_180 Specifies the coordinates for the first free joint. The continuous coordinates (x_{1},y_{1}) have been quantized to integer bin indices (125, 180). P_2 X: BIN_45 Y: BIN_78 Specifies the coordinates for the second free joint, quantized to bins (45, 78). … (Additional joints) End of sequence token. Table 1: Breakdown of the Domain-Specific Language (DSL) used to represent a mechanism as a token sequence. Sequence-to-Sequence Model Architecture We map an input sequence of control points \mathbf{X} to a sequence of mechanism tokens \mathbf{y} using a Transformer encoder-decoder ( Vaswani et al. 2017 ) . Training maximizes \log p(\mathbf{y}\mid\mathbf{X})=\sum_{t=1}^{L}\log p(y_{t}\mid\mathbf{y}_{