AI-ExpertSystem-Advanced
view release on metacpan or search on metacpan
303132333435363738394041424344454647484950
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
949596979899100101102103104105106107108109110111112113114
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.
313314315316317318319320321322323324325326327328329330331332333
});
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()
338339340341342343344345346347348349350351352353354355356357358
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
405406407408409410411412413414415416417418419420421422423424425426427428429
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.
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
1234567891011121314151617181920212223242526#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;
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
3637383940414243444546474849505152535455565758
*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
:
not:
inc/Module/Install/Metadata.pm view on Meta::CPAN
211212213214215216217218219220221222223224225226227228229230231
}
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
318319320321322323324325326327328329330331332333334335336337338
return
$self
->{
values
}->{no_index};
}
sub
read
{
my
$self
=
shift
;
$self
->include_deps(
'YAML::Tiny'
, 0 );
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
656667686970717273747576777879808182838485our
$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
152153154155156157158159160161162163164165166167168169170171172It 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
719720721722723724725726727728729730731732733734735736737738739
});
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
790791792793794795796797798799800801802803804805806807808809810
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
97797897998098198298398498598698798898999099199299399499599699799899910001001verify
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 1.386 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )