事实上,我在车辆路线问题项目中工作,我正在尝试生成一个问题实例
这是一个示例,其中我们有一个仓库 " 0" 和三个站 " 1",& #34; 2"和" 3" 。
我创建了下表:
distdata= new int[stations][(stations+1)]; // where stations is equal to number of stations
我随机填充了Matrix并将具有相同行和列索引的单元格设置为零,因此同一站点与其自身之间的距离等于零。
System.out.println("Matrix of stations's distances : \n");
try {
BufferedWriter out2 = new BufferedWriter(new FileWriter("StationsDist.bench"));
for (int i = 0; i < stations + 1; i++) {
System.out.println("\t[" + i + "]");
}
System.out.println("\n");
for (int i = 0; i < stations; i++) {
System.out.println("\n[" + (i + 1) + "]");
for (int j = 0; j < stations + 1; j++) {
if ((i + 1) == j) {
dij = 0;
} else {
dij = (int) (Math.random() * 20) + 1;
}
System.out.println("\t[" + dij + "]");
out2.write(dij + "\t");
distdata[i][j] = dij;
distdatac[i][j] = dij;
}
out2.write("\n");
System.out.println("\n");
}
out2.close();
} catch (IOException e) {
}
我得到了这个输出:
[0] [1] [2] [3] // [0]-> Depot, [1]-> station 1, [2]-> station 2 ...
[1] [3] [0] [7] [4] // [3] is the distance between station 1 and the depot, and [0] is the null distance between station 1 and itself ...
[2] [1] [9] [0] [6]
[3] [2] [5] [8] [0]
问题是距离distdata[1][2]
应该等于距离distdata[2][1]
,因为它们意味着相同的站点。
我需要创建一个对称矩阵或将表更改为以下内容:
[0] [1] [2] [3]
[1] [3] [0] [7] [4]
[2] [1] [7] [0] [6]
[3] [2] [4] [6] [0]
有人可以帮助我吗?
答案 0 :(得分:1)
看看我是如何实现对称和非对称transportCost-matrix(https://github.com/jsprit/jsprit/blob/master/jsprit-core/src/main/java/jsprit/core/util/VehicleRoutingTransportCostsMatrix.java)的。这可能会对你有所帮助。
答案 1 :(得分:1)
你把自己挖到了坑里。您的矩阵为distdata[stations][stations + 1]
,distdata[i, 0]
给出了从第i个站(其名称为i + 1)到库(其名称为0)的距离。和站点之间的距离&#34; 1&#34;和&#34; 2&#34;例如是`distdata [0] [2],以及站之间&#34; 2&#34;和&#34; 1&#34;是... distdata [1] [1]!
因此,必须为空的距离不是distdata[i][i]
而是distdata[i][i+1]
,并且对称性为distdata[i][j + 1] == distdata[j][i + 1]
。
恕我直言,这真的很容易出错...
一点改进:在你的内循环中,只要你从0循环到i + 1就足够了 写:
distdata[i][j] = d
表示j = 0,
distdata[i][j] = d;
distdata[j-1][i+1] = d
表示j&gt; 0
但我强烈建议你接受丢失第一个原始作为构建方阵,以便distdata[i][i] = 0
和distdata[i][j] == distdata[j][i]