-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# es6-fuzz | ||
# js-fuzzy-logic ( forked from: es6-fuzz ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -57,6 +51,8 @@ var res = logic | |
|
||
* hot | ||
|
||
## Example 3: Usage of the RuleBase class | ||
(see example-1-RuleBase.js) | ||
|
||
## Usage with boon-js | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const Logic = require('./lib/logic') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move this to examples folder, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); |
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} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parnthesis around a return value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right |
||
} | ||
|
||
} | ||
|
||
module.exports = RuleBase; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no problem ;)