AI-FuzzyInference
view release on metacpan or search on metacpan
FuzzyInference.pm view on Meta::CPAN
use AI::FuzzyInference::Set;
############################################
#
# First some global vars.
#
############################################
# this hash defines the possible interpretations of the
# standard fuzzy logic operations.
my %_operations = (
'&' => {
min => sub { (sort {$a <=> $b} @_)[0] },
product => sub { my $p = 1; $p *= $_ for @_; $p },
default => 'min',
},
'|' => {
max => sub { (sort {$a <=> $b} @_)[-1] },
sum => sub { my $s = 0; $s += $_ for @_; $s > 1 ? 1 : $s },
default => 'max',
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
if (defined $new and exists $_defuzzification{$new}) {
$self->{DEFUZZIFICATION} = $new;
}
return $self->{DEFUZZIFICATION};
}
# sub operation() - public method.
#
# two arguments: first one mandatory and specifies the logic operation
# in question. Second one is optional and has to match one of the keys
# of the %{$_operations{$first_arg}} hash.
# Used to query/set the logic operations method.
sub operation {
my ($self,
$op,
$new,
) = @_;
return unless defined $op && exists $_operations{$op};
if (defined $new and exists $_operations{$op}{$new}) {
FuzzyInference.pm view on Meta::CPAN
$terms{$var}{$ts} = $deg;
}
}
$self->{FUZZIFY} = \%terms;
}
# sub _infer() - private method.
#
# no arguments. This method applies the logic operations to combine
# multiple parts of the antecedent of a rule to get one crisp value
# that is the degree of support of that rule.
# Rules with positive support "fire".
sub _infer {
my $self = shift;
my @fired; # keep list of fired rules.
for my $i (0 .. $#{$self->{RULES}}) {
FuzzyInference.pm view on Meta::CPAN
=head2 Defuzzification
Finally, a defuzzification operator is applied to the aggregated fuzzy
set to generate a single crisp value for each of the output variables.
For a more detailed explanation of fuzzy inference, you can check out
the tutorial by Jerry Mendel at
S<http://sipi.usc.edu/~mendel/publications/FLS_Engr_Tutorial_Errata.pdf>.
Note: The terminology used in this module might differ from that used
in the above tutorial.
=head1 PUBLIC METHODS
The module has the following public methods:
=over 4
=item new()
This is the constructor. It takes no arguments, and returns an
initialized AI::FuzzyInference object.
=item 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 C<&> for logical I<AND>,
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.
Defuzzification
Finally, a defuzzification operator is applied to the aggregated fuzzy
set to generate a single crisp value for each of the output variables.
For a more detailed explanation of fuzzy inference, you can check out
the tutorial by Jerry Mendel at
http://sipi.usc.edu/~mendel/publications/FLS_Engr_Tutorial_Errata.pdf.
Note: The terminology used in this module might differ from that used in
the above tutorial.
PUBLIC METHODS
The module has the following public methods:
new()
This is the constructor. It takes no arguments, and returns an
initialized AI::FuzzyInference object.
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.
( run in 1.310 second using v1.01-cache-2.11-cpan-49f99fa48dc )