Beispiel #1
0
        public static IEnumerable <Method.Path> Process(double[][] matrix)
        {
            var obj  = new TSPSolver();
            var step = obj.solve(matrix.Length, cloneFull(matrix));

            if (obj.route.Count <= 0)
            {
                yield break;
            }
            var first = obj.route.Keys.Min();
            var prev  = first;
            var next  = 0;
            var found = false;

            while ((next = obj.getNextRoute(prev, out found)) != first && found)
            {
                yield return(new Method.Path {
                    From = prev, To = next, Distance = (int)matrix[prev][next]
                });

                prev = next;
            }
            if (found)
            {
                yield return new Method.Path {
                           From = prev, To = next, Distance = (int)matrix[prev][next]
                }
            }
            ;
        }
Beispiel #2
0
        public static string Test()
        {
            var m0 = new int[5, 5] {
                { -1, 20, 18, 12, 8 }, { 5, -1, 14, 7, 11 }, { 12, 18, -1, 6, 11 }, { 11, 17, 11, -1, 12 }, { 5, 5, 5, 5, -1 }
            };
            var m1 = new double[m0.GetLength(0)][];

            for (var i = 0; i < m1.Length; i++)
            {
                m1[i] = new double[m1.Length];
                for (var j = 0; j < m1.Length; j++)
                {
                    m1[i][j] = m0[i, j] > 0 ? m0[i, j] : int.MaxValue;
                }
            }
            var obj  = new TSPSolver();
            var step = obj.solve(5, m1);

            return(obj.getSortedPath("a b c d e f"));
        }
Beispiel #3
0
        public static IEnumerable <Method.Path> Process(string[] screen, World world0)
        {
            var dict = new Dictionary <Tuple <int, int>, int>();

            for (var y = 0; y < screen.Length; y++)
            {
                for (var x = 0; x < screen[y].Length; x++)
                {
                    if (screen[y][x] == '*')
                    {
                        dict[Tuple.Create(y, x)] = dict.Count;
                    }
                }
            }
            var matrix = Enumerable.Range(0, dict.Count)
                         .Select(_ => Enumerable.Range(0, dict.Count).Select(__ => double.PositiveInfinity).ToArray())
                         .ToArray();
            var isolated = new HashSet <int>();

            foreach (var item in dict.ToArray())
            {
                var src  = item.Value;
                var wave = new WaveOld(world0.Clone(), "*", ":*", null);
                wave.Update(item.Key.Item1, item.Key.Item2, true, 0);
                foreach (var scored in wave.Scored)
                {
                    var coord = Tuple.Create(scored.Y, scored.X);
                    if (dict.ContainsKey(coord))
                    {
                        var dst = dict[coord];
                        var val = scored.StepsCount < 20 ? 20 : 10000;
                        matrix[src][dst] = val;
                    }
                }
                if (wave.Scored.Length == 0)
                {
                    dict.Remove(item.Key);
                    isolated.Add(src);
                }
            }
            if (isolated.Count != 0)
            {
                matrix = matrix.Where((x, i) => !isolated.Contains(i))
                         .Select(z => z.Where((x, i) => !isolated.Contains(i)).ToArray())
                         .ToArray();
            }

            //for (var i = 0; i < matrix.Length; i++)
            //    System.Diagnostics.Trace.WriteLine("[" + i + "] " + string.Join(" ", matrix[i]));

            var obj   = new TSPSolver();
            var step  = obj.solve(dict.Count, cloneFull(matrix));
            var first = obj.route.Keys.Min();
            var prev  = first;
            var next  = 0;
            var found = false;

            while ((next = obj.getNextRoute(prev, out found)) != first && found)
            {
                yield return(new Method.Path {
                    From = prev, To = next, Distance = (int)matrix[prev][next]
                });

                prev = next;
            }
            if (found)
            {
                yield return new Method.Path {
                           From = prev, To = next, Distance = (int)matrix[prev][next]
                }
            }
            ;
        }