-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemfuncs.py
More file actions
106 lines (67 loc) · 2.56 KB
/
memfuncs.py
File metadata and controls
106 lines (67 loc) · 2.56 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import numpy as np
import matplotlib.pyplot as plt
from file import File
fsize = 10
class MemFunc():
"""Creates the specific membership function and is used to continue the process"""
def __init__(self, name,specs=[], fMemFunc = False, fNumDiff = .2):
#Get the specs of how to define the membership functions
self.specs = specs
self.memFuncName = name
if fMemFunc:
self.fMemFunc = getattr(self,fMemFunc)
self.fNumDiff = fNumDiff
self.memFunc = getattr(self,name)
#The to_string function to show the name of the membership function
# when trying to print
def __repr__(self):
return self.memberFuncName
def fMem(self,input):
#Get the fuzzy value of the input
b = self.fMemFunc(input)
c = min(1,b + self.fNumDiff)
a = max(0,b - self.fNumDiff)
#print("input: ",input, "specs: ",self.specs,"out:",b)
#print("Fmem:",[a,b,c])
#Convert it to a fuzzy number
print("MEM:",[a,b,c])
File("fmemFile.csv").writeA([self.memFuncName,a,b,c],"a")
return [a, b, b, c]
def gauss(self,input):
#Find the mean of the specs passed in
# easy way to convert the same definition of a trap func to a gauss
mu = np.mean(self.specs)
sigma = np.std(self.specs)
return np.exp(-((input - mu) ** 2.) / float(sigma) ** 2.)
def trap(self,input):
if(len(self.specs) < 4):
raise Exception("Not enough specs passed in for the Trapezoid membership function")
a = self.specs[0]
b = self.specs[1]
c = self.specs[2]
d = self.specs[3]
if input <= b:
return self.tri(input, vals=[a, b, b])
elif input >= c:
return self.tri(input,vals=[c, c, d])
else:
return 0
def tri(self, input, vals = False):
if(len(self.specs) < 3):
raise Exception("Not enough specs passed in for the Triangle membership function")
if vals:
a = vals[0]
b = vals[1]
c = vals[2]
else:
a = self.specs[0]
b = self.specs[1]
c = self.specs[2]
if input == b:
return 1
elif a != b and input > a and input < b:
return (input - a) / float(b - a)
elif b != c and input > b and input < c:
return (c - input) / float(c - b)
else:
return 0