// Done! #region Methods private bool Search() { PriorityQueue <CognitiveState <T> > open = new PriorityQueue <CognitiveState <T> >(); IDictionary <int, CognitiveState <T> > closed = new Dictionary <int, CognitiveState <T> >(500); // Reinicializar as variáveis result = null; // Vamos começar a expandir a partir deste estado! open.Enqueue(0, start); while (open.Count > 0) { // Apanhar o "melhor" estado possivel! CognitiveState <T> node = open.Dequeue(); // Adicionar à lista de fechados! <<extension>> closed.Add(node); // Verificar se chegamos à solução! if (node.IsGoal(goal)) { result = node; return(true); } // Adicionar filhos à lista de abertos! // Depois de os filtramos é claro! open.FilterAdd(closed, node, goal, this.agentCharacter); } return(false); }
private bool SearchAStar(PriorityQueue <CognitiveState <T> > open, PriorityQueue <CognitiveState <T> > frontier) { IDictionary <int, CognitiveState <T> > closed = new Dictionary <int, CognitiveState <T> >(500); while (open.Count > 0) { // Apanhar o "melhor" estado possivel! CognitiveState <T> node = open.Dequeue(); // Adicionar à lista de fechados! <<extension>> ((ICollection <CognitiveState <T> >)closed).Add(node); // Verificar se chegamos à solução! if (node.IsGoal(goal)) { result = node; return(true); } // Adicionar filhos à lista de abertos! // Depois de os filtramos é claro! open.FilterAdd(closed, node, goal, this.agentCharacter); } cutOff += (int)Math.Ceiling(frontier.Peek().Layout.GetHeuristic(frontier.Peek().Layout, goal.Layout)); return(false); }
// Done! public static void Add <T>(this IDictionary <int, CognitiveState <T> > dic, CognitiveState <T> e) where T : class, ICognitiveLayout <T> { int key = e.Layout.GetHashCode(); if (dic.ContainsKey(key) == false) { dic.Add(key, e); } }
// Done! #region .Ctor public IDAStar(T start, T goal, AgentCharacter agentCharacter) { this.start = new CognitiveState <T>(start, agentCharacter); this.goal = new CognitiveState <T>(goal, agentCharacter); this.agentCharacter = agentCharacter; // Vamos definir um nivel de corte! cutOff = (int)Math.Ceiling(start.GetHeuristic(start, goal)); }
// Done! public static void FilterAdd <T>(this PriorityQueue <CognitiveState <T> > list, IDictionary <int, CognitiveState <T> > other, CognitiveState <T> node, CognitiveState <T> goal, AgentCharacter agentCharacter) where T : class, ICognitiveLayout <T> { // Expandir o estado actual! IList <T> childreen = node.Children(node, agentCharacter); int count = childreen.Count; for (int i = 0; i < count; ++i) { T c = childreen[i]; // Adicionar à lista somente se não existir na lista de fechados. if (other.ContainsKey(c.GetHashCode())) { continue; } CognitiveState <T> s = new CognitiveState <T>(c, node, agentCharacter); list.Enqueue(s.Cost + CognitiveState <T> .Estimate(node, c, goal), s); } }
// Done! #region Methods private bool SearchIDAStar() { PriorityQueue <CognitiveState <T> > open = new PriorityQueue <CognitiveState <T> >(); PriorityQueue <CognitiveState <T> > frontier = new PriorityQueue <CognitiveState <T> >(); // Reinicializar as variáveis result = null; // Vamos começar a expandir a partir deste estado! open.Enqueue(0, start); bool found; do { found = SearchAStar(open, frontier); open = frontier; frontier = new PriorityQueue <CognitiveState <T> >(); } while (found == false || frontier.Count != 0); return(true); }
// Done! #region .Ctor public CognitiveAStar(T start, T goal, AgentCharacter agentCharacter) { this.start = new CognitiveState <T>(start, agentCharacter); this.goal = new CognitiveState <T>(goal, agentCharacter); this.agentCharacter = agentCharacter; }
public static float Estimate(CognitiveState <T> node, T c, CognitiveState <T> goal) { return(c.GetHeuristic(c, goal.Layout)); }
public IList <T> Children(CognitiveState <T> node, AgentCharacter ac) { return(Layout.Children(ac)); }
// Done! #region Methods public bool IsGoal(CognitiveState <T> value) { return(Layout.IsGoal(value.Layout)); }
// Don't remember! public CognitiveState(CognitiveState <T> state) { Parent = state.Parent; Layout = state.Layout; }
} // Custo Real #endregion // Done! #region .Ctor // Normal and most used ctor public CognitiveState(T layout, CognitiveState <T> parent, AgentCharacter agentCharacter) { Parent = parent; Layout = layout; Cost = parent == null ? 0 : parent.Cost + parent.Layout.GetCost(layout, agentCharacter); }