Hi
I have written such a code below but it returns wrong output which is not the least value.
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;
}
I send a list to this method several times but it will return wrong output.
output:
[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);
MY question is: why it doesn’t return the node which has the least greedy (least value)?
thanks
Replace the initialisation function to
Element node =objectList.get(0);as shown here