22
33Tools to solve [ constraint satisfaction problems] ( https://en.wikipedia.org/wiki/Constraint_satisfaction_problem ) : ** C** onstraint ** S** atisfaction ** P** roblem ** S** olvers.
44
5- < a href = " https://www.npmjs.com/package/csps " >< img src = " https://img .shields.io/npm/v/csps.svg" alt = " npm version " ></ a >
6- < a href = " " disabled >< img src = " https://img.shields.io/badge/node-%3E%3D10-blue.svg?cacheSeconds=2592000 " alt = " node >= 10 " ></ a >
7- < a href = " https://www.npmjs.com/package/csps " >< img src = " https://img.shields.io/bundlephobia/minzip/csps.svg " alt = " size < 1k " ></ a >
8- < a href = " https://github.com/charkour/csps " >< img src = " https://img .shields.io/github/workflow/status/charkour/csps/CI.svg" alt = " build status " ></ a >
9- < a href = " https://www.npmjs.com/package/ csps " >< img src = " https://img.shields.io/david/charkour/ csps " alt = " zero deps " ></ a >
10- [ ![ Downloads ] ( https://img.shields.io/npm/dt/csps.svg )] ( https://www.npmjs.com/package/csps )
5+ [ ![ npm version ] ( https://img.shields.io/npm/v/csps.svg )] ( https://www.npmjs.com/package/csps )
6+ ![ node >= 10 ] ( https://img.shields.io/badge/node-%3E%3D10-blue.svg?cacheSeconds=2592000 )
7+ [ ![ size < 1k ] ( https://img.shields.io/bundlephobia/minzip/csps.svg )] ( https://www.npmjs.com/package/csps )
8+ [ ![ build status ] ( https://img.shields.io/github/workflow/status/charkour/csps/CI.svg )] ( https://github.com/charkour/csps )
9+ [ ![ zero deps ] ( https://img.shields.io/david/charkour/ csps )] ( https://www.npmjs.com/package/ csps )
10+ [ ![ downloads ] ( https://img.shields.io/npm/dt/csps.svg )] ( https://www.npmjs.com/package/csps )
1111
1212> Inspired by [ Russell and Norvig's "Artificial Intelligence - A Modern Approach" Python code] ( https://github.com/aimacode/aima-python ) and modified under the MIT license.
1313
@@ -30,21 +30,26 @@ index.ts:
3030``` ts
3131import { CSP , min_conflicts } from " csps" ;
3232
33+ // define the attributes on your variable
34+ interface VariableAttributes {
35+ // {[key: string]: string}
36+ }
37+
3338// Setup your problem
3439const variables = [
3540 /* array of strings */
3641];
3742const domains = {
38- /* var: possible_attributes<string>[] */
43+ /* var: possible_attributes (type: VariableAttributes[]) */
3944};
4045const neighbors = {
41- /* var: neighbors<string>[] */
46+ /* var: neighbors (type: <string>[]) */
4247};
4348const constraints = (
4449 var1 : string ,
45- var1Attributes : string [],
50+ var1Attributes : VariableAttributes [],
4651 var2 : string ,
47- var2Attributes : string [],
52+ var2Attributes : VariableAttributes [],
4853): boolean => {
4954 // Return true if same variable.
5055 if (var1 === var2 ) {
@@ -57,11 +62,16 @@ const constraints = (
5762 return true ;
5863};
5964
60- const aCSP = new CSP <string >(variables , domains , neighbors , constraints );
65+ const aCSP = new CSP <VariableAttributes >(variables , domains , neighbors , constraints );
6166
6267// run min_conflicts on problem
6368const res = min_conflicts (aCSP );
6469console .log (res );
70+ // {
71+ // var1: { attr1: 'value1', attr2: 'value1', attr3: 'value1' },
72+ // var2: { attr1: 'value2', attr2: 'value2', attr3: 'value2' },
73+ // var3: { attr1: 'value3', attr2: 'value3', attr3: 'value3' },
74+ // } // or something similar.
6575```
6676
6777### Demo
0 commit comments