Giter Site home page Giter Site logo

npuzzle's Introduction

n-puzzle

This program solve n-puzzle game using A* algorithm. It's a work in progress.

How to use this software?

make
python3 main.py ...

Tips on Cython

With Cython, use:

cython file.pyx -a

To generate .html file with Cython's annotation telling you where your program is taking time.

A* algorithm

A* helps us to find the shortest way to solve the n-puzzle. Here is a description of how it works.

In order to find the path, you need an first state and the goal state. The goal state is generated with goal_generator. It generates the n-puzzle with spiral solution (it is not the usual way). For example, for a 4 by 4 puzzle the goal will be:

 1  2  3  4
12 13 14  5
11  0 15  6
10  9  8  7

Once, we did this we need to check if inputs are valid. Indeed, some puzzles are unsolvable. We can check using the technique described in this paper.

The solver is based on A* algorithm. To do so you need to create two lists: one for open 'states' and another for the 'closed' states. As we are going to explore the possibilities, we are going to maintain those lists to keep track of the previous and unexplored solutions. neighbors is the list of moves admissible to solve the puzzle.

def solve(current, end):
	neighbors = [-3, -1, 1, 3]
	open_set = []
	close_set = []
	open_list = [] # ???

We initiate open_set and open_list with the first state start.

	heapq.heappush(open_set, current)
	heapq.heappush(open_list, current)

Since we have states in our open_set or we didn't reach the goal state, we loop.

	while open_set:
		current = heapq.heappop(open_list) # take the current state (heapop gives us directly the one with the lowest heuristic - keep this in mind for later)
		if current == end:
			return retracePath(current) # retracePath() is just a function that go all the way up to recover the full solution and returns it in reverse order (from the first state to the goal state)

We actualize both lists. From open_set to close_set.

		open_set.remove(current)
		close_set.append(current)

Now we are going to generate the new states from our current state.

... to be done

Finally, we check if the new state is not in the close_set, otherwise that would mean we are going wrong way or we have already visited this state. If not, we compute the heuristic (in this example, it's manhattan) and we save the state, the heuristic, the parent and push it to the heap queue. As our heap queue is sorted, the next current initialization will be with the lowest heuristic.

			if tile not in close_set:
				tile.heuristic = abs(x_1 - x_2) + abs(y_1 - y_2)
				if tile not in open_set:
					open_set.add(tile)
					heapq.heappush(open_list, (tile.H, tile))
				tile.parent = current

Some useful link that we used

How to set your environment

Check your python version:

$ python -V

We are working with python 3.4.4. You'll need some packages such as Cython and numpy. We recommend you to set a virualenv. Here is a step by step:

# Install Virtualenv
$ pip install --user virtualenv
# Set Python 3 for Virtualenv
$ virtualenv -p python3 envname
# Start the env to your project folder
$ python -m venv /path/to/your/project
# OR
$ python3 -m venv /path/to/your/project
# Should be done, now activate the env. Go to you project folder and:
$ source bin/activate
# Check your python (should be 3...)
$ python -V
# Then install Cython and Numpy
$ pip install cython
$ pip install numpy

OR you can do

$ pip3 install --user cython
$ pip3 install --user numpy

npuzzle's People

Contributors

ademenet avatar aderragui avatar

Watchers

 avatar  avatar

npuzzle's Issues

N'arrive pas a resoudre

On le laisse generer un puzzle >>> il tente de le resoudre >>> mais ne finit jamais ! Il ne tombe pas sur le state goal en fait... Donc soit il y a un probleme dans le test de solvabilite soit il y a un probleme dans l'algo du Astar

la trace pour reproduire

Start:  [8 3 2 5 1 6 0 7 4]
--- Generating goal state
Goal:  [1 2 3 8 0 4 7 6 5]
--- Solving puzzle using A-star
--- END of the program

isSolvable bug

Voici le npuzzle qui fout le bordel :

3
8 1 3
7 4 0
6 2 5

Il m'affiche is not solvable alors que si, je l'ai crée de mes propres mains en faisant 6/7 moves. Et si je coupe la fonction isSolvable bah, notre algo résout bien le truc en fait...
Ça donne :

[[1 2 3]
 [8 0 4]
 [7 6 5]]
[[1 0 3]
 [8 2 4]
 [7 6 5]]
[[0 1 3]
 [8 2 4]
 [7 6 5]]
[[8 1 3]
 [0 2 4]
 [7 6 5]]
[[8 1 3]
 [7 2 4]
 [0 6 5]]
[[8 1 3]
 [7 2 4]
 [6 0 5]]
[[8 1 3]
 [7 0 4]
 [6 2 5]]
[[8 1 3]
 [7 4 0]
 [6 2 5]]

à lire de bas en haut pour la résolution

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.