Class: FuzzyAssociativeMemory::LinguisticVariable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ LinguisticVariable



15
16
17
18
# File 'lib/fuzzy_associative_memory/linguistic_variable.rb', line 15

def initialize(name)
  @name       = name
  @sets = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/fuzzy_associative_memory/linguistic_variable.rb', line 13

def name
  @name
end

#setsObject

Returns the value of attribute sets.



12
13
14
# File 'lib/fuzzy_associative_memory/linguistic_variable.rb', line 12

def sets
  @sets
end

Instance Method Details

#[](n) ⇒ Object



113
114
115
# File 'lib/fuzzy_associative_memory/linguistic_variable.rb', line 113

def [](n)
  @sets[n]
end

#add_set(set) ⇒ Object



20
21
22
# File 'lib/fuzzy_associative_memory/linguistic_variable.rb', line 20

def add_set(set)
  @sets << set
end

#gnuplot(options = {}) ⇒ Object

Shell out to your system’s installed Gnuplot binary to create a graphical depiction of this FLV.

  • Args :

    • options -> a hash of options; see below



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fuzzy_associative_memory/linguistic_variable.rb', line 30

def gnuplot(options = {})
  return if @sets.empty?

  opts = {
    :logarithmic_x => false # Default to non-log X axis
  }.merge(options)

  datafile = Tempfile.new('fam_')
  begin
    @sets.each_with_index do |s, i|
      if s.is_a? FuzzyAssociativeMemory::Triangle
        datafile.puts "Set#{i} #{s.left} 0"
        datafile.puts "Set#{i} #{s.center} 1"
        datafile.puts "Set#{i} #{s.right} 0"
      end

      if s.is_a? FuzzyAssociativeMemory::Trapezoid
        datafile.puts "Set#{i} #{s.left} 0"
        datafile.puts "Set#{i} #{s.top_left} 1"
        datafile.puts "Set#{i} #{s.top_right} 1"
        datafile.puts "Set#{i} #{s.right} 0"
      end

      datafile.puts "\n\n"
    end
    datafile.close

    min = @sets[0].left
    max = @sets[0].right
    @sets.each do |s|
      min = s.left if s.left < min
      max = s.right if s.right > max
    end

    fn = "#{Dir.tmpdir}/plot of #{name}.svg"

    # set term dumb
    commands = %Q(
      set terminal svg size 1024,400
      set xlabel 'value'
      set ylabel 'membership'
      set xtics autofreq
      set ytics autofreq
      set output "#{fn}"
      set title "Fuzzy sets for #{name}"
      set mytics 4
      set mxtics 4
      set size ratio 0.25
      set grid mxtics xtics ytics

    )

    if opts[:logarithmic_x]
      commands += "set xr [[#{[min, 1].max}:#{max}]\nset logscale x\n"
    else
      commands += "set xr [#{min}:#{max}]\n"
    end

    #         plot "#{datafile.path}" using 2:3 notitle with linespoints lw 2

    plotarr=[]
    @sets.size.times do |i|
      plotarr[i] = %Q("#{datafile.path}" index #{i} using 2:3 notitle with linespoints pt 5 lw 2)
    end

    commands += "  plot " + plotarr.join(', ') + "\n"
    # puts commands

    IO.popen("gnuplot", "w") do |io|
      io.puts commands
    end
    #puts "Plot saved in #{fn}"

  rescue StandardError => e
    puts "Unable to run Gnuplot because #{e.message}"
  ensure
    datafile.close
    datafile.unlink
  end

  return fn if defined?(fn)
end