AI-Fuzzy
view release on metacpan or search on metacpan
Revision history for Perl extension AI::Fuzzy.
0.05 Sat Jan 4 10:20:29 EST 2003
- found problem with stringifycation in Set.pm
- fixed warning messages due to not checking "exists" for hash
values in Set.pm (union,intersection). Thanks to Richard Jelinek
for pointing this out, and a problem in the code in the docs.
0.04 Fri Dec 6 13:49:55 EST 2002
- replaced current AI::Fuzzy::Label with a new AI::Fuzzy::Axis (a container for label objects)
and changed AI::Fuzzy::Label to be concerned only about label data. This
will allow us to add new AI::Fuzzy::Label{Spline, Trapezoid, etc.} subclasses
of labels to the now independent Axis class. Axis will defer to the Label
itself to decide applicability, >,<,>=,<=, and the like.
- changed test.pl to work with the new setup
- added functions: greaterthan, greaterequal, lessthan, lessequal, and between
to AI::Fuzzy::Label
- added overriding of >,>=,<,<=, and <=> in AI::Fuzzy::Label.
0.03 Wed Oct 9 18:07:34 EDT 2002
- added functions: support, core, height, is_normal, is_subnormal
to AI::Fuzzy::Set
0.02 Wed Oct 9 16:41:29 EDT 2002
- ownership transfering to Tom Scanlan <tscanlan@openreach.com>
- added functions to AI::Fuzzy::Set for intersection, union,
complement, equal, and as_string
- made a heck of a lot of tests. use them as examples...
0.01 Mon Jul 19 19:33:46 1999
- original version; created by h2xs 1.18
print "b is: " . $b->as_string . "\n";
print "a is equal to b" if ($a->equal($b));
my $c = $a->complement();
print "complement of a is: " . $c->as_string . "\n";
$c = $a->union($b);
print "a union b is: " . $c->as_string . "\n";
$c = $a->intersection($b);
print "a intersection b is: " . $c->as_string . "\n";
__END__
=head1 DESCRIPTION
AI::Fuzzy really consists of three modules - AI::Fuzzy::Axis, AI::Fuzzy::Label, and
AI::Fuzzy::Set.
A fuzzy set is simply a mathematical set to which members can
I<partially> belong. For example, a particular shade of gray may
$fs_tall_people = B<new> AI::Fuzzy::Set( Lester=>.34, Bob=>1.00, Max=>.86 );
# $x will be .86
$x = B<membership> $fs_tall_people, "Max";
# get list of members, sorted from least membership to greatest:
@shortest_first = B<members> $fs_tall_people;
$fs = B<new> AI::Fuzzy::Set( x1 => .3, x2 => .5, x3 => .8, x4 => 0, x5 => 1);
B<complement>, B<union>, B<intersection>
Thesie are the fuzzy set version of the typical functions.
B<equal>
Returns true if the sets have the same elements and those elements
are all equal.
B<as_string>
Prints the set as tuples:
$b = new AI::Fuzzy::Set( x5 => .3, x6 => .5, x7 => .8, x8 => 0, x9 => 1);
print "b is: " . $b->as_string . "\n";
print "b is: " . $b->as_string . "\n";
print "a is equal to b" if ($a->equal($b));
$c = $a->complement();
print "complement of a is: " . $c->as_string . "\n";
$c = $a->union($b);
print "a union b is: " . $c->as_string . "\n";
$c = $a->intersection($b);
print "a intersection b is: " . $c->as_string . "\n";
__END__
DESCRIPTION
AI::Fuzzy really consists of two modules - AI::Fuzzy::Label and
AI::Fuzzy::Set.
A fuzzy set is simply a mathematical set to which members can
*partially* belong. For example, a particular shade of gray may
partially belong to the set of dark colors, whereas black would have
$fs_tall_people = B<new> AI::Fuzzy::Set( Lester=>.34, Bob=>1.00, Max=>.86 );
# $x will be .86
$x = B<membership> $fs_tall_people, "Max";
# get list of members, sorted from least membership to greatest:
@shortest_first = B<members> $fs_tall_people;
$fs = B<new> AI::Fuzzy::Set( x1 => .3, x2 => .5, x3 => .8, x4 => 0, x5 => 1);
B<complement>, B<union>, B<intersection>
Thesie are the fuzzy set version of the typical functions.
B<equal>
Returns true if the sets have the same elements and those elements
are all equal.
B<as_string>
Prints the set as tuples:
$b = new AI::Fuzzy::Set( x5 => .3, x6 => .5, x7 => .8, x8 => 0, x9 => 1);
print "b is: " . $b->as_string . "\n";
demo/fuzz.pl view on Meta::CPAN
print "b is: " . $b->as_string . "\n";
print "a is equal to b" if ($a->equal($b));
my $c = $a->complement();
print "complement of a is: " . $c->as_string . "\n";
$c = $a->union($b);
print "a union b is: " . $c->as_string . "\n";
$c = $a->intersection($b);
print "a intersection b is: " . $c->as_string . "\n";
lib/AI/Fuzzy/Set.pm view on Meta::CPAN
if ($us{$key} >= $them{$key}) {
$new{$key} = $us{$key};
} else {
$new{$key} = $them{$key};
}
}
return new AI::Fuzzy::Set(%new);
}
sub intersection {
# returns a set that is the intersection of us and the argument set
my $self = shift;
my $otherset = shift;
my (%us, %them, %new);
%us = %{$self->{members}} if (exists $self->{members});
%them = %{$otherset->{members}} if (exists $otherset->{members});
# for all keys in us and them
foreach my $key (keys (%us), keys (%them)) {
if (not exists $us{$key} or not exists $them{$key}) {
$sc = new AI::Fuzzy::Set( Lester=>.35, Bob=>1.00, Max=>.86 );
ok ($sa->equal($ns),1);
ok ($sa->equal($sc),0);
ok ($sa->equal($sb),0);
ok ($sa->equal($sa),1);
$sd = $sa->union($sc);
ok ($sd->membership("Lester"), .35);
$sd = $sa->intersection($sb);
ok ($sd->membership("Lester"), 0);
$sd = $sd->complement();
ok ($sd->membership("Max"), .14);
# the complement of the complement should be the original
$se = $sa->complement() || print "problem with complement\n";
$se = $se->complement() || print "problem with complement\n";
ok ($se->equal($sa));
# a union b should equal b union a
$aUb = $sa->union($sb);
$bUa = $sb->union($sa);
ok($aUb->equal($bUa));
# a intersection b should equal b intersection a
$aNb = $sa->intersection($sb);
$bNa = $sb->intersection($sa);
ok($aNb->equal($bNa));
# a union b union c should equal b union c union a
$abc = $sa->union($sb);
$abc = $abc->union($sc);
$bca = $sb->union($sc);
$bca = $bca->union($sa);
ok($abc->equal($bca));
# a intersection b intersection c should equal b intersection c intersection a
$abc = $sa->intersection($sb);
$abc = $abc->intersection($sc);
$bca = $sb->intersection($sc);
$bca = $bca->intersection($sa);
ok($abc->equal($bca));
# comment this to run extra output tests.
#exit 0;
$a = new AI::Fuzzy::Set( x1 => .3, x2 => .5, x3 => .8, x4 => 0, x5 => 1);
$b = new AI::Fuzzy::Set( x5 => .3, x6 => .5, x7 => .8, x8 => 0, x9 => 1);
print "a is: " . $a->as_string . "\n";
print "b is: " . $b->as_string . "\n";
print "a is equal to b" if ($a->equal($b));
$c = $a->complement();
print "complement of a is: " . $c->as_string . "\n";
$c = $a->union($b);
print "a union b is: " . $c->as_string . "\n";
$c = $a->intersection($b);
print "a intersection b is: " . $c->as_string . "\n";
#---------- test < and > -----
$f = new AI::Fuzzy::Axis;
$f->addlabel("baby", -1, 1, 2.5);
$f->addlabel("toddler", 1, 1.5, 3.5);
$f->addlabel("little kid", 2, 7, 12);
$f->addlabel("kid", 6, 10, 14);
$f->addlabel("teenager", 12, 16, 20);
$f->addlabel("young adult", 11, 27, 35);
( run in 0.683 second using v1.01-cache-2.11-cpan-39bf76dae61 )