# Path: fuzzy/defuzzify/Base.py
# class Base(object):
#     """Abstract base class for defuzzification
#        which results in a numeric value.
#        
#         @ivar INF: inference norm, used with set of adjective and given value for it
#         @type INF: L{fuzzy.norm.Norm.Norm}
#         @ivar ACC: norm for accumulation of set of adjectives
#         @type ACC: L{fuzzy.norm.Norm.Norm}
#         @cvar _INF: default value when INF is None
#         @type _INF: L{fuzzy.norm.Norm.Norm}
#         @cvar _ACC: default value when ACC is None
#         @type _ACC: L{fuzzy.norm.Norm.Norm}
#         @ivar activated_sets: results of activation of adjectives of variable.
#         @type activated_sets: {string:L{fuzzy.set.Polygon.Polygon}}
#         @ivar accumulated_set: result of accumulation of activated sets
#         @type accumulated_set: L{fuzzy.set.Polygon.Polygon}
#        """
# 
#     # default values if instance values are not set 
#     _INF = Min()
#     _ACC = Max()
# 
#     def __init__(self, INF=None, ACC=None):
#         """
#         @param INF: inference norm, used with set of adjective and given value for it
#         @type INF: L{fuzzy.norm.Norm.Norm}
#         @param ACC: norm for accumulation of set of adjectives
#         @type ACC: L{fuzzy.norm.Norm.Norm}
#         """
#         self.ACC = ACC # accumulation
#         self.INF = INF # inference
#         self.activated_sets = {}
#         self.accumulated_set = None
# 
#     def getValue(self,variable):
#         """Defuzzyfication."""
#         raise DefuzzificationException("don't use the abstract base class")
# 
# # helper methods for sub classes
# 
#     def accumulate(self,variable,segment_size=None):
#         """combining adjective values into one set"""
#         self.activated_sets = {}
#         temp = None
#         for name,adjective in variable.adjectives.items():
#             # get precomputed adjective set
#             temp2 = norm((self.INF or self._INF),adjective.set,adjective.getMembership(),segment_size)
#             self.activated_sets[name] = temp2
#             # accumulate all adjectives
#             if temp is None:
#                 temp = temp2
#             else:
#                 temp = merge((self.ACC or self._ACC),temp,temp2,segment_size)
#         self.accumulated_set = temp
#         return temp
# 
#     def value_table(self,set):
#         """get a value table of the polygon representation"""
#         # get polygon representation
#         ig = set.getIntervalGenerator()
#         next = ig.nextInterval(None,None)
#         while next is not None:
#             x = next
#             y = set(x)
#             yield (x,y)
#             # get next point from polygon
#             next = ig.nextInterval(next,None)
# 
# class DefuzzificationException(fuzzy.Exception.Exception):
#     pass


from fuzzy.defuzzify.Base import Base,DefuzzificationException
import fuzzy.set.Singleton
# this program; if not, see <http://www.gnu.org/licenses/>. 
#

__revision__ = "$Id: COGS.py,v 1.4 2009/08/07 07:19:18 rliebscher Exp $"


class COGS(Base):
    """defuzzification for singletons."""

    def __init__(self, INF=None, ACC=None, failsafe=None,*args,**keywords):
        """
            @param failsafe: if is not possible to calculate a center of gravity,
            return this value if not None or forward the exception
        """
        super(COGS, self).__init__(INF,ACC,*args,**keywords)
        self.failsafe = failsafe # which value if COG not calculable

    def getValue(self,variable):
        """Defuzzyfication using center of gravity method."""
        sum_1,sum_2 = 0.,0.
        for adjective in variable.adjectives.values():
            # get precomputed adjective set
            set = adjective.set
            if not isinstance(set,fuzzy.set.Singleton.Singleton):
                raise DefuzzificationException("Only Singleton for COGS defuzzification allowed.")
            a = (self.INF or self._INF)(set(set.x),adjective.getMembership())
            sum_1 += set.x*a
            sum_2 += a
        try:
            if sum_2 == 0.:

# Path: fuzzy/defuzzify/Base.py
# class Base(object):
#     """Abstract base class for defuzzification
#        which results in a numeric value.
#        
#         @ivar INF: inference norm, used with set of adjective and given value for it
#         @type INF: L{fuzzy.norm.Norm.Norm}
#         @ivar ACC: norm for accumulation of set of adjectives
#         @type ACC: L{fuzzy.norm.Norm.Norm}
#         @cvar _INF: default value when INF is None
#         @type _INF: L{fuzzy.norm.Norm.Norm}
#         @cvar _ACC: default value when ACC is None
#         @type _ACC: L{fuzzy.norm.Norm.Norm}
#         @ivar activated_sets: results of activation of adjectives of variable.
#         @type activated_sets: {string:L{fuzzy.set.Polygon.Polygon}}
#         @ivar accumulated_set: result of accumulation of activated sets
#         @type accumulated_set: L{fuzzy.set.Polygon.Polygon}
#        """
# 
#     # default values if instance values are not set 
#     _INF = Min()
#     _ACC = Max()
# 
#     def __init__(self, INF=None, ACC=None):
#         """
#         @param INF: inference norm, used with set of adjective and given value for it
#         @type INF: L{fuzzy.norm.Norm.Norm}
#         @param ACC: norm for accumulation of set of adjectives
#         @type ACC: L{fuzzy.norm.Norm.Norm}
#         """
#         self.ACC = ACC # accumulation
#         self.INF = INF # inference
#         self.activated_sets = {}
#         self.accumulated_set = None
# 
#     def getValue(self,variable):
#         """Defuzzyfication."""
#         raise DefuzzificationException("don't use the abstract base class")
# 
# # helper methods for sub classes
# 
#     def accumulate(self,variable,segment_size=None):
#         """combining adjective values into one set"""
#         self.activated_sets = {}
#         temp = None
#         for name,adjective in variable.adjectives.items():
#             # get precomputed adjective set
#             temp2 = norm((self.INF or self._INF),adjective.set,adjective.getMembership(),segment_size)
#             self.activated_sets[name] = temp2
#             # accumulate all adjectives
#             if temp is None:
#                 temp = temp2
#             else:
#                 temp = merge((self.ACC or self._ACC),temp,temp2,segment_size)
#         self.accumulated_set = temp
#         return temp
# 
#     def value_table(self,set):
#         """get a value table of the polygon representation"""
#         # get polygon representation
#         ig = set.getIntervalGenerator()
#         next = ig.nextInterval(None,None)
#         while next is not None:
#             x = next
#             y = set(x)
#             yield (x,y)
#             # get next point from polygon
#             next = ig.nextInterval(next,None)
# 
# class DefuzzificationException(fuzzy.Exception.Exception):
#     pass


# Path: fuzzy/defuzzify/COGS.py
from fuzzy.defuzzify.Base import Base,DefuzzificationException
import fuzzy.set.Singleton

# this program; if not, see <http://www.gnu.org/licenses/>. 
#

__revision__ = "$Id: COGS.py,v 1.4 2009/08/07 07:19:18 rliebscher Exp $"


class COGS(Base):
    """defuzzification for singletons."""

    def __init__(self, INF=None, ACC=None, failsafe=None,*args,**keywords):
        """
            @param failsafe: if is not possible to calculate a center of gravity,
            return this value if not None or forward the exception
        """
        super(COGS, self).__init__(INF,ACC,*args,**keywords)
        self.failsafe = failsafe # which value if COG not calculable

    def getValue(self,variable):
        """Defuzzyfication using center of gravity method."""
        sum_1,sum_2 = 0.,0.
        for adjective in variable.adjectives.values():
            # get precomputed adjective set
            set = adjective.set
            if not isinstance(set,fuzzy.set.Singleton.Singleton):
                raise DefuzzificationException("Only Singleton for COGS defuzzification allowed.")
            a = (self.INF or self._INF)(set(set.x),adjective.getMembership())
            sum_1 += set.x*a
            sum_2 += a
        try:
            if sum_2 == 0.:

                raise DefuzzificationException("No result, all singletons set to 0.")