AI-ExpertSystem-Advanced
view release on metacpan or search on metacpan
lib/AI/ExpertSystem/Advanced.pm view on Meta::CPAN
next WAIT_FOR_MORE_GOALS;
}
}
}
}
return 1;
}
=head2 B<mixed()>
As its name says, it's a mix of L<forward()> and L<backward()> algorithms, it
requires to have at least one initial fact.
The first thing it does is to run the L<forward()> algorithm (hence the need of
at least one initial fact). If the algorithm fails then the mixed algorithm
also ends unsuccessfully.
Once the first I<run> of L<forward()> algorithm happens it starts looking for
any positive inference fact, if only one is found then this ends the algorithm
with the assumption it knows what's happening.
In case no positive inference fact is found then it will start reading the
rules and creating a list of intuitive facts.
For each rule it will get a I<certainty factor> of its causes versus the
initial, inference and asked facts. In case the certainity factor is greater or
equal than L<found_factor> then all of its goals will be copied to the
intuitive facts (eg, read it as: it assumes the goals have something to do with
our first initial facts).
Once all the rules are read then it verifies if there are intuitive facts, if
no facts are found then it ends with the intuition, otherwise it will run the
L<backward()> algorithm for each one of these facts (eg, each fact will be
converted to a goal). After each I<run> of the L<backward()> algorithm it will
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;
}
use Data::Dumper;
while(1) {
# We are satisfied if only one inference fact is positive (eg, means we
# got to our result)
while(my $fact = $self->{'inference_facts'}->iterate) {
my $sign = $self->{'inference_facts'}->get_value($fact, 'sign');
if ($sign eq FACT_SIGN_POSITIVE) {
$self->{'viewer'}->debug(
"We are done, a positive fact was found"
);
return 1;
}
}
my $intuitive_facts = AI::ExpertSystem::Advanced::Dictionary->new(
stack => []);
my ($more_rules, $current_rule) = (1, undef);
while($more_rules) {
$current_rule = $self->{'knowledge_db'}->get_next_rule($current_rule);
# No more rules?
if (!defined $current_rule) {
$self->{'viewer'}->debug("We are done with all the rules, bye")
if $self->{'verbose'};
$more_rules = 0;
last;
}
# Wait, we already shot this rule?
if ($self->is_rule_shot($current_rule)) {
$self->{'viewer'}->debug("We already shot rule: $current_rule")
if $self->{'verbose'};
next;
}
my $factor = $self->get_causes_match_factor($current_rule);
if ($factor ge $self->{'found_factor'} && $factor lt 1.0) {
# Copy all of the goals (usually only one) of the current rule to
# the intuitive facts
my $goals = $self->get_goals_by_rule($current_rule);
while(my $goal = $goals->iterate_reverse) {
$intuitive_facts->append($goal,
{
name => $goal,
sign => $goals->get_value($goal, 'sign')
});
}
}
}
if ($intuitive_facts->size() eq 0) {
$self->{'viewer'}->debug("Done with intuition") if
$self->{'verbose'};
return 1;
}
$intuitive_facts->populate_iterable_array();
# now each intuitive fact will be a goal
while(my $fact = $intuitive_facts->iterate) {
( run in 1.011 second using v1.01-cache-2.11-cpan-39bf76dae61 )