/// <summary> /// Generate. /// </summary> /// <param name="writer"></param> /// <param name="problem"></param> public static void Generate(StreamWriter writer, TSPLIBProblem problem) { if (problem.Symmetric) { TSPLIBProblemGenerator.GenerateTSP(writer, problem); } else { TSPLIBProblemGenerator.GenerateATSP(writer, problem); } }
/// <summary> /// Converts problem. /// </summary> /// <param name="atsp"></param> /// <returns></returns> public static TSPLIBProblem Convert(TSPLIBProblem atsp) { // check if the problem is not already symmetric. if (atsp.Symmetric) { return atsp; } // the problem is not symmetric, convert it. string name = atsp.Name + "(SYM)"; string comment = atsp.Comment; // convert the problem to a symetric one. IProblem symetric = atsp.ConvertToSymmetric(); return new TSPLIBProblem(name, comment, symetric.Size, symetric.WeightMatrix, TSPLIBProblemWeightTypeEnum.Explicit, TSPLIBProblemTypeEnum.TSP, 0, 0); }
private double CalculateWeight(IRoute route, TSPLIBProblem problem) { int previous = -1; int first = -1; double weight = 0; foreach (int customer in route) { if (first < 0) { first = customer; } if (previous < 0) { } else { weight = weight + problem.WeightMatrix[previous][customer]; } previous = customer; } weight = weight + problem.WeightMatrix[previous][first]; return weight; }
private static void GenerateATSP(StreamWriter writer, TSPLIBProblem problem) { writer.WriteLine(string.Format("NAME: {0}", problem.Name)); writer.WriteLine("TYPE: ATSP"); writer.WriteLine(string.Format("COMMENT: {0}", problem.Comment)); writer.WriteLine(string.Format("DIMENSION: {0}", problem.Size)); writer.WriteLine("EDGE_WEIGHT_TYPE: EXPLICIT"); writer.WriteLine("EDGE_WEIGHT_FORMAT: FULL_MATRIX"); //writer.WriteLine("EDGE_WEIGHT_FORMAT: UPPER_ROW"); writer.WriteLine("DISPLAY_DATA_TYPE: TWOD_DISPLAY"); writer.WriteLine("EDGE_WEIGHT_SECTION"); //// get the biggest weight. //int max = 0; //int[][] upper_rows = new int[problem.Size - 1][]; //for (int x = 0; x < problem.Size - 1; x++) //{ // upper_rows[x] = new int[problem.Size - 1 - x]; // for (int y = 0; y < problem.Size - 1 - x; y++) // { // int value = (int)problem.WeightMatrix[x][y]; // if (value > max) // { // max = value; // } // upper_rows[x][y] = value; // } //} // get the biggest weight. int max = 0; int[][] upper_rows = new int[problem.Size][]; for (int x = 0; x < problem.Size; x++) { upper_rows[x] = new int[problem.Size]; for (int y = 0; y < problem.Size; y++) { int value = (int)problem.WeightMatrix[x][y]; if (value > max) { max = value; } upper_rows[x][y] = value; } } int length = max.ToString().Length; for (int x = 0; x < upper_rows.Length; x++) { for (int y = 0; y < upper_rows[x].Length; y++) { if (y > 0) { writer.Write(" "); } writer.Write(upper_rows[x][y].ToString().PadLeft(length)); } writer.WriteLine(); } writer.WriteLine("EOF"); writer.Flush(); }
/// <summary> /// Generate. /// </summary> /// <param name="file"></param> /// <param name="problem"></param> public static void Generate(FileInfo file, TSPLIBProblem problem) { StreamWriter writer = new StreamWriter(file.OpenWrite()); TSPLIBProblemGenerator.Generate(writer, problem); }