public IDecision GetEdge(GoalNode neighbor) { IDecision edge; _edges.TryGetValue(neighbor, out edge); if (edge == null) { throw new ArgumentException("neighbor", "Neighbor is not connected to this node"); } return edge; }
public void GoalNodeNeighborIterator() { CurrentState state = new CurrentState(); List<IDecision> decisions = new List<IDecision>() { new MoveToFridge(state), new KillFridgeGuardian(state), new OpenFridgeDecision(state), new GetBananaFromFridge(state) }; StateOffset goal = new StateOffset(); //goal.SetStateValue("HasBanana", true, false); GoalNode end = new GoalNode(goal, decisions); int i = decisions.Count - 1; foreach (GoalNode neighbor in end.GetNeighbors()) { Console.WriteLine(end.GetEdge(neighbor)); //Assert.AreSame(decisions[i--], end.GetEdge(neighbor)); } }
public IEnumerable<IGraphNode> GetNeighbors() { if (_cacheComplete) { foreach (GoalNode neighbor in _edges.Keys) { yield return neighbor; } } else { for (int i = 0; i < _decisions.Count; i++) { if (_decisions[i].ArePreconditionsMet(State)) { StateOffset post = new StateOffset(State); _decisions[i].ApplyPostconditions(post); GoalNode previousGoal = new GoalNode(post, _decisions); _edges.Add(previousGoal, _decisions[i]); yield return previousGoal; } } _cacheComplete = true; } }
/* public void BuildGraph() { Stack<GoalNode> open = new Stack<GoalNode>(); _goals.Clear(); open.Push(_root); _goals.Add(_root); while (open.Count > 0) { GoalNode current = open.Peek(); bool finishedNode = true; bool causeFound = false; foreach (IDecision cause in _decisions) { if (causeFound) { // Break on the opening of the following Decision // This ensures that if the current neighbor is the last, and it is an eligible cause, it triggers the finishing pops finishedNode = false; break; } if (!cause.ArePreconditionsMet(current.State)) { continue; } GoalNode next = new GoalNode(cause.ApplyPostconditions(current.State)); next.AddNeighbor(current, cause); open.Push(next); causeFound = true; _goals.Add(next); } if (finishedNode) { open.Pop(); } } }*/ private int Heuristic(GoalNode g1, GoalNode g2) { return _heuristic(g1.State, g2.State); }
public Queue<IDecision> CreatePlan(StateOffset goal) { GoalNode start = new GoalNode(new StateOffset(), _decisions); GoalNode end = new GoalNode(goal, _decisions); Queue<GoalNode> goals = new Queue<GoalNode>(GraphMethods<GoalNode>.FindPath(start, end, Heuristic)); Queue<IDecision> plan = new Queue<IDecision>(); if (goals.Count > 0) { GoalNode previous = goals.Dequeue(); while (goals.Count > 0) { GoalNode top = goals.Dequeue(); IDecision edge = previous.GetEdge(top); plan.Enqueue(edge); previous = top; } } return plan; }
private bool IsMatch(GoalNode other) { return other.State.IsSubsetOf(State); }