AI-ExpertSystem-Advanced
view release on metacpan or search on metacpan
get_causes_match_factor($rule)
Similar to compare_causes_with_facts() but with the difference that it
will count the "match factor" of each matched cause and return the total
of this weight.
The match factor is used by the mixed() algorithm and is useful to know
if a certain rule should be shoot or not even if not all of the causes
exist in our facts.
The *match factor* is calculated in two ways:
* Will do a sum of the weight for each matched cause. Please note that
if only one cause of a rule has a specified weight then the
remaining causes will default to the total weight minus 1 and then
divided with the total number of causes (matched or not) that don't
have a weight.
* If no weight is found with all the causes of the given rule, then
the total number of matches will be divided by the total number of
causes.
is_goal_in_our_facts($goal)
Checks if the given $goal is in:
1 The initial facts
2 The inference facts
3 The asked facts
remove_last_ivisited_rule()
Removes the last visited rule and return its number.
visit_rule($rule, $total_causes)
Adds the given $rule to the end of the visited_rules.
copy_to_goals_to_check($rule, $facts)
Copies a list of facts (usually a list of causes of a rule) to
goals_to_check_dict.
The rule ID of the goals that are being copied is also stored in the
hahs.
ask_about($fact)
Uses viewer to ask the user for the existence of the given $fact.
The valid answers are:
+ or FACT_SIGN_POSITIVE
In case user knows of it.
- or FACT_SIGN_NEGATIVE
In case user doesn't knows of it.
~ or FACT_SIGN_UNSURE
In case user doesn't have any clue about the given fact.
get_rule_by_goal($goal)
Looks in the knowledge_db for the rule that has the given goal. If a
rule is found its number is returned, otherwise undef.
forward()
use AI::ExpertSystem::Advanced;
use AI::ExpertSystem::Advanced::KnowledgeDB::Factory;
my $yaml_kdb = AI::ExpertSystem::Advanced::KnowledgeDB::Factory->new('yaml',
{
filename => 'examples/knowledge_db_one.yaml'
});
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;
use AI::ExpertSystem::Advanced::KnowledgeDB::Factory;
my $yaml_kdb = AI::ExpertSystem::Advanced::KnowledgeDB::Factory->new('yaml',
{
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
how many of the causes of this given rule are still in the
goals_to_check_dict.
In case there are still causes of this rule in goals_to_check_dict
then the amount of causes pending will be reduced by one. Otherwise
(if the amount is 0) then the rule of this last removed goal will be
( run in 0.993 second using v1.01-cache-2.11-cpan-d80b1682f3f )