Giter Site home page Giter Site logo

cspjs's Introduction

Csp-Js

This library is for educational purposes only. it is suboptimal and not ready for production.

Means of CSP

A way to solve a wide variety of problems more efficiently: A factored representation for each state: a set of variables, each of which has a value. A problem is solved when each variable has a value that satisfies all the constraints on the variable. A problem described this way is called a constraint satisfaction problem, or CSP.

How it works?

CSP search algorithms take advantage of the structure of states and use general-purpose rather than problem-specific heuristics to enable the solution of complex problems. The main idea is to eliminate large portions of the search space all at once by identifying variable/value combinations that violate the constraints. [From 'Artificial Intelligence: A Modern Approach'].

Example:

demo_image The principal states and territories of Australia. Coloring this map can be viewed as a constraint satisfaction problem (CSP). The goal is to assign colors to each region so that no neighboring regions have the same color.

const m1 = new Model([
    // Initial map state: Only 'SA' region has a color assignament
    new Variable( 'SA', 'R' ),
    new Variable( 'WA', undefined ),
    new Variable( 'NT', undefined ),
    new Variable( 'Q', undefined ),
    new Variable( 'NSW', undefined ),
    new Variable( 'V', undefined ),
    new Variable( 'T', undefined ),
], [
    // Possible domain values for each variable 
    // ('SA' region has some pruned values due to the early assignement).
    new Domain('SA',  [ 'R' ], ['G', 'B'], [] ),
    new Domain('WA',  [ 'R', 'G', 'B' ], [], [] ),
    new Domain('NT',  [ 'R', 'G', 'B' ], [], [] ),
    new Domain('Q',  [ 'R', 'G', 'B' ], [], [] ),
    new Domain('NSW',  [ 'R', 'G', 'B' ], [], [] ),
    new Domain('V',  [ 'R', 'G', 'B' ], [], [] ),
    new Domain('T',  [ 'R', 'G', 'B' ], [], [] ),
], [
    // Neighbors relationships
    new Relationship('SA', [ 'WA', 'NT', 'Q', 'NSW', 'V'] ),
    new Relationship( 'WA', [ 'SA', 'NT' ] ),
    new Relationship( 'NT', [ 'SA', 'WA', 'Q' ] ),
    new Relationship( 'Q', [ 'NT', 'NSW', 'SA' ] ),
    new Relationship( 'NSW', [ 'SA', 'Q', 'V' ] ),
    new Relationship( 'V', [ 'SA', 'NSW' ] ),
    new Relationship( 'T', [ ] ),
], [
    (iDVal, jDVal) => {
        return iDVal !== jDVal;
    }
]);

const bt1 = Backtracking(m1, FirstUnassignedVariableStrategy, UnorderedDomainValuesStrategy, NoInference);
const prettyModel = JSON.stringify(m1.variables, undefined, 2);
console.log(`Resolved: ${bt1}`);
console.log(`Model: ${prettyModel}`);

Output:

Resolved: true
Model: [
  {
    "value": "R",
    "key": "SA"
  },
  {
    "value": "G",
    "key": "WA"
  },
  {
    "value": "B",
    "key": "NT"
  },
  {
    "value": "G",
    "key": "Q"
  },
  {
    "value": "B",
    "key": "NSW"
  },
  {
    "value": "G",
    "key": "V"
  },
  {
    "value": "R",
    "key": "T"
  }
]

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.