Optimal 8/15-Puzzle Solver (2024)

Optimal 8/15-Puzzle Solver

The 8-puzzle is a classic problem in AI that can be solved with the A* algorithm.

  • A* maintains two lists, called open and closed.
  • At the beginning of the algorithm, the initial node is placed on the open list. The list is sorted according to an admissible heuristic that measures how close the state of the node is to the goal state.
  • At each step, bestNode is removed from the open list. In this case, bestNode is always the head of the open list. If the state of bestNode is the goal state, then the algorithm terminates. Otherwise bestNode is expanded (we determine all the possible states that can be reached within a single move), and bestNode is placed on the closed list.
  • The successors of bestNode are placed on the open list if either:
    • the successor is not already on the open or closed list, or
    • the successor is already on the open or closed list but has a lower cost.

Many 15-puzzles cannot be solved with the A* algorithm, since it generates too many new states and consumes a lot of memory maintaining these lists. To solve this larger puzzle, Iterative-Deepening-A* (IDA*) can be used. Like the A* algorithm, it finds optimal solutions when paired with an admissible heuristic but is much more efficient with respect to space. IDA* is described as follows:

  • Set threshold equal to the heuristic evaluation of the initial state.
  • Conduct a depth-first search, pruning a branch when the cost of the latest node exceeds threshold. If a solution is found during the search, return it.
  • If no solution is found during the current iteration, increment threshold by the minimum amount it was exceeded, and go back to the previous step.

Three heuristics have been implemented for comparison's sake:

  • Manhattan Distance
  • Manhattan Distance + Linear Conflict
  • O. Hansson, A. Mayer, and M. Yung, "Criticizing Solutions to Relaxed Models Yields Powerful Admissible Heuristics," Information Sciences, Vol. 63, Issue 3, pp. 207-227, 1992.
  • Additive Pattern Database Heuristic with Static 6-6-3 Partitioning
    A. Felner, R. Korf, and S. Hanan, "Additive Pattern Database Heuristics," Journal of AI Research, Vol. 22, pp. 279-318, 2004.

This JAR file is about 5.8 MB. Please be patient while it loads.

Download executable JAR: PuzzleSolver.jar


Optimal 8/15-Puzzle Solver (1)
Screenshot of 8/15-Puzzle Solver.

Source Code

  • AStar.java
  • AStarNode.java
  • AboutDialog.java
  • Algorithm.java
  • Application.java
  • ApplicationStarter.java
  • BFSNode.java
  • DFSWorker.java
  • Entry.java
  • FibonacciHeap.java
  • GUI.java
  • IDAStar.java
  • IDAStarNode.java
  • ImagePanel.java
  • MessageBox.java
  • Node.java
  • PatternDatabaseGenerator.java
  • PrimitiveHashMap.java
  • Puzzle.java
  • PuzzleConfiguration.java
  • PuzzleSolver.java
  • Utility.java

Implementation

This applet provides a means of comparing how different algorithms and heuristics perform on the sliding-tile puzzles. A* is more than acceptable for solving the the 8-puzzle, with its 9! / 2 = 181,440 states and optimal solutions of up to 31 moves. It becomes painfully slow, however, for even average length solutions of the 15-puzzle, which has 16! / 2 = 10,461,394,944,000 states and optimal solutions of up to 80 moves.


IDA* works well on most 15-puzzle configurations, especially when paired with the additive pattern database heuristic. 6-6-3 partitioning provides a good compromise between speed and the amount of memory required to hold the state-to-cost mappings, of which there are 11,534,880. The application runs slightly faster on 64-bit systems running a 64-bit JVM, since there are operations on long primitives.

Starting with version 2.2.0, multi-threaded IDA* can be used to more efficiently solve puzzles. This new algorithm starts with a breadth-first search of the tree to a level that has a number of nodes greater than or equal to the number of threads. Any nodes that contain the same board configuration are pruned before starting the thread pool. Then multiple depth-first search workers crawl through the search space, looking for a solution of up to threshold moves, as discussed above.

The number of threads is curently hard-coded at twice the number of available cores. This seemingly unusual number has provided the best performance in most cases on my AMD Phenom II x6. Ideally, we want the algorithm to minimize CPU idle time. Idle time occurs toward the end of an iteration in IDA*, when the depth-first search workers start to taper off. Without more complicated code, the next iteration cannot start until we are certain there is no solution for the given threshold. Thus, starting more threads than cores keeps the CPU at max capacity over most of the iteration and generally performs better than when starting fewer threads, even with the added cost of context switching.

Why bother with multi-threading? A single-threaded Java implementation of IDA* cannot compete with the same C implementation. When profiling this code, one will observe that a large majority of execution time is spent in looking up costs in Node.h(). This section of this method that implements with the pattern database heuristic is simple, but Java's array bounds checking truly hinders performance. In a simple test of Java versus C (compiled with gcc, -O2) where array elements are accessed in random order millions of times, C outperformed Java by a factor of 8. When applying the applet's pattern database heuristic, arrays are used only for cost lookups and to store the path to the goal state. Everything else has been implemented with primitives and shift operations. So, in order to improve performance, one must turn to multi-threading.


Usage

The 8/15-Puzzle Solver is easy to use. The options should be straightforward. To enter the configuration of a puzzle, simply list the numbers of the tiles from left to right, top to bottom, each separated by a comma. Enter 0 for the space. For example, "2,4,0,1,8,5,3,6,7" is what you would enter for the board configuration below:


2 4
1 8 5
3 6 7

Note that you must use IDA* to solve 15-puzzles, as the A* option has been purposefully disabled. Multi-threading is not available unless you accept the certificate, since the thread pool cannot be shutdown with the default policy settings, and is not available with the 8-puzzle (since a single thread is more than sufficient).


Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Last modified on December 29, 2021.
© 2000-2002, 2011-2013 Brian S. Borowski. All Rights Reserved.

Optimal 8/15-Puzzle Solver (2024)

FAQs

What is the algorithm for solving the 15-puzzle? ›

The IDA* and A* are the two most popular heuristic search algorithms widely used to solve the Fifteen Puzzle problem. The A* algorithm is one of most well-regarded algorithms in artificial intelligence for finding the shortest path or the smallest number of moves from the initial state to the goal [7].

How many moves does it take to solve A 15-puzzle? ›

For the 15 puzzle, lengths of optimal solutions range from 0 to 80 single-tile moves (there are 17 configurations requiring 80 moves) or 43 multi-tile moves; the 8 Puzzle always can be solved in no more than 31 single-tile moves or 24 multi-tile moves (integer sequence A087725).

Is 15-puzzle always solvable? ›

For an initial configuration, the fifteen puzzle is solvable when the parity of the permutation for all the blocks and the parity of the taxicab distance between the empty block and the bottom right corner of the grid are both even, or are both odd. The puzzle cannot be solved if one is even and the other is odd.

Is there any trick to solve puzzles? ›

Do not be obsessed with the same piece, the easiest thing is to start with the simple pieces or areas. The more pieces you place, the easier it is to continue. There are puzzles in which the edges are very complicated because they are the same color, for example, in this case, it is better to start with simpler areas.

How do you solve puzzles efficiently? ›

Our Top 7 Tricks To Complete Puzzles Like The Pros
  1. Turn all the pieces up the right way. ...
  2. Start with the borders. ...
  3. Sort the pieces into similar colours and build from there. ...
  4. Work on smaller images within the bigger image. ...
  5. Sort pieces into the same shapes (especially if you're struggling at the end with one block of colour)
Aug 20, 2021

What is the God's number in the 15-puzzle? ›

The Fifteen puzzle can be solved in 80 single-tile moves or 43 multi-tile moves in the worst case.

How many combinations does a 15-puzzle have? ›

The number of permutations of 15 objects is 15! = 1307674368000. The number of even permutations of 15 objects is 15!/2 = 653837184000. By Corollary 2.9, 15!/2 is an upper bound on the number of (legal) positions of the pieces in the 15-puzzle with the empty space in the lower right.

What is the inversion count in 15-puzzle? ›

Similarly, in the above random arrangement of squares, the inversion counts are 12, 9, 9, 5, 4, 4, 3, 3, 0, 3, 3, 2, 1, 1, and 0, giving an inversion sum of 59.

Is the 15 puzzle hard? ›

The puzzle is simple enough that it can be solved by children, but adults can have a difficult time solving it at first if they aren't good at solving puzzles. This set of instructions will be easy, and will only take 1 - 2 minutes for someone who is familiar with how to move the pieces around on a 15 Puzzle board.

Is the impossible 15 puzzle possible? ›

which is a combination of 4 odd permutations which is itself even. The empty space is on a white position so this problem (including the problem of having the tiles on their sides) is unsolvable.

What is the 1 9 equals 15 puzzle? ›

Magic Squares: then and now

The pattern was a 3x3 grid of nine squares, each containing one of the numbers between 1 and 9. No matter which way the 3 numbers in each row, each column and both diagonals of the square were added, the sum was always 15. This arrangement is what we now know as the 3x3 magic square.

What is the 15-puzzle problem algorithm? ›

The 15-Puzzle consists of a 4x4 frame of square tiles (numbered from 1 to 15), with one tile missing. The object of the game is to place the tiles in numerical order by sliding tiles, using the empty space. The puzzle can come in different sizes; in general, a NxN frame will use tiles numbered from 1 to N2 - 1.

What is the time complexity of the 15-puzzle problem? ›

Time-Complexity

Another, more commonly used, notation for V is b^d, where b is the branching factor and d is the deapth of the goal. Since branching factor in 15-puzzle is 3 on average and depth of the goal always <= 80, we have worst case time-complexity of O(3^80).

What is the inversion count in 15 puzzle? ›

Similarly, in the above random arrangement of squares, the inversion counts are 12, 9, 9, 5, 4, 4, 3, 3, 0, 3, 3, 2, 1, 1, and 0, giving an inversion sum of 59.

What is the puzzle where everything equals 15? ›

Magic Squares: then and now

The pattern was a 3x3 grid of nine squares, each containing one of the numbers between 1 and 9. No matter which way the 3 numbers in each row, each column and both diagonals of the square were added, the sum was always 15. This arrangement is what we now know as the 3x3 magic square.

What is the Fifteen Puzzle? ›

Fifteen Puzzle, puzzle consisting of 15 squares, numbered 1 through 15, which can be slid horizontally or vertically within a four-by-four grid that has one empty space among its 16 locations.

References

Top Articles
In The Heights Gomovies
Indiana Wesleyan University Sharepoint
Syracuse Pets Craigslist
Between Friends Comic Strip Today
O'reilly's In Monroe Georgia
Boomerang Uk Screen Bug
Abga Gestation Calculator
Umc Webmail
What Auto Parts Stores Are Open
Mileage To Walmart
Craigslist/Phx
Dr. Nicole Arcy Dvm Married To Husband
nycsubway.org: The Independent Fleet (1932-1939)
Uwa Schedule
University Of Toledo Email
Cooktopcove Com
DRAGON BALL Z - Goku Evolution - Light Canvas 40X3 NEU • EUR 37,63
Cool Math Games Unblocked 76
Les Schwab Product Code Lookup
The Woman King Showtimes Near Cinemark 14 Lancaster
Banned in NYC: Airbnb One Year Later
Maximise Your Funding: Key Insights on Accounting for Grants
15:30 Est
Persona 5 R Fusion Calculator
Norte Asesores Nanda
Kay Hansen blowj*b
Appraisalport Com Dashboard /# Orders
G Data IS lastet 16 GB RAM vollständig aus
Wolf Of Wall Street Tamil Dubbed Full Movie
Jvid Rina Sauce
Burlington Spectrum Tucson
Cal Poly 2027 College Confidential
Resident Evil Netflix Wiki
Footfetish Telegram
Lawson Uhs
Terraria Water Gun
Hotcopper Ixr
Krunker.io . Online Games . BrightestGames.com
Witchwood Icon
Pack & Ship Electronics, Artwork, Antiques and more at The UPS Store Newnan, GA at 90-F Glenda Trace
1875 Grams To Pounds And Ounces
Jetnet Retirees Aa
Sayre Australian Shepherds
City Of Irving Tx Jail In-Custody List
Aces Fmc Charting
Neo Geo Bios Raspberry Pi 3
Kieaira.boo
Joy Ride 2023 Showtimes Near Mjr Chesterfield
Ap Chem 2022 Frq Scoring Guidelines
Fast X Showtimes Near Regal Spartan
Physician Dressed As A Sorceress Crossword Clue
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6170

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.