-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFIS.py
58 lines (47 loc) · 2.76 KB
/
FIS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
from Rule import Rule
from InferenceMethod import *
from DefuzzificationStrategy import *
class FIS:
def __init__(self, inputVariables, outputVariables, domainOutputVariables, linguisticVariables, linguisticVariablesMembershipFunction, inferenceMethod = 'Mamdani', defuzzificationStrategy = 'DiscreteCOA'):
self.inputVariables = inputVariables
self.outputVariables = outputVariables
self.membershipFunctions = dict()
for variable, function in zip(linguisticVariables, linguisticVariablesMembershipFunction):
self.membershipFunctions[variable] = function
self.domainOutputVariables = dict()
for variable, domain in zip(outputVariables, domainOutputVariables):
self.domainOutputVariables[variable] = domain
self.inferenceMethod = inferenceMethod
self.defuzzificationStrategy = defuzzificationStrategy
self.rules = []
def AddRule(self, inputVariables, linguisticInputVariables, outputVariables, linguisticOutputVariables):
membershipFunctionsDict = dict()
for inputVariable, linguisticVariable in zip(inputVariables, linguisticInputVariables):
membershipFunctionsDict[inputVariable] = self.membershipFunctions[linguisticVariable]
for outputVariable, linguisticVariable in zip(outputVariables, linguisticOutputVariables):
membershipFunctionsDict[outputVariable] = self.membershipFunctions[linguisticVariable]
self.rules.append(Rule(inputVariables, outputVariable, membershipFunctionsDict))
def Run(self, inputVariablesValues):
outputFunctions = None
outputValues = dict()
if self.inferenceMethod == 'Mamdani':
outputFunctions = Mamdani(self.rules, inputVariablesValues)
elif self.inferenceMethod == 'Larsen':
outputFunctions = Larsen(self.rules, inputVariablesValues)
else:
pass
if self.inferenceMethod == 'Mamdani' or self.inferenceMethod == 'Larsen':
if self.defuzzificationStrategy == 'DiscreteCOA':
for variable in self.outputVariables:
outputValues[variable] = DiscreteCOA(outputFunctions[variable], self.domainOutputVariables[variable])
elif self.defuzzificationStrategy == 'BOA':
for variable in self.outputVariables:
outputValues[variable] = BOA(outputFunctions[variable], self.domainOutputVariables[variable])
elif self.defuzzificationStrategy == 'DiscreteMOM':
for variable in self.outputVariables:
outputValues[variable] = DiscreteMOM(outputFunctions[variable], self.domainOutputVariables[variable])
else:
pass
else:
pass
return outputValues