es6-fuzz 3.0.6 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jscsrc +0 -1
- package/Readme.md +55 -0
- package/docs/CHANGELOG.md +21 -25
- package/lib/logic.js +30 -5
- package/package.json +5 -24
- package/test/js-bool.test.js +86 -0
- package/test/logic-test.js +2 -2
package/.jscsrc
CHANGED
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"disallowEmptyBlocks": true,
|
|
15
15
|
"disallowSpacesInsideParentheses": true,
|
|
16
16
|
"disallowSpaceAfterObjectKeys": true,
|
|
17
|
-
"requireSpaceAfterBinaryOperators": true,
|
|
18
17
|
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
|
|
19
18
|
"requireSpaceBeforeBinaryOperators": [
|
|
20
19
|
"=",
|
package/Readme.md
CHANGED
|
@@ -58,6 +58,61 @@ var res = logic
|
|
|
58
58
|
|
|
59
59
|
* hot
|
|
60
60
|
|
|
61
|
+
|
|
62
|
+
## Usage with boon-js
|
|
63
|
+
|
|
64
|
+
In order to combine 2 fuzzy functions with boolean logic, there is a compat layer for boon-js which allows the sauge of 'boolean expression language'.
|
|
65
|
+
|
|
66
|
+
Example of a monster biting when its cold and you are close to it:
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
Heat part:
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
var logicHeat = new Logic();
|
|
73
|
+
const optimalTemperature = new Triangle(10, 20, 30);
|
|
74
|
+
const toColdTemperature = new Triangle(0, 10, 15);
|
|
75
|
+
const toHotTemperature = new Triangle(25, 40, 60);
|
|
76
|
+
|
|
77
|
+
logicHeat.init('cold', toColdTemperature)
|
|
78
|
+
logicHeat.or('optimal', optimalTemperature)
|
|
79
|
+
logicHeat.or('hot', toHotTemperature);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Distance Part
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
|
|
86
|
+
var logicDistance = new Logic();
|
|
87
|
+
const close = new Triangle(0, 10, 20);
|
|
88
|
+
const far = new Triangle(5, 50, 100);
|
|
89
|
+
|
|
90
|
+
logicDistance.init('close', close)
|
|
91
|
+
logicDistance.or('far', far)
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Now we marry the 2 and use boon js
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const monsterBiteTest = getEvaluator(
|
|
99
|
+
'heat.cold AND distance.close',
|
|
100
|
+
);
|
|
101
|
+
const resHeat = logicHeat.defuzzify(2, 'heat');
|
|
102
|
+
const resClose = logicDistance.defuzzify(2, 'distance');
|
|
103
|
+
|
|
104
|
+
const jsBoonInput = { ...resHeat.boonJsInputs, ...resClose.boonJsInputs }
|
|
105
|
+
|
|
106
|
+
monsterBiteTest(jsBoonInput)
|
|
107
|
+
// returns true
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
61
116
|
## development
|
|
62
117
|
|
|
63
118
|
**Tests** use mocha and a plugin for traceur
|
package/docs/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
|
+
2022-06-14
|
|
2
|
+
==========
|
|
3
|
+
|
|
4
|
+
* 4.0.1
|
|
5
|
+
* fix: update readme
|
|
6
|
+
* (churn): changelog
|
|
7
|
+
* 4.0.0
|
|
8
|
+
* feature: make the api work with boonJsInputs
|
|
9
|
+
* feature: add a test for outoput names only to consist of alphabet
|
|
10
|
+
letters
|
|
11
|
+
|
|
12
|
+
2022-06-11
|
|
13
|
+
==========
|
|
14
|
+
|
|
15
|
+
* (churn): changelog
|
|
16
|
+
* 3.0.7
|
|
17
|
+
* fix: revert dep change
|
|
18
|
+
* refactor: make dependencies lighter
|
|
19
|
+
|
|
1
20
|
2022-06-02
|
|
2
21
|
==========
|
|
3
22
|
|
|
23
|
+
* 3.0.6
|
|
24
|
+
* (churn): changelog
|
|
4
25
|
* feature: chehck if init was called before any logic os performed
|
|
5
26
|
* test: add another example to the tests
|
|
6
27
|
* chore: remove travis config
|
|
@@ -129,28 +150,3 @@
|
|
|
129
150
|
* 2.5.7
|
|
130
151
|
* 2.5.6
|
|
131
152
|
* Added docs
|
|
132
|
-
* Fix output ght pages
|
|
133
|
-
* DOcs
|
|
134
|
-
* Ghpages
|
|
135
|
-
* 2.5.5
|
|
136
|
-
* Autorelease
|
|
137
|
-
|
|
138
|
-
2017-01-27
|
|
139
|
-
==========
|
|
140
|
-
|
|
141
|
-
* (churn): changelog
|
|
142
|
-
* 2.5.4
|
|
143
|
-
* Merge branch 'master' of github.com:sebs/es6-fuzz
|
|
144
|
-
* Merge pull request [#64](https://github.com/sebs/es6-fuzz/issues/64) from sebs/greenkeeper-browserify-14.0.0
|
|
145
|
-
Update browserify to version 14.0.0 🚀
|
|
146
|
-
|
|
147
|
-
2017-01-25
|
|
148
|
-
==========
|
|
149
|
-
|
|
150
|
-
* chore(package): update browserify to version 14.0.0
|
|
151
|
-
https://greenkeeper.io/
|
|
152
|
-
|
|
153
|
-
2017-01-09
|
|
154
|
-
==========
|
|
155
|
-
|
|
156
|
-
* (churn): changelog
|
package/lib/logic.js
CHANGED
|
@@ -48,40 +48,51 @@ class Logic {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
checkOutputName(name) {
|
|
52
|
+
const reg = /^[a-z]+$/i;
|
|
53
|
+
if (!reg.test(name)) {
|
|
54
|
+
throw Error('Output names can only be strings without space, without numbers and without special chars');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
51
60
|
init(output, shape) {
|
|
61
|
+
this.checkOutputName(output);
|
|
52
62
|
this.initCalled = true;
|
|
53
63
|
let type = TYPE_INIT;
|
|
54
64
|
this.rules.push({ output, shape, type });
|
|
55
65
|
return this;
|
|
56
66
|
}
|
|
57
67
|
and(output, shape) {
|
|
68
|
+
this.checkOutputName(output);
|
|
58
69
|
this.checkInitCalled();
|
|
59
70
|
let type = TYPE_AND;
|
|
60
71
|
this.rules.push({ output, shape, type });
|
|
61
72
|
return this;
|
|
62
73
|
}
|
|
63
|
-
or(output, shape) {
|
|
74
|
+
or(output, shape) {+
|
|
75
|
+
this.checkOutputName(output);
|
|
64
76
|
this.checkInitCalled();
|
|
65
77
|
let type = TYPE_OR;
|
|
66
78
|
this.rules.push({ output, shape, type });
|
|
67
79
|
return this;
|
|
68
80
|
}
|
|
69
81
|
not(output, shape) {
|
|
82
|
+
this.checkOutputName(output);
|
|
70
83
|
this.checkInitCalled();
|
|
71
84
|
let type = TYPE_NOT;
|
|
72
85
|
this.rules.push({ output, shape, type });
|
|
73
86
|
return this;
|
|
74
87
|
}
|
|
75
|
-
defuzzify(value) {
|
|
88
|
+
defuzzify(value, as=undefined) {
|
|
76
89
|
this.checkInitCalled();
|
|
77
90
|
let defuzzified = 'none';
|
|
78
91
|
let fuzzified = 0;
|
|
79
92
|
let lastShape;
|
|
80
|
-
|
|
81
93
|
this.rules.forEach(rule => {
|
|
82
94
|
rule.fuzzy = rule.shape.fuzzify(value);
|
|
83
95
|
// lets keep the initial value
|
|
84
|
-
|
|
85
96
|
if (rule.type === TYPE_INIT) {
|
|
86
97
|
defuzzified = rule.output;
|
|
87
98
|
fuzzified = rule.fuzzy;
|
|
@@ -97,14 +108,28 @@ class Logic {
|
|
|
97
108
|
defuzzified = rule.output;
|
|
98
109
|
fuzzified = rule.fuzzy;
|
|
99
110
|
// if there is no shape, like for example for a NOT keep the last one
|
|
100
|
-
lastShape = rule.shape ||
|
|
111
|
+
lastShape = rule.shape || lastShape;
|
|
101
112
|
}
|
|
102
113
|
});
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
var namePrefix = '';
|
|
118
|
+
if (!!as && typeof as === 'string') {
|
|
119
|
+
namePrefix = as + '.';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const boonJsInputs = {};
|
|
123
|
+
this.rules.forEach(rule => {
|
|
124
|
+
boonJsInputs[`${namePrefix}${rule.output}`] = rule.output === defuzzified;
|
|
125
|
+
});
|
|
126
|
+
|
|
103
127
|
/**
|
|
104
128
|
*
|
|
105
129
|
* @example fuzzy.defuzzify(10)
|
|
106
130
|
*/
|
|
107
131
|
return {
|
|
132
|
+
boonJsInputs,
|
|
108
133
|
fuzzified: fuzzified,
|
|
109
134
|
defuzzified: defuzzified,
|
|
110
135
|
rules: this.rules,
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es6-fuzz",
|
|
3
3
|
"description": "fuzzy logic with and for es6",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "4.0.1",
|
|
5
5
|
"main": "lib/logic.js",
|
|
6
6
|
"repository": "git@github.com:sebs/es6-fuzz.git",
|
|
7
7
|
"homepage": "https://github.com/sebs/es6-fuzz",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"test": "mocha -r blanket",
|
|
9
|
+
"changelog": "rm ./docs/CHANGELOG.md && npx changelog https://github.com/sebs/es6-fuzz all > ./docs/CHANGELOG.md && git add . && git commit . -m '(churn): changelog'",
|
|
10
|
+
"test": "npx mocha",
|
|
12
11
|
"test:watch": "npm test -- -w",
|
|
13
12
|
"addpush": "git add . && git commit . -m 'docs'",
|
|
14
13
|
"pages": "node ./scripts/publish-gh.js",
|
|
@@ -23,32 +22,14 @@
|
|
|
23
22
|
"javascript",
|
|
24
23
|
"es6"
|
|
25
24
|
],
|
|
26
|
-
"config": {
|
|
27
|
-
"blanket": {
|
|
28
|
-
"pattern": "lib/*",
|
|
29
|
-
"data-cover-never": [
|
|
30
|
-
"node_modules",
|
|
31
|
-
"tests"
|
|
32
|
-
]
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
25
|
"author": "Sebastian Schürmann",
|
|
36
26
|
"license": "MIT",
|
|
37
27
|
"devDependencies": {
|
|
38
|
-
"browserify": "17.0.0",
|
|
39
|
-
"changelog": "1.4.2",
|
|
40
|
-
"documentation": "13.2.5",
|
|
41
|
-
"es6ify": "1.6.0",
|
|
42
|
-
"eslint": "8.16.0",
|
|
43
28
|
"gh-pages": "4.0.0",
|
|
44
29
|
"ink-docstrap": "1.3.2",
|
|
45
|
-
"
|
|
46
|
-
"jsdoc": "3.6.10",
|
|
47
|
-
"mocha": "10.0.0",
|
|
48
|
-
"mocha-traceur": "2.1.0",
|
|
49
|
-
"traceur": "0.0.111"
|
|
30
|
+
"jsdoc": "3.6.10"
|
|
50
31
|
},
|
|
51
32
|
"dependencies": {
|
|
52
|
-
"
|
|
33
|
+
"boon-js": "2.0.3"
|
|
53
34
|
}
|
|
54
35
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const Logic = require('../lib/logic');
|
|
3
|
+
const Triangle = require('../lib/curve/triangle');
|
|
4
|
+
const Trapezoid = require('../lib/curve/trapezoid');
|
|
5
|
+
const assert = require('assert');
|
|
6
|
+
const getEvaluator = require('boon-js').getEvaluator;
|
|
7
|
+
|
|
8
|
+
describe('js-bool test', () => {
|
|
9
|
+
var logicHeat = new Logic();
|
|
10
|
+
const optimalTemperature = new Triangle(10, 20, 30);
|
|
11
|
+
const toColdTemperature = new Triangle(0, 10, 15);
|
|
12
|
+
const toHotTemperature = new Triangle(25, 40, 60);
|
|
13
|
+
|
|
14
|
+
logicHeat.init('cold', toColdTemperature)
|
|
15
|
+
logicHeat.or('optimal', optimalTemperature)
|
|
16
|
+
logicHeat.or('hot', toHotTemperature)
|
|
17
|
+
|
|
18
|
+
var logicDistance = new Logic();
|
|
19
|
+
const close = new Triangle(0, 10, 20);
|
|
20
|
+
const far = new Triangle(5, 50, 100);
|
|
21
|
+
|
|
22
|
+
logicDistance.init('close', close)
|
|
23
|
+
logicDistance.or('far', far)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
it('provides output for js-bool', () => {
|
|
27
|
+
const res = logicHeat.defuzzify(20, 'heat');
|
|
28
|
+
assert.ok(res.boonJsInputs);
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('jsbool output cold is false', () => {
|
|
32
|
+
const res = logicHeat.defuzzify(20);
|
|
33
|
+
assert.equal(res.boonJsInputs.cold, false);
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('jsbool output hot is false', () => {
|
|
37
|
+
const res = logicHeat.defuzzify(20);
|
|
38
|
+
assert.equal(res.boonJsInputs.cold, false);
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('jsbool output optimal is false', () => {
|
|
42
|
+
const res = logicHeat.defuzzify(20);
|
|
43
|
+
assert.equal(res.boonJsInputs.optimal, true);
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('jsbool output cold as heat is false', () => {
|
|
47
|
+
const res = logicHeat.defuzzify(20, 'heat');
|
|
48
|
+
assert.equal(res.boonJsInputs['heat.cold'], false);
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('jsbool output hot as heat is false', () => {
|
|
52
|
+
const res = logicHeat.defuzzify(20, 'heat');
|
|
53
|
+
assert.equal(res.boonJsInputs['heat.hot'], false);
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('jsbool output optimal as heat is true', () => {
|
|
57
|
+
const res = logicHeat.defuzzify(20, 'heat');
|
|
58
|
+
assert.equal(res.boonJsInputs['heat.optimal'], true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
describe('can combine 2 fuzzy decisions via boonJs', ()=>{
|
|
63
|
+
it.only('monster bites when heat is cold and distance is close', ()=>{
|
|
64
|
+
|
|
65
|
+
const resHeat = logicHeat.defuzzify(2, 'heat');
|
|
66
|
+
const resClose = logicDistance.defuzzify(2, 'distance');
|
|
67
|
+
|
|
68
|
+
const jsBoonInput = { ...resHeat.boonJsInputs, ...resClose.boonJsInputs }
|
|
69
|
+
console.log(jsBoonInput);
|
|
70
|
+
|
|
71
|
+
const monsterBiteTest = getEvaluator(
|
|
72
|
+
'heat.cold AND distance.close',
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const monsterSleepTest = getEvaluator(
|
|
76
|
+
'heat.hot AND distance.far',
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const resBite = monsterBiteTest(jsBoonInput)
|
|
80
|
+
assert.equal(resBite, true);
|
|
81
|
+
|
|
82
|
+
const resSleep = monsterSleepTest(jsBoonInput)
|
|
83
|
+
assert.equal(resSleep, false);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
package/test/logic-test.js
CHANGED
|
@@ -43,9 +43,9 @@ describe('logic', () => {
|
|
|
43
43
|
var logic = new Logic();
|
|
44
44
|
var res = logic
|
|
45
45
|
.init('rage', rageRange)
|
|
46
|
-
.not('
|
|
46
|
+
.not('norage', rageRange)
|
|
47
47
|
.defuzzify(20);
|
|
48
|
-
assert.equal(res.toString(), '
|
|
48
|
+
assert.equal(res.toString(), 'norage');
|
|
49
49
|
});
|
|
50
50
|
});
|
|
51
51
|
|