Module: FuzzyLogic::Generate

Defined in:
lib/fuzzy-logic/generate.rb

Class Method Summary collapse

Class Method Details

.and(seta, setb, soft = false) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fuzzy-logic/generate.rb', line 88

def self.and(seta, setb, soft=false)
  raise ArgumentError, "Arguments should be fuzzy-sets" unless seta.is_a? Set and setb.is_a? Set

  if soft then
    return Set.new { |n|
      seta.get(n) * setb.get(n)
    }
  end

  return Set.new { |n|
    [seta.get(n), setb.get(n)].min
  }
end

.gaussObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/fuzzy-logic/generate.rb', line 7

def self.gauss
  #
  # Gauss would be calculated with this expression:
  #
  # Math.exp(
  #   -( (n - center)/diffusion )**2
  # )
  #
  # Problem: On an overange feedback are big numbers instead of zero
end

.list(*args) ⇒ Object



64
65
66
# File 'lib/fuzzy-logic/generate.rb', line 64

def self.list(*args)
  args_test_array_filled_with_arrays_length(args, "Arguments of a list fuzzy-set should be Arrays of length 2", 2)
end

.not(seta) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
# File 'lib/fuzzy-logic/generate.rb', line 102

def self.not(seta)
  raise ArgumentError, "Argument should be a fuzzy-set" unless seta.is_a? Set

  return Set.new { |n|
    1 - seta.get(n)
  }
end

.or(seta, setb, soft = false) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fuzzy-logic/generate.rb', line 68

def self.or(seta, setb, soft=false)
  raise ArgumentError, "Arguments should be fuzzy-sets" unless seta.is_a? Set and setb.is_a? Set
  
  h1 = seta.height || 0
  h2 = setb.height || 0
  
  hmax = [h1,h2].max
  hmax = hmax > 0 ? hmax : nil

  if soft then
    return Set.new(hmax) { |n|
      seta.get(n) + setb.get(n) - seta.get(n) * setb.get(n)
    }
  end

  return Set.new(hmax) { |n|
    [seta.get(n), setb.get(n)].max
  }
end

.singleton(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fuzzy-logic/generate.rb', line 46

def self.singleton(*args)
  args_test_array_filled_with_arrays_length(args, "Arguments of a singleton fuzzy-set should be Arrays of length 2", 2)

  args = args.sort { |a,b| a.first <=> b.first }
  max = args.collect { |a| a.last } .sort.last

  return Set.new(max) { |n|
    o = 0.0
    args.each { |a|
      if n == a.first then
        o = a.last.to_f
        break
      end
    }
    o
  }
end

.trapezoid(supmin, cormin, cormax, supmax) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fuzzy-logic/generate.rb', line 31

def self.trapezoid(supmin, cormin, cormax, supmax)
  supmin, cormin, cormax, supmax = *([supmin, cormin, cormax, supmax].sort.map(&:to_f))
  return Set.new(1) { |n|
    o = 0.0
    if n.between?(cormin,cormax) then
      o = 1.0
    elsif n.between?(supmin,cormin) then
      o = 1 - (cormin - n)/(cormin - supmin)
    elsif n.between?(cormax,supmax) then
      o = 1 - (n - cormax)/(supmax - cormax)
    end
    o
  }
end

.triangle(center, range) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fuzzy-logic/generate.rb', line 18

def self.triangle(center, range)
  range = range.abs
  return Set.new(1) { |n|
    o = 0.0
    if n == center then
      o = 1.0
    elsif n.between?((center-range/2.0), (center+range/2.0)) then
      o = 1.0 - [1.0, (n - center).abs.to_f/(range/2.0)].min
    end
   o
  }
end