Code Monkey home page Code Monkey logo

n-puzzle's Introduction

N-Puzzle

A project about solving N-Puzzles (with N ≥ 3)

Implementation :

This project uses 3 different search algorithms :

  • The A* algorithm
  • The Greedy Search algorithm
  • The Uniform Cost algorithm

And 4 different heuristics :

  • The manhanttan distance
  • The Euclidian distance
  • The diagonal distance
  • The Hamming distance

These algorithms are very similar, using the same search function but different cost functions :

  • A* uses h(x) + g(x)
  • Greedy uses h(x)
  • Uniform uses g(x)

With h(x) being an admissible heuristic function, and g(x) being the cost of the path from the start node.

Note that the program stops at the first valid solution and won't go for the shortest, as it is an NP-Hard problem.

Running the program :

Clone the repo, and simply run :

stack build
stack run

Make sure that you have installed The Haskell Tool Stack first.

The program takes a file as argument, being your puzzle, and optionally a custom search and custom heuristic, like so :

stack run path/to/your/file [optional custom search] [optional custom heuristic]

The input file should be as following :

3       # size of your puzzle
0 2 3   # the puzzle itself
1 4 5
8 7 6

You should get the following as output :

Solving grid using the A* algorithm and the Manhattan distance
0 2 3
1 4 5
8 7 6
-----
1 2 3
0 4 5
8 7 6
-----
1 2 3
8 4 5
0 7 6
-----
1 2 3
8 4 5
7 0 6
-----
1 2 3
8 4 5
7 6 0
-----
1 2 3
8 4 0
7 6 5
-----
1 2 3
8 0 4
7 6 5
-----
Solved with :
- Time complexity  : 6
- Space complexity : 3

Possible improvements :

  • Add proper solvability check
  • Add more heurstics
  • Update data structures for better perfs
  • Correct output to get the number of steps properly (current path is too large)
  • Add number of moves needed from initial state to final state

n-puzzle's People

Contributors

keuhdall avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

n-puzzle's Issues

Priority error in parsing

Comments should be stripped before removing the first line of the puzzle and not after after current behavior leads us into having test2.puzzle failing

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.