Skip to content

Instantly share code, notes, and snippets.

@uhfx
Last active May 30, 2021 22:45
Show Gist options
  • Save uhfx/fa85f6a62adcaef09cbc103c7115e25f to your computer and use it in GitHub Desktop.
Save uhfx/fa85f6a62adcaef09cbc103c7115e25f to your computer and use it in GitHub Desktop.
fuzzy
class Obs: # 観測値 observed value
def __init__(self, e, d_e, d_u):
self.e = e # e
self.d_e = d_e # delta_e
self.d_u = d_u # delta_u
def rule(bottom, rang):
degree = [0] * (2 * rang + 1)
bottom = bottom + rang - 2
degree[bottom] = 0.3
degree[bottom + 1] = 0.7
degree[bottom + 2] = 1
degree[bottom + 3] = 0.7
degree[bottom + 4] = 0.3
return degree
def result1(Obs, switch, rang, r):
e = Obs.e + rang
d_e = Obs.d_e + rang
result1 = []
if(switch == 'min'):
for x in r:
a = rule(x[0], rang)[e]
b = rule(x[1], rang)[d_e]
y = min(a, b)
result1.append(y)
# print(result1)
return result1 # 一致度
if(switch == 'times'):
for x in r:
a = rule(x[0], rang)[e]
b = rule(x[1], rang)[d_e]
y = a * b
result1.append(y)
# print(result1)
return result1 # 一致度
else:
print("Error! result1 switch = 'min' or 'times'.")
exit(1)
def result2(result1, switch, rang, r):
result2 = []
u = range(len(result1))
if(switch == 'min'):
for s in u:
group = rule(r[s][2], rang)
# print(group)
v = range(len(group))
for y in v:
if group[y] > result1[s]:
group[y] = result1[s]
result2.append(group)
return result2
if(switch == 'times'):
for x in u:
group = rule(r[x][2], rang)
v = range(len(group))
for y in v:
group[y] = group[y] * result1[x]
result2.append(group)
return result2
else:
print("Error! result2 switch = 'min' or 'times'.")
exit(1)
def result3(result2, switch, rang):
result3 = [0] * (2 * rang + 1)
u = range(2 * rang + 1)
if(switch == 'max'):
for y in u:
result3[y] = max([x[y] for x in result2])
return result3
if(switch == 'sum'):
for y in u:
result3[y] = sum([x[y] for x in result2])
return result3
else:
print("Error! switch = 'max' or 'sum'.")
exit(1)
def balance_point(result3, rang):
a, b = 0, 0
c = range(len(result3))
for i in c:
x = i - rang
a = a + x * result3[i]
b = b + result3[i]
# print(a, b)
bp = a / b
return bp
def min_times_max(Obs, r, rang):
res1 = result1(Obs, 'min', rang, r)
res2 = result2(res1, 'times', rang, r)
res3 = result3(res2, 'max', rang)
bp = balance_point(res3, rang)
return bp
def min_min_max(Obs, r, rang):
res1 = result1(Obs, 'min', rang, r)
# print(res1)
res2 = result2(res1, 'min', rang, r)
# print(res2)
res3 = result3(res2, 'max', rang)
# print(res3)
bp = balance_point(res3, rang)
# print(bp)
return bp
def times_times_sum(Obs, r, rang):
res1 = result1(Obs, 'times', rang, r)
res2 = result2(res1, 'times', rang, r)
res3 = result3(res2, 'sum', rang)
bp = balance_point(res3, rang)
return bp
import argparse
class Obs: # 観測値 observed value
def __init__(self, e, d_e, d_u):
self.e = e # e
self.d_e = d_e # delta_e
self.d_u = d_u # delta_u
# class Fuzzyvalue1: # ファジー集合の数値 [度合い / 各値]
# def __init__(self, degree, value):
# self.degree = degree
# self.value = value
# class Fuzzyvalue2: # ファジー集合の数値 [度合い / 各値]
# def __init__(self, degree, value):
# self.degree = degree
# self.value = value
rang = 6 # 幅 range は Python の関数と被るので妥協.
def rule(bottom):
degree = [0] * (2 * rang + 1)
bottom = bottom + rang - 2
degree[bottom] = 0.3
degree[bottom + 1] = 0.7
degree[bottom + 2] = 1
degree[bottom + 3] = 0.7
degree[bottom + 4] = 0.3
return degree
def result1(Obs, switch, r):
e = Obs.e + rang
d_e = Obs.d_e + rang
result1 = []
if(switch == 'min'):
for x in r:
a = rule(x[0])[e]
b = rule(x[1])[d_e]
y = min(a, b)
result1.append(y)
# print(result1)
return result1 # 一致度
if(switch == 'times'):
for x in r:
a = rule(x[0])[e]
b = rule(x[1])[d_e]
y = a * b
result1.append(y)
# print(result1)
return result1 # 一致度
else:
print("Error! result1 switch = 'min' or 'times'.")
exit(1)
def result2(result1, switch, r):
result2 = []
u = range(len(result1))
if(switch == 'min'):
for s in u:
group = rule(r[s][2])
# print(group)
v = range(len(group))
for y in v:
if group[y] > result1[s]:
group[y] = result1[s]
result2.append(group)
return result2
if(switch == 'times'):
for x in u:
group = rule(r[x][2])
v = range(len(group))
for y in v:
group[y] = group[y] * result1[x]
result2.append(group)
return result2
else:
print("Error! result2 switch = 'min' or 'times'.")
exit(1)
def result3(result2, switch):
result3 = [0] * (2 * rang + 1)
u = range(2 * rang + 1)
if(switch == 'max'):
for y in u:
result3[y] = max([x[y] for x in result2])
return result3
if(switch == 'sum'):
for y in u:
result3[y] = sum([x[y] for x in result2])
return result3
else:
print("Error! switch = 'max' or 'sum'.")
exit(1)
def balance_point(result3):
a, b = 0, 0
c = range(len(result3))
for i in c:
x = i - rang
a = a + x * result3[i]
b = b + result3[i]
# print(a, b)
bp = a / b
return bp
def min_times_max(Obs, r):
res1 = result1(Obs, 'min', r)
res2 = result2(res1, 'times', r)
res3 = result3(res2, 'max')
bp = balance_point(res3)
return bp
def min_min_max(Obs, r):
res1 = result1(Obs, 'min', r)
# print(res1)
res2 = result2(res1, 'min', r)
# print(res2)
res3 = result3(res2, 'max')
# print(res3)
bp = balance_point(res3)
# print(bp)
return bp
def times_times_sum(Obs, r):
res1 = result1(Obs, 'times', r)
res2 = result2(res1, 'times', r)
res3 = result3(res2, 'sum')
bp = balance_point(res3)
return bp
def main(obs1, obs2):
r = [[0, 2, -2], # rule1(ZO + PS -> NS), 0個目が e, 1個目が de, 2個目が du の三角形のルール
[0, 0, 0], # rule2(ZO + ZO -> ZO)
[-2, 0, 2]] # rule3 のルール
# Obs.e = -2
# Obs.d_e = 1
Obs.e = obs1
Obs.d_e = obs2
bp1 = min_times_max(Obs, r)
bp2 = min_min_max(Obs, r)
bp3 = times_times_sum(Obs, r)
print('min - times - max : ' + str(bp1))
print('min - min - max : ' + str(bp2))
print('times - times - max : ' + str(bp3))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("obs1", type = int)
parser.add_argument("obs2", type = int)
args = parser.parse_args()
main(args.obs1, args.obs2)
import argparse
import func
class Obs: # 観測値 observed value
def __init__(self, e, d_e, d_u):
self.e = e # e
self.d_e = d_e # delta_e
self.d_u = d_u # delta_u
def main(obs1, obs2):
r = [[0, 2, -2], # rule1(ZO + PS -> NS), 0個目が e, 1個目が de, 2個目が du の三角形のルール
[0, 0, 0], # rule2(ZO + ZO -> ZO)
[-2, 0, 2]] # rule3 のルール
# Obs.e = -2
# Obs.d_e = 1
rang = 6 # 幅 range は Python の関数と被るので妥協.
Obs.e = obs1
Obs.d_e = obs2
bp1 = func.min_times_max(Obs, r, rang)
bp2 = func.min_min_max(Obs, r, rang)
bp3 = func.times_times_sum(Obs, r, rang)
print('min - times - max : ' + str(bp1))
print('min - min - max : ' + str(bp2))
print('times - times - max : ' + str(bp3))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("obs1", type = int)
parser.add_argument("obs2", type = int)
args = parser.parse_args()
main(args.obs1, args.obs2)
(fuzzy) user@MacBook-Pro fuzzy % python3 main.py -2 1
min - times - max : 0.8
min - min - max : 0.6153846153846152
times - times - max : 0.875
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment