您好 我在下面编写了这样的代码,但它返回错误的输出,这不是最小值。
private Element heuristic_Function(List<Element> objectList) {
System.out.println(objectList.toString());
Element node =objectList.get(objectList.size() - 1);
double leastValue = objectList.get(0).getGreedy();
System.out.println(leastValue);
for (Element e : objectList) {
if (leastValue > e.getGreedy()) {
leastValue = e.getGreedy();
node = e;
}
}
System.out.println(node.toString());
System.out.println(leastValue);
return node;
}
我多次向此方法发送一个列表,但它会返回错误的输出。
输出:
[digit:1 greedy2.87 , digit:2 greedy3.67 ,digit:3 greedy3.24 , digit:4 greedy3.67 ] //System.out.println(objectList.toString());
2.87 //System.out.println(leastValue);
digit:4 greedy3.67 //System.out.println(node.toString());
2.87 //System.out.println(leastValue);
[digit:1 greedy2.87 , digit:2 greedy3.67 , digit:3 greedy3.24 , digit:1 greedy3.67 , digit:2 greedy4.47 , digit:3 greedy4.3500000000000005 ] // System.out.println(objectList.toString());
2.87 //System.out.println(leastValue);
digit:3 greedy4.3500000000000005 //System.out.println(node.toString());
2.87 //System.out.println(leastValue);
我的问题是:为什么它不返回最少贪心(最小值)的节点? 感谢
答案 0 :(得分:5)
将初始化函数替换为Element node =objectList.get(0);
如图所示
private Element heuristic_Function(List<Element> objectList) {
System.out.println(objectList.toString());
Element node =objectList.get(0); // REPLACE WITH THIS
double leastValue = objectList.get(0).getGreedy();
System.out.println(leastValue);
for (Element e : objectList) {
if (leastValue > e.getGreedy()) {
leastValue = e.getGreedy();
node = e;
}
}
System.out.println(node.toString());
System.out.println(leastValue);
return node;
}