gpt4 book ai didi

java - 返回成本最低!

转载 作者:行者123 更新时间:2023-11-29 08:15:26 26 4
gpt4 key购买 nike

嗨我在下面写了这样的代码,但它返回了错误的输出,这不是最小值。

    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);

我的问题是:为什么它不返回具有最少贪婪(最小值)的节点?谢谢

最佳答案

将初始化函数替换为 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;
}

关于java - 返回成本最低!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5186568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com