Prolog中的铁路立方体
Rail Cube in Prolog

原始链接: https://chen.cat/prolog/railcube/

Owen Lacey 的 Rail Cube 挑战已用 Prolog 解决。用户最佳布局得 47 分,超越最初 42 分,使用全部 35 件组成带三叶结的单个回路。评分公式结合近距离和朝向,减去最长包围盒边长与重复项,并加 10 分奖励结。模型跟踪三维姿态、占用单元格、库存,并执行碰撞、距离、朝向、奇偶性与剪枝检查。穷举计数发现 14,137,062,002 种不同布局(约 523 GB 规范字符串)。计数在 X 处拆分为两个环并固定库存/部件。布局按循环起点、方向、反转和镜像规则规范化。验证包括重复运行、校验和、几何检查与小库存比较。给出了最佳 47 分布局字符串;Prolog 评估器验证指标。

Hacker News 对不起,我们无法如此快速地处理您的请求。 重新加载
相关文章

原文
Rail Cube in Prolog

I came across Owen Lacey’s article on optimising Rail Cube and wanted to try the problem in Prolog. Rail Cube is a toy train set whose tracks can run along walls and upside down. The challenge was to build a complete track and give it an interesting shape.

The score to beat when I started was 42. My best layout so far scores 47. I also ran a separate exhaustive count, which found more than 14 billion distinct layouts.

The pieces and the rules

I used the article’s inventory:

Letter Piece Count
S Straight 14
L Left curve 4
R Right curve 4
I Inside curve 8
O Outside curve 4
X Cross 1

A layout must use every piece and form one complete circuit. The train passes through the same X block twice. There are 35 physical pieces, but a complete track string contains 36 letters.

The article gives points for close calls with other parts of the track and for each different orientation the train takes. It subtracts points for the longest side of the bounding box and for repeated pieces. Each piece after the second in a consecutive run counts as a repeat. A knot earns ten points.

score = close calls + orientations - longest side - repeats + knot bonus

This gives the search several competing goals. A smaller box can bring parts of the track closer together. Curves change the train’s orientation, but they also use space that another part of the layout may need.

The Prolog model

Owen’s article describes a grid model for the pieces. I used the same model. A train’s pose contains its position, the direction of its floor, and its heading. Six floor directions and four headings on each face give 24 possible orientations.

Each piece changes the pose. These facts describe a straight and a left curve in their local coordinates:

Scryer Prolog and Trealla, without a constraint library.

Early cuts

A search through every piece order would spend most of its time on tracks that cannot close. I reject a partial track as soon as a rule makes completion impossible.

  • Each move consumes a piece from the inventory. Copies of the same piece do not create separate choices.
  • Material and train-clearance checks reject a collision when the next piece causes it.
  • A distance bound checks that the remaining pieces can cover the gap to the required finishing position.
  • Orientation and parity checks reject combinations that cannot reach the required finishing pose.
  • Precomputed endings handle the last six pieces. They must match the remaining inventory and the finishing pose exactly.

For the score search, I also keep an upper bound on what a partial track could earn. A branch must still be able to tie or beat the current best. The program checks the cheaper score terms before it does the knot calculation.

The program saves the best layouts and distinct ties as ordinary Prolog terms. It checks these saved tracks again when the next search starts.

A track that scores 47

This is my best known layout under the article’s scoring rules:

XOISISLISSISSSOSLSLLOSXSRRIROSISIRIS

view the visualisation here

It fits in a box of 9 by 7 by 6 cubes and forms a trefoil knot.

Metric Value Points
Longest side 9 -9
Close calls 32 +32
Different orientations 15 +15
Repeats 1 -1
Knotted Yes +10
Total 47
32 + 15 - 9 - 1 + 10 = 47

An earlier result scored 45 and fit inside a 7 by 7 by 7 box. The 47-point layout uses more space, but its extra close calls make up for that penalty.

The Prolog evaluator checks the 47-point layout directly:

Prolog canonicaliser checks a supplied track and prints its canonical form. For example, the 47-point layout above becomes:

$ scryer-prolog canonicalise.pl XOISISLISSISSSOSLSLLOSXSRRIROSISIRIS
XSILISISOLILLSXSORRSRSOSSSISSIRSISIO

Both strings describe the same layout. This gives me a consistent string to compare with the database, even when the input starts elsewhere or runs backwards.

How many track strings?

One physical layout can have many different strings. A string may begin halfway through a loop, and the train can travel in either direction.

Representation Count
Canonical layouts 14,137,062,002
Strings beginning with X 56,548,248,008
Strings beginning at any piece 1,017,868,464,144

For this inventory, every layout has 36 distinct cyclic starting points and two distinct travel directions:

14,137,062,002 × 36 × 2 = 1,017,868,464,144

This multiplication needs a symmetry check. A valid X-to-X loop contains an even number of straight pieces. A repeating half or a reversal that gives the same canonical form would require seven straights in each half. Seven is odd, so neither symmetry is possible.

Every layout contributes exactly 72 strings. Other inventories can have fewer forms because of symmetry. A rotation of the whole assembly adds no new strings, since the letters describe relative moves. Mirror layouts are already included in the layout count.

Checks on the results

I repeated the full count from a fresh search directory. The second run used batches of 257 first loops instead of 512. Each job ran on one worker, without help from other workers. Both runs finished all 6,251 jobs and returned 14,137,062,002.

I also checked the results in several other ways:

  • For small inventories, I compared complete solution sets with an independent geometry model and search.
  • A separate archive reader checked every frame checksum and recounted every saved partner set. Its totals matched every job’s saved counters.
  • An independent geometry checker tested 11,506 layouts sampled across all 2,376 jobs that contained solutions. Every sampled layout passed.

The two full runs share the rule model and parts of the search implementation. Their agreement gives evidence for the count, but it does not replace a separate implementation or a proof of the search rules.

The counting runs do not compute scores, so 47 remains my best known score.

Run the Prolog program

Keep rail-cube2.pl and its knot helper, rail-cube2-knot.pl, in the same folder. Then load the solver: