public void UpdateVertex(int x, int y, int nx, int ny) { worldNode current = worldNodes[x, y]; worldNode next = worldNodes[nx, ny]; float css = cost(x, y, nx, ny); if (css == 0) { closedList.Add(next); return; } if (current.g + css < next.g) { next.g = current.g + css; next.h = computeHeuristic(nx, ny); next.f = next.g + next.h; next.parent = current; if (fringe.Contains(next)) { fringe.UpdatePriority(next, next.f); } else { fringe.Enqueue(next, next.f); // f- c*g where c = 200 } //next.g + h - 200*next.g } }
public float keyMe(worldNode s) { if (s != null) { return(s.g + weight * computeHeuristic(s.x, s.y)); } return(30000); }
public float minkey() { if (fringe.Any()) { worldNode a = fringe.Dequeue(); fringe.Enqueue(a, a.f); return(a.f); } return(30000); }
private static void snapshot(Naiive searchLocal, string filename, int w, int p, int a, int h, float w1, float w2) { StringBuilder buffer = new StringBuilder(); worldNode aaa = searchLocal.end; float path = searchLocal.end.g; //World number, s/e pair index, algo index, heuristic index, weight, weight2, pathlength, nodesexpanded, runtime, Memory, buffer.Append($"{w},{p},{a},{h},{w1},{w2},{path},{searchLocal.expanded},{searchLocal.sw.ElapsedMilliseconds},{GC.GetTotalMemory(false)}"); using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename, true)) {//Append to existing files file.WriteLine(buffer.ToString()); } }
private void chunkClick(object sender, RoutedEventArgs e) { Button clicked = (Button)sender; int[] coordinates = (int[])clicked.Content; if (search.worldNodes[coordinates[0], coordinates[1]] != null) { worldNode chunky = search.worldNodes[coordinates[0], coordinates[1]]; if (chunky != null) { mvm.f = chunky.f; mvm.g = chunky.g; mvm.h = chunky.h; } } }
public void setup() { worldNodes = new worldNode[120, 160]; sw = Stopwatch.StartNew(); fringe = new SimplePriorityQueue <worldNode, float>(); closedList = new List <worldNode>(); worldNode currentNode = new worldNode(startx, starty); worldNodes[startx, starty] = currentNode; end = new worldNode(endx, endy); worldNodes[endx, endy] = end; currentNode.g = 0; float h = computeHeuristic(startx, starty); //Place heuristic in here. currentNode.f = weight * h; fringe.Enqueue(currentNode, currentNode.f);//currentNode.g + h - 200*currentNode.g expanded = 0; }
public void expandNode(worldNode currentNode) { expanded++; int cx = currentNode.x; int cy = currentNode.y; worldNode nextNode; int nx, ny; closedList.Add(currentNode); for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { nx = cx + i; ny = cy + j; if (nx < 0 || nx > 119 || ny < 0 || ny > 159) { continue; } if (worldNodes[nx, ny] == null) { //Was never generated. nextNode = new worldNode(nx, ny); worldNodes[nx, ny] = nextNode; UpdateVertex(cx, cy, nx, ny); } else { nextNode = worldNodes[nx, ny]; if (!closedList.Contains(worldNodes[nx, ny])) { /*if (!fringe.Contains(worldNodes[nx, ny])) * { * UpdateVertex(cx, cy, nx, ny, heuristic, algo, weight, endx, endy); * }*/ UpdateVertex(cx, cy, nx, ny); } } } } }
private void expandState(worldNode s) { if (anchor.fringe.Contains(s)) { anchor.fringe.Remove(s); } if (inad.fringe.Contains(s)) { inad.fringe.Remove(s); } expanded++; worldNode sp; int nx, ny; int cx = s.x; int cy = s.y; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { nx = cx + i; ny = cy + j; if (nx < 0 || nx > 119 || ny < 0 || ny > 159) { continue; } if (anchor.worldNodes[nx, ny] == null) { //Was never generated. anchor.worldNodes[nx, ny] = new worldNode(nx, ny); } float cg = anchor.worldNodes[cx, cy].g; float css = anchor.cost(cx, cy, nx, ny); sp = anchor.worldNodes[nx, ny]; if (css == 0) { anchor.closedList.Add(sp); inad.closedList.Add(sp); } if (sp.g > cg + css) { sp.g = cg + css; sp.parent = s; if (!anchor.closedList.Contains(sp)) { sp.h = anchor.computeHeuristic(nx, ny); sp.f = sp.h + sp.g; anchor.fringe.Enqueue(sp, anchor.keyMe(sp)); if (!inad.closedList.Contains(sp)) { for (int k = 0; k < 5; k++) { if (k == consistent) { continue; } if (inad.keyMe(sp) < weight2 * anchor.keyMe(sp)) { sp.h = inad.computeHeuristic(nx, ny); sp.f = sp.h + sp.g; if (inad.fringe.Contains(sp)) { inad.fringe.UpdatePriority(sp, inad.keyMe(sp)); } else { inad.fringe.Enqueue(sp, inad.keyMe(sp)); } } } } } } } } }
private void recalculateAlgorithm() { SolidColorBrush whiteBrush = new SolidColorBrush(); whiteBrush.Color = Colors.White; SolidColorBrush blackBrush = new SolidColorBrush(); blackBrush.Color = Colors.Black; SolidColorBrush chartreuseBrush = new SolidColorBrush(); chartreuseBrush.Color = Colors.Chartreuse; SolidColorBrush beigeBrush = new SolidColorBrush(); beigeBrush.Color = Colors.Beige; SolidColorBrush grayBrush = new SolidColorBrush(); grayBrush.Color = Colors.Gray; SolidColorBrush pathBrush = new SolidColorBrush(); pathBrush.Color = Colors.BlueViolet; SolidColorBrush startBrush = new SolidColorBrush(); startBrush.Color = Colors.Green; SolidColorBrush endBrush = new SolidColorBrush(); endBrush.Color = Colors.Red; SolidColorBrush transparent = new SolidColorBrush(); transparent.Color = Colors.Transparent; int spi = StartEndPairs.SelectedIndex; if (spi < 0) { spi = 0; } int startx = mvm.startPairs[spi, 0]; int starty = mvm.startPairs[spi, 1]; int endx = mvm.endPairs[spi, 0]; int endy = mvm.endPairs[spi, 1]; //Rerun the algorithm, update runtime int pairIdx = StartEndPairs.SelectedIndex; if (pairIdx < 0) { pairIdx = 0; } float wght; if (!float.TryParse(Weight.Text, out wght)) { wght = 1; } float wght2; if (!float.TryParse(Weight2.Text, out wght2)) { wght2 = 1; } if (Algo.SelectedIndex < 3) { search.initAttr(Heuristic.SelectedIndex, Algo.SelectedIndex, wght, mvm.startPairs[pairIdx, 0], mvm.startPairs[pairIdx, 1], mvm.endPairs[pairIdx, 0], mvm.endPairs[pairIdx, 1]); search.hSearch(); } else if (Algo.SelectedIndex == 3) { seqsearch.initAttr(wght, wght2, mvm.startPairs[pairIdx, 0], mvm.startPairs[pairIdx, 1], mvm.endPairs[pairIdx, 0], mvm.endPairs[pairIdx, 1]); search = seqsearch.seqSearch(); } else { intsearch.initAttr(wght, wght2, mvm.startPairs[pairIdx, 0], mvm.startPairs[pairIdx, 1], mvm.endPairs[pairIdx, 0], mvm.endPairs[pairIdx, 1]); search = intsearch.intSearch(); } mvm.Expanded = search.expanded; myPath.Children.Clear(); //Interpret search data. Button chunk; worldNode a = search.end; //int path = 1; while (a.parent != null) { //path++; chunk = new Button(); if (a.x == endx && a.y == endy) { chunk.BorderBrush = endBrush; } else { chunk.BorderBrush = pathBrush; } chunk.Background = transparent; chunk.BorderThickness = new Thickness(1); chunk.Content = new int[] { a.x, a.y }; chunk.Click += chunkClick; Grid.SetColumn(chunk, a.y); Grid.SetRow(chunk, a.x); a = a.parent; myPath.Children.Add(chunk); } //mvm.PathLen = path; mvm.PathLen = search.end.g; chunk = new Button(); chunk.Background = transparent; chunk.BorderBrush = startBrush; chunk.BorderThickness = new Thickness(1); chunk.Content = new int[] { a.x, a.y }; chunk.Click += chunkClick; Grid.SetColumn(chunk, a.y); Grid.SetRow(chunk, a.x); myPath.Children.Add(chunk); //Update Start/end pair appearance }