Skip to content

Commit

Permalink
tried to correct error with cost evaluation in Heuristic
Browse files Browse the repository at this point in the history
  • Loading branch information
caesuric committed Sep 11, 2023
1 parent 8cfefd1 commit 79eb889
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 27 deletions.
88 changes: 72 additions & 16 deletions Examples/ConsumerDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static void Run() {
var agent = new Agent(
name: "Consumer Agent",
state: new() {
{ "food", 4 },
{ "food", 0 },
{ "energy", 100 },
{ "money", 0 },
{ "inCar", false },
Expand All @@ -36,22 +36,25 @@ internal static void Run() {
// }
// }
// })
//new Goal(
// name: "Get 5 food",
// desiredState: new() {
// { "food", 5 }
// })
new ExtremeGoal(
name: "Get food",
new Goal(
name: "Get 5 food",
desiredState: new() {
{ "food", true }
{ "food", 1 }
})
//new ExtremeGoal(
// name: "Get food",
// desiredState: new() {
// { "food", true }
// })
},
actions: new() {
new(
name: "Walk",
cost: 10,
cost: 6f,
executor: GenericExecutor,
preconditions: new() {
{ "inCar", false }
},
permutationSelectors: new() {
{ "location", PermutationSelectorGenerators.SelectFromCollection(locations) }
},
Expand All @@ -67,7 +70,7 @@ internal static void Run() {
),
new(
name: "Drive",
cost: 1,
cost: 1f,
preconditions: new() {
{ "inCar", true }
},
Expand All @@ -85,9 +88,43 @@ internal static void Run() {
{ "location", "location" }
}
),
//new(
// name: "Drive to work",
// cost: 2f,
// preconditions: new() {
// { "inCar", true }
// },
// comparativePreconditions: new() {
// { "energy", new() { Operator = ComparisonOperator.GreaterThan, Value = 0 } }
// },
// executor: GenericExecutor,
// arithmeticPostconditions: new() {
// { "energy", -1 }
// },
// postconditions: new() {
// { "location", "work" }
// }
//),
//new(
// name: "Drive to store",
// cost: 2f,
// preconditions: new() {
// { "inCar", true }
// },
// comparativePreconditions: new() {
// { "energy", new() { Operator = ComparisonOperator.GreaterThan, Value = 0 } }
// },
// executor: GenericExecutor,
// arithmeticPostconditions: new() {
// { "energy", -1 }
// },
// postconditions: new() {
// { "location", "store" }
// }
//),
new(
name: "Get in Car",
cost: 1f,
name: "Get in car",
cost: 0.1f,
preconditions: new() {
{ "inCar", false }
},
Expand All @@ -102,11 +139,29 @@ internal static void Run() {
},
executor: GenericExecutor
),
new(
name: "Get out of car",
cost: 0.1f,
preconditions: new() {
{ "inCar", true }
},
comparativePreconditions: new() {
{ "energy", new() { Operator = ComparisonOperator.GreaterThan, Value = 0 } }
},
postconditions: new() {
{ "inCar", false }
},
arithmeticPostconditions: new() {
{ "energy", -1 }
},
executor: GenericExecutor
),
new(
name: "Work",
cost: 1f,
cost: 6f,
preconditions: new() {
{ "location", "work" },
{ "inCar", false }
},
comparativePreconditions: new() {
{ "energy", new() { Operator = ComparisonOperator.GreaterThan, Value = 0 } }
Expand All @@ -119,9 +174,10 @@ internal static void Run() {
),
new(
name: "Shop",
cost: 1f,
cost: 1.2f,
preconditions: new() {
{ "location", "store" }
{ "location", "store" },
{ "inCar", false }
},
comparativePreconditions: new() {
{ "energy", new() { Operator = ComparisonOperator.GreaterThan, Value = 0 } },
Expand Down
21 changes: 10 additions & 11 deletions MountainGoap/Internals/ActionAStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ private static float Heuristic(ActionNode actionNode, BaseGoal goal, ActionNode
var cost = 0f;
if (goal is Goal normalGoal) {
normalGoal.DesiredState.Select(kvp => kvp.Key).ToList().ForEach(key => {
if (!actionNode.State.ContainsKey(key)) cost++;
else if (actionNode.State[key] == null && actionNode.State[key] != normalGoal.DesiredState[key]) cost++;
else if (actionNode.State[key] is object obj && !obj.Equals(normalGoal.DesiredState[key])) cost++;
if (!actionNode.State.ContainsKey(key)) cost += actionNode.Cost(actionNode.State);
else if (actionNode.State[key] == null && actionNode.State[key] != normalGoal.DesiredState[key] ) cost += actionNode.Cost(actionNode.State);
else if (actionNode.State[key] is object obj && !obj.Equals(normalGoal.DesiredState[key])) cost += actionNode.Cost(actionNode.State);
});
}
else if (goal is ExtremeGoal extremeGoal) {
Expand All @@ -78,20 +78,19 @@ 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 && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsHigherThan(a, b)) cost++;
else if (!kvp.Value && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsLowerThan(a2, b2)) cost++;
else if (kvp.Value && actionNode.State[kvp.Key] is object a && current.State[kvp.Key] is object b && IsHigherThan(a, b)) cost += actionNode.Cost(actionNode.State);
else if (!kvp.Value && actionNode.State[kvp.Key] is object a2 && current.State[kvp.Key] is object b2 && IsLowerThan(a2, b2)) cost += actionNode.Cost(actionNode.State);
}
}
else if (goal is ComparativeGoal comparativeGoal) {
foreach (var kvp in comparativeGoal.DesiredState) {
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++;
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++;
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++;
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++;
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++;
else if (kvp.Value.Operator == ComparisonOperator.Equals && actionNode.State[kvp.Key] is object obj && obj.Equals(comparativeGoal.DesiredState[kvp.Key].Value)) cost += actionNode.Cost(actionNode.State);
else if (kvp.Value.Operator == ComparisonOperator.LessThan && actionNode.State[kvp.Key] is object a && comparativeGoal.DesiredState[kvp.Key] is object b && IsLowerThan(a, b)) cost += actionNode.Cost(actionNode.State);
else if (kvp.Value.Operator == ComparisonOperator.GreaterThan && actionNode.State[kvp.Key] is object a2 && comparativeGoal.DesiredState[kvp.Key] is object b2 && IsHigherThan(a2, b2)) cost += actionNode.Cost(actionNode.State);
else if (kvp.Value.Operator == ComparisonOperator.LessThanOrEquals && actionNode.State[kvp.Key] is object a3 && comparativeGoal.DesiredState[kvp.Key] is object b3 && IsLowerThanOrEquals(a3, b3)) cost += actionNode.Cost(actionNode.State);
else if (kvp.Value.Operator == ComparisonOperator.GreaterThanOrEquals && actionNode.State[kvp.Key] is object a4 && comparativeGoal.DesiredState[kvp.Key] is object b4 && IsHigherThanOrEquals(a4, b4)) cost += actionNode.Cost(actionNode.State);
}
}
return cost;
Expand Down

0 comments on commit 79eb889

Please sign in to comment.