- Timestamp:
- 03/01/12 15:15:22 (12 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3
- Files:
-
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/HeuristicLab.Problems.Instances.QAPLIB-3.3.csproj
r7448 r7538 51 51 <ItemGroup> 52 52 <Compile Include="QAPLIBParser.cs" /> 53 <Compile Include="QAPLIBInstance.cs" />54 53 <Compile Include="QAPLIBInstanceDescriptor.cs" /> 55 54 <Compile Include="QAPLIBSolutionParser.cs" /> -
branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/Plugin.cs.frame
r7443 r7538 25 25 [Plugin("HeuristicLab.Problems.Instances.QAPLIB", "3.3.6.$WCREV$")] 26 26 [PluginFile("HeuristicLab.Problems.Instances.QAPLIB-3.3.dll", PluginFileType.Assembly)] 27 [PluginDependency("HeuristicLab.Common", "3.3")] 28 [PluginDependency("HeuristicLab.Problems.Instances", "3.3")] 27 29 public class HeuristicLabProblemsInstancesQAPLIBPlugin : PluginBase { 28 30 } -
branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBInstanceProvider.cs
r7482 r7538 28 28 29 29 namespace HeuristicLab.Problems.Instances.QAPLIB { 30 public class QAPLIBInstanceProvider : ProblemInstanceProvider< IQAPInstance> {30 public class QAPLIBInstanceProvider : ProblemInstanceProvider<QAPInstance> { 31 31 public override string Name { 32 32 get { return "QAPLIB"; } … … 62 62 } 63 63 64 public override IQAPInstance GetInstance(IInstanceDescriptor id) {64 public override QAPInstance LoadInstance(IInstanceDescriptor id) { 65 65 var descriptor = (QAPLIBInstanceDescriptor)id; 66 var instance = new QAPLIBInstance();67 66 using (var stream = Assembly.GetExecutingAssembly() 68 67 .GetManifestResourceStream(descriptor.InstanceIdentifier)) { 69 QAPLIBParser datParser = new QAPLIBParser(); 70 datParser.Parse(stream); 71 if (datParser.Error != null) throw datParser.Error; 72 instance.Dimension = datParser.Size; 73 instance.Distances = datParser.Distances; 74 instance.Weights = datParser.Weights; 75 68 var parser = new QAPLIBParser(); 69 parser.Parse(stream); 70 var instance = Load(parser); 76 71 instance.Name = id.Name; 77 72 instance.Description = id.Description; … … 80 75 using (Stream solStream = Assembly.GetExecutingAssembly() 81 76 .GetManifestResourceStream(descriptor.SolutionIdentifier)) { 82 QAPLIBSolutionParser slnParser = new QAPLIBSolutionParser();77 var slnParser = new QAPLIBSolutionParser(); 83 78 slnParser.Parse(solStream, true); 84 79 if (slnParser.Error != null) throw slnParser.Error; … … 88 83 } 89 84 } 85 return instance; 90 86 } 87 } 88 89 public override QAPInstance LoadInstance(string path) { 90 var parser = new QAPLIBParser(); 91 parser.Parse(path); 92 var instance = Load(parser); 93 instance.Name = Path.GetFileName(path); 94 instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString(); 95 return instance; 96 } 97 98 public override void SaveInstance(QAPInstance instance, string path) { 99 throw new NotSupportedException(); 100 } 101 102 private QAPInstance Load(QAPLIBParser parser) { 103 var instance = new QAPInstance(); 104 instance.Dimension = parser.Size; 105 instance.Distances = parser.Distances; 106 instance.Weights = parser.Weights; 91 107 return instance; 92 108 } -
branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBParser.cs
r7445 r7538 28 28 public double[,] Distances { get; private set; } 29 29 public double[,] Weights { get; private set; } 30 public Exception Error { get; private set; }31 30 32 31 public QAPLIBParser() { … … 38 37 Distances = null; 39 38 Weights = null; 40 Error = null;41 39 } 42 40 43 public boolParse(string file) {41 public void Parse(string file) { 44 42 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 45 returnParse(stream);43 Parse(stream); 46 44 } 47 45 } … … 55 53 /// <param name="stream">The stream to read data from.</param> 56 54 /// <returns>True if the file was successfully read or false otherwise.</returns> 57 public bool Parse(Stream stream) { 58 Error = null; 59 try { 60 StreamReader reader = new StreamReader(stream); 61 Size = int.Parse(reader.ReadLine()); 62 Distances = new double[Size, Size]; 63 Weights = new double[Size, Size]; 64 string valLine = null; 65 char[] delim = new char[] { ' ' }; 66 int read = 0; 67 int k = 0; 68 while (k < Size) { 69 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading first matrix."); 70 valLine = reader.ReadLine(); 71 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 72 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 73 foreach (string val in vals) { 74 Weights[k, read++] = double.Parse(val); 75 if (read == Size) { 76 read = 0; 77 k++; 78 } 55 public void Parse(Stream stream) { 56 var reader = new StreamReader(stream); 57 Size = int.Parse(reader.ReadLine()); 58 Distances = new double[Size, Size]; 59 Weights = new double[Size, Size]; 60 char[] delim = new char[] { ' ' }; 61 62 Distances = ParseMatrix(reader, delim); 63 Weights = ParseMatrix(reader, delim); 64 } 65 66 private double[,] ParseMatrix(StreamReader reader, char[] delim) { 67 int read = 0, k = 0; 68 double[,] result = new double[Size, Size]; 69 while (k < Size) { 70 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix."); 71 string valLine = reader.ReadLine(); 72 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 73 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 74 foreach (string val in vals) { 75 Distances[k, read++] = double.Parse(val); 76 if (read == Size) { 77 read = 0; 78 k++; 79 79 } 80 80 } 81 82 read = 0;83 k = 0;84 85 while (k < Size) {86 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix.");87 valLine = reader.ReadLine();88 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();89 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);90 foreach (string val in vals) {91 Distances[k, read++] = double.Parse(val);92 if (read == Size) {93 read = 0;94 k++;95 }96 }97 }98 return true;99 } catch (Exception e) {100 Error = e;101 return false;102 81 } 82 return result; 103 83 } 104 84 }
Note: See TracChangeset
for help on using the changeset viewer.