Skip to content

Commit

Permalink
Add defuzzyfication method 'COG' (centroid/center of gravity). refs #4
Browse files Browse the repository at this point in the history
  • Loading branch information
arruda committed Aug 31, 2017
1 parent ca1bfa1 commit 66f91b4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
32 changes: 25 additions & 7 deletions scikit_fuzzy_fcl/fcl_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,39 @@ def enterFuzzify_block(self, ctx):

def enterDefuzzify_block(self, ctx):
label = ctx.ID().getText()
override_universe_range = None
for item in ctx.defuzzify_item():
if item.defuzzify_range():
override_universe_range = [float(r.getText()) for r in item.defuzzify_range().REAL()]

# if variable already defined a range then use this to instantiate the consequent
universe_range = self.vars.get(label, {}).get('range', None)

# but if there's a RANGE defined in the defuzzify block, then it should be used instead
for item in ctx.defuzzify_item():
defuzzify_range = item.defuzzify_range()
if defuzzify_range:
universe_range = [float(r.getText()) for r in defuzzify_range.REAL()]
break
if override_universe_range:
universe_range = override_universe_range

new_consequent = Consequent(universe_range, label=label)

self.consequents[label] = {
'value': Consequent(universe_range, label=label),
'value': new_consequent,
'terms': OrderedDict(),
}

def map_fcl_defuzz_method_to_skf(self, method):
defuzz_map = {
'COG': 'centroid'
}
return defuzz_map[method]

def enterDefuzzification_method(self, ctx):
consequent_label = ctx.parentCtx.parentCtx.ID().getText()
consequent = self.consequents[consequent_label].get('value')

fcl_method = ctx.getChild(2).getText()
defuzzify_method = self.map_fcl_defuzz_method_to_skf(fcl_method)

consequent.defuzzify_method = defuzzify_method

def getLinguistic_term_var_dict(self, linguistic_term_ctx):
var_dict = None
if hasattr(linguistic_term_ctx.parentCtx, 'ID'):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,24 @@ def test_consequents_term_defined_if_present(self):
self.assertEqual('consequent1', consequents.get('consequent1').get('value').label)
self.assertIn('mf1', consequents.get('consequent1').get('value').terms)
self.assertIn('mf1', consequents.get('consequent1').get('value').terms.get('mf1').label)

def test_consequents_defuzzify_method_cog_as_centroid(self):
fcl_text = """
FUNCTION_BLOCK my_system
DEFUZZIFY consequent1
METHOD : COG;
END_DEFUZZIFY
END_FUNCTION_BLOCK
"""
lexer = FclLexer(InputStream(fcl_text))
stream = CommonTokenStream(lexer)
parser = FclParser(stream)
tree = parser.main()

listener = ScikitFuzzyFclListener()
walker = ParseTreeWalker()
walker.walk(listener, tree)
consequents = listener.consequents
self.assertIn('consequent1', consequents)
self.assertEqual('consequent1', consequents.get('consequent1').get('value').label)
self.assertEqual('centroid', consequents.get('consequent1').get('value').defuzzify_method)

0 comments on commit 66f91b4

Please sign in to comment.