AI-FuzzyInference

 view release on metacpan or  search on metacpan

FuzzyInference.pm  view on Meta::CPAN


    my $obj = bless {} => $class;

    $obj->_init;

    return $obj;
}

# sub _init() - private method.
#
# no arguments. Initializes the data structures we will need.
# It also defines the default logic operations we might need.

sub _init {
    my $self = shift;

    $self->{SET}     = new AI::FuzzyInference::Set;
    $self->{INVARS}  = {};
    $self->{OUTVARS} = {};
    $self->{RULES}   = [];
    $self->{RESULTS} = {};

FuzzyInference.pm  view on Meta::CPAN


sub value {
    my ($self,
	$var,
	) = @_;

    return undef unless exists $self->{RESULTS}{$var};
    return $self->{RESULTS}{$var};
}

# sub reset() - public method
# 
# cleans the data structures used.

sub reset {
    my $self = shift;

    my @list   =  $self->{SET}->listMatching(q|:implicated$|);
    push @list => $self->{SET}->listMatching(q|:aggregated$|);

    $self->{SET}->delete($_) for @list;

    $self->{RESULTS} = {};
}

FuzzyInference.pm  view on Meta::CPAN

# based on the application of the fuzzy if-then rules.
# ex.
# $z = $obj->compute(x => 5,
#                    y => 24);

sub compute {
    my ($self,
	%vars,
	) = @_;

    $self->reset();

    # First thing we do is to fuzzify the inputs.
    $self->_fuzzify(%vars);

    # Now, apply the rules to see which ones fire.
    $self->_infer;

    # implicate
    $self->_implicate;

FuzzyInference.pm  view on Meta::CPAN

# var.

sub _defuzzify {
    my $self = shift;

    my $_defuzzification = $self->{DEFUZZIFICATION};

    # iterate through all output vars.
    for my $var (keys %{$self->{OUTVARS}}) {

	my $result = 0;
	if ($self->{SET}->exists("$var:aggregated")) {
	    $result = $self->{SET}->$_defuzzification("$var:aggregated");
	}

	$self->{RESULTS}{$var} = $result;
    }
}

# sub _aggregate() - private method.
#
# no arguments. This method applies the aggregation technique to get
# one fuzzy set out of the implicated sets of each output var.

sub _aggregate {
    my $self = shift;

FuzzyInference.pm  view on Meta::CPAN

In this step, the crisp values of the input variables are used to
compute a degree of membership of each of the input variables in each
of its term sets. This produces a set of fuzzy sets.

=head2 Inference

In this step, all the defined rules are examined. Each rule has two parts:
the I<precedent> and the I<consequent>. The degree of support for each
rule is computed by applying fuzzy operators (I<and>, I<or>) to combine
all parts of its precendent, and generate a single crisp value. This value
indicates the "strength of firing" of the rule, and is used to reshape
(I<implicate>) the consequent part of the rule, generating modified
fuzzy sets.

=head2 Aggregation

Here, all implicated fuzzy sets of the fired rules are combined using
fuzzy operators to generate a single fuzzy set for each of the
output variables.

=head2 Defuzzification

FuzzyInference.pm  view on Meta::CPAN

C<|> for logical I<OR>, or C<!> for logical I<NOT>. The second
argument is used to set what method to use for the given operator.
The following values are possible:

=item &

=over 8

=item min

The result of C<A and B> is C<min(A, B)>. This is the default.

=item product

The result of C<A and B> is C<A * B>.

=back

=item |

=over 8

=item max

The result of C<A or B> is C<max(A, B)>. This is the default.

=item sum

The result of C<A or B> is C<min(A + B, 1)>.

=back

=item !

=over 8

=item complement

The result of C<not A> is C<1 - A>. This is the default.

=back

The method returns the name of the method to be used for the given
operation.

=item implication()

This method is used to set/query the implication method used to alter
the shape of the implicated output fuzzy sets. It takes one optional

FuzzyInference.pm  view on Meta::CPAN

This method defines an output variable, along with its universe of
discourse, and its term sets. The arguments are identical to those for
the C<inVar()> method.

=item addRule()

This method is used to add the fuzzy rules. Its arguments are hash-value
pairs; the keys are the precedents and the values are the consequents.
Each antecedent has to be a combination of 1 or more strings. The
strings have to be separated by C<&> or C<|> indicating the fuzzy
I<AND> and I<OR> operations respectively. Each consequent must be a
single string. Each string has the form: C<var = term_set>. Spaces
are completely optional. Example:

    $obj->addRule('height=short & weight=big' => 'diet = necessary',
		  'height=tall & weight=tiny' => 'diet = are_you_kidding_me');

The first rule basically says I<If the height is short, and the weight is
big, then diet is necessary>.

=item compute()

This method takes as input a set of hash-value pairs; the keys are names
of input variables, and the values are the values of the variables. It
runs those values through the FIS, generating corresponding values for
the output variables. It always returns a true value. To get the actual
values of the output variables, look at the C<value()> method below.
Example:

    $obj->compute(x => 5,
		  y => 24);

Note that any subsequent call to C<compute()> will implicitly clear out
the old computed values before recomputing the new ones. This is done
through a call to the C<reset()> method below.

=item value()

This method returns the value of the supplied output variable. It only
works for output variables (defined using the C<outVar()> method),
and only returns useful results after a call to C<compute()> has been
made.

=item reset()

This method resets all the data structures used to compute crisp values
of the output variables. It is implicitly called by the C<compute()>
method above.

=back

=head1 INSTALLATION

It's all in pure Perl. Just place it somewhere and point your @INC to it.

But, if you insist, here's the traditional way:

FuzzyInference.pm  view on Meta::CPAN

To install this module type the following:

   perl Makefile.PL
   make
   make test
   make install


=head1 AUTHOR

Copyright 2002, Ala Qumsieh. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

Address bug reports and comments to: aqumsieh@cpan.org

README  view on Meta::CPAN

    compute a degree of membership of each of the input variables in each of
    its term sets. This produces a set of fuzzy sets.

  Inference

    In this step, all the defined rules are examined. Each rule has two
    parts: the *precedent* and the *consequent*. The degree of support for
    each rule is computed by applying fuzzy operators (*and*, *or*) to
    combine all parts of its precendent, and generate a single crisp value.
    This value indicates the "strength of firing" of the rule, and is used
    to reshape (*implicate*) the consequent part of the rule, generating
    modified fuzzy sets.

  Aggregation

    Here, all implicated fuzzy sets of the fired rules are combined using
    fuzzy operators to generate a single fuzzy set for each of the output
    variables.

  Defuzzification

README  view on Meta::CPAN


    operation()
        This method is used to set/query the fuzzy operations. It takes at
        least one argument, and at most 2. The first argument specifies the
        logic operation in question, and can be either "&" for logical
        *AND*, "|" for logical *OR*, or "!" for logical *NOT*. The second
        argument is used to set what method to use for the given operator.
        The following values are possible:

    &
        min     The result of "A and B" is "min(A, B)". This is the default.

        product The result of "A and B" is "A * B".

    |
        max     The result of "A or B" is "max(A, B)". This is the default.

        sum     The result of "A or B" is "min(A + B, 1)".

    !
        complement
                The result of "not A" is "1 - A". This is the default.

        The method returns the name of the method to be used for the given
        operation.

    implication()
        This method is used to set/query the implication method used to
        alter the shape of the implicated output fuzzy sets. It takes one
        optional argument which specifies the name of the implication method
        used. This can be one of the following:

README  view on Meta::CPAN

    outVar()
        This method defines an output variable, along with its universe of
        discourse, and its term sets. The arguments are identical to those
        for the "inVar()" method.

    addRule()
        This method is used to add the fuzzy rules. Its arguments are
        hash-value pairs; the keys are the precedents and the values are the
        consequents. Each antecedent has to be a combination of 1 or more
        strings. The strings have to be separated by "&" or "|" indicating
        the fuzzy *AND* and *OR* operations respectively. Each consequent
        must be a single string. Each string has the form: "var = term_set".
        Spaces are completely optional. Example:

            $obj->addRule('height=short & weight=big' => 'diet = necessary',
                          'height=tall & weight=tiny' => 'diet = are_you_kidding_me');

        The first rule basically says *If the height is short, and the
        weight is big, then diet is necessary*.

    compute()
        This method takes as input a set of hash-value pairs; the keys are
        names of input variables, and the values are the values of the
        variables. It runs those values through the FIS, generating
        corresponding values for the output variables. It always returns a
        true value. To get the actual values of the output variables, look
        at the "value()" method below. Example:

            $obj->compute(x => 5,
                          y => 24);

        Note that any subsequent call to "compute()" will implicitly clear
        out the old computed values before recomputing the new ones. This is
        done through a call to the "reset()" method below.

    value()
        This method returns the value of the supplied output variable. It
        only works for output variables (defined using the "outVar()"
        method), and only returns useful results after a call to "compute()"
        has been made.

    reset()
        This method resets all the data structures used to compute crisp
        values of the output variables. It is implicitly called by the
        "compute()" method above.

INSTALLATION
    It's all in pure Perl. Just place it somewhere and point your @INC to
    it.

    But, if you insist, here's the traditional way:

    To install this module type the following:

       perl Makefile.PL
       make
       make test
       make install

AUTHOR
    Copyright 2002, Ala Qumsieh. All rights reserved. This library is free
    software; you can redistribute it and/or modify it under the same terms
    as Perl itself.

    Address bug reports and comments to: ala_qumsieh@yahoo.com.



( run in 1.143 second using v1.01-cache-2.11-cpan-49f99fa48dc )