Advertisement
Guest User

Untitled

a guest
May 13th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {MembershipFunction, triangle} from './membershipFunctions'
  2. import {cloneDeep} from 'lodash'
  3.  
  4. export type LinguisticVariablesDefinition = {
  5.   [variableName: string]: {
  6.     [variableValue: string]: MembershipFunction
  7.   }
  8. }
  9.  
  10. export interface FuzzySystemDefinition<
  11.   TInput extends LinguisticVariablesDefinition,
  12.   TOutput extends LinguisticVariablesDefinition
  13. > {
  14.   inputs: TInput
  15.   outputs: TOutput
  16. }
  17.  
  18. export type FuzzyOperator = 'is' | 'is not'
  19.  
  20. /**
  21.  * A condition represents a single fuzzy variable value membership.
  22.  * For example, for basic "service is poor" expression it'll be
  23.  * {variable: 'service', operator: 'is', value: 'poor'}
  24.  */
  25. export interface Condition<T extends LinguisticVariablesDefinition> {
  26.   /** Fuzzy variable name */
  27.   variable: keyof T
  28.   /**
  29.    * Fuzzy variable value membership operator - 'is' or 'is not';
  30.    * defaults to 'is' where applicable
  31.    */
  32.   operator?: FuzzyOperator
  33.   /** Fuzzy variable value to compare against */
  34.   value: keyof T[keyof T]
  35. }
  36.  
  37. export type FuzzyRuleConnection = 'and' | 'or'
  38.  
  39. /**
  40.  * A condition group is a tree-like structure that represents several
  41.  * conditions or condition groups united with a logical operator 'and' or 'or'.
  42.  */
  43. export interface ConditionGroup<T extends LinguisticVariablesDefinition> {
  44.   operator: FuzzyRuleConnection
  45.   conditions: (Condition<T> | ConditionGroup<T>)[]
  46. }
  47.  
  48. export interface FuzzyRule<
  49.   TInput extends LinguisticVariablesDefinition,
  50.   TOutput extends LinguisticVariablesDefinition
  51. > {
  52.   if: ConditionGroup<TInput>
  53.   then: ConditionGroup<TOutput>
  54.   weight?: number
  55. }
  56.  
  57. export type TupleRule<
  58.   TVar extends keyof TVariable,
  59.   TVariable extends LinguisticVariablesDefinition
  60. > = [TVar, FuzzyOperator, keyof TVariable[TVar]]
  61.  
  62. interface VariableHelper<TVariable extends LinguisticVariablesDefinition> {
  63.   <TVar extends keyof TVariable>(
  64.     input: TupleRule<TVar, TVariable>
  65.   ): ConditionGroup<TVariable>
  66. }
  67.  
  68. export function createVariableHelper<
  69.   TVariable extends LinguisticVariablesDefinition
  70. >(): VariableHelper<TVariable> {
  71.   const helper: VariableHelper<TVariable> = ([name, operator, value]) => ({
  72.     operator: 'and',
  73.     conditions: [{variable: name, operator, value: value as string}]
  74.   })
  75.  
  76.   return helper
  77. }
  78.  
  79. export class FuzzyInferenceSystem<
  80.   TInput extends LinguisticVariablesDefinition,
  81.   TOutput extends LinguisticVariablesDefinition
  82. > {
  83.   private rules = [] as FuzzyRule<TInput, TOutput>[]
  84.  
  85.   constructor(private definition: FuzzySystemDefinition<TInput, TOutput>) {}
  86.  
  87.   public input = createVariableHelper<TInput>()
  88.   public output = createVariableHelper<TOutput>()
  89.  
  90.   addRule(rule: FuzzyRule<TInput, TOutput>) {
  91.     this.rules.push(rule)
  92.   }
  93. }
  94.  
  95. const system = new FuzzyInferenceSystem({
  96.   inputs: {
  97.     service: {
  98.       poor: triangle(0, 1, 2),
  99.       good: triangle(1, 2, 3),
  100.       excellent: triangle(2, 3, 4)
  101.     },
  102.     food: {
  103.       rancid: triangle(0, 1, 2),
  104.       delicious: triangle(5, 6, 7)
  105.     }
  106.   },
  107.   outputs: {
  108.     tip: {
  109.       cheap: triangle(0, 1, 2),
  110.       average: triangle(1, 2, 3),
  111.       generous: triangle(2, 3, 4)
  112.     }
  113.   }
  114. })
  115.  
  116. system.addRule({
  117.   if: system.input(['food', 'is', 'rancid']),
  118.   then: system.output(['tip', 'is', 'generous'])
  119. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement