Skip to content

Add positional encoding algorithms for NCO#280

Open
cbhua wants to merge 3 commits into
mainfrom
dev-pe
Open

Add positional encoding algorithms for NCO#280
cbhua wants to merge 3 commits into
mainfrom
dev-pe

Conversation

@cbhua
Copy link
Copy Markdown
Member

@cbhua cbhua commented May 12, 2026

Description

This PR adds a new self-contained module rl4co/utils/pe.py implementing a broad taxonomy of positional encodings (PEs) relevant to Neural Combinatorial Optimization, plus tests and a demonstration notebook.

Motivation and Context

NCO models benefit from positional/structural encodings that respect the geometry of routing problems (closed-tour circularity, multi-route hierarchy, pairwise graph distances). Existing PE code in the repo (rl4co/models/nn/pos_embeddings.py, rl4co/models/nn/ops.py) is tightly coupled to the improvement-policy / env-embedding pipelines. This PR provides a standalone, well-tested library of PE building blocks (classic baselines + proposed IPE/XPE/Hierarchical methods) plus a factory, so they can be reused and composed independently — and a notebook documenting their behavior. Nothing under rl4co/models/** or configs/** is changed; wiring a selected PE into an encoder forward pass is left as a follow-up.

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

Comment thread rl4co/utils/pe.py
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to call this positional_encodings.py instead? Or do you think pe is good already?

Comment thread rl4co/utils/pe.py
return out


class RotaryPE(nn.Module):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comment: I thik these would be great as documents in the mkdocs too!

Comment thread rl4co/utils/pe.py
flat_src = src.reshape(-1, src.shape[-1])
flat_dst = dst.reshape(-1, dst.shape[-1])
flat_w = w.reshape(-1, w.shape[-1])
for b in range(flat_adj.shape[0]):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be vectorized?

"\n",
"This notebook demonstrates how to *use* and *visualize* every positional encoding (PE) in\n",
"[`rl4co/utils/pe.py`](../../rl4co/utils/pe.py). It is organized around the three desiderata\n",
"that the survey/method paper uses to characterize PEs for neural combinatorial optimization\n",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the citations too! 🔝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants