Source code for pyinference.fuzzy.tnorm

# -*- coding: UTF-8 -*-

"""Модуль реализует набор простых и параметрических треугольных норм и конорм.
"""

import math


[docs]class Tnorm(object):
[docs] def norm(self, i, j): pass
[docs] def conorm(self, i, j): pass
[docs]class MinMax(Tnorm):
[docs] def norm(self, i, j): return min(i, j)
[docs] def conorm(self, i, j): return max(i, j)
[docs]class SumProd(Tnorm):
[docs] def norm(self, i, j): return i * j
[docs] def conorm(self, i, j): return i + j - i * j
[docs]class Margin(Tnorm):
[docs] def norm(self, i, j): return max(i + j - 1, 0)
[docs] def conorm(self, i, j): return min(i + j, 1)
[docs]class Drastic(Tnorm):
[docs] def norm(self, i, j): if i == 1: return j elif j == 1: return i else: return 0
[docs] def conorm(self, i, j): if i == 0: return j elif j == 0: return i else: return 1
[docs]class ParametricNorm(Tnorm): def __init__(self, param): self.param = param
[docs]class Tnorm1(ParametricNorm): def __init__(self, param): super(Tnorm1, self).__init__(param)
[docs] def norm(self, i, j): return i * j / (self.param + (1 - self.param) * (i + j - i * j))
[docs] def conorm(self, i, j): return (i + j - (2 - self.param) * i * j) / (1 - (1 - self.param) * i * j)
[docs]class Tnorm2(ParametricNorm): def __init__(self, param): super(Tnorm2, self).__init__(param)
[docs] def norm(self, i, j): return i * j / max(i, j, self.param)
[docs] def conorm(self, i, j): return (i + j - i * j - min(i, j, 1 - self.param)) / max(1 - i, 1 - j, self.param)
[docs]class Tnorm3(ParametricNorm): def __init__(self, param): super(Tnorm3, self).__init__(param)
[docs] def norm(self, i, j): try: return 1 / (1 + ((1 / i - 1) ** self.param + (1 / j - 1) ** self.param) ** (1 / self.param)) except ZeroDivisionError: return 0.0
[docs] def conorm(self, i, j): return 1 / (1 + ((1 / i - 1) ** -self.param + (1 / i - 1) ** -self.param) ** (1 / self.param))
[docs]class Tnorm4(ParametricNorm): def __init__(self, param): super(Tnorm4, self).__init__(param)
[docs] def norm(self, i, j): return 1 - ((1 - i) ** self.param + (1 - j) ** self.param - (1 - i) ** self.param * (1 - j) ** self.param) ** (1 / self.param)
[docs] def conorm(self, i, j): return (i ** self.param + j ** self.param - i ** self.param * j ** self.param) ** (1 / self.param)
[docs]class Tnorm5(ParametricNorm): def __init__(self, param): super(Tnorm5, self).__init__(param)
[docs] def norm(self, i, j): return max((1 - ((1 - i) ** self.param + (1 - j) ** self.param) ** (1 / self.param)), 0)
[docs] def conorm(self, i, j): return min((i ** self.param + j ** self.param), 1)
[docs]class Tnorm6(ParametricNorm): def __init__(self, param): super(Tnorm6, self).__init__(param)
[docs] def norm(self, i, j): try: return math.log((1 + (self.param ** i - 1) * (self.param ** j - 1) / (self.param - 1)), self.param) except ZeroDivisionError: return 0.0
[docs] def conorm(self, i, j): return 1 - math.log((1 + (self.param ** (1 - i) + self.param ** (1 - j)) / (self.param - 1)), self.param)
[docs]class Tnorm7(ParametricNorm): def __init__(self, param): super(Tnorm7, self).__init__(param)
[docs] def norm(self, i, j): return max((i + j - 1 + self.param * i * j) / (1 + self.param), 0)
[docs] def conorm(self, i, j): return min((i + j - 1 + self.param * i * j), 1)
if __name__ == "__main__": import doctest doctest.testmod(verbose=False) # ~ doctest.testmod(verbose=True)