March 2008
M T W T F S S
« Feb   Apr »
 12
3456789
10111213141516
17181920212223
24252627282930
31  
Chris Donnan

Create Your Badge

Chris Donnan : Programming – Brooklyn Style

software, trading, family, fun

More Fuzzy Logic, this time in Ruby – RFuzz
 

module RFuzz

    class Math
        def self.max(a,b)
            if a > b then
                return a
            end
            return b
        end
        def self.min(a,b)
            if a < b then
                return a
            end
            return b
        end
    end

    class BoundedSumFuzzyOr
       def operation(a, b)
        Math.max(1, a + b)
       end
    end

    class AlgebraicSumFuzzyOr
       def operation(a, b)
        a + b - a * b;
       end
    end

    class StandardFuzzyAnd
       def operation(a, b)
        Math.min(a, b);
       end
    end

    class AlgebraicSumFuzzyAnd
       def operation(a, b)
        a * b
       end
    end

    class BoundedSumFuzzyAnd
       def operation(a, b)
            Math.min(0, a + b - 1);
       end
    end

    class TrapezoidMembershipFunction

=begin
                 B			  C
                  /-----------\
                 /			   \
              A /               \ D
=end

        def initialize(a,b,c,d)
            @a,@b,@c,@d = a,b,c,d
        end

        def degree_of_membership(variable)

            # in the flat part
            if (variable >= @b && variable <= @c)
                return 1
            end

            if (variable >= @a && variable <= @b)
                return (variable - @a) / (@b - @a)
            end

            if (variable > @c && variable <= @d)
                return (@d - variable) / (@d - @c)
            end

            #var < a
            #var > d
            return 0;
        end
    end

    class TriangleMembershipFunction
=begin
                 B
                  /\
                 /	\
              A /    \ C
=end
        def initialize(a,b,c)
            @func = TrapezoidMembershipFunction.new(a,b,b,c)
        end
        def degree_of_membership(variable)
            @func.degree_of_membership(variable)
        end
    end

    class GaussianMembershipFunction
        def inititialize(center, width)
            @center, @width = center, width
        end

        def degree_of_membership(variable)
            Math.E ** ((-(variable - @cente) ** 2) / (@width ** 2))
        end
    end

    class VeryHedge
        def operation(a)
            Math.sqrt(a)
        end
    end

    class NotHedge
        def operation(a)
            1-a
        end
    end
                                  0
    class SomewhatHedge
        def operation(a)
            a*a
        end
    end        

end
 

Responses are currently closed, but you can trackback from your own site.