I'm implementing the A* algorithm to allocate a given number of taxis to a given number of customers:
public SearchNode AStarSearch(List<Car> allCars, List<Customer> allCustomers) {
// creates first node
SearchNode firstNode = createFirstNode(allCars, allCustomers);
open.add(firstNode);
SearchNode currentNode;
while(true) {
currentNode = open.poll(); //error thrown here
if (currentNode.allCustomers.isEmpty()) break;
closed.add(currentNode);
SearchNode temp;
Iterator<Customer> itrCus = currentNode.allCustomers.iterator();
Iterator<Car> itrCar = currentNode.allCars.iterator();
while (itrCus.hasNext()) {
Customer currentCustomer = itrCus.next();
while (itrCar.hasNext()) {
Car currentCar = itrCar.next();
temp = new SearchNode(currentNode, currentCar, currentCustomer);
openUpdate(currentNode, temp);
}
}
}
return currentNode;
}
Now for the children SearchNode I want to delete the customer, that it has served. But this should only happen inside the search node.
This is what's happening in the SearchNode class:
public class SearchNode implements Comparable<SearchNode> {
List<Car> allCars;
List<Customer> allCustomers;
SearchNode parent;
public SearchNode(SearchNode parent, Car currentCar, Customer currentCustomer) {
// inheriting values from parent search node
this.allCars = parent.allCars;
this.allCustomers = parent.allCustomers;
this.parent = parent;
// now updating this node's values
this.allCustomers.remove(currentCustomer);
}
I checked with some prints and in the second iteration of the while(itrCar.hasNext())
, the parent's allCustomer
list is missing the currentCustomer
.
Edit: I should rather ask: can someone please tell my why I changed my parent node's value? It's probably because I haven't understood the whole java being actually pass-by-reference.