/// <summary> /// Checks if this world state fulfills of the other. /// Compare all symbols. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Satisfies(StratusWorldState other) { foreach (var symbol in other.symbols) { // Look for a matching symbol var matchingSymbol = Find(symbol.key); if (matchingSymbol != null) { // If the symbols were not equal... if (!matchingSymbol.value.Compare(symbol.value)) { //Trace.Script(Symbols[symbol.Key].Print() + " is not equal to " + symbol.Value.Print()); return(false); } } // The symbol was not found else { return(false); } } // All symbols were a match return(true); }
public StratusWorldState Copy() { var newState = new StratusWorldState(); newState = this; return(newState); }
/// <summary> /// Merges the symbols of the other world state with this one. /// It will overwrite matching symbols, and add any not found in the current one. /// </summary> /// <param name="other"></param> public void Merge(StratusWorldState other) { foreach (var otherSymbol in other.symbols) { Apply(otherSymbol); } }
public Node(Node parent, float cost, StratusWorldState state, StatefulAction action) { Parent = parent; Cost = cost; State = state; Action = action; }
/// <summary> /// Constructor /// </summary> /// <param name="startingState"></param> /// <param name="goal"></param> /// <param name="actions"></param> public Search(StratusWorldState startingState, StratusWorldState goal, StatefulAction[] actions) { StartState = startingState.Copy(); EndState = goal; Actions = actions; }
/// <summary> /// Given a goal, formulates a plan. /// </summary> /// <param name="goal"></param> /// <returns></returns> public static Plan Formulate(Planner planner, StatefulAction[] actions, StratusWorldState currentState, StratusGoal goal) { // Reset all actions foreach (var action in actions) { action.Reset(); } // Get all valid actions whose context preconditions are true var usableActions = (from action in actions where action.contextPrecondition && !currentState.Satisfies(action.effects) select action).ToArray(); if (planner.debug) { StratusDebug.Log("Making plan to satisfy the goal '" + goal.Name + "' with preconditions:" + goal.DesiredState.ToString(), planner.agent); StratusDebug.Log("Actions available:", planner.agent); foreach (var action in usableActions) { StratusDebug.Log("- " + action.description, planner.agent); } } // The path of actions Search.Path path; if (Plan.UseAstar) { Search search = new Search(currentState, goal.DesiredState, usableActions); search.Tracing = planner.debug; search.Initialize(); path = search.FindSolution(); } else { // Build up a tree of nodes path = new Search.Path(); Search.Node starting = new Search.Node(null, 0f, goal.DesiredState, null); // Look for a solution, backtracking from the goal's desired world state until // we have fulfilled every precondition leading up to it! var hasFoundPath = FindSolution(path, starting, usableActions, planner); // If the path has not been found if (!hasFoundPath) { if (planner.debug) { StratusDebug.Log("No plan could be formulated!", planner.agent); } return(new Plan()); } } // If no solution was found if (path == null) { return(null); } // Make the plan var plan = new Plan(); foreach (var action in path) { plan.Add(action); } return(plan); }