Class: FuzzyInfer::FuzzyInferenceMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_infer/fuzzy_inference_machine.rb

Constant Summary collapse

MYSQL_ADAPTER_NAME =
/mysql/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kernel, targets, config) ⇒ FuzzyInferenceMachine

Returns a new instance of FuzzyInferenceMachine.



11
12
13
14
15
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 11

def initialize(kernel, targets, config)
  @kernel = kernel
  @targets = targets
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 7

def config
  @config
end

#kernelObject (readonly)

Returns the value of attribute kernel.



5
6
7
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 5

def kernel
  @kernel
end

#targetsObject (readonly)

Returns the value of attribute targets.



6
7
8
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 6

def targets
  @targets
end

Instance Method Details

#arel_tableObject

TODO technically I could use this to generate the SQL



34
35
36
37
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 34

def arel_table
  calculate_table!
  Arel::Table.new table_name
end

#basisObject



39
40
41
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 39

def basis
  @basis ||= kernel.attributes.symbolize_keys.slice(*config.basis).reject { |k, v| v.nil? }
end

#inferObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 17

def infer
  calculate_table!
  pieces = targets.map do |target|
    "SUM(#{qc(target, :v)})/SUM(#{qc(:fuzzy_membership)})"
  end
  values = connection.select_rows(%{SELECT #{pieces.join(', ')} FROM #{table_name}}).first.map do |value|
    value.nil? ? nil : value.to_f
  end
  execute %{DROP TABLE #{table_name}}
  if targets.length == 1
    return values.first
  else
    return *values
  end
end

#membershipObject



43
44
45
46
47
48
49
50
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 43

def membership
  return @membership if @membership
  sql = kernel.send(config.membership, basis).dup
  basis.keys.each do |k|
    sql.gsub! ":#{k}_n_w", qc(k, :n_w)
  end
  @membership = sql
end