IF “quality” IS bad AND “speed” IS slow THEN “award” IS minimum IF “quality” IS good AND “speed” IS slow THEN “award” IS small IF “quality” IS ok AND “speed” IS fast THEN “award” IS good IF “quality” IS excellent AND “speed” IS fast THEN “award” IS excellent #### use AI::FuzzyInference; #### my $fis = AI::FuzzyInference->new; #### $fis->inVar('quality', 0, 10, bad => [ 0, 1, 2, 1, 4, 0 ], ok => [ 2, 0, 4, 1, 6, 0 ], good => [ 4, 0, 6, 1, 8, 0 ], excelent => [ 6, 0, 8, 1, 10, 1 ], ); #### | 1.00|BBBB | B 0.50| B | B |________B________________ 0 2 4 6 8 10 #### $fis->outVar('award', 0, 100, minimum => [ 0,1, 20,1, 40,0 ], small => [ 20,0, 40,1, 60,0 ], good => [ 40,0, 60,1, 80,0 ], excellent => [ 60,0, 80,1, 100,1 ], ); #### | 1.00|MMMM | M 0.50| M | M |_______M_____________________ 0 20 40 60 80 100 #### $fis->addRule( 'quality=bad & speed=slow' => 'award=minimum', 'quality=ok & speed=slow' => 'award=minimum', 'quality=good & speed=slow' => 'award=small', 'quality=excellent & speed=slow' => 'award=small', #### $fis->compute( quality => $quality_of_service, speed => $speed_of_service); #### my $award = $fis->value('award'); #### $fis->operation( '&' ); #for the AND operation $fis->operation( '|' ); #for the OR operation $fis->implication(); #for the implication function $fis->aggregation(); #for the aggregation function $fis->defuzzification(); #for the defuzzification #### $fis->operation( '&', 'difference'); $fis->operation( '|', 'sum'); $fis->implication( 'scale' ); #### #!/usr/bin/perl use warnings; use strict; use AI::FuzzyInference; ############## # problem: to assign an award to a deserving Monk ############## #my $fis = new AI::FuzzyInference; # this indirect object # notation was replaced # for the direct # notation below as # suggested by ovid my $fis = AI::FuzzyInference->new; ############## # input variables: inVar # 'service' relates to the quality of the nodes written by the Monk # 'speed' relates to how fast the Monk offers a hand to a fellow # monk in need ############## $fis->inVar('quality', 0, 10, bad => [ 0, 1, 2, 1, 4, 0 ], ok => [ 2, 0, 4, 1, 6, 0 ], good => [ 4, 0, 6, 1, 8, 0 ], excellent => [ 6, 0, 8, 1,10, 1 ], ); $fis->inVar('speed', 0, 10, slow => [ 0, 1, 2, 1, 4, 0 ], regular => [ 2, 0, 4, 1, 6, 0 ], fast => [ 4, 0, 6, 1, 8, 0 ], fastest => [ 6, 0, 8, 1,10, 1 ], ); ############## # output variable: outVar # 'award' relates to the quality of the award to be given ############## $fis->outVar('award', 0, 100, minimum => [ 0,1, 20,1, 40,0 ], small => [ 20,0, 40,1, 60,0 ], good => [ 40,0, 60,1, 80,0 ], excellent => [ 60,0, 80,1, 100,1 ], ); ############## # Adding the rules: addRule ############## $fis->addRule( 'quality=bad & speed=slow' => 'award=minimum', 'quality=ok & speed=slow' => 'award=minimum', 'quality=good & speed=slow' => 'award=small', 'quality=excellent & speed=slow' => 'award=small', 'quality=bad & speed=regular' => 'award=minimum', 'quality=ok & speed=regular' => 'award=small', 'quality=good & speed=regular' => 'award=small', 'quality=excellent & speed=regular' => 'award=excellent', 'quality=bad & speed=fast' => 'award=small', 'quality=ok & speed=fast' => 'award=good', 'quality=good & speed=fast' => 'award=good', 'quality=excellent & speed=fast' => 'award=excellent', 'quality=bad & speed=fastest' => 'award=small', 'quality=ok & speed=fastest' => 'award=good', 'quality=good & speed=fastest' => 'award=excellent', 'quality=excellent & speed=fastest' => 'award=excellent', ); ############## # Requesting information from the user: # $quality_of_service, $speed_of_service ############## my ($quality_of_service, $speed_of_service); print "Please, enter the quality of service [0, 10]: "; chomp ($quality_of_service = ); print "Please, enter the speed of service [0, 10]: "; chomp($speed_of_service = ); ############## # computing the "award" given the "quality_of_service" # and the "speed_of_service" as entered by the user # # Note: we are using the default values for: # & (min), # | (max), # implication (clip), # aggregation (max), and # defuzzification (centroid) ############## $fis->compute( quality => $quality_of_service, speed => $speed_of_service); ############## # reading and showing the value of the 'award' ############## my $award = $fis->value('award'); print "\nAward (quality = $quality_of_service, speed = $speed_of_service) [0, 100]: $award\n\n"; ############## # showing the values of: # &, |, implication, aggregation, defuzzification ############## print "values of &, |, implication, aggregation, and defuzzification\n"; print "& = ", $fis->operation( '&' ),"\n"; print "| = ", $fis->operation( '|' ),"\n"; print "implication = ", $fis->implication(), "\n"; print "aggregation = ", $fis->aggregation(), "\n"; print "defuzzification = ", $fis->defuzzification(), "\n\n"; ############## # modifying the values of | and implication ############## $fis->operation( '|', 'sum'); $fis->implication( 'scale' ); ############## # showing the new values of: | and implication ############## print "new values of | and implication\n"; print "| = ", $fis->operation( '|' ),"\n"; print "implication = ", $fis->implication(), "\n";