Changeset 3151
- Timestamp:
- 03/21/10 23:55:23 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Problems.TSP.Views/3.3/HeuristicLab.Problems.TSP.Views-3.3.csproj ¶
r3107 r3151 92 92 </Compile> 93 93 <Compile Include="HeuristicLabProblemsTSPViewsPlugin.cs" /> 94 <Compile Include="TSPLIBImportDialog.cs"> 95 <SubType>Form</SubType> 96 </Compile> 97 <Compile Include="TSPLIBImportDialog.Designer.cs"> 98 <DependentUpon>TSPLIBImportDialog.cs</DependentUpon> 99 </Compile> 94 100 <Compile Include="TSPView.cs"> 95 101 <SubType>UserControl</SubType> … … 109 115 <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project> 110 116 <Name>HeuristicLab.Collections-3.3</Name> 117 </ProjectReference> 118 <ProjectReference Include="..\..\HeuristicLab.Common.Resources\3.2\HeuristicLab.Common.Resources-3.2.csproj"> 119 <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project> 120 <Name>HeuristicLab.Common.Resources-3.2</Name> 111 121 </ProjectReference> 112 122 <ProjectReference Include="..\..\HeuristicLab.Core.Views\3.3\HeuristicLab.Core.Views-3.3.csproj"> -
TabularUnified trunk/sources/HeuristicLab.Problems.TSP.Views/3.3/HeuristicLabProblemsTSPViewsPlugin.cs.frame ¶
r3107 r3151 29 29 [PluginFile("HeuristicLab.Problems.TSP.Views-3.3.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.Collections", "3.3")] 31 [PluginDependency("HeuristicLab.Common.Resources", "3.2")] 31 32 [PluginDependency("HeuristicLab.Core", "3.3")] 32 33 [PluginDependency("HeuristicLab.Core.Views", "3.3")] -
TabularUnified trunk/sources/HeuristicLab.Problems.TSP.Views/3.3/TSPView.Designer.cs ¶
r2917 r3151 33 33 protected override void Dispose(bool disposing) { 34 34 if (disposing) { 35 if (tsplibImportDialog != null) tsplibImportDialog.Dispose(); 35 36 if (components != null) components.Dispose(); 36 37 } … … 45 46 /// </summary> 46 47 private void InitializeComponent() { 47 this.openFileDialog = new System.Windows.Forms.OpenFileDialog();48 48 this.importButton = new System.Windows.Forms.Button(); 49 49 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit(); … … 63 63 // 64 64 this.descriptionTextBox.Size = new System.Drawing.Size(457, 20); 65 //66 // openFileDialog67 //68 this.openFileDialog.DefaultExt = "tsp";69 this.openFileDialog.FileName = "tspdata";70 this.openFileDialog.Filter = "TSPLIB Files|*.tsp|All Files|*.*";71 this.openFileDialog.Title = "Import TSP Data";72 65 // 73 66 // importButton … … 104 97 #endregion 105 98 106 private System.Windows.Forms.OpenFileDialog openFileDialog;107 99 private System.Windows.Forms.Button importButton; 108 100 -
TabularUnified trunk/sources/HeuristicLab.Problems.TSP.Views/3.3/TSPView.cs ¶
r2998 r3151 33 33 [Content(typeof(TSP), true)] 34 34 public sealed partial class TSPView : ProblemView { 35 private TSPLIBImportDialog tsplibImportDialog; 36 35 37 public new TSP Content { 36 38 get { return (TSP)base.Content; } … … 63 65 64 66 private void importButton_Click(object sender, System.EventArgs e) { 65 if (openFileDialog.ShowDialog(this) == DialogResult.OK) { 67 if (tsplibImportDialog == null) tsplibImportDialog = new TSPLIBImportDialog(); 68 69 if (tsplibImportDialog.ShowDialog(this) == DialogResult.OK) { 66 70 try { 67 Content.ImportFromTSPLIB(openFileDialog.FileName); 71 if (tsplibImportDialog.Quality == null) 72 Content.ImportFromTSPLIB(tsplibImportDialog.TSPFileName, tsplibImportDialog.TourFileName); 73 else 74 Content.ImportFromTSPLIB(tsplibImportDialog.TSPFileName, tsplibImportDialog.TourFileName, (double)tsplibImportDialog.Quality); 68 75 } 69 76 catch (Exception ex) { -
TabularUnified trunk/sources/HeuristicLab.Problems.TSP/3.3/HeuristicLab.Problems.TSP-3.3.csproj ¶
r3107 r3151 84 84 </ItemGroup> 85 85 <ItemGroup> 86 <Compile Include="TSPLIBTourParser.cs" /> 86 87 <Compile Include="Interfaces\ICoordinatesTSPSolutionsVisualizer.cs" /> 87 88 <Compile Include="Interfaces\IPathCoordinatesTSPSolutionsVisualizer.cs" /> -
TabularUnified trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs ¶
r3147 r3151 82 82 get { return BestKnownQualityParameter; } 83 83 } 84 public OptionalValueParameter<Permutation> BestKnownSolutionParameter { 85 get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; } 86 } 84 87 #endregion 85 88 … … 124 127 get { return BestKnownQualityParameter.Value; } 125 128 set { BestKnownQualityParameter.Value = value; } 129 } 130 public Permutation BestKnownSolution { 131 get { return BestKnownSolutionParameter.Value; } 132 set { BestKnownSolutionParameter.Value = value; } 126 133 } 127 134 private List<IPermutationOperator> operators; … … 145 152 Parameters.Add(new OptionalValueParameter<ITSPSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize TSP solutions.", visualizer)); 146 153 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.")); 154 Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance.")); 147 155 148 156 creator.PermutationParameter.ActualName = "TSPTour"; … … 163 171 } 164 172 165 public void ImportFromTSPLIB(string filename) { 166 TSPLIBParser parser = new TSPLIBParser(filename); 167 parser.Parse(); 168 Name = parser.Name + " TSP (imported from TSPLIB)"; 169 Coordinates = new DoubleMatrix(parser.Vertices); 173 public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) { 174 TSPLIBParser tspParser = new TSPLIBParser(tspFileName); 175 tspParser.Parse(); 176 Name = tspParser.Name + " TSP (imported from TSPLIB)"; 177 if (!string.IsNullOrEmpty(tspParser.Comment)) Description = tspParser.Comment; 178 Coordinates = new DoubleMatrix(tspParser.Vertices); 179 BestKnownQuality = null; 180 181 if (!string.IsNullOrEmpty(optimalTourFileName)) { 182 TSPLIBTourParser tourParser = new TSPLIBTourParser(optimalTourFileName); 183 tourParser.Parse(); 184 BestKnownSolution = new Permutation(tourParser.Tour); 185 } 186 } 187 public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName, double bestKnownQuality) { 188 ImportFromTSPLIB(tspFileName, optimalTourFileName); 189 BestKnownQuality = new DoubleValue(bestKnownQuality); 170 190 } 171 191 -
TabularUnified trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPLIBParser.cs ¶
r2883 r3151 26 26 namespace HeuristicLab.Problems.TSP { 27 27 /// <summary> 28 /// Parses a *.tsp file in theTSPLIB format and extracts its information about a TSP.28 /// Parses a *.tsp file in TSPLIB format and extracts its information about a TSP. 29 29 /// </summary> 30 30 public class TSPLIBParser { … … 32 32 private const int NAME = 1; 33 33 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; 38 39 39 40 private StreamReader source; 40 41 41 private string myName;42 private string name; 42 43 /// <summary> 43 44 /// Gets the name of the parsed TSP. 44 45 /// </summary> 45 46 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; 49 57 /// <summary> 50 58 /// Gets the vertices of the parsed TSP. 51 59 /// </summary> 52 60 public double[,] Vertices { 53 get { return myVertices; }54 } 55 private int myWeightType;61 get { return vertices; } 62 } 63 private int weightType; 56 64 /// <summary> 57 65 /// Gets the weight type of the parsed TSP. 58 66 /// </summary> 59 67 public int WeightType { 60 get { return myWeightType; }61 } 62 63 /// <summary> 64 /// Initializes a new instance of <see cref="TSP Parser"/> 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) 67 75 /// </exception> 68 76 /// <param name="path">The path where the TSP is stored.</param> 69 77 public TSPLIBParser(String path) { 70 78 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)."); 72 80 73 81 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> 83 92 public void Parse() { 84 93 int section = -1; … … 100 109 typeIsChecked = true; 101 110 break; 111 case COMMENT: 112 ReadComment(str); 113 break; 102 114 case DIM: 103 115 InitVerticesArray(str); … … 118 130 119 131 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."); 121 133 } 122 134 … … 136 148 if (token.Equals("type", StringComparison.OrdinalIgnoreCase)) 137 149 return TYPE; 150 if (token.Equals("comment", StringComparison.OrdinalIgnoreCase)) 151 return COMMENT; 138 152 if (token.Equals("dimension", StringComparison.OrdinalIgnoreCase)) 139 153 return DIM; … … 150 164 private void ReadName(string str) { 151 165 string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None); 152 myName = tokens[tokens.Length - 1].Trim();166 name = tokens[tokens.Length - 1].Trim(); 153 167 } 154 168 … … 158 172 string type = tokens[tokens.Length - 1].Trim(); 159 173 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(); 161 180 } 162 181 … … 166 185 167 186 int dim = Int32.Parse(dimension); 168 myVertices = new double[dim, 2];187 vertices = new double[dim, 2]; 169 188 } 170 189 … … 174 193 175 194 if (type.Equals("euc_2d", StringComparison.OrdinalIgnoreCase)) 176 myWeightType = 0;195 weightType = 0; 177 196 else if (type.Equals("geo", StringComparison.OrdinalIgnoreCase)) 178 myWeightType = 1;197 weightType = 1; 179 198 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)."); 181 200 } 182 201 … … 186 205 187 206 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)."); 189 208 } 190 209 191 210 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++) { 196 215 string str = source.ReadLine(); 197 216 string[] tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 198 217 199 218 if (tokens.Length != 3) 200 throw new InvalidDataException("In valid node format");219 throw new InvalidDataException("Input file contains invalid node coordinates."); 201 220 202 221 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); 205 224 } 206 225 }
Note: See TracChangeset
for help on using the changeset viewer.