IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New I'm thinking more along the lines of...
Ready? :P

"Knot theory". Don't have time to pursue it, but there's just got to be some way to represent the board in such a way that:

1) Trivial movements which get you nearer your goal are "normal", cost = 1.

2) Turning back upon your own path is a "knot", cost of two, sometimes three. Basically following the theory that any given problem is one of three cases: 0 knots, odd number of knots, even number of knots. Solve the knots (which for sufficiently long distances (> ~4), the actual position of the "knot" move is variable), and the rest is trivial.

-------------------------\n|   |   |   |   |   |   |\n-------------------------\n|   | 0 | 3 |   | 3 |   |\n-------------------------\n|   |   |   | 1 |   |   |\n-------------------------\n|   | 2 |   | 4 |   | 2 |\n-------------------------\n|   |   |   |   |   |   |\n-------------------------


Initial Cost = (distance between pt 0 and pt 4, no fair counting diagonal moves) / 3 = (4/3) = 1

Added Cost =
+1 for "undershooting" on move 0->1
+1 for "overshooting" (increasing the distance to target) on move 1->2
+1 for overshooting on move 2->3

The Added Cost occurs because we are seeking a penultimate position whose distance differential to the target is 3; i.e. where f(x, y) = ((x'-x) + (y'-y)) mod 3 = 0. Because the "distance" from 0 to 4 is 4 squares, 4 mod 3 = 1. Does every point where f(x, y) = 1 result in 3 "extra" moves? Does every point where f(x, y) = 2 result in 2 "extra" moves? I don't have the motivation to be rigorous here, just stating my research direction.

Note that for square boards, multiple equal-cost options are available at almost all decision points; this also implies that at least one of those options will be available even when we are near a board edge, assuming our board is at least 4x4. Gotta prove that one.

So, a brute-force comparison process could determine initial cost mathematically, saving a good chunk of time, and then solve the "endgame" either mathematically as well, or with a lookup table, if I'm wrong about f(x, y) patterns.

Does your algorithm need to show the path? or just calculate cost?

Many fears are born of stupidity and ignorance -
Which you should be feeding with rumour and generalisation.
BOfH, 2002 "Episode" 10
New That is a blind alley
Heuristics are a balance between the cost of calculating the heuristic and the value that the heuristic provides. With brute force algorithms, complex heuristics cost more than they win you. So KISS.

In this case all that I had to do was just keep track of choices. Just use recursion and order your neighbours from fewest moves left to most. That means visit the corners and edges first, then spiral around as you use up board. For the tour that solved the problem perfectly on the first try. (Perfection may be an accident of my implementation.)

For the circuit I had to add logic about what acceptable places to stop were, and logic that I absolutely could not leave any squares that I would have to end up on other than the desired one (which completed the circuit). With that reasoning something like 10% of my moves have to backtrack. Which is an effectiveness beyond all reasonable belief.

I have only tried square boards. Here is a sample result.


tilly@localhost:~$ ./knight_circuit.pl 8
SOLVED in 67 calls!
|---------------------------------------|
|  1 |  4 | 59 | 20 | 57 |  6 | 43 | 50 |
|---------------------------------------|
| 30 | 19 |  2 |  5 | 60 | 49 | 56 |  7 |
|---------------------------------------|
|  3 | 64 | 31 | 58 | 21 | 44 | 51 | 42 |
|---------------------------------------|
| 18 | 29 | 22 | 63 | 48 | 61 |  8 | 55 |
|---------------------------------------|
| 23 | 14 | 47 | 32 | 45 | 54 | 41 | 52 |
|---------------------------------------|
| 28 | 17 | 26 | 35 | 62 | 33 | 38 |  9 |
|---------------------------------------|
| 13 | 24 | 15 | 46 | 11 | 36 | 53 | 40 |
|---------------------------------------|
| 16 | 27 | 12 | 25 | 34 | 39 | 10 | 37 |
|---------------------------------------|


Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New A few moves I don't get
I don't see why step 3 backtracked when the rest of the first lap seems to follow a predictable pattern.

Then I see why step 14 was a backtrack (couldn't land on step 3) then the pattern resumes.

But then step 21 veers backwards. That seems to be where it reverses course to do a lap in the other direction, but I would have precdicted step 21 would go where 49 is.

How much does the logic depend on random selection? Could you actually determine at each step why it took the moves that seem counter-intuitive?
===

Implicitly condoning stupidity since 2001.
New Trivial
The choices won't make sense unless I tell you the exact algorithm. First the list of neighbours in order of preference is as follows:

    [1,2], [-1,2], [1,-2], [-1,-2],
    [2,1], [-2,1], [2,-1], [-2,-1],

Where, for instance, [1,2] means 1 left and 2 down (if that is within the board. I then resort this list from fewest ways to reach a neighbour up. The sort is stable - ties will resolve in the above order.

The choice to backtrack is made when you move into square (1,2) (where move 64 will be) before the last move, or when there are 2 or more required moves (a required move is to move into a space other than (1,2) which can only be reached one other way, or to move to a space with no other ways of moving into it). If there is a required move, you only try it. If there isn't, then you try your neighbours in the order given above, exiting on success.

Here is the reasoning process used in generating the above solution (walk through the reasoning and compare with where the numbers go):

At call 1, depth 1, pos (0, 0)
At call 2, depth 2, pos (1, 2)
  Backtrack to depth 2, position (0, 0)
At call 3, depth 2, pos (2, 1)
At call 4, depth 3, pos (0, 2)
At call 5, depth 4, pos (1, 0)
At call 6, depth 5, pos (3, 1)
At call 7, depth 6, pos (5, 0)
At call 8, depth 7, pos (7, 1)
At call 9, depth 8, pos (6, 3)
At call 10, depth 9, pos (7, 5)
At call 11, depth 10, pos (6, 7)
At call 12, depth 11, pos (4, 6)
At call 13, depth 12, pos (2, 7)
At call 14, depth 13, pos (0, 6)
At call 15, depth 14, pos (1, 4)
At call 16, depth 15, pos (2, 6)
At call 17, depth 16, pos (0, 7)
At call 18, depth 17, pos (1, 5)
At call 19, depth 18, pos (0, 3)
At call 20, depth 19, pos (1, 1)
At call 21, depth 20, pos (3, 0)
At call 22, depth 21, pos (4, 2)
At call 23, depth 22, pos (2, 3)
At call 24, depth 23, pos (0, 4)
At call 25, depth 24, pos (1, 6)
At call 26, depth 25, pos (3, 7)
At call 27, depth 26, pos (2, 5)
At call 28, depth 27, pos (1, 7)
At call 29, depth 28, pos (0, 5)
At call 30, depth 29, pos (1, 3)
At call 31, depth 30, pos (0, 1)
At call 32, depth 31, pos (2, 2)
At call 33, depth 32, pos (3, 4)
At call 34, depth 33, pos (5, 5)
At call 35, depth 34, pos (4, 7)
At call 36, depth 35, pos (3, 5)
At call 37, depth 36, pos (5, 6)
At call 38, depth 37, pos (7, 7)
At call 39, depth 38, pos (6, 5)
At call 40, depth 39, pos (5, 7)
At call 41, depth 40, pos (7, 6)
At call 42, depth 41, pos (6, 4)
At call 43, depth 42, pos (7, 2)
At call 44, depth 43, pos (6, 0)
At call 45, depth 44, pos (5, 2)
At call 46, depth 45, pos (4, 4)
At call 47, depth 46, pos (3, 6)
At call 48, depth 47, pos (2, 4)
At call 49, depth 48, pos (1, 2)
  Backtrack to depth 48, position (2, 4)
At call 50, depth 48, pos (4, 3)
At call 51, depth 49, pos (5, 1)
At call 52, depth 50, pos (7, 0)
At call 53, depth 51, pos (6, 2)
At call 54, depth 52, pos (7, 4)
At call 55, depth 53, pos (6, 6)
At call 56, depth 54, pos (5, 4)
At call 57, depth 55, pos (7, 3)
At call 58, depth 56, pos (6, 1)
At call 59, depth 57, pos (4, 0)
At call 60, depth 58, pos (3, 2)
At call 61, depth 59, pos (2, 0)
At call 62, depth 60, pos (1, 2)
  Backtrack to depth 60, position (2, 0)
At call 63, depth 60, pos (4, 1)
At call 64, depth 61, pos (5, 3)
At call 65, depth 62, pos (4, 5)
At call 66, depth 63, pos (3, 3)
At call 67, depth 64, pos (1, 2)
SOLVED in 67 calls!

Erm, um. This means that the moves that confused you were generally not the backtracks. The condition to always go to the neighbour which can be reached in the fewest other ways forces most choices...

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Sorry, I should've replied higher up.
I was developing this for Knight's Path only, not Tour/Circuit. And I wrote part of that in a misleading way...my hope would be that it would *not* be brute-force, but straightforward division.

Many fears are born of stupidity and ignorance -
Which you should be feeding with rumour and generalisation.
BOfH, 2002 "Episode" 10
Expand Edited by tseliot March 19, 2003, 11:55:55 AM EST
New Ah...
The path algorithm looks much easier to me.

I would personally be inclined to use the standard breadth-first shortest-path algorithm, with a heuristic saying that there is no need to fill in information about the shortest path outside of some distance from the shortest-line path.

No need to bring in knot theory. Besides which, knot theory is interesting because we don't understand it well. For computer algorithms it is better to work with things that we understand. :-)

Anyways I won't be trying that. I tried the tour and circuit because they looked like much harder (and therefore more interesting) problems.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
     Knight's Path and Knight's Tour - (jake123) - (10)
         Idea - (drewk)
         Heuristic for Knight's Tour - (ben_tilly) - (7)
             Works great for knight's circuit as well :-) - (ben_tilly) - (6)
                 I'm thinking more along the lines of... - (tseliot) - (5)
                     That is a blind alley - (ben_tilly) - (4)
                         A few moves I don't get - (drewk) - (1)
                             Trivial - (ben_tilly)
                         Sorry, I should've replied higher up. - (tseliot) - (1)
                             Ah... - (ben_tilly)
         I'm paying attention - (jake123)

Spyingwithdroids on sheep.
125 ms