lkh_pi_reader.java

来自「用于求解TSP(Traveling salesman problem」· Java 代码 · 共 46 行

JAVA
46
字号
/**
 * Description: provide the information for read the penalty values for TSP nodes
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    May 22, 2005
 * Xiaofeng Xie    Apr 28, 2006    MAOS-TSP Beta 1.1.002
 *
 * @ Reference:
 * [1] K. Helsgaun,"An Effective Implementation of the Lin-Kernighan Traveling
 *  Salesman Heuristic", European J Operational Research 126 (1), 106-130 (2000).
 * [2] Reinelt, G. The Traveling Salesman: Computational Solutions for TSP
 *  Applications. Berlin: Springer, 1994.
 */

package implement.TSP.infoIO;

import Global.define.*;
import Global.methods.*;
import Global.util.*;

public class LKH_PI_Reader {
  public static final int precision = 100;

  public static String writeResult(double[] piValues) throws Exception {
    return writeResult(BasicArray.getFullIDArray(piValues.length), piValues);
  }

  public static String writeResult(int[] tour, double[] piValues) throws Exception {
    String piStr = "";
    for (int i=0; i<piValues.length; i++) {
      piStr += (tour[i]+1)+" "+(int)(piValues[i]*precision)+BasicTag.RETURN_TAG;
    }
    return piStr;
  }

  public static double[] readResult(String piContent) throws Exception {
    String[] lines = GlobalString.getMeaningfulLines(piContent);
    double[] piValues = new double[lines.length];
    for (int i=0; i<lines.length; i++) {
      String[] lineInfos = GlobalString.tokenize(lines[i], " \t");
      piValues[new Integer(lineInfos[0]).intValue()-1] = new Double(lineInfos[1]).doubleValue()/precision;
    }
    return piValues;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?