Skip to content

Commit

Permalink
hopefully fixed MeetsGoal and Heuristic function
Browse files Browse the repository at this point in the history
  • Loading branch information
caesuric committed Sep 11, 2023
1 parent 58b6879 commit 9333da1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Examples/ConsumerDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ internal static class ConsumerDemo {
/// Runs the demo.
/// </summary>
internal static void Run() {
//_ = new DefaultLogger();
_ = new DefaultLogger();
var locations = new List<string> { "home", "work", "store" };
var agent = new Agent(
name: "Consumer Agent",
state: new() {
{ "food", 0 },
{ "food", 4 },
{ "energy", 100 },
{ "money", 0 },
{ "inCar", false },
Expand Down
38 changes: 19 additions & 19 deletions MountainGoap/Internals/ActionAStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ internal ActionAStar(ActionGraph graph, ActionNode start, BaseGoal goal) {
while (frontier.Count > 0) {
var current = frontier.Dequeue();
if (current.Action != null) {
Console.WriteLine($"evaluating {current.Action.Name}");
if (current.Action.GetParameter("location") is not null) Console.WriteLine($"location: {current.Action.GetParameter("location")}");
var traceback = current;
while (CameFrom[traceback].Action != null) {
Console.WriteLine($"\t\ttraceback: {traceback.Action?.Name}");
traceback = CameFrom[traceback];
}
//Console.WriteLine($"evaluating {current.Action.Name}");
//if (current.Action.GetParameter("location") is not null) Console.WriteLine($"location: {current.Action.GetParameter("location")}");
//var traceback = current;
//while (CameFrom[traceback].Action != null) {
// Console.WriteLine($"\t\ttraceback: {traceback.Action?.Name}");
// traceback = CameFrom[traceback];
//}
}
if (MeetsGoal(current, start)) {
FinalPoint = current;
break;
}
foreach (var next in graph.Neighbors(current)) {
Console.WriteLine($"\tneighbor: {next.Action?.Name}");
if (next.Action?.GetParameter("location") is not null) Console.WriteLine($"\tlocation: {next.Action.GetParameter("location")}");
//Console.WriteLine($"\tneighbor: {next.Action?.Name}");
//if (next.Action?.GetParameter("location") is not null) Console.WriteLine($"\tlocation: {next.Action.GetParameter("location")}");
float newCost = CostSoFar[current] + next.Cost(current.State);
Console.WriteLine($"\tcost so far is {newCost}");
//Console.WriteLine($"\tcost so far is {newCost}");
if (!CostSoFar.ContainsKey(next) || newCost < CostSoFar[next]) {
CostSoFar[next] = newCost;
float priority = newCost + Heuristic(next, goal, current);
Expand Down Expand Up @@ -103,11 +103,11 @@ private static float Heuristic(ActionNode actionNode, BaseGoal goal, ActionNode
if (!actionNode.State.ContainsKey(kvp.Key)) cost += float.PositiveInfinity;
else if (!current.State.ContainsKey(kvp.Key)) cost += float.PositiveInfinity;
else if (kvp.Value.Operator == ComparisonOperator.Undefined) cost += float.PositiveInfinity;
else if (kvp.Value.Operator == ComparisonOperator.Equals && actionNode.State[kvp.Key] is object obj && obj.Equals(comparativeGoal.DesiredState[kvp.Key].Value)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.LessThan && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsLowerThan(a, b)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThan && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsHigherThan(a2, b2)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.LessThanOrEquals && actionNode.State[kvp.Key] is object a3 && current.State[kvp.Key] is object b3 && IsLowerThanOrEquals(a3, b3)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThanOrEquals && actionNode.State[kvp.Key] is object a4 && current.State[kvp.Key] is object b4 && IsHigherThanOrEquals(a4, b4)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.Equals && actionNode.State[kvp.Key] is object obj && !obj.Equals(comparativeGoal.DesiredState[kvp.Key].Value)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.LessThan && actionNode.State[kvp.Key] is object a && comparativeGoal.DesiredState[kvp.Key].Value is object b && !IsLowerThan(a, b)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThan && actionNode.State[kvp.Key] is object a2 && comparativeGoal.DesiredState[kvp.Key].Value is object b2 && !IsHigherThan(a2, b2)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.LessThanOrEquals && actionNode.State[kvp.Key] is object a3 && comparativeGoal.DesiredState[kvp.Key].Value is object b3 && !IsLowerThanOrEquals(a3, b3)) cost += valueDiff2;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThanOrEquals && actionNode.State[kvp.Key] is object a4 && comparativeGoal.DesiredState[kvp.Key].Value is object b4 && !IsHigherThanOrEquals(a4, b4)) cost += valueDiff2;
}
}
return cost;
Expand Down Expand Up @@ -155,10 +155,10 @@ private bool MeetsGoal(ActionNode actionNode, ActionNode current) {
else if (!current.State.ContainsKey(kvp.Key)) return false;
else if (kvp.Value.Operator == ComparisonOperator.Undefined) return false;
else if (kvp.Value.Operator == ComparisonOperator.Equals && actionNode.State[kvp.Key] is object obj && !obj.Equals(comparativeGoal.DesiredState[kvp.Key].Value)) return false;
else if (kvp.Value.Operator == ComparisonOperator.LessThan && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && !IsLowerThan(a, b)) return false;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThan && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && !IsHigherThan(a2, b2)) return false;
else if (kvp.Value.Operator == ComparisonOperator.LessThanOrEquals && actionNode.State[kvp.Key] is object a3 && current.State[kvp.Key] is object b3 && !IsLowerThanOrEquals(a3, b3)) return false;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThanOrEquals && actionNode.State[kvp.Key] is object a4 && current.State[kvp.Key] is object b4 && !IsHigherThanOrEquals(a4, b4)) return false;
else if (kvp.Value.Operator == ComparisonOperator.LessThan && actionNode.State[kvp.Key] is object a && comparativeGoal.DesiredState[kvp.Key].Value is object b && !IsLowerThan(a, b)) return false;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThan && actionNode.State[kvp.Key] is object a2 && comparativeGoal.DesiredState[kvp.Key].Value is object b2 && !IsHigherThan(a2, b2)) return false;
else if (kvp.Value.Operator == ComparisonOperator.LessThanOrEquals && actionNode.State[kvp.Key] is object a3 && comparativeGoal.DesiredState[kvp.Key].Value is object b3 && !IsLowerThanOrEquals(a3, b3)) return false;
else if (kvp.Value.Operator == ComparisonOperator.GreaterThanOrEquals && actionNode.State[kvp.Key] is object a4 && comparativeGoal.DesiredState[kvp.Key].Value is object b4 && !IsHigherThanOrEquals(a4, b4)) return false;
}
}
return true;
Expand Down

0 comments on commit 9333da1

Please sign in to comment.