fuzzy_associative_memory 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +16 -0
- data/bin/hvac_system_example.rb +5 -5
- data/bin/weapon_choice_example.rb +22 -22
- data/lib/fuzzy_associative_memory/rule.rb +5 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -45,6 +45,22 @@ To do (in descending importance, roughly):
|
|
45
45
|
* Hedges ('very' and 'fairly')
|
46
46
|
* Additional examples
|
47
47
|
|
48
|
+
## Gem installation
|
49
|
+
|
50
|
+
I recommend you clone the Git repository and browse the examples and source code to fully understand how FAMs work.
|
51
|
+
|
52
|
+
But if / when you want to use this in your own project, the easiest way to do so is via the Gem:
|
53
|
+
```
|
54
|
+
gem install fuzzy_associative_memory
|
55
|
+
```
|
56
|
+
|
57
|
+
Then in your code:
|
58
|
+
```
|
59
|
+
require 'fuzzy_associative_memory'
|
60
|
+
```
|
61
|
+
|
62
|
+
The gem is fully namespaced to prevent collisions. See the examples for usage details.
|
63
|
+
|
48
64
|
## Included examples
|
49
65
|
|
50
66
|
The `bin` directory contains the following examples:
|
data/bin/hvac_system_example.rb
CHANGED
@@ -49,11 +49,11 @@ fan_speed.sets = [stop, slow, medium, fast, blast]
|
|
49
49
|
# "If the temperature is cool, the fan motor speed should be slow."
|
50
50
|
system = FuzzyAssociativeMemory::Ruleset.new("HVAC control", :larsen)
|
51
51
|
|
52
|
-
rule_1 = FuzzyAssociativeMemory::Rule.new('If room is cold, the fan motor stops'
|
53
|
-
rule_2 = FuzzyAssociativeMemory::Rule.new('If room is cool, the fan motor is slow'
|
54
|
-
rule_3 = FuzzyAssociativeMemory::Rule.new('If room is just right, the fan motor is medium'
|
55
|
-
rule_4 = FuzzyAssociativeMemory::Rule.new('If room is warm, the fan motor speeds up'
|
56
|
-
rule_5 = FuzzyAssociativeMemory::Rule.new('If room is hot, the fan motor runs full-blast'
|
52
|
+
rule_1 = FuzzyAssociativeMemory::Rule.new([cold], nil, stop , 'If room is cold, the fan motor stops' )
|
53
|
+
rule_2 = FuzzyAssociativeMemory::Rule.new([cool], nil, slow , 'If room is cool, the fan motor is slow' )
|
54
|
+
rule_3 = FuzzyAssociativeMemory::Rule.new([just_right], nil, medium , 'If room is just right, the fan motor is medium' )
|
55
|
+
rule_4 = FuzzyAssociativeMemory::Rule.new([warm], nil, fast , 'If room is warm, the fan motor speeds up' )
|
56
|
+
rule_5 = FuzzyAssociativeMemory::Rule.new([hot], nil, blast , 'If room is hot, the fan motor runs full-blast' )
|
57
57
|
|
58
58
|
system.rules = [rule_1, rule_2, rule_3, rule_4, rule_5]
|
59
59
|
|
@@ -50,25 +50,25 @@ rocket_ammo_status.sets = [rkt_ammo_low, rkt_ammo_okay, rkt_ammo_loads]
|
|
50
50
|
# fan speed' variable is the assemblage of all our fuzzy output sets.
|
51
51
|
desirability = FuzzyAssociativeMemory::LinguisticVariable.new("weapon desirability")
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
undesirable = FuzzyAssociativeMemory::Trapezoid.new(0, 0, 20, 50)
|
54
|
+
desirable = FuzzyAssociativeMemory::Triangle.new(30, 50, 70)
|
55
|
+
very_desirable = FuzzyAssociativeMemory::Trapezoid.new(50, 80, 100, 100)
|
56
56
|
|
57
|
-
desirability.sets = [
|
57
|
+
desirability.sets = [undesirable, desirable, very_desirable]
|
58
58
|
|
59
59
|
# Natural-language marriage of the inputs to the outputs, e.g.
|
60
60
|
# "If the temperature is cool, the fan motor speed should be slow."
|
61
61
|
rkt_ruleset = FuzzyAssociativeMemory::Ruleset.new("Rocket launcher desirability", implication)
|
62
62
|
|
63
|
-
rule_1 = FuzzyAssociativeMemory::Rule.new('If target is far and I have loads of rocket ammo, rocket launcher is desirable'
|
64
|
-
rule_2 = FuzzyAssociativeMemory::Rule.new('If target is far and I have some rocket ammo, rocket launcher is undesirable'
|
65
|
-
rule_3 = FuzzyAssociativeMemory::Rule.new('If target is far and I have low rocket ammo, rocket launcher is undesirable'
|
66
|
-
rule_4 = FuzzyAssociativeMemory::Rule.new('If target is medium-distance and I have loads of rocket ammo, rocket launcher is very desirable'
|
67
|
-
rule_5 = FuzzyAssociativeMemory::Rule.new('If target is medium-distance and I have some rocket ammo, rocket launcher is very desirable'
|
68
|
-
rule_6 = FuzzyAssociativeMemory::Rule.new('If target is medium-distance and I have low rocket ammo, rocket launcher is desirable'
|
69
|
-
rule_7 = FuzzyAssociativeMemory::Rule.new('If target is close and I have loads of rocket ammo, rocket launcher is undesirable'
|
70
|
-
rule_8 = FuzzyAssociativeMemory::Rule.new('If target is close and I have some rocket ammo, rocket launcher is undesirable'
|
71
|
-
rule_9 = FuzzyAssociativeMemory::Rule.new('If target is close and I have low rocket ammo, rocket launcher is undesirable'
|
63
|
+
rule_1 = FuzzyAssociativeMemory::Rule.new([tgt_far, rkt_ammo_loads], :and, desirable, 'If target is far and I have loads of rocket ammo, rocket launcher is desirable')
|
64
|
+
rule_2 = FuzzyAssociativeMemory::Rule.new([tgt_far, rkt_ammo_okay], :and, undesirable, 'If target is far and I have some rocket ammo, rocket launcher is undesirable')
|
65
|
+
rule_3 = FuzzyAssociativeMemory::Rule.new([tgt_far, rkt_ammo_low], :and, undesirable, 'If target is far and I have low rocket ammo, rocket launcher is undesirable')
|
66
|
+
rule_4 = FuzzyAssociativeMemory::Rule.new([tgt_medium, rkt_ammo_loads], :and, very_desirable, 'If target is medium-distance and I have loads of rocket ammo, rocket launcher is very desirable')
|
67
|
+
rule_5 = FuzzyAssociativeMemory::Rule.new([tgt_medium, rkt_ammo_okay], :and, very_desirable, 'If target is medium-distance and I have some rocket ammo, rocket launcher is very desirable')
|
68
|
+
rule_6 = FuzzyAssociativeMemory::Rule.new([tgt_medium, rkt_ammo_low], :and, desirable, 'If target is medium-distance and I have low rocket ammo, rocket launcher is desirable')
|
69
|
+
rule_7 = FuzzyAssociativeMemory::Rule.new([tgt_close, rkt_ammo_loads], :and, undesirable, 'If target is close and I have loads of rocket ammo, rocket launcher is undesirable')
|
70
|
+
rule_8 = FuzzyAssociativeMemory::Rule.new([tgt_close, rkt_ammo_okay], :and, undesirable, 'If target is close and I have some rocket ammo, rocket launcher is undesirable')
|
71
|
+
rule_9 = FuzzyAssociativeMemory::Rule.new([tgt_close, rkt_ammo_low], :and, undesirable, 'If target is close and I have low rocket ammo, rocket launcher is undesirable')
|
72
72
|
|
73
73
|
rkt_ruleset.rules = [rule_1, rule_2, rule_3, rule_4, rule_5, rule_6, rule_7, rule_8, rule_9]
|
74
74
|
|
@@ -91,15 +91,15 @@ gun_ammo_loads = FuzzyAssociativeMemory::Trapezoid.new(10, 30, 40, 40)
|
|
91
91
|
shotgun_ammo_status.sets = [gun_ammo_low, gun_ammo_okay, gun_ammo_loads]
|
92
92
|
|
93
93
|
gun_ruleset = FuzzyAssociativeMemory::Ruleset.new("Shotgun desirability", implication)
|
94
|
-
rule_1 = FuzzyAssociativeMemory::Rule.new('If target is far and I have loads of shotgun ammo, shotgun is undesirable'
|
95
|
-
rule_2 = FuzzyAssociativeMemory::Rule.new('If target is far and I have some shotgun ammo, shotgun is undesirable'
|
96
|
-
rule_3 = FuzzyAssociativeMemory::Rule.new('If target is far and I have low shotgun ammo, shotgun is undesirable'
|
97
|
-
rule_4 = FuzzyAssociativeMemory::Rule.new('If target is medium-distance and I have loads of shotgun ammo, shotgun is desirable'
|
98
|
-
rule_5 = FuzzyAssociativeMemory::Rule.new('If target is medium-distance and I have some shotgun ammo, shotgun is desirable'
|
99
|
-
rule_6 = FuzzyAssociativeMemory::Rule.new('If target is medium-distance and I have low shotgun ammo, shotgun is undesirable'
|
100
|
-
rule_7 = FuzzyAssociativeMemory::Rule.new('If target is close and I have loads of shotgun ammo, shotgun is very desirable'
|
101
|
-
rule_8 = FuzzyAssociativeMemory::Rule.new('If target is close and I have some shotgun ammo, shotgun is very desirable'
|
102
|
-
rule_9 = FuzzyAssociativeMemory::Rule.new('If target is close and I have low shotgun ammo, shotgun is very desirable'
|
94
|
+
rule_1 = FuzzyAssociativeMemory::Rule.new([tgt_far, gun_ammo_loads], :and, undesirable, 'If target is far and I have loads of shotgun ammo, shotgun is undesirable')
|
95
|
+
rule_2 = FuzzyAssociativeMemory::Rule.new([tgt_far, gun_ammo_okay], :and, undesirable, 'If target is far and I have some shotgun ammo, shotgun is undesirable')
|
96
|
+
rule_3 = FuzzyAssociativeMemory::Rule.new([tgt_far, gun_ammo_low], :and, undesirable, 'If target is far and I have low shotgun ammo, shotgun is undesirable')
|
97
|
+
rule_4 = FuzzyAssociativeMemory::Rule.new([tgt_medium, gun_ammo_loads], :and, desirable, 'If target is medium-distance and I have loads of shotgun ammo, shotgun is desirable')
|
98
|
+
rule_5 = FuzzyAssociativeMemory::Rule.new([tgt_medium, gun_ammo_okay], :and, desirable, 'If target is medium-distance and I have some shotgun ammo, shotgun is desirable')
|
99
|
+
rule_6 = FuzzyAssociativeMemory::Rule.new([tgt_medium, gun_ammo_low], :and, undesirable, 'If target is medium-distance and I have low shotgun ammo, shotgun is undesirable')
|
100
|
+
rule_7 = FuzzyAssociativeMemory::Rule.new([tgt_close, gun_ammo_loads], :and, very_desirable, 'If target is close and I have loads of shotgun ammo, shotgun is very desirable')
|
101
|
+
rule_8 = FuzzyAssociativeMemory::Rule.new([tgt_close, gun_ammo_okay], :and, very_desirable, 'If target is close and I have some shotgun ammo, shotgun is very desirable')
|
102
|
+
rule_9 = FuzzyAssociativeMemory::Rule.new([tgt_close, gun_ammo_low], :and, very_desirable, 'If target is close and I have low shotgun ammo, shotgun is very desirable')
|
103
103
|
gun_ruleset.rules = [rule_1, rule_2, rule_3, rule_4, rule_5, rule_6, rule_7, rule_8, rule_9]
|
104
104
|
|
105
105
|
sa = 12
|
@@ -19,7 +19,11 @@ class FuzzyAssociativeMemory::Rule
|
|
19
19
|
# - +consequent+ -> the output fuzzy set
|
20
20
|
# - +natural_language+ -> a rule description (your own words), useful in output
|
21
21
|
#
|
22
|
-
def initialize(
|
22
|
+
def initialize(antecedent_array, boolean, consequent, natural_language=nil)
|
23
|
+
if antecedent_array.is_a? String
|
24
|
+
raise ArgumentError, "As of v1.0.1, Rule::initialize() has changed. Please see the code and CHANGELOG. (Sorry for the trouble.)"
|
25
|
+
end
|
26
|
+
|
23
27
|
raise ArgumentError, "Antecedent array must contain at least one fuzzy set" unless antecedent_array.size > 0
|
24
28
|
raise ArgumentError, "Consequent must be provided" unless consequent
|
25
29
|
|