Class: FuzzyInfer::FuzzyInferenceMachine
- Inherits:
-
Object
- Object
- FuzzyInfer::FuzzyInferenceMachine
- Defined in:
- lib/fuzzy_infer/fuzzy_inference_machine.rb
Constant Summary collapse
- MYSQL_ADAPTER_NAME =
/mysql/i
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#kernel ⇒ Object
readonly
Returns the value of attribute kernel.
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
Instance Method Summary collapse
-
#arel_table ⇒ Object
TODO technically I could use this to generate the SQL.
- #basis ⇒ Object
- #infer ⇒ Object
-
#initialize(kernel, targets, config) ⇒ FuzzyInferenceMachine
constructor
A new instance of FuzzyInferenceMachine.
- #membership ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 7 def config @config end |
#kernel ⇒ Object (readonly)
Returns the value of attribute kernel.
5 6 7 |
# File 'lib/fuzzy_infer/fuzzy_inference_machine.rb', line 5 def kernel @kernel end |
#targets ⇒ Object (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_table ⇒ Object
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 |
#basis ⇒ Object
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 |
#infer ⇒ Object
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 |
#membership ⇒ Object
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 |