所以我100%不知道是什么导致了这样的事情。这是我与
一起使用的整段代码public static void getBestHeuristic(int[] nextParent) {
int bestHeuristic = 0;
Random rndm = new Random();
int randomIndex = 0;
HashMap<Integer, int[]> childHuristics = new HashMap<Integer, int[]>();
int k = 0;
while (k < nextParent.length - 1) {
for (int i = 0; i < (nextParent.length) - 2; i++) {
randomIndex = rndm.nextInt(nextParent.length / 5) + i;
int temp = nextParent[i];
nextParent[i] = nextParent[randomIndex];
nextParent[randomIndex] = temp;
}
// Assign this random set to the child
int[] childArray = nextParent;
int childDistance = 0;
// Calculate the heuristic of the new current child
for (int j = 0; j < childArray.length - 1; j++) {
childDistance += cities[childArray[j]][childArray[j + 1]];
}
childHuristics.put(childDistance, childArray);
// Display the current values added to the HashMap
System.out.println("Distance: " + childDistance);
System.out.println(Arrays.toString(childArray));
System.out.println();
// Keep track of the lowest value
if (bestHeuristic == 0) {
bestHeuristic = childDistance;
} else if (childDistance < bestHeuristic) {
bestHeuristic = childDistance;
}
k++;
}
// Display the lowest distance
System.out.println();
System.out.println("The best child was: ");
System.out.println("Distance: " + bestHeuristic);
System.out.println(Arrays.toString(childHuristics.get(bestHeuristic)));
以下是我从中获得的输出:
Distance: 670
[12, 5, 6, 10, 2, 3, 4, 8, 0, 9, 7, 1, 14, 11, 13]
Distance: 680
[6, 10, 2, 12, 4, 8, 5, 0, 9, 7, 3, 14, 13, 11, 1]
Distance: 611
[2, 12, 6, 4, 5, 8, 9, 7, 0, 10, 3, 13, 14, 11, 1]
Distance: 668
[2, 4, 5, 12, 9, 7, 6, 0, 3, 13, 10, 8, 14, 11, 1]
Distance: 684
[2, 5, 9, 7, 6, 12, 4, 13, 3, 8, 0, 14, 10, 11, 1]
Distance: 634
[2, 9, 5, 12, 6, 7, 4, 8, 3, 14, 0, 11, 13, 10, 1]
Distance: 736
[9, 12, 6, 7, 2, 8, 5, 4, 3, 11, 14, 13, 0, 10, 1]
Distance: 622
[9, 7, 6, 12, 5, 4, 3, 11, 8, 14, 0, 10, 1, 13, 2]
Distance: 585
[9, 7, 5, 6, 4, 11, 3, 12, 8, 0, 10, 13, 14, 1, 2]
Distance: 554
[9, 5, 7, 4, 6, 11, 8, 12, 3, 10, 0, 14, 13, 1, 2]
Distance: 587
[9, 7, 4, 6, 11, 5, 8, 3, 12, 0, 10, 1, 13, 14, 2]
Distance: 575
[7, 4, 6, 11, 8, 9, 5, 12, 3, 1, 0, 10, 14, 13, 2]
Distance: 642
[4, 6, 7, 8, 5, 12, 9, 3, 1, 0, 11, 14, 10, 13, 2]
Distance: 634
[4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]
The best child was:
Distance: 554
[4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]
为了给所有这些提供一些上下文,基本上每个数组中的每个数字都有一个与它们相关的距离值。那就是每个人的距离值代表什么。 这个城市&#39; array是包含所有这些距离值的2D数组。它看起来像这样:
0 29 82 46 68 52 72 42 51 55 29 74 23 72 46
29 0 55 46 42 43 43 23 23 31 41 51 11 52 21
82 55 0 68 46 55 23 43 41 29 79 21 64 31 51
46 46 68 0 82 15 72 31 62 42 21 51 51 43 64
68 42 46 82 0 74 23 52 21 46 82 58 46 65 23
52 43 55 15 74 0 61 23 55 31 33 37 51 29 59
72 43 23 72 23 61 0 42 23 31 77 37 51 46 33
42 23 43 31 52 23 42 0 33 15 37 33 33 31 37
51 23 41 62 21 55 23 33 0 29 62 46 29 51 11
55 31 29 42 46 31 31 15 29 0 51 21 41 23 37
29 41 79 21 82 33 77 37 62 51 0 65 42 59 61
74 51 21 51 58 37 37 33 46 21 65 0 61 11 55
23 11 64 51 46 51 51 33 29 41 42 61 0 62 23
72 52 31 43 65 29 46 31 51 23 59 11 62 0 59
46 21 51 64 23 59 33 37 11 37 61 55 23 59 0
我遇到的问题是&#39; get(key)&#39;我拥有的HashMap方法没有返回与我提供的密钥相关联的Array。
它说 距离:554 [4,6,7,5,9,12,1,8,0,11,10,13,14,3,2]
当距离554实际排列到此数组时: [9,5,7,4,6,11,8,12,3,10,0,14,13,2]
我不知道我是否将它们放入HashMap是否有问题。我尝试使用TreeMap来强制它进行排序并获得第一个值,但结果是一样的。
感谢您的时间。
答案 0 :(得分:2)
这是因为您将多次相同的引用放入地图中。如果你这样做
int[] childArray = nextParent.clone();
或
int[] childArray = Arrays.copyOf(nextParent, nextParent.length)
相反,每次都会使用一个新数组。