Commit 5133693a authored by Tanmim Hanifa's avatar Tanmim Hanifa
Browse files

attempted to implement greedy best search, getting a compilation error

parent d4998ef0
......@@ -28,7 +28,7 @@ class Examples {
}
private static void AddEdge(Graph g, Node a, Node b) {
AddEdge(g, a, b, AStar.Distance(a, b));
AddEdge(g, a, b, Greedy.Distance(a, b));
}
......
......@@ -4,12 +4,13 @@ using System.Collections.Generic;
/*
* An A* Pathfinder
*/
public class AStar : Pathfinder {
public class Greedy : Pathfinder {
private Graph graph;
private PriorityQueue frontier;
private Dictionary<Node, Node> previous;
private Dictionary<Node, int> cost;
private Graph graph;
private PriorityQueue frontier;
private Dictionary<Node, Node> previous;
private Dictionary<Node, int> cost;
private Dictionary<Node, Node> visitedFrom;
public void SetGraph(Graph g) {
graph = g;
......@@ -45,45 +46,76 @@ public class AStar : Pathfinder {
cost[start] = 0; // start->start costs nothing
}
// A* search: returns true if path found
// Greedy search: returns true if path found--------------------------
private bool Search(Node goal) {
bool found = false; // Did we find the goal yet?
while (frontier.Count != 0) {
// Select a current node from frontier
Node current = (Node) frontier.Dequeue();
Console.WriteLine("Current node: " + current);
// Is this the goal?
if (current.Equals(goal)) {
found = true;
break; // Stop at goal
}
// Expand current node
List<Node> neighbours = graph.Neighbours(current);
foreach (Node next in neighbours) {
// Find cost to next node
int nextCost = cost[current] + graph.Cost(current, next);
// Have we been here before?
bool visitedBefore = cost.ContainsKey(next);
// New node OR cheaper path to known node
if (!visitedBefore || nextCost < cost[next]) {
// Add it to the frontier
frontier.Enqueue(next, nextCost + Distance(next, goal));
// Record how we got to next and it's cost
previous[next] = current;
cost[next] = nextCost;
}
}
}
return found;
// Greedy Best Search-------------------------------------------------
bool found = false;
Node start;
visitedFrom = new Dictionary<Node, Node>();
frontier.Enqueue(start, 0);
visitedFrom[start];
while(frontier.Count > 0){
Node current = frontier.Dequeue();
if(current == goal){
found = true;
break;
}
List<Node> neighbours = graph.Neighbours(current);
foreach (Node next in neighbours) {
if(!visitedFrom.ContainsKey(next)){
int nxt = next.Id;
frontier.Enqueue(goal, nxt);
visitedFrom[next] = current;
}
}
}
return found;
//end of greedy-------------------------------------------------------
//AStar--------------------------------------------------
// bool found = false; // Did we find the goal yet?
//
// while (frontier.Count != 0) {
// // Select a current node from frontier
// Node current = (Node) frontier.Dequeue();
// Console.WriteLine("Current node: " + current);
//
// // Is this the goal?
// if (current.Equals(goal)) {
// found = true;
// break; // Stop at goal
// }
//
// // Expand current node
// List<Node> neighbours = graph.Neighbours(current);
//
// foreach (Node next in neighbours) {
// // Find cost to next node
// int nextCost = cost[current] + graph.Cost(current, next);
//
// // Have we been here before?
// bool visitedBefore = cost.ContainsKey(next);
//
// // New node OR cheaper path to known node
// if (!visitedBefore || nextCost < cost[next]) {
// // Add it to the frontier
// frontier.Enqueue(next, nextCost + Distance(next, goal));
// // Record how we got to next and it's cost
// previous[next] = current;
// cost[next] = nextCost;
// }
// }
// }
// return found;
//end of AStar----------------------------------------------------------
}
// Straight line distance between nodes
public static int Distance(Node a, Node b) {
......
......@@ -63,7 +63,7 @@ class Pathfind {
Node start = new Node(a);
Node goal = new Node(b);
Pathfinder finder = new AStar(); // ***** Change to GreedyBestFirst when completed *****
Pathfinder finder = new Greedy(); // ***** Change to GreedyBestFirst when completed *****
finder.SetGraph(g);
List<Node> path = finder.FindPath(start, goal);
if (path != null) {
......
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment