AI-Fuzzy

 view release on metacpan or  search on metacpan

Fuzzy.pm  view on Meta::CPAN

  $f->addlabel("little kid",   2,   7,  12);
  $f->addlabel("kid",          6,  10,  14);
  $f->addlabel("teenager",    12,  16,  20);
  $f->addlabel("young adult", 18,  27,  35);
  $f->addlabel("adult",       25,  50,  75);
  $f->addlabel("senior",      60,  80, 110);
  $f->addlabel("relic",      100, 150, 200);


  for (my $x = 0; $x<50; $x+=4) {
      print "$x years old => " . $f->labelvalue($x) . "\n";
  }

  $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));
  
  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

Fuzzy.pm  view on Meta::CPAN

    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"; 
    prints:
	b is: x8/0, x5/0.3, x6/0.5, x7/0.8, x9/1

=head2 Fuzzy Labels

A Fuzzy::Label label has four attributes: the text of the label (it
can be any scalar, really), and three numbers: low, mid, high if you
imagine a cartesian plane (remember graph paper in algebra?)  of all
possible values, the label applies to a particular range.  the graph
might look something like this:

Fuzzy.pm  view on Meta::CPAN


note that labels can overlap, and that the
mid number isn't always in the exact center, so the slope
of the two sides may vary...

$fl = new AI::Fuzzy::Label ( "hot", 77, 80, 100 );
$fx = new AI::Fuzzy::Label ( "cold", 0, 10, 200 );
    # what I consider hot. :) (in Farenheit, of course!)

if ( $fl->lessthan($fx) ) {
    print "the laws of nature have changed\n";
}

# there is a lessthan, greaterthan, lessequal, greaterequal, and between 
#  that functions as above or using <,>,<=,>=

$a = $fl->applicability($value);
    # $a is now the degree to which this label applies to $value

=head2 Fuzzy Axis

README  view on Meta::CPAN

      $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", 18,  27,  35);
      $f->addlabel("adult",       25,  50,  75);
      $f->addlabel("senior",      60,  80, 110);
      $f->addlabel("relic",      100, 150, 200);

      for (my $x = 0; $x<50; $x+=4) {
          print "$x years old => " . $f->label($x) . "\n";
      }

      $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"; 

    __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

README  view on Meta::CPAN

        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"; 
        prints:
            b is: x8/0, x5/0.3, x6/0.5, x7/0.8, x9/1

  Fuzzy Labels

    A Fuzzy::Label label has four attributes: the text of the label (it can
    be any scalar, really), and three numbers: low, mid, high if you imagine
    a cartesian plane (remember graph paper in algebra?) of all possible
    values, the label applies to a particular range. the graph might look
    something like this:

demo/cpu.pl  view on Meta::CPAN

   open (STAT, "vmstat -n 1 $count |") or die ("can't find vmstat"); 
 
   my $cpu = <STAT>;    # headers
   $cpu = <STAT>;	    # headers

    for (1 .. $count ) {
	$cpu = <STAT>;       # read data
  	$cpu =~ s/.* (\d+)$/$1/;

	chomp $cpu;
	print "the cpu is: $cpu " . $f->label($cpu) . "\n";
    }
    close STAT;
    sleep 1;
}

demo/fuzz.pl  view on Meta::CPAN

#!/usr/bin/perl
use lib qw(blib/arch blib/lib ../blib/arch ../blib/lib);
use strict;
use warnings;
use AI::Fuzzy;

my $f = new AI::Fuzzy::Axis;
my $l = new AI::Fuzzy::Label("toddler",      1, 1.5, 3.5);

#print "$l\n";

$f->addlabel("baby",        -1,   1, 2.5);
$f->addlabel($l);
$f->addlabel("little kid",   2,   7,  12);
$f->addlabel("kid",          6,  10,  14);
$f->addlabel("teenager",    12,  16,  20);
$f->addlabel("young adult", 18,  27,  35);
$f->addlabel("adult",       25,  50,  75);
$f->addlabel("senior",      60,  80, 110);
$f->addlabel("relic",      100, 150, 200);

 for (my $x = 0; $x<50; $x+=4) {
     print "$x years old => " . $f->labelvalue($x) . "\n";
 }

$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));

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

    foreach my $member ($self->members) {
	push (@members, "$member/" . $self->membership($member) );
    }

    return join(', ', @members);
}

sub float_equal {
    my ($A, $B, $dp) = @_;

#    print  sprintf("%.${dp}g", $A). " eq " . sprintf("%.${dp}g", $B) . "\n";
    return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
}

1;

test.pl  view on Meta::CPAN

$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);

test.pl  view on Meta::CPAN

$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);
$f->addlabel("adult",       25,  50,  75);
$f->addlabel("senior",      60,  80, 110);
$f->addlabel("relic",      100, 150, 200);

my ($a, $b) = ($f->label("baby"), $f->label("toddler") );

if ($a->lessthan($b) ) {
    print "baby < toddler\n";
} else {
    print "baby !< toddler\n";
}

($a, $b) = ($f->label("baby"), $f->label("toddler") );
if ($a->greaterthan($b) ) {
    print "baby > toddler\n";
} else {
    print "baby !> toddler\n";
}

($a, $b) = ($f->label("relic"), $f->label("toddler") );
($a->greaterthan($b) ) ? ( print "relic > toddler\n" ) : ( print "relic !> toddler\n" );

# these are a strange case ...
($f->greaterthan("teenager", "young adult") ) ? 
    ( print "teenager > young adult\n" ) : ( print "teenager !> young adult\n" );
($f->lessthan("teenager", "young adult") ) ? 
    ( print "teenager < young adult\n" ) : ( print "teenager !< young adult\n" );

($f->between("toddler", "little kid", "baby") ) ? 
    ( print "toddler is between little kid and baby\n" ) : ( print "toddler is not between little kid and baby\n" );
($f->between("adult", "little kid", "baby") ) ? 
    ( print "adult is between little kid and baby\n" ) : ( print "adult is not between little kid and baby\n" );



( run in 0.907 second using v1.01-cache-2.11-cpan-de7293f3b23 )