AI-FuzzyInference
view release on metacpan or search on metacpan
FuzzyInference.pm view on Meta::CPAN
# 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',
},
'!' => {
complement => sub { 1 - $_[0] },
custom => sub {},
default => 'complement',
},
);
# this hash defines the currently implemented implication methods.
my %_implication = qw(
clip 1
scale 1
default clip
);
# this hash defines the currently implemented aggregation methods.
my %_aggregation = qw(
max 1
default max
);
# this hash defines the currently implemented defuzzification methods.
my %_defuzzification = qw(
centroid 1
default centroid
);
# sub new() - constructor.
#
# doesn't take any arguments. Returns an initialized AI::FuzzyInference object.
sub new {
my $self = shift;
my $class = ref($self) || $self;
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} = {};
$self->{IMPLICATION} = $_implication{default};
$self->{AGGREGATION} = $_aggregation{default};
$self->{DEFUZZIFICATION} = $_defuzzification{default};
for my $op (qw/& | !/) {
$self->{OPERATIONS}{$op} = $_operations{$op}{default};
}
}
# sub implication() - public method.
#
# one optional argument: has to match one of the keys of the %_implication hash.
# used to query/set the implication method.
sub implication {
my ($self,
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
argument which specifies the name of the implication method used.
This can be one of the following:
=over 8
=item clip
This causes the output fuzzy set to be clipped at its support value.
This is the default.
=item scale
This scales the output fuzzy set by multiplying it by its support value.
=back
=item aggregation()
This method is used to set/query the aggregation method used to combine
the output fuzzy sets. It takes one optional
argument which specifies the name of the aggregation method used.
This can be one of the following:
=over 8
=item max
The fuzzy sets are combined by taking at each point the maximum value of
all the fuzzy sets at that point.
This is the default.
=back
=item defuzzification()
This method is used to set/query the defuzzification method used to
extract a single crisp value from the aggregated fuzzy set.
It takes one optional
argument which specifies the name of the defuzzification method used.
This can be one of the following:
=over 8
=item centroid
The centroid (aka I<center of mass> and I<center of gravity>) of the
aggregated fuzzy set is computed and returned.
This is the default.
=back
=item inVar()
This method defines an input variable, along with its universe of
discourse, and its term sets. Here's an example:
$obj->inVar('height',
5, 8, # xmin, xmax (in feet, say)
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:
clip This causes the output fuzzy set to be clipped at its
support value. This is the default.
scale This scales the output fuzzy set by multiplying it by its
support value.
aggregation()
This method is used to set/query the aggregation method used to
combine the output fuzzy sets. It takes one optional argument which
specifies the name of the aggregation method used. This can be one
of the following:
max The fuzzy sets are combined by taking at each point the
maximum value of all the fuzzy sets at that point. This is
the default.
defuzzification()
This method is used to set/query the defuzzification method used to
extract a single crisp value from the aggregated fuzzy set. It takes
one optional argument which specifies the name of the
defuzzification method used. This can be one of the following:
centroid
The centroid (aka *center of mass* and *center of gravity*)
of the aggregated fuzzy set is computed and returned. This
is the default.
inVar()
This method defines an input variable, along with its universe of
discourse, and its term sets. Here's an example:
$obj->inVar('height',
5, 8, # xmin, xmax (in feet, say)
'tall' => [5, 0,
5.5, 1,
6, 0],
( run in 0.410 second using v1.01-cache-2.11-cpan-0a6323c29d9 )