Routing Subtour Elimination logo

Routing Subtour Elimination

OrganizationPopular
benchflow-ai
routing-subtour-elimination

Subtour-elimination methods for TSP, VRP, pickup/dropoff routing, and routing MIPs with binary arc variables. Use when route-continuity constraints may permit disconnected cycles and the model needs MTZ constraints, flow-based connectivity constraints, DFJ subset cuts, or lazy/iterative subtour cuts.

Overview

Publisherbenchflow-ai
Repositoryskillsbench
Skill namerouting-subtour-elimination
Stars
1.8K
Forks
367
Bundled files
Instructions only
LicenseApache-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by benchflow-ai on GitHub. Read the source before you install it.

Installation

Install the Routing Subtour Elimination AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/benchflow-ai/skillsbench.git /tmp/skillsbench
mkdir -p .claude/skills
cp -r /tmp/skillsbench/tasks/bike-rebalance/environment/skills/routing-subtour-elimination .claude/skills/routing-subtour-elimination
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Routing Subtour Elimination in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Routing Subtour Elimination on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Routing Subtour Elimination is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Routing Subtour Elimination

In routing MIPs, degree and continuity constraints are not enough. A vehicle can have one depot-to-depot path and a separate closed cycle among stations. Add subtour-elimination constraints whenever binary arc variables decide routes.

Use this base notation:

python
START = "depot_start"
END = "depot_end"
vehicles = range(K)
stations = range(n)
from_nodes = [START, *stations]
to_nodes = [*stations, END]
arcs = [(i, j) for i in from_nodes for j in to_nodes if i != j and not (i == START and j == END)]

x = {(v, i, j): model.addVar(vtype="B", name=f"x_{v}_{i}_{j}") for v in vehicles for i, j in arcs}

Required Base Route Constraints

Subtour elimination assumes each selected station has matching inbound and outbound route arcs.

python
for v in vehicles:
    model.addCons(quicksum(x[v, START, j] for j in stations) == 1)
    model.addCons(quicksum(x[v, i, END] for i in stations) == 1)

    for i in stations:
        incoming = quicksum(x[v, j, i] for j in from_nodes if j != i)
        outgoing = quicksum(x[v, i, j] for j in to_nodes if j != i)
        model.addCons(incoming == outgoing)
        model.addCons(outgoing <= 1)

The subtour methods below prevent station-only cycles that are disconnected from START.

1. MTZ Order Constraints

MTZ adds an order variable for each vehicle-station pair. If vehicle v travels from station i to station j, then order[v, j] must be greater than order[v, i].

python
order = {
    (v, i): model.addVar(vtype="C", lb=1, ub=max(1, n), name=f"order_{v}_{i}")
    for v in vehicles
    for i in stations
}

for v in vehicles:
    for i in stations:
        for j in stations:
            if i != j:
                model.addCons(order[v, i] - order[v, j] + n * x[v, i, j] <= n - 1)

Pros:

  • Compact: O(K n^2) constraints and O(K n) extra variables.
  • Easy to implement in common Python optimization APIs.
  • Good default for small and medium benchmark instances.

Cons:

  • LP relaxation is weak compared with cutset or flow formulations.
  • Can be slow for larger VRPs.
  • Order variables are artificial; do not interpret them as service times unless you also model time.

Use MTZ first when correctness and implementation speed matter more than best possible MIP strength.

2. Single-Commodity Flow Connectivity

Add an artificial connectivity flow that starts at the depot and sends one unit to every visited station. This flow is not physical vehicle load.

python
visit = {
    (v, i): quicksum(x[v, i, j] for j in to_nodes if j != i)
    for v in vehicles
    for i in stations
}

flow_arcs = [(i, j) for i in [START, *stations] for j in stations if i != j]
f = {
    (v, i, j): model.addVar(vtype="C", lb=0, ub=n, name=f"conn_flow_{v}_{i}_{j}")
    for v in vehicles
    for i, j in flow_arcs
}

for v in vehicles:
    total_visits = quicksum(visit[v, i] for i in stations)
    model.addCons(quicksum(f[v, START, j] for j in stations) == total_visits)

    for i, j in flow_arcs:
        model.addCons(f[v, i, j] <= n * x[v, i, j])

    for i in stations:
        incoming_flow = quicksum(f[v, h, i] for h in [START, *stations] if h != i)
        outgoing_flow = quicksum(f[v, i, j] for j in stations if j != i)
        model.addCons(incoming_flow - outgoing_flow == visit[v, i])

Pros:

  • Stronger connectivity logic than MTZ in many models.
  • Static constraints, no callback needed.
  • Works with optional station visits.

Cons:

  • Adds O(K n^2) continuous variables.
  • Do not reuse truck load as the connectivity flow when the vehicle can both pick up and drop off. Physical load can increase and decrease; connectivity flow should monotonically distribute artificial units.
  • More memory than MTZ.

Use this when MTZ gives weak incumbents or slow progress and the instance is still modest in size.

3. DFJ Subset Cuts

For every nonempty proper subset S of stations, selected station-to-station arcs inside S cannot form a closed cycle:

text
sum_{i in S, j in S, i != j} x[v,i,j] <= |S| - 1

For very small n, static enumeration is possible:

python
from itertools import combinations

for v in vehicles:
    for r in range(2, n):
        for S_tuple in combinations(stations, r):
            S = set(S_tuple)
            model.addCons(
                quicksum(x[v, i, j] for i in S for j in S if i != j) <= len(S) - 1
            )

Pros:

  • Strong, direct subtour elimination.
  • No artificial order or flow variables.

Cons:

  • Exponential number of constraints.
  • Static enumeration is only acceptable for small station counts.

Use static DFJ only for tiny instances or as a debugging baseline.

4. Lazy or Iterative Cut Separation

The strongest practical pattern is to solve with base route constraints, detect subtours in incumbents, add only the violated DFJ cuts, and continue.

In solvers with convenient lazy callbacks, add cuts during branch-and-bound. Some Python solver APIs require callback or constraint-handler plumbing for true lazy enforcement, so iterative cut separation is often simpler for portable benchmark code:

python
def selected_arcs(model, x, v, arcs):
    return [(i, j) for i, j in arcs if model.getVal(x[v, i, j]) > 0.5]


def station_cycles_without_start(selected, stations):
    succ = {i: j for i, j in selected}
    cycles = []
    seen = set()

    for start in stations:
        if start in seen or start not in succ:
            continue
        path = []
        cur = start
        pos = {}
        while cur in succ and cur not in pos and cur not in seen:
            pos[cur] = len(path)
            path.append(cur)
            cur = succ[cur]
        seen.update(path)
        if cur in pos:
            cycle = path[pos[cur]:]
            if START not in cycle and END not in cycle:
                cycles.append(cycle)
    return cycles


while True:
    model.optimize()
    if model.getNSols() == 0:
        raise RuntimeError(f"no feasible solution; status={model.getStatus()}")

    cuts_added = 0
    for v in vehicles:
        selected = selected_arcs(model, x, v, arcs)
        for cycle in station_cycles_without_start(selected, stations):
            if len(cycle) >= 2:
                S = set(cycle)
                model.freeTransform()
                model.addCons(
                    quicksum(x[v, i, j] for i in S for j in S if i != j) <= len(S) - 1
                )
                cuts_added += 1

    if cuts_added == 0:
        break

Pros:

  • Adds only cuts that are needed.
  • Often stronger than MTZ.
  • Avoids exponential static SEC generation.

Cons:

  • Iterative resolve can be slower than a true callback.
  • Requires reliable subtour detection.
  • More moving parts than MTZ.

Use this when static MTZ is too weak and the solver environment does not make lazy callbacks convenient.

Method Choice

MethodBest ForAvoid When
MTZQuick, compact, small/medium MIPsLarge hard VRPs where relaxation strength matters
Single-commodity flowStronger static connectivity, optional visitsMemory is tight, or n is large
Multi-commodity flowVery strong small routing modelsMost practical benchmark tasks; too many variables
Static DFJTiny instances, debuggingMore than roughly 15-18 stations without careful filtering
Lazy/iterative DFJ cutsStrong routing models with many possible SECsSolver API/callback complexity is too risky

For pickup/dropoff rebalancing, start with MTZ or artificial connectivity flow. Do not use physical truck load as the only subtour-elimination mechanism because pickup/dropoff load can increase and decrease and may not prove route connectivity.

Validation

After solving, reconstruct each route by following selected arcs:

python
def extract_route(selected):
    outgoing = dict(selected)
    route = [START]
    cur = START
    seen = {START}
    while cur != END:
        if cur not in outgoing:
            raise RuntimeError(f"route disconnected at {cur!r}")
        cur = outgoing[cur]
        if cur in seen and cur != END:
            raise RuntimeError(f"cycle detected at {cur!r}")
        route.append(cur)
        seen.add(cur)
    return route

Fail fast if a selected solution has a disconnected cycle, repeated station, missing depot start, or missing depot end.

Frequently asked questions

What does the Routing Subtour Elimination AI skill do?

Subtour-elimination methods for TSP, VRP, pickup/dropoff routing, and routing MIPs with binary arc variables. Use when route-continuity constraints may permit disconnected cycles and the model needs MTZ constraints, flow-based connectivity constraints, DFJ subset cuts, or lazy/iterative subtour cuts.

Why use Routing Subtour Elimination on TypingMind?

Because you install it once and use it with any model. Routing Subtour Elimination is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Routing Subtour Elimination in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/benchflow-ai/skillsbench/tree/main/tasks/bike-rebalance/environment/skills/routing-subtour-elimination. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Routing Subtour Elimination?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Routing Subtour Elimination?

As many as you like. As long as a model supports skills, you can use Routing Subtour Elimination with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Routing Subtour Elimination AI skill free?

Yes. It is published on GitHub by benchflow-ai under the Apache-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇