Exemple #1
0
        static TSPLIBProblem CreateProblem(string path, string file, float best)
        {
            TSPLIBProblem problem = TSPLIBProblemParser.ParseFromFile(new FileInfo(new FileInfo(
                                                                                       Assembly.GetExecutingAssembly().Location).DirectoryName + string.Format(path, file)));

            problem.Best = best;
            return(problem);
        }
 /// <summary>
 /// Writes the given TSP problem to the given file.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="problem"></param>
 public static void Write(StreamWriter writer, TSPLIBProblem problem)
 {
     if (problem.Symmetric)
     {
         TSPLIBProblemWriter.GenerateTSP(writer, problem);
     }
     else
     {
         TSPLIBProblemWriter.GenerateATSP(writer, problem);
     }
 }
        private static void GenerateTSP(StreamWriter writer, TSPLIBProblem problem)
        {
            writer.WriteLine(string.Format("NAME: {0}", problem.Name));
            writer.WriteLine("TYPE: TSP");
            writer.WriteLine(string.Format("COMMENT: {0}", problem.Comment));
            writer.WriteLine(string.Format("DIMENSION: {0}", problem.Size));
            writer.WriteLine("EDGE_WEIGHT_TYPE: EXPLICIT");
            writer.WriteLine("EDGE_WEIGHT_FORMAT: FULL_MATRIX");
            //writer.WriteLine("EDGE_WEIGHT_FORMAT: UPPER_ROW");
            writer.WriteLine("DISPLAY_DATA_TYPE: TWOD_DISPLAY");
            writer.WriteLine("EDGE_WEIGHT_SECTION");

            // get the biggest weight.
            int max = 0;

            int[][] upper_rows = new int[problem.Size][];
            for (int x = 0; x < problem.Size; x++)
            {
                upper_rows[x] = new int[problem.Size];
                for (int y = 0; y < problem.Size; y++)
                {
                    int value = (int)problem.WeightMatrix[x][y];
                    if (value > max)
                    {
                        max = value;
                    }
                    upper_rows[x][y] = value;
                }
            }

            int length = max.ToString().Length;

            for (int x = 0; x < upper_rows.Length; x++)
            {
                for (int y = 0; y < upper_rows[x].Length; y++)
                {
                    if (y > 0)
                    {
                        writer.Write(" ");
                    }
                    writer.Write(upper_rows[x][y].ToString().PadLeft(length));
                }
                writer.WriteLine();
            }
            writer.WriteLine("EOF");
            writer.Flush();
        }
        /// <summary>
        /// Converts the given A-TSP to a symetric equivalent S-TSP.
        /// </summary>
        /// <param name="atsp"></param>
        /// <returns></returns>
        public static TSPLIBProblem Convert(TSPLIBProblem atsp)
        {
            // check if the problem is not already symmetric.
            if (atsp.Symmetric)
            {
                return(atsp);
            }

            // the problem is not symmetric, convert it.
            var name    = atsp.Name + "(SYM)";
            var comment = atsp.Comment;

            // convert the problem to a symetric one.
            var symetric = atsp.ConvertToSymmetric();

            return(new TSPLIBProblem(name, comment, symetric.Size, symetric.WeightMatrix,
                                     TSPLIBProblemWeightTypeEnum.Explicit, TSPLIBProblemTypeEnum.TSP));
        }
Exemple #5
0
        private double CalculateWeight(IRoute route, TSPLIBProblem problem)
        {
            int    previous = -1;
            int    first    = -1;
            double weight   = 0;

            foreach (int customer in route)
            {
                if (first < 0)
                {
                    first = customer;
                }
                if (previous < 0)
                {
                }
                else
                {
                    weight = weight + problem.WeightMatrix[previous][customer];
                }
                previous = customer;
            }
            weight = weight + problem.WeightMatrix[previous][first];
            return(weight);
        }
        /// <summary>
        /// Writes the given TSP problem to the given file.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="problem"></param>
        public static void Write(FileInfo file, TSPLIBProblem problem)
        {
            StreamWriter writer = new StreamWriter(file.OpenWrite());

            TSPLIBProblemWriter.Write(writer, problem);
        }
Exemple #7
0
        /// <summary>
        /// Does the testing.
        /// </summary>
        public void StartTests()
        {
            // open a writer to the output file.
            FileInfo     output_file = new FileInfo(string.Format("{0}.log", _name));
            StreamWriter writer      = output_file.AppendText();

            //Tools.Core.Output.OutputStreamHost.WriteLine("====== {0} started! ======", _name);
            //Tools.Core.Output.OutputStreamHost.WriteLine();
            //Tools.Core.Output.OutputStreamHost.WriteLine("Started: {0}", problem.Name);
            OsmSharp.Logging.Log.TraceEvent("TSPLIBTester.StartTests", Logging.TraceEventType.Information,
                                            string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}",
                                                          PadRight("Problem", 15),
                                                          PadRight("Name", 40),
                                                          PadRight("Time"),
                                                          PadRight("Time (Avg)"),
                                                          PadRight("# Runs", 6),
                                                          PadRight("# Bests", 7),
                                                          PadRight("Avg"),
                                                          PadRight("Best"),
                                                          PadRight("Worst"),
                                                          PadRight("Optimal"),
                                                          PadRight("%")));

            // test each combination of solver and problem test_count-times.
            for (int problem_idx = 0; problem_idx < _problems.Count; problem_idx++)
            { // get the problem instance.
                TSPLIBProblem problem = _problems[problem_idx];

                // loop over all solvers.
                for (int solver_idx = 0; solver_idx < _solvers.Count; solver_idx++)
                { // get the solver instance.
                    ISolver solver = _solvers[solver_idx];
                    //Tools.Core.Output.OutputStreamHost.WriteLine("Started: {0} -> {1}", problem.Name, solver.Name);

                    // do the tests.
                    double best  = float.MaxValue;
                    double worst = float.MinValue;

                    int    best_count  = 0;
                    long   start_ticks = DateTime.Now.Ticks;
                    double total       = 0;
                    int    test_count  = _test_count;
                    while (test_count > 0)
                    {
                        // do the actual test.
                        IRoute route = solver.Solve(problem);
                        // calculate the weight.
                        double weight = this.CalculateWeight(route, problem);
                        total = total + weight;
                        if (best > weight)
                        {
                            best = weight;
                        }
                        if (worst < weight)
                        {
                            worst = weight;
                        }

                        if (weight == problem.Best)
                        {
                            best_count++;
                        }

                        test_count--; // a test has been done
                    }
                    long stop_ticks = DateTime.Now.Ticks;

                    // report the result.
                    TimeSpan time = new TimeSpan(stop_ticks - start_ticks);
                    string   line = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};",
                                                  ToStringEmptyWhenNull(problem.Name),
                                                  ToStringEmptyWhenNull(solver.Name),
                                                  ToStringEmptyWhenNull(problem.Best),
                                                  ToStringEmptyWhenNull(_test_count),
                                                  ToStringEmptyWhenNull(System.Math.Round(time.TotalSeconds / (double)_test_count, 3)),
                                                  ToStringEmptyWhenNull(best_count),
                                                  ToStringEmptyWhenNull(System.Math.Round(total / (double)_test_count, 3)),
                                                  ToStringEmptyWhenNull(best),
                                                  ToStringEmptyWhenNull(worst));
                    OsmSharp.Logging.Log.TraceEvent("TSPLIBTester.StartTests", Logging.TraceEventType.Information,
                                                    string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}",
                                                                  PadRight(problem.Name, 15),
                                                                  PadRight(solver.Name, 40),
                                                                  PadRight(System.Math.Round(time.TotalSeconds, 3)),
                                                                  PadRight(System.Math.Round(time.TotalSeconds / (double)_test_count, 3)),
                                                                  PadRight(_test_count, 6),
                                                                  PadRight(best_count, 7),
                                                                  PadRight(System.Math.Round(total / (double)_test_count, 3)),
                                                                  PadRight(best),
                                                                  PadRight(worst),
                                                                  PadRight(problem.Best),
                                                                  PadRight(System.Math.Round(
                                                                               (((total / (double)_test_count) - problem.Best) / problem.Best) * 100.0, 3))));
                    writer.WriteLine(line);
                    writer.Flush();
                }
            }
            writer.Flush();
            writer.Close();
        }