8-puzzle | Solution of 8-puzzle problem using A * algorithm | Learning library
kandi X-RAY | 8-puzzle Summary
kandi X-RAY | 8-puzzle Summary
Solution of 8-puzzle problem using A* algorithm
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Solves a slider
- Returns the board of all boards
- Returns the number of moves to solve
- Returns true if the initial board is solvable
- Returns a tie between two blocks
- Exchange two board elements
- Returns all the neighbors of this board
- Returns a string representation of the board
- Returns true if this board equals another board
8-puzzle Key Features
8-puzzle Examples and Code Snippets
Community Discussions
Trending Discussions on 8-puzzle
QUESTION
Considering three heuristics for 8-puzzle:
...ANSWER
Answered 2020-Oct-11 at 08:53Yes, because you would get the same value only if all misplaced tiles are just next to their correct place (i.e. manhattan distance = 1). In all other cases the manhattan distance for a misplaced tile is > 1.
QUESTION
I'm working on implementing a Best-First Search algorithm to solve an 8-Puzzle problem for an assignment. Based on the requirements, it must be implemented using a (min) Priority Queue or Heap located in the Standard Template Library (STL).
I understand that it would be useful to use either data structure to organise expanded puzzle states by best heuristic cost (ie. the smallest cost).
Beginning with a 3x3 matrix (implemented using an array)
...ANSWER
Answered 2020-Mar-31 at 07:25Yes, you can specify a custom compare function for priority_queue
QUESTION
I'm currently writing an algorithm that solves the 8-puzzle game through an A* search algorithm with Python. However, when I time my code, I find that get_manhattan_distance
takes a really long amount of time.
I ran my code with cProfile
for Python, and the results are below what is printed out by the program. Here is a gist for my issue.
I've already made my program more efficient by copying using Numpy Arrays instead of Python's lists. I don't quite know how to make this step more efficient. My current code for get_manhattan_distance
is
ANSWER
Answered 2019-Mar-30 at 06:17It looks to me like you should only need to calculate the full Manhattan distance sum for an entire board once - for the first board. After that, you're creating new Board
entities from existing ones by swapping two adjacent numbers. The total Manhattan distance on the new board will differ only by the sum of changes in Manhattan distance for these two numbers.
If one of the numbers is the blank (0
), then the total distance changes by minus one or one depending on whether the non-blank number moved closer to its proper place or farther from it. If both of the numbers are non-blank, as when you're making "twins", the total distance changes by minus two, zero, or two.
Here's what I would do: add a manhattan_distance = None
argument to Board.__init__
. If this is not given, calculate the board's total Manhattan distance; otherwise simply store the given distance. Create your first board without this argument. When you create a new board from an existing one, calculate the change in the total distance and pass the result in to the new board. (The cached_manhattan
becomes irrelevant.)
This should reduce the total number of calculations involved with distance by quite a bit - I'd expect it to speed things up by several times, more the larger your board size.
QUESTION
My implementation of BFS in Python to solve the 8-puzzle is taking at least 21 minutes to find a solution. How can I improve my code in order to achieve a better time? The way I've implemented is very inefficient. I'd like to know any advice about how can I improve it in a way to solve in an acceptable time.
...ANSWER
Answered 2019-Aug-15 at 18:37QUESTION
I am implementing a best first search and A* algorithms to solve the 8-piece puzzle program, but first need to verify if a state is solvable or not.
To find if a state is solvable or not I use the number of inversions and the parity of that number. Look here more information on finding solvable states.
My question resides inside the solvable function:
...ANSWER
Answered 2019-May-17 at 18:24You have a typo. You've passed three lists:
QUESTION
I'm new to Objective-C, so I'm trying to learn by implementing algorithms. I'm doing an A* search for solving the 8-puzzle problem. Before running the algorithm itself, I want to check whether the given puzzle combination is solvable. I've written this code in C++ and Swift before, but it doesn't work correctly in Objective-C for me. For this array it gives inversion count of 7, while it should be 0. Maybe I should use [NSArray objectAtIndex:] method to access elements and then convert them to integers to compare? I've tested different ways and the comparison works right. Please help me to find the bug.
...ANSWER
Answered 2017-Jul-23 at 17:37Basically this line of code is wrong:
QUESTION
implementing a greedy solution to solve and 8-puzzle
Greedy.h:
...ANSWER
Answered 2018-Sep-07 at 04:02The problem is in Greedy::doGreedy
and your usage of current
.
The assignment current = greedyQueue.top();
creates a copy of the top object in the queue. Later, when you call vector childrenFound = current.expandNode();
, all the returned states have parent pointers that refer to current
. On the next loop iteration, you make that assignment to current
again, changing the parent pointed to by all those returned states.
There isn't an easy fix with your code. You need to rethink how you store State
objects so that the parent objects stay around and unmodified. Often this sort of thing is done with a stack or list, adding each node to the end and popping them off to move up to the parent.
QUESTION
Background: I am currently working on an 8-puzzle implementation of the original A Star algorithm and comparing this with a slightly modified algorithm which intends to improve node expansion (using additional information, ofcourse A Star in an equally informed unidirectional search has been proven optimal). The Open List of nodes are currently ordered by their F values (G-Cost+ H-Cost).
So in implementing this in Java I have set up a comparator which orders the List by their F-Values in ascending order.
...ANSWER
Answered 2018-May-25 at 04:17I think this is OK for two reasons
1) You are only changing the behaviour in the case of ties, so all you are doing is selecting one possible execution path from a larger set of execution paths which are possible with the original version.
2) You preserve the property that if you retrieve the goal node from the open List, every other node in the open has G-Cost + H-Cost at least as expensive as that of the node you have just retrieved, and so must lead to a path to the goal node at least as expensive as the node you have just retrieved.
By favoring nodes with low heuristic cost in the case of ties, you are favoring any goal nodes in the case of ties, so I guess you might retrieve the goal node slightly earlier and so finish slightly earlier.
QUESTION
I'm trying to solve an 8-puzzle, and I'm trying to generate possible board configurations for moves of the blank tile. I'm going to return these configurations in a puzzle array with board configuration as data. When I run my code below, it only stores the last move for the blank tile in instances where there are multiple moves for the blank tile. I'm not sure how do stop it from overwriting the previous array data.
...ANSWER
Answered 2017-Apr-28 at 06:52The problem is that you are creating a shallow copy of the current puzzle, so you change the current puzzle in each loop. You should, instead create a deep copy of the current puzzle, and leave the current puzzle intact. I don't know the full implementation of your Puzzle class, but you may want to check your constructor and setter methods.
Create a new constructor for Puzzle:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install 8-puzzle
You can use 8-puzzle like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the 8-puzzle component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page