luxkun / ReGoap

Generic C# GOAP (Goal Oriented Action Planning) library with Unity3d examples
Apache License 2.0
1.03k stars 147 forks source link

Another Planning bug #25

Closed TMPxyz closed 6 years ago

TMPxyz commented 6 years ago

The planner might make wrong plan on some special conditions. Here's a unit-test to reproduce it.

The one goal is "GatherAppleAndPeach"

With special cost setting, the planner might give wrong plan with steps:

0) GatherPeach 1) Goto 2) GatherApple

        [Test]
        public void TestGatherGotoGather()
        {
            var gameObject = new GameObject();

            ReGoapTestsHelper.GetCustomAction(gameObject, "GatherApple",
                new Dictionary<string, object> { { "At", "Farm" } },
                new Dictionary<string, object> { { "hasApple", true } }, 1);
            ReGoapTestsHelper.GetCustomAction(gameObject, "GatherPeach",
                new Dictionary<string, object> { { "At", "Farm" } },
                new Dictionary<string, object> { { "hasPeach", true } }, 2);
            ReGoapTestsHelper.GetCustomAction(gameObject, "Goto",
                new Dictionary<string, object> { },
                new Dictionary<string, object> { { "At", "Farm" } }, 10);

            var theGoal = ReGoapTestsHelper.GetCustomGoal(gameObject, "GatherAll",
                new Dictionary<string, object> { { "hasApple", true }, { "hasPeach", true } });

            var memory = gameObject.AddComponent<ReGoapTestMemory>();
            memory.Init();

            var agent = gameObject.AddComponent<ReGoapTestAgent>();
            agent.Init();

            var plan = GetPlanner().Plan(agent, null, null, null);

            Assert.That(plan, Is.EqualTo(theGoal));
            // validate plan actions
            ReGoapTestsHelper.ApplyAndValidatePlan(plan, memory);
        }
TestGatherGotoGather (0.051s)
---
Expected: True
  But was:  False
---
at ReGoap.Unity.Editor.Test.ReGoapTestsHelper.ApplyAndValidatePlan (ReGoap.Core.IReGoapGoal`2[T,W] plan, ReGoap.Unity.Test.ReGoapTestMemory memory) [0x0003d] in E:\works\programs\GameProjects\TestGround\TestReGOAP\Assets\ReGoap\Unity\Editor\Test\ReGoapTestsHelper.cs:48
at ReGoap.Unity.Editor.Test.ReGoapTests.TestGatherGotoGather () [0x0011d] in E:\works\programs\GameProjects\TestGround\TestReGOAP\Assets\ReGoap\Unity\Editor\Test\ReGoapTests.cs:70
---
[ReGoalPlanner] Starting planning calculation for agent: GoapAgent('')
[Astar] Success iterations: 9
[ReGoapPlanner] Calculated plan for goal 'GoapGoal('GatherAll')', plan length: 3
[ReGoapPlanner] 0) GoapAction('GatherPeach')
[ReGoapPlanner] 1) GoapAction('Goto')
[ReGoapPlanner] 2) GoapAction('GatherApple')

Here is a plan making graph made with graphviz; debug

luxkun commented 6 years ago

Again, thanks for the report, this one has also been fixed with the last two commits.

About graphviz, it looks like a very nice addon on the planner, do you have a working code change with it? I would be interested in a pull request if so 👍

TMPxyz commented 6 years ago

About graphviz, it looks like a very nice addon on the planner, do you have a working code change with it? I would be interested in a pull request if so 👍

Sure, I'll send a pull request to you soon.

But it is quite rough in the sense of user-friendliness. The code would just write the plan log into the .dot files, and it requires users to pre-install graphviz and use gvedit.exe/dot.exe to transform the log files into pictures.

You might need to edit it a bit to make it better fit the need.