public int GetgenomeLenght() { const int bitsPerPoint = 4; int pointsCount = PathMatrix.GetLength(0); return(bitsPerPoint * pointsCount); }
private void findPath() { int row = PathMatrix.GetLength(0), col = PathMatrix.GetLength(1); Stack <int[]> stack = new Stack <int[]>(); char[,] path = new char[row, col]; int length = 1; for (int i = row - 1, j = col - 1; i > 0 || j > 0; length++) { char c = PathMatrix[i, j]; stack.Push(new int[] { i, j }); path[i, j] = c; if (c == '←') { j--; } else if (c == '↑') { i--; } else if (c == '↖') { i--; j--; } } stack.Push(new int[] { 0, 0 }); path[0, 0] = 'o'; this.PathLength = length; this.Map = new List <int[]>(); while (stack.Count > 0) { Map.Add(stack.Pop()); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { sb.Append(path[i, j] == '\0' ? ' ' : path[i, j]); } sb.Append('\n'); } this.PathStr = sb.ToString(); }
private int CalculateAveragePathLenght() { var lenght = 0; for (int i = 0; i < PathMatrix.GetLength(0); i++) { for (int j = 0; j < PathMatrix.GetLength(1); j++) { if (PathMatrix[i, j] != 0) { lenght += PathMatrix[i, j]; } } } lenght /= PathMatrix.Length; return(lenght * PathMatrix.GetLength(0)); }
public void MakeTurn() { var graph = new Graph(MapData.Select(x => new MapCell { Location = x.Key, SurfaceType = x.Value }), Start, Finish); var node = graph.GetNodes(Position).FirstOrDefault(n => n.Direction == Direction && n.Speed == Speed); if (node == null) { throw new Exception("Unable to find ourselves in the map graph"); } var paths = new PathMatrix(graph, node); var reachable = paths.GetReachableNodes(); var target = reachable.Where(n => n.node.Location == Finish).Select(n => n.node).FirstOrDefault() ?? reachable.Where(n => n.node.HasNearbyTi) .OrderBy(n => n.node.SurfaceType) .ThenBy(n => n.node.Speed) .ThenBy(n => n.node.TiCount) .ThenBy(n => n.distance) .ThenBy(n => n.node.Location.Distance(Finish)) .Select(n => n.node).FirstOrDefault(); if (target == null) { throw new Exception("Nowhere to go"); } Console.WriteLine($"Chosen {target} as final target"); var path = paths.GetPath(target); if (path == null) { Console.WriteLine("Bug in pathfinder"); } var nextCell = path.First(x => x.Location != Position); Navigate(nextCell); }
/// <summary> /// Converts genome (array of bits) to the path through the all cities (where index in array = sequential number of city; value = city "name") /// </summary> /// <param name="genome"></param> /// <example> /// Input: 0010.0110.1000.1001.0001 /// Covert bits to int ↓ /// 2.6.8.9.1 /// Convert to path ↓ /// 5.1.2.3.4 /// Path (5=>1=>2=>3=>4) /// </example> /// <returns></returns> private int[] genomeToPath(IIndividual genome) { const int bitsPerPoint = 4; int pointsCount = PathMatrix.GetLength(0); if (genome.Count() != pointsCount * bitsPerPoint) { throw new ArgumentException(); } return(Enumerable.Range(0, pointsCount) .Select(i => new { city = i, number = BitsToInt(genome .Skip(i * bitsPerPoint) .Take(bitsPerPoint) .ToArray()) }) .OrderBy(x => x.number) .Select(x => x.city) .ToArray()); }