/// <summary> /// ��������� ����� � ����������� ��������� ����� ������������ ������ ��������������� /// </summary> /// <param name="expansionMatrices">������� ���������������</param> /// <param name="allPoints">��� ����� ����</param> /// <returns>����� � ����������� ������</returns> private static AStarPoint GetMinCostPoint(IDictionary <AStarPoint, ExpansionMatrixContainer> expansionMatrices, IEnumerable <AStarPoint> allPoints) { var summCosts = new Dictionary <AStarPoint, double>(); foreach (var matrixPoint in allPoints) { summCosts.Add(matrixPoint, 0d); foreach (var startPoint in expansionMatrices.Keys) { summCosts[matrixPoint] += expansionMatrices[startPoint].ExpansionMatrix[matrixPoint]; } } AStarPoint cps = null; var summCost = double.MaxValue; foreach (var matrixPoint in summCosts.Keys) { if (summCosts[matrixPoint] < summCost) { cps = matrixPoint; summCost = summCosts[matrixPoint]; } } return(cps); }
/// <summary> /// ��������� ����� � ����������� ��������� ������� �� (� ��) ������� /// </summary> /// <param name="expansionMatrices">������� ���������������</param> /// <param name="notTraversedStartPoints">������ ������������ �����. ����� ��� ����� ����������� ����� �����������</param> /// <param name="collectionPoint">������� �����</param> /// <returns>����� � ����������� ��������� �������</returns> private static AStarPoint GetNearestPoint( IDictionary <AStarPoint, ExpansionMatrixContainer> expansionMatrices, IEnumerable <AStarPoint> notTraversedStartPoints, AStarPoint collectionPoint) { AStarPoint nearestPoint = null; var minCost = double.MaxValue; foreach (var point in notTraversedStartPoints) { if (expansionMatrices[point].ExpansionMatrix[collectionPoint] < minCost) { nearestPoint = point; minCost = expansionMatrices[point].ExpansionMatrix[collectionPoint]; } } return(nearestPoint); }
/// <summary> /// ����� ����� � ����������� ������������� �������� (F) /// </summary> /// <param name="points">������ �����</param> /// <returns>����� � ����������� ������������� ��������</returns> private static AStarPoint GetPointWithMinF(IEnumerable <AStarPoint> points) { if (!points.Any()) { throw new Exception("������ ������ �����"); } var minF = double.MaxValue; AStarPoint resultPoint = null; foreach (var point in points) { if (point.F < minF) { minF = point.F; resultPoint = point; } } return(resultPoint); }
/// <summary> /// ������ ������� ��������������� /// </summary> /// <param name="start">�����, ��� ������� �������������� ������� ���������������</param> /// <param name="goal">������� �����. ���� null, �� ������� ��������������� �������������� �� ��������� ����� �� ���� ��������� ����� ����</param> /// <param name="allPoints">��� ����� ����</param> /// <returns>������� ���������������</returns> private static ExpansionMatrixContainer GetExpansionMatrix(AStarPoint start, AStarPoint goal, IEnumerable <AStarPoint> allPoints) { foreach (var point in allPoints) { point.CameFromPoint = null; } var emc = new ExpansionMatrixContainer { ExpansionMatrix = new Dictionary <AStarPoint, double>(), //Path = new Dictionary<Point, IList<Point>>() }; var closedSet = new HashSet <AStarPoint>(); var openSet = new HashSet <AStarPoint> { start }; start.G = 0d; start.H = goal == null ? 0d : start.GetHeuristicCost(goal); var pathFound = false; while (openSet.Count > 0) { var x = GetPointWithMinF(openSet); if (goal != null && x == goal) { pathFound = true; break; } openSet.Remove(x); closedSet.Add(x); emc.ExpansionMatrix.Add(x, x.G); //emc.Path.Add(x, ReconstructPath(x)); var neighbors = x.GetNeighbors(); foreach (var y in neighbors) { if (closedSet.Contains(y)) { continue; } var tentativeGScore = x.G + x.GetCost(y); bool tentativeIsBetter; if (!openSet.Contains(y)) { openSet.Add(y); tentativeIsBetter = true; } else { tentativeIsBetter = tentativeGScore < y.G; } if (tentativeIsBetter) { y.CameFromPoint = x; y.G = tentativeGScore; y.H = goal == null ? 0d : y.GetHeuristicCost(goal); } } } if (goal != null && !pathFound) { throw new Exception("���� �� �������� ����� �� ������"); } return(emc); }
public Infrastructure(AStarPoint cps, IEnumerable <IList <AStarPoint> > channels) { Cps = cps; Channels = channels; }