public static void WritePlansWithBest(List <List <IGOAPReadOnlyAction> > plans, IGOAPCostComparer comparer) { Console.WriteLine(); var bestCost = comparer.BadCost; foreach (var plan in plans) { IGOAPCost fullCost = comparer.ZeroCost; foreach (var action in plan) { fullCost = fullCost.GetSumWith(action.Cost); } int value = comparer.Compare(bestCost, fullCost); if (value < 1) { bestCost = fullCost; } } foreach (var plan in plans) { IGOAPCost fullCost = comparer.ZeroCost; foreach (var action in plan) { fullCost = fullCost.GetSumWith(action.Cost); } int value = comparer.Compare(bestCost, fullCost); if (value > 0) { WritePlan(plan); } } Console.ForegroundColor = ConsoleColor.Green; foreach (var plan in plans) { IGOAPCost fullCost = comparer.ZeroCost; foreach (var action in plan) { fullCost = fullCost.GetSumWith(action.Cost); } int value = comparer.Compare(bestCost, fullCost); if (value == 0) { WritePlan(plan); } } Console.ResetColor(); }
private List <List <IGOAPReadOnlyAction> > GetAllBestPlans(TreeDecision <IGOAPReadOnlyAction> plans) { var bestCost = _comparer.BadCost; var currentPlan = new List <IGOAPReadOnlyAction>(); var bestPlans = new List <List <IGOAPReadOnlyAction> >(); foreach (var child in plans.Root.Children) { Iterate(_comparer.ZeroCost, child); } void Iterate(IGOAPCost previusCost, TreeElement <IGOAPReadOnlyAction> element) { var newCost = previusCost.GetSumWith(element.Content.Cost); currentPlan.Add(element.Content); if (!element.HasChildren) { int value = _comparer.Compare(bestCost, newCost); if (value <= 0) { if (value < 0) { bestCost = newCost; bestPlans = new List <List <IGOAPReadOnlyAction> >(); } bestPlans.Add(new List <IGOAPReadOnlyAction>(currentPlan.Where(x => !x.IsConnector))); } currentPlan.RemoveAt(currentPlan.Count - 1); return; } foreach (var child in element.Children) { Iterate(newCost, child); } while (currentPlan[currentPlan.Count - 1] != element.Content) { currentPlan.RemoveAt(currentPlan.Count - 1); } currentPlan.RemoveAt(currentPlan.Count - 1); } return(bestPlans); }
private List <GOAPAction> GetBestPlan(TreeDecision <GOAPAction> plans) { var bestCost = _comparer.BadCost; var bestPlan = new List <GOAPAction>(); var stack = new Stack <GOAPAction>(); foreach (var child in plans.Root.Children) { Iterate(_comparer.ZeroCost, child); } void Iterate(IGOAPCost previusCost, TreeElement <GOAPAction> element) { var newCost = previusCost.GetSumWith(element.Content.Cost); stack.Push(element.Content); if (!element.HasChildren) { int value = _comparer.Compare(bestCost, newCost); if (value < 0 || (value == 0 && BaseMath.Probability(0.5f))) { bestCost = newCost; bestPlan = stack.Where(x => !x.IsConnector).ToList(); } return; } foreach (var child in element.Children) { Iterate(newCost, child); } while (stack.Pop() != element.Content) { ; } } return(bestPlan); }