AI-ExpertSystem-Advanced
view release on metacpan or search on metacpan
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
initial_facts => ['I'],
verbose => 1);
$ai->mixed();
$ai->summary();
Attributes
initial_facts
A list/set of initial facts the algorithms start using.
During the forward algorithm the task is to find a list of goals
caused by these initial facts (the only data we have in that
moment).
Lets imagine your knowledge database is about symptoms and diseases.
You need to find what diseases are caused by the symptoms of a
patient, these first symptons are the initial facts.
Initial facts as also asked and inference facts can be negative or
not enough information).
goals_to_check_dict
Very similar to goals_to_check (and indeed of initial_facts_dict).
We want to make the job easier.
It will be a dictionary made of the data of goals_to_check.
inference_facts
Inference facts are basically the core of an expert system. These
are facts that are found and copied when a set of facts (initial,
inference or asked) match with the causes of a goal.
inference_facts is a AI::ExpertSystem::Advanced::Dictionary, it will
store the name of the fact, the rule that caused these facts to be
copied to this dictionary, the sign and the algorithm that triggered
it.
knowledge_db
The object reference of the knowledge database
AI::ExpertSystem::Advanced is using.
});
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
initial_facts => ['F', 'J']);
$ai->forward();
$ai->summary();
The forward chaining algorithm is one of the main methods used in Expert
Systems. It starts with a set of variables (known as initial facts) and
reads the available rules.
It will be reading rule by rule and for each one it will compare its
causes with the initial, inference and asked facts. If all of these
causes are in the facts then the rule will be shoot and all of its goals
will be copied/converted to inference facts and will restart reading
from the first rule.
backward()
use AI::ExpertSystem::Advanced;
filename => 'examples/knowledge_db_one.yaml'
});
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
goals_to_check => ['J']);
$ai->backward();
$ai->summary();
The backward algorithm starts with a set of *assumed* goals (facts). It
will start reading goal by goal. For each goal it will check if it
exists in the initial, inference and asked facts (see
is_goal_in_our_facts()) for more information).
* If the goal exist then it will be removed from the dictionary, it
will also verify if there are more visited rules to shoot.
If there are still more visited rules to shoot then it will check
from what rule the goal comes from, if it was copied from a rule
then this data will exist. With this information then it will see
just one is found then the algorithm ends.
At the end (if there are still no positive inference facts) it will run
the forward() algorithm and restart (by looking again for any positive
inference fact).
A good example to understand how this algorithm is useful is: imagine
you are a doctor and know some of the symptoms of a patient. Probably
with the first symptoms you have you can get to a positive conclusion
(eg that a patient has *X* disease). However in case there's still no
clue, then a set of questions (done by the call of backward()) of
symptons related to the initial symptoms will be asked to the user. For
example, we know that that the patient has a headache but that doesn't
give us any positive answer, what if the patient has flu or another
disease? Then a set of these *related* symptons will be asked to the
user.
summary($return)
The main purpose of any expert system is the ability to explain: what is
happening, how it got to a result, what assumption(s) it required to
make, the fatcs that were excluded and the ones that were used.
This method will use the viewer (or return the result) in YAML format of
all the rules that were shot. It will explain how it got to each one of
the causes so a better explanation can be done by the viewer.
inc/Module/Install.pm view on Meta::CPAN
#line 1
package Module::Install;
# For any maintainers:
# The load order for Module::Install is a bit magic.
# It goes something like this...
#
# IF ( host has Module::Install installed, creating author mode ) {
# 1. Makefile.PL calls "use inc::Module::Install"
# 2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install
# 3. The installed version of inc::Module::Install loads
# 4. inc::Module::Install calls "require Module::Install"
# 5. The ./inc/ version of Module::Install loads
# } ELSE {
# 1. Makefile.PL calls "use inc::Module::Install"
# 2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install
# 3. The ./inc/ version of Module::Install loads
# }
use 5.005;
use strict 'vars';
use vars qw{$VERSION $MAIN};
BEGIN {
# All Module::Install core packages now require synchronised versions.
# This will be used to ensure we don't accidentally load old or
inc/Module/Install.pm view on Meta::CPAN
*inc::Module::Install::VERSION = *VERSION;
@inc::Module::Install::ISA = __PACKAGE__;
}
# Whether or not inc::Module::Install is actually loaded, the
# $INC{inc/Module/Install.pm} is what will still get set as long as
# the caller loaded module this in the documented manner.
# If not set, the caller may NOT have loaded the bundled version, and thus
# they may not have a MI version that works with the Makefile.PL. This would
# result in false errors or unexpected behaviour. And we don't want that.
my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
unless ( $INC{$file} ) { die <<"END_DIE" }
Please invoke ${\__PACKAGE__} with:
use inc::${\__PACKAGE__};
not:
inc/Module/Install/Metadata.pm view on Meta::CPAN
}
return 1;
}
sub all_from {
my ( $self, $file ) = @_;
unless ( defined($file) ) {
my $name = $self->name or die(
"all_from called with no args without setting name() first"
);
$file = join('/', 'lib', split(/-/, $name)) . '.pm';
$file =~ s{.*/}{} unless -e $file;
unless ( -e $file ) {
die("all_from cannot find $file from $name");
}
}
unless ( -f $file ) {
die("The path '$file' does not exist, or is not a file");
}
inc/Module/Install/Metadata.pm view on Meta::CPAN
return $self->{values}->{no_index};
}
sub read {
my $self = shift;
$self->include_deps( 'YAML::Tiny', 0 );
require YAML::Tiny;
my $data = YAML::Tiny::LoadFile('META.yml');
# Call methods explicitly in case user has already set some values.
while ( my ( $key, $value ) = each %$data ) {
next unless $self->can($key);
if ( ref $value eq 'HASH' ) {
while ( my ( $module, $version ) = each %$value ) {
$self->can($key)->($self, $module => $version );
}
} else {
$self->can($key)->($self, $value);
}
}
lib/AI/ExpertSystem/Advanced.pm view on Meta::CPAN
use YAML::Syck qw(Dump);
our $VERSION = '0.03';
=head1 Attributes
=over 4
=item B<initial_facts>
A list/set of initial facts the algorithms start using.
During the forward algorithm the task is to find a list of goals caused
by these initial facts (the only data we have in that moment).
Lets imagine your knowledge database is about symptoms and diseases. You need
to find what diseases are caused by the symptoms of a patient, these first
symptons are the initial facts.
Initial facts as also asked and inference facts can be negative or positive. By
default the initial facts are positive.
lib/AI/ExpertSystem/Advanced.pm view on Meta::CPAN
It will be a dictionary made of the data of L<goals_to_check>.
=cut
has 'goals_to_check_dict' => (
is => 'ro',
isa => 'AI::ExpertSystem::Advanced::Dictionary');
=item B<inference_facts>
Inference facts are basically the core of an expert system. These are facts
that are found and copied when a set of facts (initial, inference or asked)
match with the causes of a goal.
L<inference_facts> is a L<AI::ExpertSystem::Advanced::Dictionary>, it will
store the name of the fact, the rule that caused these facts to be copied to
this dictionary, the sign and the algorithm that triggered it.
=cut
has 'inference_facts' => (
is => 'ro',
isa => 'AI::ExpertSystem::Advanced::Dictionary');
lib/AI/ExpertSystem/Advanced.pm view on Meta::CPAN
});
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
initial_facts => ['F', 'J']);
$ai->forward();
$ai->summary();
The forward chaining algorithm is one of the main methods used in Expert
Systems. It starts with a set of variables (known as initial facts) and reads
the available rules.
It will be reading rule by rule and for each one it will compare its causes
with the initial, inference and asked facts. If all of these causes are in the
facts then the rule will be shoot and all of its goals will be copied/converted
to inference facts and will restart reading from the first rule.
=cut
sub forward {
my ($self) = @_;
lib/AI/ExpertSystem/Advanced.pm view on Meta::CPAN
filename => 'examples/knowledge_db_one.yaml'
});
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
goals_to_check => ['J']);
$ai->backward();
$ai->summary();
The backward algorithm starts with a set of I<assumed> goals (facts). It will
start reading goal by goal. For each goal it will check if it exists in the
initial, inference and asked facts (see L<is_goal_in_our_facts()>) for more
information).
=over 4
=item *
If the goal exist then it will be removed from the dictionary, it will also
verify if there are more visited rules to shoot.
lib/AI/ExpertSystem/Advanced.pm view on Meta::CPAN
verify for any positive inference fact, if just one is found then the algorithm
ends.
At the end (if there are still no positive inference facts) it will run the
L<forward()> algorithm and restart (by looking again for any positive inference
fact).
A good example to understand how this algorithm is useful is: imagine you are
a doctor and know some of the symptoms of a patient. Probably with the first
symptoms you have you can get to a positive conclusion (eg that a patient has
I<X> disease). However in case there's still no clue, then a set of questions
(done by the call of L<backward()>) of symptons related to the initial symptoms
will be asked to the user. For example, we know that that the patient has a
headache but that doesn't give us any positive answer, what if the patient has
flu or another disease? Then a set of these I<related> symptons will be asked
to the user.
=cut
sub mixed {
my ($self) = @_;
if (!$self->forward()) {
$self->{'viewer'}->print_error("The first execution of forward failed");
return 0;
}
( run in 0.374 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )