Giter Club home page Giter Club logo

csp's Introduction

Constraint Satisfaction Problem

Implementing CSP with Forward Checking constraint propagation on the example of solving Binary and Futoshiki puzzle

Features

  • Problem is defined by set of Variables with their possible values
  • Each Variable contains their own number and can be set to a value of int type
  • Model class is a problem representation (like Binary puzzle, Futoshiki etc.) that is defined as a set of Variables, Domain and Constraints based on the solving problem
  • There is the implementation of both search algoritms: Backtracking and Forward chechikg. See below the comparison of using this methods

Problems

Below are two problems taken for an example to demonstrate the use of CSP algorithm

The objective is to fill the grid with 1s and 0s, where there is an equal number of 1s and 0s in each row and column and no more than two of either number adjacent to each other. Additionally, there can be no identical rows or columns.
image

The puzzle is played on a square grid. The goal is to arrange the numbers so that each row and column contains only one of each digit. Some digits may be given at the start. Inequality constraints are initially specified between some of the squares, such that one must be higher or lower than its neighbor. These constraints must be honored in order to complete the puzzle.

Chosen heuristics

  1. Variable-Selection Heuristics The CSP was implemented with the MRV (Minimum remaining values) heuristic, that means choosing the variable with the fewest “legal” remaining values in its domain. Selecting this value helps detect inconsistencies earlier and, consequently, reduce the number of searches.
  2. Value-Selection Heuristic After selecting the variable, the order in which the values should be assigned to it must be selected. And to this problem has been implemented a heuristic, which is order values based on how many times each value is shared in a constraint. It means that the least "limiting" value will be selected. Btw after studying the impact of this heuristic was found that using Value-Selection Heuristic is senseles because eventually each value will be selected anyway

Forward cheking with constraint propagation is better than Backtrack algorithm

Forward checking detects the inconsistency earlier than simple backtracking and thus it allows branches of the search tree that will lead to failure to be pruned earlier than with simple backtracking. This reduces the search tree and (hopefully) the overall amount of work done. Below you can see two graphs that compare both algorithms.
This first graph shows dependence of the used algorithms on the time of finding the first / all solutions (on the example of Binary puzzle). The second graph represents the visited states dependence

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.