Skip to content

Commit

Permalink
Merge pull request #23 from benhawkinsicon/fix-extreme-goal-evaluation
Browse files Browse the repository at this point in the history
fail extreme goal check when no arithmetic change
  • Loading branch information
caesuric authored Dec 23, 2023
2 parents 38156ce + b119931 commit 722502e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
24 changes: 23 additions & 1 deletion Examples/ExtremeHappinessIncrementer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static void Run() {
name: "Happiness Agent",
state: new() {
{ "happiness", 0 },
{ "health", false },
},
goals: new() {
new ExtremeGoal(
Expand All @@ -31,6 +32,9 @@ internal static void Run() {
new(
name: "Seek Happiness",
executor: SeekHappinessAction,
preconditions: new() {
{ "health", true }
},
arithmeticPostconditions: new() {
{
"happiness",
Expand All @@ -41,13 +45,26 @@ internal static void Run() {
new(
name: "Seek Greater Happiness",
executor: SeekGreaterHappinessAction,
preconditions: new() {
{ "health", true }
},
arithmeticPostconditions: new() {
{
"happiness",
2
}
}
)
),
new(
name: "Seek Health",
executor: SeekHealth,
postconditions: new() {
{
"health",
true
}
}
),
}
);
while (agent.State["happiness"] is int happiness && happiness != 10) {
Expand All @@ -65,5 +82,10 @@ private static ExecutionStatus SeekGreaterHappinessAction(Agent agent, Action ac
Console.WriteLine("Seeking even greater happiness.");
return ExecutionStatus.Succeeded;
}

private static ExecutionStatus SeekHealth(Agent agent, Action action) {
Console.WriteLine("Seeking health.");
return ExecutionStatus.Succeeded;
}
}
}
4 changes: 2 additions & 2 deletions MountainGoap/Internals/ActionAStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ private static float Heuristic(ActionNode actionNode, BaseGoal goal, ActionNode
if (actionNode.State.ContainsKey(kvp.Key) && extremeGoal.DesiredState.ContainsKey(kvp.Key)) valueDiff = Convert.ToSingle(actionNode.State[kvp.Key]) - Convert.ToSingle(current.State[kvp.Key]);
if (!actionNode.State.ContainsKey(kvp.Key)) cost += float.PositiveInfinity;
else if (!current.State.ContainsKey(kvp.Key)) cost += float.PositiveInfinity;
else if (kvp.Value && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsHigherThan(a, b)) cost += 1f / valueDiff;
else if (!kvp.Value && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsLowerThan(a2, b2)) cost += 1f / (1 - valueDiff);
else if (kvp.Value && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsLowerThanOrEquals(a, b)) cost += 1f / valueDiff;
else if (!kvp.Value && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsHigherThanOrEquals(a2, b2)) cost += 1f / (1 - valueDiff);
}
}
else if (goal is ComparativeGoal comparativeGoal) {
Expand Down
6 changes: 3 additions & 3 deletions MountainGoap/Internals/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ internal static bool MeetsGoal(BaseGoal goal, ActionNode actionNode, ActionNode
foreach (var kvp in extremeGoal.DesiredState) {
if (!actionNode.State.ContainsKey(kvp.Key)) return false;
else if (!current.State.ContainsKey(kvp.Key)) return false;
else if (kvp.Value && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsLowerThan(a, b)) return false;
else if (!kvp.Value && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsHigherThan(a2, b2)) return false;
else if (kvp.Value && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsLowerThanOrEquals(a, b)) return false;
else if (!kvp.Value && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsHigherThanOrEquals(a2, b2)) return false;
}
}
else if (goal is ComparativeGoal comparativeGoal) {
Expand All @@ -117,4 +117,4 @@ internal static bool MeetsGoal(BaseGoal goal, ActionNode actionNode, ActionNode
return true;
}
}
}
}

0 comments on commit 722502e

Please sign in to comment.