Пример #1
0
        private void ApplyAlgo(TspAlgo algo)
        {
            var n = m_Points.Count;
            if (n < 3)
            {
                algo.Path = null;
                return;
            }
            /*var m = new double[n,n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    m[i, j] = m_Points[i].Dist(m_Points[j]);
                }
            }

            double avgDist = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    avgDist += m[i, j];
                }
            }
            avgDist /= (n * n);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    m[i, j] /= avgDist;
                }
            }*/
            //var measure = new MatrixMeasureFactory().CreateMatrixMeasure(m_Points);
            var measure = new PointsMeasure(m_Points);
            algo.Path = algo.Solver.GetPath(n, measure);
            algo.Length = GetLength(n, m_Points, algo.Path);
        }
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            //var lines = args[0].Split('\n');
            string text;
            using (var reader = File.OpenText(args[0]))
            {
                text = reader.ReadToEnd();
            }
            var lines = text.Split('\n');

            var n = int.Parse(lines[0]);
            var points = new Point2DReal[n];
            for (int i = 0; i < n; i++)
            {
                var line = lines[i + 1].Split();
                var x = double.Parse(line[0]);
                var y = double.Parse(line[1]);
                points[i] = new Point2DReal(x, y);
            }
            /*var matrix = new double[n,n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = points[i].Dist(points[j]);
                }
            }

            double avgDist = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    avgDist += matrix[i, j];
                }
            }
            avgDist /= (n * n);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] /= avgDist;
                }
            }*/

            IMeasure measure;
            if (n <= 6000)
            {
                measure = new MatrixMeasureFactory().CreateMatrixMeasure(points);
            }
            else
            {
                measure = new PointsMeasure(points);
            }

            var path = new Opt3().GetPath(n, measure);

            var ans = points[path[0]].Dist(points[path[n - 1]]);
            for (int i = 0; i < n - 1; i++)
            {
                ans += points[path[i]].Dist(points[path[i + 1]]);
            }

            Console.WriteLine("{0} 0", ans);
            Console.WriteLine(string.Join(" ", path));
        }