- Timestamp:
- 03/23/11 12:03:24 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Parsers/QAPLIBSolutionParser.cs
r5648 r5814 1 1 using System; 2 using System.Globalization; 2 3 using System.IO; 3 4 … … 6 7 public int Size { get; private set; } 7 8 public int[] Assignment { get; private set; } 8 public double Quali y { get; private set; }9 public double Quality { get; private set; } 9 10 public Exception Error { get; private set; } 10 11 … … 16 17 Size = 0; 17 18 Assignment = null; 18 Quali y = double.NaN;19 Quality = double.NaN; 19 20 Error = null; 20 21 } 21 22 22 public bool Parse(string file) { 23 /// <summary> 24 /// Reads from the given stream data which is expected to be in the QAPLIB solution format. 25 /// 26 /// The QAPLIB solution file format (.sln) is as follows: 27 /// First line: Size of the permutation followed by a blank followed by the quality formatted using no thousands separator and if a fractional number with the "." as decimal symbol 28 /// Remaining lines: The values of the permutation separated by blanks. Values must lie in the range [1;size of the permutation]. 29 /// </summary> 30 /// <param name="file">The file to read data from.</param> 31 /// <param name="valueAsLocation">The numbers can be interpreted either as facilities or locations. 32 /// HeuristicLab always encodes the permutation such that the index denotes the facility and the value the location. 33 /// If this parameter is true, then the permutation is expected to follow this encoding. 34 /// If this parameter is false, the meaning of value and index will be swapped when reading the permutation.</param> 35 /// <returns></returns> 36 public bool Parse(string file, bool valueAsLocation) { 23 37 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 24 return Parse(stream );38 return Parse(stream, valueAsLocation); 25 39 } 26 40 } 27 41 28 42 /// <summary> 29 /// Reads from the given stream data which is expected to be in the QAPLIB format. 43 /// Reads from the given stream data which is expected to be in the QAPLIB solution format. 44 /// 45 /// The QAPLIB solution file format (.sln) is as follows: 46 /// First line: Size of the permutation followed by a blank followed by the quality formatted using no thousands separator and if a fractional number with the "." as decimal symbol 47 /// Remaining lines: The values of the permutation separated by blanks. Values must lie in the range [1;size of the permutation]. 30 48 /// </summary> 31 49 /// <remarks> … … 33 51 /// </remarks> 34 52 /// <param name="stream">The stream to read data from.</param> 53 /// <param name="valueAsLocation">The numbers can be interpreted either as facilities or locations. 54 /// HeuristicLab always encodes the permutation such that the index denotes the facility and the value the location. 55 /// If this parameter is true, then the permutation is expected to follow this encoding. 56 /// If this parameter is false, the meaning of value and index will be swapped when reading the permutation.</param> 35 57 /// <returns>True if the file was successfully read or false otherwise.</returns> 36 public bool Parse(Stream stream ) {58 public bool Parse(Stream stream, bool valueAsLocation) { 37 59 Error = null; 38 60 try { … … 41 63 string[] firstline = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 42 64 Size = int.Parse(firstline[0]); 43 Quali y = double.Parse(firstline[1]);65 Quality = double.Parse(firstline[1], CultureInfo.InvariantCulture.NumberFormat); 44 66 Assignment = new int[Size]; 45 67 int read = 0; … … 48 70 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 49 71 for (int j = 0; j < vals.Length; j++) { 50 Assignment[read++] = int.Parse(vals[j]); 72 if (valueAsLocation) 73 Assignment[int.Parse(vals[j]) - 1] = read++; 74 else Assignment[read++] = int.Parse(vals[j]) - 1; 51 75 } 52 76 }
Note: See TracChangeset
for help on using the changeset viewer.