Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {MembershipFunction, triangle} from './membershipFunctions'
- import {cloneDeep} from 'lodash'
- export type LinguisticVariablesDefinition = {
- [variableName: string]: {
- [variableValue: string]: MembershipFunction
- }
- }
- export interface FuzzySystemDefinition<
- TInput extends LinguisticVariablesDefinition,
- TOutput extends LinguisticVariablesDefinition
- > {
- inputs: TInput
- outputs: TOutput
- }
- export type FuzzyOperator = 'is' | 'is not'
- /**
- * A condition represents a single fuzzy variable value membership.
- * For example, for basic "service is poor" expression it'll be
- * {variable: 'service', operator: 'is', value: 'poor'}
- */
- export interface Condition<T extends LinguisticVariablesDefinition> {
- /** Fuzzy variable name */
- variable: keyof T
- /**
- * Fuzzy variable value membership operator - 'is' or 'is not';
- * defaults to 'is' where applicable
- */
- operator?: FuzzyOperator
- /** Fuzzy variable value to compare against */
- value: keyof T[keyof T]
- }
- export type FuzzyRuleConnection = 'and' | 'or'
- /**
- * A condition group is a tree-like structure that represents several
- * conditions or condition groups united with a logical operator 'and' or 'or'.
- */
- export interface ConditionGroup<T extends LinguisticVariablesDefinition> {
- operator: FuzzyRuleConnection
- conditions: (Condition<T> | ConditionGroup<T>)[]
- }
- export interface FuzzyRule<
- TInput extends LinguisticVariablesDefinition,
- TOutput extends LinguisticVariablesDefinition
- > {
- if: ConditionGroup<TInput>
- then: ConditionGroup<TOutput>
- weight?: number
- }
- export type TupleRule<
- TVar extends keyof TVariable,
- TVariable extends LinguisticVariablesDefinition
- > = [TVar, FuzzyOperator, keyof TVariable[TVar]]
- interface VariableHelper<TVariable extends LinguisticVariablesDefinition> {
- <TVar extends keyof TVariable>(
- input: TupleRule<TVar, TVariable>
- ): ConditionGroup<TVariable>
- }
- export function createVariableHelper<
- TVariable extends LinguisticVariablesDefinition
- >(): VariableHelper<TVariable> {
- const helper: VariableHelper<TVariable> = ([name, operator, value]) => ({
- operator: 'and',
- conditions: [{variable: name, operator, value: value as string}]
- })
- return helper
- }
- export class FuzzyInferenceSystem<
- TInput extends LinguisticVariablesDefinition,
- TOutput extends LinguisticVariablesDefinition
- > {
- private rules = [] as FuzzyRule<TInput, TOutput>[]
- constructor(private definition: FuzzySystemDefinition<TInput, TOutput>) {}
- public input = createVariableHelper<TInput>()
- public output = createVariableHelper<TOutput>()
- addRule(rule: FuzzyRule<TInput, TOutput>) {
- this.rules.push(rule)
- }
- }
- const system = new FuzzyInferenceSystem({
- inputs: {
- service: {
- poor: triangle(0, 1, 2),
- good: triangle(1, 2, 3),
- excellent: triangle(2, 3, 4)
- },
- food: {
- rancid: triangle(0, 1, 2),
- delicious: triangle(5, 6, 7)
- }
- },
- outputs: {
- tip: {
- cheap: triangle(0, 1, 2),
- average: triangle(1, 2, 3),
- generous: triangle(2, 3, 4)
- }
- }
- })
- system.addRule({
- if: system.input(['food', 'is', 'rancid']),
- then: system.output(['tip', 'is', 'generous'])
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement