Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/21/10 23:55:23 (14 years ago)
Author:
swagner
Message:

Implemented import of optimal TSP solutions (#924).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPLIBParser.cs

    r2883 r3151  
    2626namespace HeuristicLab.Problems.TSP {
    2727  /// <summary>
    28   /// Parses a *.tsp file in the TSPLIB format and extracts its information about a TSP.
     28  /// Parses a *.tsp file in TSPLIB format and extracts its information about a TSP.
    2929  /// </summary>
    3030  public class TSPLIBParser {
     
    3232    private const int NAME = 1;
    3333    private const int TYPE = 2;
    34     private const int DIM = 3;
    35     private const int WEIGHTTYPE = 4;
    36     private const int NODETYPE = 5;
    37     private const int NODESECTION = 6;
     34    private const int COMMENT = 3;
     35    private const int DIM = 4;
     36    private const int WEIGHTTYPE = 5;
     37    private const int NODETYPE = 6;
     38    private const int NODESECTION = 7;
    3839
    3940    private StreamReader source;
    4041
    41     private string myName;
     42    private string name;
    4243    /// <summary>
    4344    /// Gets the name of the parsed TSP.
    4445    /// </summary>
    4546    public string Name {
    46       get { return myName; }
    47     }
    48     private double[,] myVertices;
     47      get { return name; }
     48    }
     49    private string comment;
     50    /// <summary>
     51    /// Gets the comment of the parsed TSP.
     52    /// </summary>
     53    public string Comment {
     54      get { return comment; }
     55    }
     56    private double[,] vertices;
    4957    /// <summary>
    5058    /// Gets the vertices of the parsed TSP.
    5159    /// </summary>
    5260    public double[,] Vertices {
    53       get { return myVertices; }
    54     }
    55     private int myWeightType;
     61      get { return vertices; }
     62    }
     63    private int weightType;
    5664    /// <summary>
    5765    /// Gets the weight type of the parsed TSP.
    5866    /// </summary>
    5967    public int WeightType {
    60       get { return myWeightType; }
    61     }
    62 
    63     /// <summary>
    64     /// Initializes a new instance of <see cref="TSPParser"/> with the given <paramref name="path"/>.
    65     /// </summary>
    66     /// <exception cref="ArgumentException">Thrown when the input file name is not in TSP format (*.tsp)
     68      get { return weightType; }
     69    }
     70
     71    /// <summary>
     72    /// Initializes a new instance of <see cref="TSPLIBParser"/> with the given <paramref name="path"/>.
     73    /// </summary>
     74    /// <exception cref="ArgumentException">Thrown if the input file is not a TSPLIB TSP file (*.tsp)
    6775    /// </exception>
    6876    /// <param name="path">The path where the TSP is stored.</param>
    6977    public TSPLIBParser(String path) {
    7078      if (!path.EndsWith(".tsp"))
    71         throw new ArgumentException("Input file name has to be in TSP format (*.tsp)");
     79        throw new ArgumentException("Input file has to be a TSPLIB TSP file (*.tsp).");
    7280
    7381      source = new StreamReader(path);
    74       myName = path;
    75       myVertices = null;
    76       myWeightType = -1;
    77     }
    78 
    79     /// <summary>
    80     /// Reads the TSP file and parses the elements.
    81     /// </summary>
    82     /// <exception cref="InvalidDataException">Thrown when file contains unknown (edge) types.</exception>
     82      name = path;
     83      comment = string.Empty;
     84      vertices = null;
     85      weightType = -1;
     86    }
     87
     88    /// <summary>
     89    /// Reads the TSPLIB TSP file and parses the elements.
     90    /// </summary>
     91    /// <exception cref="InvalidDataException">Thrown if the file has an invalid format or contains invalid data.</exception>
    8392    public void Parse() {
    8493      int section = -1;
     
    100109              typeIsChecked = true;
    101110              break;
     111            case COMMENT:
     112              ReadComment(str);
     113              break;
    102114            case DIM:
    103115              InitVerticesArray(str);
     
    118130
    119131      if (!(typeIsChecked && weightTypeIsChecked))
    120         throw new InvalidDataException("File contains unknown (edge) types");
     132        throw new InvalidDataException("Input file does not contain type or edge weight type information.");
    121133    }
    122134
     
    136148      if (token.Equals("type", StringComparison.OrdinalIgnoreCase))
    137149        return TYPE;
     150      if (token.Equals("comment", StringComparison.OrdinalIgnoreCase))
     151        return COMMENT;
    138152      if (token.Equals("dimension", StringComparison.OrdinalIgnoreCase))
    139153        return DIM;
     
    150164    private void ReadName(string str) {
    151165      string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None);
    152       myName = tokens[tokens.Length - 1].Trim();
     166      name = tokens[tokens.Length - 1].Trim();
    153167    }
    154168
     
    158172      string type = tokens[tokens.Length - 1].Trim();
    159173      if (!type.Equals("tsp", StringComparison.OrdinalIgnoreCase))
    160         throw new InvalidDataException("Input data format is not \"TSP\"");
     174        throw new InvalidDataException("Input file type is not \"TSP\"");
     175    }
     176
     177    private void ReadComment(string str) {
     178      string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None);
     179      comment = tokens[tokens.Length - 1].Trim();
    161180    }
    162181
     
    166185
    167186      int dim = Int32.Parse(dimension);
    168       myVertices = new double[dim, 2];
     187      vertices = new double[dim, 2];
    169188    }
    170189
     
    174193
    175194      if (type.Equals("euc_2d", StringComparison.OrdinalIgnoreCase))
    176         myWeightType = 0;
     195        weightType = 0;
    177196      else if (type.Equals("geo", StringComparison.OrdinalIgnoreCase))
    178         myWeightType = 1;
     197        weightType = 1;
    179198      else
    180         throw new InvalidDataException("Unsupported type of edge weights");
     199        throw new InvalidDataException("Input file contains an unsupported edge weight type (only \"EUC_2D\" and \"GEO\" are supported).");
    181200    }
    182201
     
    186205
    187206      if (!type.Equals("twod_coords", StringComparison.OrdinalIgnoreCase))
    188         throw new InvalidDataException("Unsupported node type");
     207        throw new InvalidDataException("Input file contains an unsupported node coordinates type (only \"TWOD_COORDS\" is supported).");
    189208    }
    190209
    191210    private void ReadVertices() {
    192       if (myVertices == null)
    193         throw new InvalidDataException("Dimension not found");
    194 
    195       for (int i = 0; i < (myVertices.Length / 2); i++) {
     211      if (vertices == null)
     212        throw new InvalidDataException("Input file does not contain dimension information.");
     213
     214      for (int i = 0; i < (vertices.Length / 2); i++) {
    196215        string str = source.ReadLine();
    197216        string[] tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
    198217
    199218        if (tokens.Length != 3)
    200           throw new InvalidDataException("Invalid node format");
     219          throw new InvalidDataException("Input file contains invalid node coordinates.");
    201220
    202221        CultureInfo culture = new CultureInfo("en-US");
    203         myVertices[i, 0] = Double.Parse(tokens[1], culture.NumberFormat);
    204         myVertices[i, 1] = Double.Parse(tokens[2], culture.NumberFormat);
     222        vertices[i, 0] = double.Parse(tokens[1], culture.NumberFormat);
     223        vertices[i, 1] = double.Parse(tokens[2], culture.NumberFormat);
    205224      }
    206225    }
Note: See TracChangeset for help on using the changeset viewer.