Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RuleBase class to allow easier use of the library #75

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 [these people](https://github.com/sebs/es6-fuzz/graphs/contributors)
Copyright (c) 2015 [these people](https://github.com/skanderturki/js-fuzzy-logic/graphs/contributors)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the link in my repo represents very well who contributed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'm not used to this, thanks

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem ;)


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 3 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# es6-fuzz
# js-fuzzy-logic ( forked from: es6-fuzz )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one needs to be the old value to be merged.


> Fuzzy Logic in Javascript

Expand All @@ -21,12 +21,6 @@ Supported fuzzyfiers
* [api docs](http://sebs.github.io/es6-fuzz)
* [changelog](https://github.com/sebs/es6-fuzz/blob/master/docs/CHANGELOG.md)

## Install and Usage
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this i like to get back ;)


es6-fuzz is available as a NPM package.

```
npm install es6-fuzz
```
## Example

Expand Down Expand Up @@ -57,6 +51,8 @@ var res = logic

* hot

## Example 3: Usage of the RuleBase class
(see example-1-RuleBase.js)

## Usage with boon-js

Expand Down
9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2023-01-19
==========
* 4.1.0
* Added batch evaluation api
* (churn): changelog
* feature: Add a class that allows client code to group evaluator rules in an array of 3-tuples,
each 3-tuple associates the evaluator to its output and evaluation result.


2022-06-14
==========

Expand Down
70 changes: 70 additions & 0 deletions example-1-RuleBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const Logic = require('./lib/logic')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this to examples folder,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok got it

const Trapezoid = require('./lib/curve/trapezoid');
const boon = require('boon-js');

const RuleBase = require('./lib/RuleBase');

const createLikelihoodLogic = () => {
var logicLikelihood = new Logic();
const lowLikelihood = new Trapezoid(0, 0, 2, 3);
const moderateLikelihood = new Trapezoid(2, 3, 7, 8);
const highLikelihood = new Trapezoid(7, 8, 10, 11);

logicLikelihood.init('low', lowLikelihood)
logicLikelihood.or('moderate', moderateLikelihood)
logicLikelihood.or('high', highLikelihood);

return logicLikelihood;
}

const createImpactLogic = () => {
var logicImpact = new Logic();
const lowImpact = new Trapezoid(0, 0, 2, 3);
const moderateImpact = new Trapezoid(2, 3, 7, 8);
const highImpact = new Trapezoid(7, 8, 10, 11);

logicImpact.init('low', lowImpact)
logicImpact.or('moderate', moderateImpact)
logicImpact.or('high', highImpact);

return logicImpact;
}

const assess = (likelihood, impact) => {
var logicLikelihood = createLikelihoodLogic();

var logicImpact = createImpactLogic();

const assessments = new RuleBase();

/////////////////////////////////////////////////////////////////////////////////////////////////
assessments.addRule(boon.getEvaluator('likelihood.low AND impact.low'), "very low");
assessments.addRule(boon.getEvaluator('likelihood.low AND impact.moderate'), "low");
assessments.addRule(boon.getEvaluator('likelihood.low AND impact.high'), "top low");
/////////////////////////////////////////////////////////////////////////////////////////////////
assessments.addRule(boon.getEvaluator('likelihood.moderate AND impact.low'), "low");
assessments.addRule(boon.getEvaluator('likelihood.moderate AND impact.moderate'), "top low");
assessments.addRule(boon.getEvaluator('likelihood.moderate AND impact.high'), "moderate");
/////////////////////////////////////////////////////////////////////////////////////////////////
assessments.addRule(boon.getEvaluator('likelihood.high AND impact.low'), "moderate");
assessments.addRule(boon.getEvaluator('likelihood.high AND impact.moderate'), "high");
assessments.addRule(boon.getEvaluator('likelihood.high AND impact.high'), "very high");
/////////////////////////////////////////////////////////////////////////////////////////////////

// Defuzzify //
const resLikelihood = logicLikelihood.defuzzify(likelihood, 'likelihood');
const resImpact = logicImpact.defuzzify(impact, 'impact');

const jsBoonInput = { ...resLikelihood.boonJsInputs, ...resImpact.boonJsInputs }

assessments.evaluateAllRules(jsBoonInput);

return assessments.getResult();
}

// Launch test cases

console.log("Assessment result for {likelihood: 1, impact: 2}: " + assess( 1, 2));
console.log("Assessment result for {likelihood: 1, impact: 8}: " + assess( 1, 8));
console.log("Assessment result for {likelihood: 8, impact: 2}: " + assess( 8, 2));
console.log("Assessment result for {likelihood: 9, impact: 10}: " + assess( 9, 10));
52 changes: 52 additions & 0 deletions lib/RuleBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

/**
* Helper class that allows client code to group evaluator rules in an array of 3-tuples,
* each 3-tuple associates the evaluator (built out of a rule) to its output and evaluation result.
* (For more information on to use it, please refer to example_1_RuleBase.js).
*/
class RuleBase {

/**
* just creates an empty array to hold the 3-tuples
* */
constructor() {
this.rules = [];
}

/**
* adds a 3-tuples of { evaluator, evaluation, result } and the evaluation is
* initialized to "false".
*/
addRule(evaluator, result) {
this.rules.push( { evaluator: evaluator, evaluation: false, result: result} );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will a max value for rules be useful here? just to make sure no one is creating something uncomputable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

}

/**
* launches batch evaluation of all rules against the given input.
* This method has to be called before getResult().
* @param {*} input: the input object that contains a value for each fuzzy variable of the model.
*/
evaluateAllRules(input){
for(let i = 0; i < this.rules.length; i++){
this.rules[i].evaluation = this.rules[i].evaluator(input);
}
}

/**
* gets the final result of the defuzzification process.
* It looks for the rule (should be only one if the model is correct) that
* was evaluated to true.
*/
getResult(){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming: getResult .. tehse days I try not to write get ... there are actually getters for that ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I'll modify the name, maybe to evaluation()

for(let i = 0; i < this.rules.length; i++){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a question of style: amaybe consider using .map, filter and a length check

Additionally this makes it possible to only return something from one line.

if( this.rules[i].evaluation == true )
return this.rules[i].result;
}
// If no rule evaluated to true then there is a problem with the model.
return(-1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parnthesis around a return value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right

}

}

module.exports = RuleBase;