public void CalculateNewGoal() { try { var actionPlan = CalculateActionPlan(); currentGoal = actionPlan.Goal; actions = actionPlan.Actions; } catch (NoActionPlanFoundException e) { agent.LogInfo(this, "No action plan found: " + e.Message); currentGoal = null; actions.Clear(); OnActionPlanNotFound?.Invoke(); return; } if (actions.Count > 0) { OnActionPlanFound?.Invoke(); } else { agent.LogKeyInfo(this, "unable to achieve any new goals"); OnActionPlanNotFound?.Invoke(); } }
private VisualElement GetGoalDetails(IAIGoal <L, V> goal) { var details = new VisualElement(); var conditions = goal.GetConditions(); foreach (var condition in conditions) { details.Add(CreateConditionElement(condition)); } return(details); }
public void BeginGoalInfo(IAIGoal <L, V> goal) { goalInfo = new GoalCalculationInfo { Goal = goal, IsProcessed = false, Messages = new List <string>(), ViableActions = new List <IAIAction <L, V> >(), NonViableActionDetails = new List <string>(), }; }
private void InitialiseNodeLists(IAIAgent <L, V> agent, IAIGoal <L, V> goal) { nullNodes.Clear(); openNodes.Clear(); closedNodes.Clear(); currentNode = new AINode <L, V>(agent, goal); closedNodes.Add(currentNode); foreach (var action in agent.Actions.GetActions()) { nullNodes.Add(new AINode <L, V>(agent, goal, action)); } }
private void LogGoalConditionsTrace(IAIGoal <L, V> goal) { if (agent.Logger.LogLevel != AILogger.LogLevels.Trace) { return; } string message = $"checking if {goal} can be achieved. Conditions:"; foreach (var condition in goal.GetConditions()) { message += $"\n{condition.Key} : {condition.Value}"; } agent.LogTrace(this, message); }
private bool IsAlreadyAchieved(IAIGoal <L, V> goal) { foreach (var condition in goal.GetConditions()) { if (!agent.Memory.IsMatch(condition.Key, condition.Value)) { return(false); } } if (isDiagnosticsEnabled) { diagnosticData.AddGoalMessage("Goal conditions have already been achieved."); } return(true); }
private bool IsGoalAchieveableByActions(IAIGoal <L, V> goal) { // Checks if a goal is obtainable by any of the available actions (and sensors) on the agent. // If no actions on the agent meet the goal, the goal is deemed not achievable at this point. // If the goal is achievable, the actions' preconditions are checked by the AStar algorithm LogGoalConditionsTrace(goal); var conditions = goal.GetConditions(); var conditionsMet = new Dictionary <L, bool>(); foreach (var condition in conditions) { conditionsMet.Add(condition.Key, false); } foreach (var stateValue in agent.Memory.GetState()) { if (conditionsMet.ContainsKey(stateValue.Key) && conditionsMet[stateValue.Key].Equals(conditions[stateValue.Key])) { conditionsMet[stateValue.Key] = true; } } foreach (var action in agent.Actions.GetActions()) { agent.LogTrace(this, $"checking if {action} meets goal conditions..."); //if (!action.IsEnabled()) continue; foreach (var effect in action.GetEffects()) { agent.LogTrace(this, $"Effect: {effect.Key} : {effect.Value}"); if (conditionsMet.ContainsKey(effect.Key) && effect.Value.Equals(conditions[effect.Key])) { agent.LogTrace(this, $"Meets condition: {effect.Key} : {effect.Value}"); conditionsMet[effect.Key] = true; } } } if (!conditionsMet.ContainsValue(false)) { return(true); } LogGoalUnachievableTrace(goal, conditionsMet); return(false); }
public void OnAgentPlanCalculated(Queue <IAIAction <L, V> > actions, IAIGoal <L, V> goal) { DisplayDiagnosticsInfo(aiAgent.GetDiagnostics()); if (actionPlan.IsValid) { previousPlans.Add(actionPlan); } actionPlan = new ActionPlan <L, V> { Goal = goal, Actions = new Queue <IAIAction <L, V> >(actions), }; DisplayActionInfo(); }
private void LogGoalUnachievableTrace(IAIGoal <L, V> goal, Dictionary <L, bool> conditionsMet) { if (isDiagnosticsEnabled) { diagnosticData.AddGoalMessage("Goal condition is not achievable by any of its actions."); } if (agent.Logger.LogLevel != AILogger.LogLevels.Trace) { return; } agent.LogTrace(this, $"{goal} not achievable by the available actions"); foreach (var c in conditionsMet) { agent.LogTrace(this, $"{c.Key} : {c.Value}"); } }
public AINode(IAIAgent <L, V> agent, IAIGoal <L, V> goal, IAIAction <L, V> action = null, AINode <L, V> parent = null) { this.agent = agent; this.action = action; this.parent = parent; if (action == null) { // This is a goal node state = new AIState <L, V>(goal, agent.Memory.CloneState()); state.AddUnmetPreconditions(goal.GetConditions()); g = 0; } else { // This is an action node state = new AIState <L, V>(); g = float.MaxValue; } }
public bool FindActionPlan(IAIAgent <L, V> agent, IAIGoal <L, V> goal) { InitialiseNodeLists(agent, goal); while (!currentNode.ConditionsMet()) { FindNeighbouringNodes(); if (openNodes.Count == 0) { return(false); } currentNode = FindCheapestNode(); closedNodes.Add(currentNode); openNodes.Remove(currentNode); } return(true); }
public AIState(IAIGoal <L, V> goal, Dictionary <L, V> newState) { preconditions = new Dictionary <L, V>(goal.GetConditions()); state = newState; }