Advertisement
Guest User

Untitled

a guest
May 24th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class FuzzyVariable:
  4.     def __init__(self):
  5.         self.labels = {}
  6.         self.value = 0
  7.  
  8.     def toDiscrete(self):
  9.         ret = {}
  10.         graph = self.labels
  11.         value = self.value
  12.         for key in graph.keys():
  13.             #check where it is:
  14.             for i in range(len(graph[key]) - 1):
  15.                 if graph[key][i][0] <= value and value <= graph[key][i + 1][0]:
  16.                     if graph[key][i][0] == -np.inf:
  17.                         ret[key] = graph[key][i][1]
  18.                         continue
  19.                     if graph[key][i + 1][0] == np.inf:
  20.                         ret[key] = graph[key][i + 1][1]
  21.                         continue
  22.                     deltaY = graph[key][i + 1][1] - graph[key][i][1]
  23.                     deltaX = graph[key][i + 1][0] - graph[key][i][0]
  24.                     ret[key] = graph[key][i][1] + ((value - graph[key][i][0]) / deltaX) * deltaY
  25.         return ret
  26.  
  27.  
  28. class Texture(FuzzyVariable):
  29.     def __init__(self, value):
  30.         self.value = value
  31.         self.labels = {
  32.             'very_soft': [(-np.inf, 1), (0.2, 1), (0.4, 0), (np.inf, 0)],
  33.             'soft': [(-np.inf, 0), (0.2, 0), (0.4, 1), (0.8, 0), (np.inf, 0)],
  34.             'normal': [(-np.inf, 0), (0.3, 0), (0.7, 1), (0.9, 0), (np.inf, 0)],
  35.             'resistant': [(-np.inf, 0), (0.7, 0), (0.9, 1), (np.inf, 1)]
  36.         }
  37.  
  38.  
  39. class Capactiy(FuzzyVariable):
  40.     def __init__(self, value):
  41.         self.value = value
  42.         self.labels = {
  43.             'small': [(-np.inf, 1), (1, 1), (2, 0), (np.inf, 0)],
  44.             'medium': [(-np.inf, 0), (1, 0), (2.5, 0), (4, 0), (np.inf, 0)],
  45.             'high': [(-np.inf, 0), (3, 0), (4, 1), (np.inf, 1)]
  46.         }
  47.  
  48. class CycleType(FuzzyVariable):
  49.     def __init__(self, value):
  50.         self.value = value
  51.         self.labels = {
  52.             'delicate': [(-np.inf, 1), (0.2, 1), (0.4, 0), (np.inf, 0)],
  53.             'easy': [(-np.inf, 0), (0.2, 0), (0.5, 1), (0.8, 0), (np.inf, 0)],
  54.             'normal': [(-np.inf, 0), (0.3, 0), (0.6, 1), (0.9, 1), (np.inf, 1)],
  55.             'intense': [(-np.inf, 0), (0.7, 0), (0.9, 1), (np.inf, 1)]
  56.         }
  57.  
  58. class Ruler:
  59.     def __init__(self):
  60.         self.rules = {
  61.             "very_soft": {
  62.                 "small": "delicate",
  63.                 "medium": "easy",
  64.                 "high": "normal"
  65.             },
  66.             "soft": {
  67.                 "small": "easy",
  68.                 "medium": "normal",
  69.                 "high": "normal"
  70.             },
  71.             "normal": {
  72.                 "small": "easy",
  73.                 "medium": "normal",
  74.                 "high": "intense"
  75.             },
  76.             "resistant": {
  77.                 "small": "easy",
  78.                 "medium": "normal",
  79.                 "high": "intense"
  80.             }
  81.         }
  82.     def evaluate(self, t, c):
  83.         tdic = t.toDiscrete()
  84.         cdic = c.toDiscrete()
  85.         resdic = {}
  86.         print(tdic)
  87.         print(cdic)
  88.         for tkey, tvalue in tdic.items():
  89.             for ckey, cvalue in cdic.items():
  90.                 res = self.rules[tkey][ckey]
  91.                 val = min(tvalue, cvalue)
  92.                 if res in resdic:
  93.                     resdic[res] = max(resdic[res], val)
  94.                 else:
  95.                     resdic[res] = val
  96.         return resdic
  97.  
  98. class Controller:
  99.     def __init__(self, texture, capacity):
  100.         self.rules = Ruler()
  101.         self.t = Texture(texture)
  102.         self.c = Capactiy(capacity)
  103.  
  104.     def solve(self):
  105.         agg = self.rules.evaluate(self.t, self.c)
  106.         print(agg)
  107.         print(sorted(list(agg.items()), key = lambda x: x[1])[-1][0])
  108.  
  109. c = Controller(0.4, 0.5)
  110. c.solve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement