Class: FuzzyAssociativeMemory::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_associative_memory/rule.rb

Overview

Copyright 2013, Prylis Incorporated.

This file is part of The Ruby Fuzzy Associative Memory github.com/cpowell/fuzzy-associative-memory You can redistribute and/or modify this software only in accordance with the terms found in the “LICENSE” file included with the library.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(antecedent_array, boolean, consequent, natural_language = nil) ⇒ Rule

Marries an input fuzzy set and an output fuzzy set in an if-then arrangement, i.e. if (antecedent) then (consequent).

  • Args :

    • antecedent_array -> an array of one or more input fuzzy sets

    • boolean -> term to join the antecedents, may be: nil, :and, :or

    • consequent -> the output fuzzy set

    • natural_language -> a rule description (your own words), useful in output

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fuzzy_associative_memory/rule.rb', line 22

def initialize(antecedent_array, boolean, consequent, natural_language=nil)
  if antecedent_array.is_a? String
    raise ArgumentError, "As of v1.0.1, Rule::initialize() has changed. Please see the code and CHANGELOG. (Sorry for the trouble.)"
  end

  raise ArgumentError, "Antecedent array must contain at least one fuzzy set" unless antecedent_array.size > 0
  raise ArgumentError, "Consequent must be provided" unless consequent

  if antecedent_array.size > 1
    raise ArgumentError, "boolean must be sym :and or :or for multi-element antecedent arrays" unless [:and, :or].include? boolean
  else
    raise ArgumentError, "boolean must be nil for single-element antecedent arrays" unless boolean.nil?
  end

  @natural_language = natural_language
  @antecedents      = antecedent_array
  @consequent       = consequent
  @boolean          = boolean
end

Instance Attribute Details

#antecedentsObject (readonly)

Returns the value of attribute antecedents.



11
12
13
# File 'lib/fuzzy_associative_memory/rule.rb', line 11

def antecedents
  @antecedents
end

#booleanObject (readonly)

Returns the value of attribute boolean.



11
12
13
# File 'lib/fuzzy_associative_memory/rule.rb', line 11

def boolean
  @boolean
end

#consequentObject (readonly)

Returns the value of attribute consequent.



11
12
13
# File 'lib/fuzzy_associative_memory/rule.rb', line 11

def consequent
  @consequent
end

Instance Method Details

#fire(value_array) ⇒ Object

Triggers the rule. The antecedent(s) is/are fired with the supplied inputs and the µ (degree of fit) is calculated and returned.

  • Args :

    • value_array -> an array of input values for the rule (degrees, distance, strength, whatever)

  • Returns :

    • the degree of fit for this rule

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fuzzy_associative_memory/rule.rb', line 50

def fire(value_array)
  raise ArgumentError, "value array passed to Rule::fire() cannot be nil" if value_array.nil?
  raise ArgumentError, "value array must be an collection of inputs but is a #{value_array.class}" unless value_array.is_a? Array
  raise ArgumentError, "value array passed to Rule::fire() cannot be empty" if value_array.empty?

  ant_len = @antecedents.length
  raise ArgumentError, "value array size must equal antecedent array size" if value_array.length != ant_len

  for i in 0..ant_len-1
    v = @antecedents[i].mu(value_array[i])
    @max = v if @max.nil? || v > @max
    @min = v if @min.nil? || v < @min
  end

  if @boolean==:and
    return @min # AND / Intersection == minimum
  else
    return @max # OR / Union == maximum
  end

  # puts "Fired rule '#{@natural_language}': µ choices are [#{@mus.join(',')}], final µ is #{mu}" if $verbosity
  # [@consequent, mu]
end