Thursday, July 25, 2013

Expert System Shell in Agent-Based Modeling: Demo using Zombie Sim game

Have completed my expert system shell in Java and C#, so rewrote this simple Zombie Sim game (which is originally by http://wr3cktangle.wordpress.com/2008/05/29/zombie-sim-open-source/)  using agent-based modeling, in which instead of hand-coded C# rules, expert system is used for the agent (both human and zombie) decision making.


The demo video can be found at:
http://youtu.be/PWcW6ZuLLJE

The expert system shell support major features such as forward, backward chaining, etc. Below is the code snippet for the human agent expert shell code:

RuleInferenceEngine rie = new RuleInferenceEngine();

Rule rule = new Rule("Rule 1");
rule.AddAntecedent(new IsClause("IsTargetVisible", "yes"));
rule.AddAntecedent(new IsClause("IsTargetAvailable", "yes"));
rule.AddAntecedent(new IsClause("IsCourageous", "yes"));
rule.AddAntecedent(new IsClause("IsUnderAttack", "no"));
rule.setConsequent(new IsClause("Action", "PursuitTarget"));
rie.AddRule(rule);

rule = new Rule("Rule 2");
rule.AddAntecedent(new IsClause("IsUnderAttack", "yes"));
rule.AddAntecedent(new IsClause("IsTargetAvailable", "yes"));
rule.setConsequent(new IsClause("Action", "Attack"));
rie.AddRule(rule);

rule = new Rule("Rule 3");
rule.AddAntecedent(new IsClause("IsTargetAvailable", "yes"));
rule.AddAntecedent(new IsClause("IsCourageous", "no"));
rule.AddAntecedent(new IsClause("IsUnderAttack", "no"));
rule.setConsequent(new IsClause("Action", "RunawayFromTarget"));
rie.AddRule(rule);

rule = new Rule("Rule 4");
rule.AddAntecedent(new IsClause("IsTargetAvailable", "no"));
rule.AddAntecedent(new IsClause("IsFighting", "yes"));
rule.setConsequent(new IsClause("IsFighting", "no"));
rie.AddRule(rule);
Below is the partial code the invoke the expert system for decision making in the human agent
mExpertSystem.ClearFacts();

mExpertSystem.AddFact(new IsClause("IsUnderAttack", is_under_attack ? "yes" : "no"));
mExpertSystem.AddFact(new IsClause("IsTargetVisible", mTarget != null && closest < SpotDistance ? "yes" : "no"));
mExpertSystem.AddFact(new IsClause("IsTargetAvailable", mTarget != null ? "yes" : "no"));
mExpertSystem.AddFact(new IsClause("IsCourageous", mWorld.Random.Next(9) + 1 > mCourage ? "yes" : "no"));

List<clause> unproved_conditions = new List<clause>();

Clause conclusion = mExpertSystem.Infer("Action", unproved_conditions);

if (conclusion == null)
{
 System.Console.WriteLine(mExpertSystem.Facts.ToString());
 Console.WriteLine("/////////////////////////////////////");
 return;
}

if (conclusion.getValue() == "Attack")
{
 mFighting = true;
 mMode = MentalState.Aggressive;
 mTarget.IsAttacked((int)Math.Floor(mWorld.Random.NextDouble() * mStrength));
}
Below are some images showing the ZombieSim Game rewritten using Expert System Shell and Agent-Based Modeling:

No comments:

Post a Comment