/// <summary> /// Creates a new instance of TSP file data extractor /// </summary> /// <param name="tspFile">tsp file to use</param> public TspFileDataExtractor(TspFile tspFile) { if (tspFile == null) { throw new ArgumentNullException("tspFile"); } this.tspFile = tspFile; }
/// <summary> /// Creates a new instance of TSP file data extractor /// </summary> /// <param name="tspFile">tsp file to use</param> public TspFileDataExtractor(TspFile tspFile) { if (tspFile == null) { throw new ArgumentNullException("tspFile"); } this.tspFile = tspFile; }
/// <summary> /// Loads TSP file /// </summary> /// <param name="fileName">file to load</param> /// <returns>Loaed tsp file</returns> public static TspFile Load(string fileName) { TspFile result = null; TspFileLoader loader = new TspFileLoader(); using (StreamReader reader = new StreamReader(fileName)) { result = loader.Load(reader); reader.Close(); } return(result); }
/// <summary> /// Loads TSP from string /// </summary> /// <param name="content">tsp task</param> /// <returns>Load tsp file</returns> public static TspFile LoadFromString(string content) { TspFile result = null; TspFileLoader loader = new TspFileLoader(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(content))) { using (StreamReader reader = new StreamReader(ms)) { result = loader.Load(reader); reader.Close(); } } return(result); }
/// <summary> /// Create tour from TSP file /// </summary> /// <param name="tspFile">tsp file to load from</param> /// <returns>Tour read from tsp file</returns> public static Tour FromTspFile(TspFile tspFile) { if (tspFile.Type != TSP.Defines.FileType.TOUR) { throw new ArgumentOutOfRangeException("tspFile"); } return new Tour(tspFile.Name, tspFile.Comment, tspFile.Dimension, tspFile.Tour); }
/// <summary> /// Loads TSP File structure from a stream /// </summary> /// <param name="reader">stream reader</param> /// <returns>TSP File structure loaded from a stream</returns> public TspFile Load(System.IO.StreamReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } List <string> lines = new List <string>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); line = line.Trim(); if (!string.IsNullOrEmpty(line)) { lines.Add(line); } } TspFile tspFile = new TspFile(); List <string[]> sections = SplitToSections(lines); foreach (string[] section in sections) { string line = section[0]; if (line.StartsWith("Name", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Name = this.ReadStringFromLine(line); } else if (line.StartsWith("Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Type = this.ReadFileTypeFromLine(line); } else if (line.StartsWith("Comment", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Comment = this.ReadStringFromLine(line); } else if (line.StartsWith("Dimension", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Dimension = this.ReadIntFromLine(line); } else if (line.StartsWith("Capacity", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Capacity = this.ReadIntFromLine(line); } else if (line.StartsWith("Edge_Weight_Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeWeightType = this.ReadEdgeWeightTypeFromLine(line); } else if (line.StartsWith("Edge_Weight_Format", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeWeightFormat = this.ReadEdgeWeightFormatFromLine(line); } else if (line.StartsWith("Edge_Data_Format", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeDataFormat = this.ReadEdgeDataFormatFromLine(line); } else if (line.StartsWith("Node_Coord_Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.NodeCoordinatesType = this.ReadNodeCoordinatesTypeFromLine(line); } else if (line.StartsWith("Display_Data_Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.DisplayDataType = this.ReadDisplayDataTypeFromLine(line); } else if (line.StartsWith("Node_Coord_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.DisplayDataType = Defines.DisplayDataType.Coordinates; if (tspFile.NodeCoordinatesType == Defines.NodeCoordinatesType.NoCoordinates) { tspFile.NodeCoordinatesType = Defines.NodeCoordinatesType.Coordinates2D; } if (tspFile.NodeCoordinatesType == Defines.NodeCoordinatesType.Coordinates2D) { tspFile.Nodes = this.ReadIntAndDoublesArrayList(section); } else if (tspFile.NodeCoordinatesType == Defines.NodeCoordinatesType.Coordinates3D) { tspFile.Nodes = this.ReadIntAndDoublesArrayList(section); } else { throw new NotSupportedException(); } } else if (line.StartsWith("Depot_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Depots = this.ReadIntList(section); } else if (line.StartsWith("Demand_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Demands = this.ReadIntsArrayList(section); } else if (line.StartsWith("Edge_Data_Section", StringComparison.InvariantCultureIgnoreCase)) { if (tspFile.EdgeDataFormat == Defines.EdgeDataFormat.EdgeList) { tspFile.Edges = this.ReadIntsArrayList(section); } else if (tspFile.EdgeDataFormat == Defines.EdgeDataFormat.AdjacencyList) { tspFile.Edges = this.ReadIntsArrayList(section); } else { throw new NotSupportedException(); } } else if (line.StartsWith("Fixed_Edges", StringComparison.InvariantCultureIgnoreCase)) { tspFile.FixedEdges = this.ReadIntsArrayList(section); } else if (line.StartsWith("Display_Data_Section", StringComparison.InvariantCultureIgnoreCase)) { if (tspFile.DisplayDataType == Defines.DisplayDataType.Display2D) { tspFile.DisplayNodes = this.ReadIntAndDoublesArrayList(section); } else { throw new NotSupportedException(); } } else if (line.StartsWith("Tour_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Tour = this.ReadIntList(section); } else if (line.StartsWith("Edge_Weight_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeWeights = this.ReadDoubleList(section); } else if (line.StartsWith("EOF", StringComparison.InvariantCultureIgnoreCase)) { break; } else { throw new NotSupportedException(); } } return(tspFile); }
/// <summary> /// Loads TSP File structure from a stream /// </summary> /// <param name="reader">stream reader</param> /// <returns>TSP File structure loaded from a stream</returns> public TspFile Load(System.IO.StreamReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } List<string> lines = new List<string>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); line = line.Trim(); if(!string.IsNullOrEmpty(line)) { lines.Add(line); } } TspFile tspFile = new TspFile(); List<string[]> sections = SplitToSections(lines); foreach(string[] section in sections) { string line = section[0]; if (line.StartsWith("Name", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Name = this.ReadStringFromLine("Name", line); } else if(line.StartsWith("Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Type = this.ReadFileTypeFromLine("Type", line); } else if (line.StartsWith("Comment", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Comment = this.ReadStringFromLine("Comment", line); } else if (line.StartsWith("Dimension", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Dimension = this.ReadIntFromLine("Dimension", line); } else if (line.StartsWith("Capacity", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Capacity = this.ReadIntFromLine("Capacity", line); } else if (line.StartsWith("Edge_Weight_Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeWeightType = this.ReadEdgeWeightTypeFromLine("Edge_Weight_Type", line); } else if (line.StartsWith("Edge_Weight_Format", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeWeightFormat = this.ReadEdgeWeightFormatFromLine("Edge_Weight_Format", line); } else if (line.StartsWith("Edge_Data_Format", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeDataFormat = this.ReadEdgeDataFormatFromLine("Edge_Data_Format", line); } else if (line.StartsWith("Node_Coord_Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.NodeCoordinatesType = this.ReadNodeCoordinatesTypeFromLine("Node_Coord_Type", line); } else if (line.StartsWith("Display_Data_Type", StringComparison.InvariantCultureIgnoreCase)) { tspFile.DisplayDataType = this.ReadDisplayDataTypeFromLine("Display_Data_Type", line); } else if (line.StartsWith("Node_Coord_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.DisplayDataType = Defines.DisplayDataType.Coordinates; if (tspFile.NodeCoordinatesType == Defines.NodeCoordinatesType.NoCoordinates) { tspFile.NodeCoordinatesType = Defines.NodeCoordinatesType.Coordinates2D; } if(tspFile.NodeCoordinatesType == Defines.NodeCoordinatesType.Coordinates2D) { tspFile.Nodes = this.ReadIntAndDoublesArrayList("Node_Coord_Section", section); } else if(tspFile.NodeCoordinatesType == Defines.NodeCoordinatesType.Coordinates3D) { tspFile.Nodes = this.ReadIntAndDoublesArrayList("Node_Coord_Section", section); } else throw new NotSupportedException(); } else if (line.StartsWith("Depot_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Depots = this.ReadIntList("Depot_Section", section); } else if (line.StartsWith("Demand_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Demands = this.ReadIntsArrayList("Demand_Section", section); } else if (line.StartsWith("Edge_Data_Section", StringComparison.InvariantCultureIgnoreCase)) { if (tspFile.EdgeDataFormat == Defines.EdgeDataFormat.EdgeList) { tspFile.Edges = this.ReadIntsArrayList("Edge_Data_Section", section); } else if (tspFile.EdgeDataFormat == Defines.EdgeDataFormat.AdjacencyList) { tspFile.Edges = this.ReadIntsArrayList("Edge_Data_Section", section); } else throw new NotSupportedException(); } else if (line.StartsWith("Fixed_Edges", StringComparison.InvariantCultureIgnoreCase)) { tspFile.FixedEdges = this.ReadIntsArrayList("Fixed_Edges", section); } else if (line.StartsWith("Display_Data_Section", StringComparison.InvariantCultureIgnoreCase)) { if(tspFile.DisplayDataType == Defines.DisplayDataType.Display2D) { tspFile.DisplayNodes = this.ReadIntAndDoublesArrayList("Display_Data_Section", section); } else throw new NotSupportedException(); } else if (line.StartsWith("Tour_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.Tour = this.ReadIntList("Tour_Section", section); } else if (line.StartsWith("Edge_Weight_Section", StringComparison.InvariantCultureIgnoreCase)) { tspFile.EdgeWeights = this.ReadDoubleList("Edge_Weight_Section", section); } else if (line.StartsWith("EOF", StringComparison.InvariantCultureIgnoreCase)) { break; } else throw new NotSupportedException(); } return tspFile; }