public void checkNode(int i, int j, Vector2 from, Vector2 to, Dictionary <int, ANode> open, Dictionary <int, ANode> closed, ANode best) { Vector2 curNe = new Vector2(i, j); ANode curNeNode = new ANode(curNe, cost(from, curNe), cost(curNe, to), best); float NewCost = best.costFromStart + 1f; ANode foundOpen = null; open.TryGetValue(ServiceData.posInTableA(curNe), out foundOpen); ANode foundClosed = null; closed.TryGetValue(ServiceData.posInTableA(curNe), out foundClosed); if (curNeNode.costFromStart <= NewCost && (foundClosed != null || foundOpen != null)) { return; } curNeNode.costFromStart = NewCost; curNeNode.TotalCost = curNeNode.costToGoal + curNeNode.costFromStart; if (foundClosed != null) { closed.Remove(ServiceData.posInTableA(foundClosed.vector)); } if (foundOpen != null) { foundOpen = curNeNode; } else { open.Add(ServiceData.posInTableA(curNeNode.vector), curNeNode); visited.Add(new Vector2(i, j)); } }
public static void FindWayDistTest(Map.Data.MapData map) { int powtorzenia = 100; map.randomFill(10); for (int j = 1; j < 300; j = j + 50) { Vector2 from = new Vector2(0, 0); Vector2 to = new Vector2(j, j); using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"pomiar\pomiary\FindWayDistTest.txt", true)) { file.WriteLine("PKTY (0, 0) (" + j + ", " + j + ") odleglosc: " + ServiceA.cost(from, to)); } ServiceData.start = 800 - 300 / 2; ServiceData.end = 800 + 300 / 2; ServiceData.num = (ServiceData.end - ServiceData.start); ServiceData.NewTabLength = ServiceData.num * ServiceData.num; Debug.Log("0"); long[] elapsedMs1 = new long[powtorzenia]; long[] elapsedMs2 = new long[powtorzenia]; long[] elapsedMs3 = new long[powtorzenia]; long[] elapsedMs4 = new long[powtorzenia]; long[] elapsedMs5 = new long[powtorzenia]; System.GC.Collect(); ServiceData.listaSasiedztwa = null; ServiceData.macierzSasiedztwa = null; ServiceData.listaIncydencji = null; ServiceData.ListaIncydencji(map, false); //ServiceData.MacierzSasiedztwa(map, false); for (int i = 0; i < powtorzenia; i++) { Debug.Log("1 " + ((int)from.x + 800) + " " + ((int)from.y + 800)); map.Table[(int)from.x + 800][(int)from.y + 800] = null; Debug.Log("1 " + ((int)to.x + 800) + " " + ((int)to.y + 800)); map.Table[(int)to.x + 800][(int)to.y + 800] = null; var watch1 = System.Diagnostics.Stopwatch.StartNew(); ServiceDjikstra.FindWay(from, to, map, true); watch1.Stop(); elapsedMs1[i] = watch1.ElapsedMilliseconds; var watch5 = System.Diagnostics.Stopwatch.StartNew(); ServiceA.FindWay(from, to, map); watch5.Stop(); elapsedMs5[i] = watch5.ElapsedMilliseconds; Debug.Log("3"); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"pomiar\pomiary\FindWayDistTest.txt", true)) { file.WriteLine("Djikstra lista "); long sum = 0; foreach (var el in elapsedMs1) { sum = sum + el; } file.WriteLine(sum / powtorzenia); sum = 0; file.WriteLine("A*"); foreach (var el in elapsedMs5) { sum = sum + el; } file.WriteLine(sum / powtorzenia); } } }
public static void FindWaySizeTest(Map.Data.MapData map) { int powtorzenia = 100; map.randomFill(10); for (int j = 10; j < 200; j = j + 10) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"pomiar\pomiary\FindWaySizeTest.txt", true)) { file.WriteLine("Rozmiar " + j); } ServiceData.start = 800 - j / 2; ServiceData.end = 800 + j / 2; ServiceData.num = (ServiceData.end - ServiceData.start); ServiceData.NewTabLength = ServiceData.num * ServiceData.num; long[] elapsedMs1 = new long[powtorzenia]; long[] elapsedMs2 = new long[powtorzenia]; long[] elapsedMs3 = new long[powtorzenia]; long[] elapsedMs4 = new long[powtorzenia]; long[] elapsedMs5 = new long[powtorzenia]; long[] elapsedMs6 = new long[powtorzenia]; System.GC.Collect(); ServiceData.listaSasiedztwa = null; ServiceData.macierzSasiedztwa = null; ServiceData.listaIncydencji = null; ServiceData.ListaSasiedztwa(map, false); ServiceData.MacierzSasiedztwa(map, false); ServiceData.ListaIncydencji(map, false); for (int i = 0; i < powtorzenia; i++) { Vector2 from = new Vector2(2, 2); Vector2 to = new Vector2(50, 50); map.Table[(int)from.x][(int)from.y] = null; map.Table[(int)to.x][(int)to.y] = null; var watch1 = System.Diagnostics.Stopwatch.StartNew(); ServiceDjikstra.FindWay(from, to, map, true); watch1.Stop(); elapsedMs1[i] = watch1.ElapsedMilliseconds; var watch2 = System.Diagnostics.Stopwatch.StartNew(); ServiceDjikstra.FindWay(from, to, map, false); watch2.Stop(); elapsedMs2[i] = watch2.ElapsedMilliseconds; var watch3 = System.Diagnostics.Stopwatch.StartNew(); //ServiceFord.FindWay(from, to, map, true); watch3.Stop(); elapsedMs3[i] = watch3.ElapsedMilliseconds; var watch4 = System.Diagnostics.Stopwatch.StartNew(); //ServiceFord.FindWay(from, to, map, false); watch4.Stop(); elapsedMs4[i] = watch4.ElapsedMilliseconds; var watch5 = System.Diagnostics.Stopwatch.StartNew(); ServiceA.FindWay(from, to, map); watch5.Stop(); elapsedMs5[i] = watch5.ElapsedMilliseconds; var watch6 = System.Diagnostics.Stopwatch.StartNew(); ServiceA.FindWay(from, to, map); watch6.Stop(); elapsedMs6[i] = watch6.ElapsedMilliseconds; } using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"pomiar\pomiary\FindWaySizeTest.txt", true)) { file.WriteLine("Djikstra lista "); foreach (var el in elapsedMs1) { file.WriteLine(el); } file.WriteLine("Djikstra macierz "); foreach (var el in elapsedMs2) { file.WriteLine(el); } file.WriteLine("Ford Lista"); foreach (var el in elapsedMs3) { file.WriteLine(el); } file.WriteLine("Ford macierz"); foreach (var el in elapsedMs4) { file.WriteLine(el); } file.WriteLine("A*"); foreach (var el in elapsedMs5) { file.WriteLine(el); } file.WriteLine("A* Modified"); foreach (var el in elapsedMs6) { file.WriteLine(el); } } } }
public static void generateTest(Map.Data.MapData map) { int powtorzenia = 100; map.randomFill(10); for (int j = 10; j < 1000; j = j + 10) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"pomiar\pomiary\generateTest.txt", true)) { file.WriteLine("Rozmiar " + j); } ServiceData.start = 800 - j / 2; ServiceData.end = 800 + j / 2; ServiceData.num = (ServiceData.end - ServiceData.start); ServiceData.NewTabLength = ServiceData.num * ServiceData.num; long[] elapsedMs1 = new long[powtorzenia]; long[] elapsedMs2 = new long[powtorzenia]; long[] elapsedMs3 = new long[powtorzenia]; for (int i = 0; i < powtorzenia; i++) { System.GC.Collect(); ServiceData.listaSasiedztwa = null; ServiceData.macierzSasiedztwa = null; ServiceData.listaIncydencji = null; if (ServiceData.listaSasiedztwa == null) { var watch1 = System.Diagnostics.Stopwatch.StartNew(); ServiceData.ListaSasiedztwa(map, false); watch1.Stop(); elapsedMs1[i] = watch1.ElapsedMilliseconds; } if (ServiceData.macierzSasiedztwa == null) { var watch2 = System.Diagnostics.Stopwatch.StartNew(); ServiceData.MacierzSasiedztwa(map, false); watch2.Stop(); elapsedMs2[i] = watch2.ElapsedMilliseconds; } if (ServiceData.listaIncydencji == null) { var watch3 = System.Diagnostics.Stopwatch.StartNew(); ServiceData.ListaIncydencji(map, false); watch3.Stop(); elapsedMs3[i] = watch3.ElapsedMilliseconds; } } using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"pomiar\pomiary\generateTest.txt", true)) { file.WriteLine("ListaSasiedztwa"); foreach (var el in elapsedMs1) { file.WriteLine(el); } file.WriteLine("macierzSasiedztwa"); foreach (var el in elapsedMs2) { file.WriteLine(el); } file.WriteLine("listaIncydencji"); foreach (var el in elapsedMs3) { file.WriteLine(el); } } } }
public IList <Vector2> FindWay_AModified(Vector2 from, Vector2 to, MapData map) { visited = new List <Vector2>(); IList <Vector2> list = new List <Vector2>(); from = new Vector2((int)from.x, (int)from.y); to = new Vector2((int)to.x, (int)to.y); var watch = System.Diagnostics.Stopwatch.StartNew(); //Debug.Log(from + " to " + to); Dictionary <int, ANode> open = new Dictionary <int, ANode>(); Dictionary <int, ANode> closed = new Dictionary <int, ANode>(); Vector2 cur = from; ANode s = new ANode(from, 0, cost(cur, to), null); if (!open.ContainsKey(ServiceData.posInTableA(s.vector))) { open.Add(ServiceData.posInTableA(s.vector), s); } int max = 0; while (open.Count > 0 && max++ < 5000) { var best = open.OrderBy(f => f.Value.TotalCost).ToList().FirstOrDefault(); if (best.Value.vector.Equals(to)) { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; list.Add(best.Value.vector); ANode act = best.Value.parent; do { if (act != null) { list.Add(act.vector); act = act.parent; } } while (act != null); list = list.Reverse().ToList(); saveLogs(elapsedMs, list); break; } else { int i = (int)best.Value.vector.x; int j = (int)best.Value.vector.y; //top if (isEmpty(i, j + 1, map)) { checkNode_AModified(i, j + 1, from, to, open, closed, best.Value); } //bot if (isEmpty(i, j - 1, map)) { checkNode_AModified(i, j - 1, from, to, open, closed, best.Value); } //r if (isEmpty(i + 1, j, map)) { checkNode_AModified(i + 1, j, from, to, open, closed, best.Value); } //l if (isEmpty(i - 1, j, map)) { checkNode_AModified(i - 1, j, from, to, open, closed, best.Value); } } open.Remove(ServiceData.posInTableA(best.Value.vector)); if (!closed.ContainsKey(ServiceData.posInTableA(best.Value.vector))) { closed.Add(ServiceData.posInTableA(best.Value.vector), best.Value); } } return(list); }
public IList <Vector2> FindWay(Vector2 from, Vector2 to, MapData map, bool useList) { IList <Vector2> list = new List <Vector2>(); int DEST = ServiceData.posInTable(to); int START = ServiceData.posInTable(from); //Debug.Log("start " + START + " dest " + DEST); var watch = System.Diagnostics.Stopwatch.StartNew(); int wielkoscGrafu = ServiceData.NewTabLength; // int[] odl = new int[ServiceData.NewTabLength]; // aktualna najmniejsza odleglosc int[] odleglosc = new int[wielkoscGrafu]; int[] poprzedni = new int[wielkoscGrafu]; int[] wezly = new int[wielkoscGrafu]; for (int i = 0; i < ServiceData.NewTabLength; i++) { odleglosc[i] = poprzedni[i] = ServiceData.INF; wezly[i] = i; } odleglosc[START] = 0; if (!useList) { do { int najmniejsza = wezly[0]; int smallestIndex = 0; for (int i = 1; i < wielkoscGrafu; i++) { if (odleglosc[wezly[i]] < odleglosc[najmniejsza]) { najmniejsza = wezly[i]; smallestIndex = i; } } wielkoscGrafu--; wezly[smallestIndex] = wezly[wielkoscGrafu]; //Debug.Log("wywolanie "); if (odleglosc[najmniejsza] == ServiceData.INF || najmniejsza == DEST) { //Debug.Log("break | dist " + odleglosc[najmniejsza] + " smallest " + najmniejsza); break; } for (int i = 0; i < wielkoscGrafu; i++) { int v = wezly[i]; int newDist = odleglosc[najmniejsza] + ServiceData.macierzSasiedztwa[najmniejsza][v]; if (newDist < odleglosc[v]) { odleglosc[v] = newDist; poprzedni[v] = najmniejsza; } } } while (wielkoscGrafu > 0); } else { do { int najmniejsza = wezly[0]; int smallestIndex = 0; for (int i = 1; i < wielkoscGrafu; i++) { if (odleglosc[wezly[i]] < odleglosc[najmniejsza]) { najmniejsza = wezly[i]; smallestIndex = i; } } wielkoscGrafu--; wezly[smallestIndex] = wezly[wielkoscGrafu]; //Debug.Log("wywolanie "); if (odleglosc[najmniejsza] == ServiceData.INF || najmniejsza == DEST) { //Debug.Log("break | dist " + odleglosc[najmniejsza] + " smallest " + najmniejsza); break; } foreach (var i in ServiceData.listaIncydencji[najmniejsza]) { int v = wezly[i]; int waga = ServiceData.listaIncydencji[najmniejsza].Where(f => f == v).FirstOrDefault(); int newDist = odleglosc[najmniejsza] + (waga == 0 ? ServiceData.INF : 1); if (newDist < odleglosc[v]) { odleglosc[v] = newDist; poprzedni[v] = najmniejsza; } } } while (wielkoscGrafu > 0); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int[] sciezka = OdtworzSciezke(poprzedni, START, DEST); saveLogs(elapsedMs, odleglosc, poprzedni, wezly, sciezka, useList); foreach (var elem in sciezka) { list.Add(ServiceData.posInTableToVector(elem)); //Debug.Log("pkt " + ServiceData.posInTableToVector(elem)); } return(list); }
public IList <Vector2> FindWay(Vector2 from, Vector2 to, MapData map, bool useList) { IList <Vector2> list = new List <Vector2>(); int DEST = ServiceData.posInTable(to); int START = ServiceData.posInTable(from); //Debug.Log("start " + START + " dest " + DEST); var watch = System.Diagnostics.Stopwatch.StartNew(); Queue <int> q = new Queue <int>(); int wielkoscGrafu = ServiceData.NewTabLength; // int[] odl = new int[ServiceData.NewTabLength]; // aktualna najmniejsza odleglosc int[] odleglosc = new int[wielkoscGrafu]; int[] poprzedni = new int[wielkoscGrafu]; int[] wezly = new int[wielkoscGrafu]; for (int i = 0; i < ServiceData.NewTabLength; i++) { odleglosc[i] = poprzedni[i] = ServiceData.INF; wezly[i] = i; } odleglosc[START] = 0; if (!useList) { for (int i = 1; i < wielkoscGrafu - 1; i++) { for (int w1 = 0; w1 < wielkoscGrafu; w1++) { for (int w2 = 0; w2 < wielkoscGrafu; w2++) { if (ServiceData.macierzSasiedztwa[w1][w2] != 0) { int tmp = odleglosc[w2] + ServiceData.macierzSasiedztwa[w1][w2]; if (odleglosc[w1] > tmp) { odleglosc[w1] = tmp; poprzedni[w1] = w2; } } } } } } else { for (int i = 1; i < wielkoscGrafu - 1; i++) { for (int w1 = 0; w1 < wielkoscGrafu; w1++) { foreach (var w2 in ServiceData.listaIncydencji[w1]) { int waga = w2; if (waga != 0) { //Debug.Log(" waga "+ waga + " w2 " + w2 + " w1 " + w1); int tmp = odleglosc[w2] + 1; // Debug.Log(" ustawiam " + odleglosc[w1] + " odl" + (odleglosc[w2] + 1)); if (odleglosc[w1] > tmp) { odleglosc[w1] = tmp; poprzedni[w1] = w2; } } } } } } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int[] sciezka = OdtworzSciezke(poprzedni, START, DEST); saveLogs(elapsedMs, odleglosc, poprzedni, wezly, sciezka, useList); foreach (var elem in sciezka) { list.Add(ServiceData.posInTableToVector(elem)); //Debug.Log("pkt " + ServiceData.posInTableToVector(elem)); } return(list); }