예제 #1
0
        /// <summary>
        /// Parses a TSP-lib file and creates a TSPLIBProblem.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static TSPLIBProblem ParseFromFile(FileInfo file)
        {
            TSPLIBProblemTypeEnum?      problem_type = null;
            TSPLIBProblemWeightTypeEnum?weight_type  = null;
            int size = -1;

            double[][] weights = null;
            string     comment = string.Empty;
            string     name    = string.Empty;

            StreamReader reader = file.OpenText();

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine().Trim().Replace(" :", ":");

                if (line.StartsWith(TOKEN_NAME))
                {
                    name = line.Replace(TOKEN_NAME, string.Empty).Trim();
                }
                else if (line.StartsWith(TOKEN_TYPE))
                {
                    string type = line.Replace(TOKEN_TYPE, string.Empty).Trim();
                    switch (type.ToUpper())
                    {
                    case TOKEN_TYPE_VALUE_TSP:
                        problem_type = TSPLIBProblemTypeEnum.TSP;
                        break;

                    case TOKEN_TYPE_VALUE_ATSP:
                        problem_type = TSPLIBProblemTypeEnum.ATSP;
                        break;
                    }
                }
                else if (line.StartsWith(TOKEN_COMMENT))
                {
                    comment = line.Replace(TOKEN_COMMENT, string.Empty).Trim();
                }
                else if (line.StartsWith(TOKEN_DIMENSION))
                {
                    string dimension = line.Replace(TOKEN_DIMENSION, string.Empty).Trim();
                    size = int.Parse(dimension);
                }
                else if (line.StartsWith(TOKEN_EDGE_WEIGHT_TYPE))
                {
                    string edge_weight_type = line.Replace(TOKEN_EDGE_WEIGHT_TYPE, string.Empty).Trim();
                    switch (edge_weight_type.ToUpper())
                    {
                    case TOKEN_EDGE_WEIGHT_TYPE_VALUE_EUC_2D:
                        weight_type = TSPLIBProblemWeightTypeEnum.Euclidian2D;
                        break;

                    case TOKEN_EDGE_WEIGHT_TYPE_EXPLICIT:
                        weight_type = TSPLIBProblemWeightTypeEnum.Explicit;
                        break;
                    }
                }
                else if (line.StartsWith(TOKEN_EDGE_WEIGHT_SECTION))
                {
                    weights    = new double[size][];
                    weights[0] = new double[size];

                    int x = 0;
                    int y = 0;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine().Trim();

                        if (line.StartsWith(TOKEN_EOF))
                        {
                            break;
                        }
                        else
                        {
                            if (line != null && line.Length > 0)
                            {
                                string[] splitted_line = Regex.Split(line, @"\s+");
                                foreach (string weight_string in splitted_line)
                                {
                                    weights[x][y] = float.Parse(weight_string, CultureInfo.InvariantCulture);

                                    if (x == y)
                                    {
                                        weights[x][y] = 0;
                                    }

                                    if (y == size - 1)
                                    {
                                        x = x + 1;
                                        if (x < size)
                                        {
                                            weights[x] = new double[size];
                                            y          = 0;
                                        }
                                    }
                                    else
                                    {
                                        y = y + 1;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (line.StartsWith(TOKEN_NODE_COORD_SECTION))
                {
                    var points = new List <Point>();
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine().Trim();

                        if (line.StartsWith(TOKEN_EOF))
                        {
                            break;
                        }
                        else
                        {
                            var splitted_line = Regex.Split(line, @"\s+");
                            int idx           = (int)double.Parse(splitted_line[0]);
                            int x             = (int)double.Parse(splitted_line[1]);
                            int y             = (int)double.Parse(splitted_line[2]);

                            Point p = new Point(x, y);
                            points.Add(p);
                        }
                    }

                    weights = TSPLIBProblemParser.CalculateEuclideanWeights(points);
                }
            }

            return(new TSPLIBProblem(name, comment, size, weights, weight_type.Value, problem_type.Value));
        }
예제 #2
0
 /// <summary>
 /// Parses a TSP-lib file and creates a TSPLIBProblem.
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static TSPLIBProblem ParseFrom(string path)
 {
     return(TSPLIBProblemParser.ParseFromFile(new FileInfo(path)));
 }