public void Dijkstra_Alg() { Clear(); List <Field> fieldList = new List <Field>(); foreach (Field field in fieldArray) { field.prevField = null; field.visited = false; if (!field.wall) { field.distance = field.start ? 0 : int.MaxValue; } fieldList.Add(field); if (!field.wall && !field.start && !field.finish) { btnArray[field.point.row, field.point.col].Background = Brushes.White; } } var fieldToVisit = new List <Field>(fieldList); Dijkstra alg = new Dijkstra(fieldToVisit); thr = new Thread(() => alg.Algorithm()); thr.Start(); }
private void CheckExhaustedNodes(int[] _path) { foreach (int n in _path) { if (remainingFlow[n] == 0) { List <int> toRemove = new List <int>(); for (int i = 0; i < path.Count; i++) { bool inPath = false; foreach (int j in path[i]) { if (j == n) { inPath = true; } } if (inPath) { toRemove.Add(i); // Rebuild path between source and sink int _start = path[i][0]; int _end = path[i][path[i].Length - 1]; Debug.Log("Recalculating path from " + _start + " to " + _end); float[,] _distance = CalculateDistances(nodeX, nodeY); DijkstraSolution solution = Dijkstra.Algorithm(_start, _end, _distance, distanceExponent); if (solution.cost < float.MaxValue) { source.Add(_start); // Record source index sink.Add(_end); // Record sink index cost.Add(solution.cost); // Record cost of delivery path.Add(solution.path); // Record path of delivery } } } for (int a = 0; a < toRemove.Count; a++) { int i = toRemove[a]; for (int b = 0; b < a; b++) { if (toRemove[b] < toRemove[a]) { i--; } } source.RemoveAt(i); sink.RemoveAt(i); cost.RemoveAt(i); path.RemoveAt(i); } } } }
void Start() { // Initialise minCost = new float[nNodes, nNodes]; minCostPath = new Dictionary <Vector2, int[]>(); flow = new float[nNodes, nNodes]; frameCount = 0; // Generate random nodes nodeX = RandomValues(nNodes, nNodes); nodeY = RandomValues(nNodes, nNodes); // Calculate distances distance = CalculateDistances(nodeX, nodeY); // Find shortest paths for (int a = 0; a < nNodes; a++) { for (int b = 0; b < nNodes; b++) { DijkstraSolution solution = Dijkstra.Algorithm(a, b, distance, distanceExponent); minCost[a, b] = solution.cost; UpdateMinCostPath(a, b, solution.path); } } // Generate random sources and sinks rate = RandomRates(totalProduction, minProduction, maxProduction); maxRate = 0f; foreach (float r in rate) { if (Mathf.Abs(r) > maxRate) { maxRate = Mathf.Abs(r); } } // Find each delivery option source = new List <int>(); sink = new List <int>(); cost = new List <float>(); remain = new float[nNodes]; for (int i = 0; i < nNodes; i++) { remain[i] = rate[i]; } for (int a = 0; a < nNodes; a++) { if (rate[a] > 0) { // For each source, ... for (int b = 0; b < nNodes; b++) { if (rate[b] < 0) { // ... and each sink source.Add(a); // Record source index sink.Add(b); // Record sink index cost.Add(minCost[a, b]); // Record cost of delivery } } } } }
void Start() { // Initialise flow = new float[nNodes, nNodes]; source = new List <int>(); sink = new List <int>(); cost = new List <float>(); path = new List <int[]>(); frameCount = 0; // Generate random nodes nodeX = RandomValues(nNodes, 0, nNodes); nodeY = RandomValues(nNodes, 0, nNodes); // Generate random sources and sinks rate = RandomRates(totalProduction, minProduction, maxProduction); maxRate = 0f; foreach (float r in rate) { if (Mathf.Abs(r) > maxRate) { maxRate = Mathf.Abs(r); } } // Generate maximum node flows nodeMaxFlow = RandomValues(nNodes, minFlowLimit, maxFlowLimit); // Initialise rate and flow trackers remainingRate = new float[nNodes]; for (int i = 0; i < nNodes; i++) { remainingRate[i] = rate[i]; } remainingFlow = new float[nNodes]; for (int i = 0; i < nNodes; i++) { remainingFlow[i] = nodeMaxFlow[i]; } // Calculate distances distance = CalculateDistances(nodeX, nodeY); // Find each delivery option for (int a = 0; a < nNodes; a++) { if (rate[a] > 0) { // For each source, ... for (int b = 0; b < nNodes; b++) { if (rate[b] < 0) { // ... and each sink source.Add(a); // Record source index sink.Add(b); // Record sink index DijkstraSolution solution = Dijkstra.Algorithm(a, b, distance, distanceExponent); float minCost = solution.cost; cost.Add(minCost); // Record cost of delivery path.Add(solution.path); // Record path of delivery } } } } }
void Start() { // Initialise nodeX = new float[nNodes]; nodeY = new float[nNodes]; distance = new float[nNodes, nNodes]; minCost = new float[nNodes, nNodes]; minCostPath = new Dictionary <Vector2, int[]>(); rate = new float[nNodes]; flow = new float[nNodes, nNodes]; frameCount = 0; // Generate random nodes for (int n = 0; n < nNodes; n++) { nodeX[n] = Random.value * 10f; nodeY[n] = Random.value * 10f; } // Calculate distances for (int m = 0; m < nNodes; m++) { for (int n = 0; n < nNodes; n++) { distance[m, n] = Mathf.Sqrt(Mathf.Pow(nodeX[m] - nodeX[n], 2) + Mathf.Pow(nodeY[m] - nodeY[n], 2)); } } // Find shortest paths for (int m = 0; m < nNodes; m++) { for (int n = 0; n < nNodes; n++) { DijkstraSolution solution = Dijkstra.Algorithm(m, n, distance, distanceExponent); minCost[m, n] = solution.cost; UpdateMinCostPath(m, n, solution.path); } } // Generate random sources and sinks for (int i = 0; i < nEnds; i++) { int n = Mathf.FloorToInt(Random.value * nNodes); if (rate[n] == 0) { rate[n] = 1; } else { i--; } } for (int i = 0; i < nEnds; i++) { int n = Mathf.FloorToInt(Random.value * nNodes); if (rate[n] == 0) { rate[n] = -1; } else { i--; } } // Find each delivery option source = new List <int>(); sink = new List <int>(); cost = new List <float>(); remain = new float[nNodes]; for (int i = 0; i < nNodes; i++) { remain[i] = rate[i]; } for (int m = 0; m < nNodes; m++) { if (rate[m] > 0) { for (int n = 0; n < nNodes; n++) { if (rate[n] < 0) { // Cost of delivering is minCost[m, n] along path minCostPath[(m, n)] source.Add(m); sink.Add(n); cost.Add(minCost[m, n]); } } } } }