my target is to iterate the node searching logic that I have created. For doing this, I've implemented a do...while condition. In do{} i say "choose the action that satisfy the precondition with the lower cost" from an initial state and initial node. When it is found, set the initial state and node with the new ones. In while() I express the condition "until the state is equal to the goal state" go ahead.
The problem is that when I run this, is printing only the result of the first iteration but seems that is still continuing to compute no stop.
This is the code
private static Node nodeStatus;
static Action loadPlaneP1 = new Action("loadPlaneP1",pkg1Location[1], pkg2Location[0], truckLocation[0], planeLocation[0], cityLocation[0], 30);
static Action loadPlaneP2 = new Action("loadPlaneP2", pkg1Location[0], pkg2Location[1], truckLocation[0], planeLocation[0], cityLocation[0], 40);
....//other actions
State state = new State(0, pkg1Location[0], pkg2Location[0], truckLocation[0], planeLocation[0], cityLocation[0]);
State newState = new State(0, pkg1Location[0], pkg2Location[0], truckLocation[0], planeLocation[0], cityLocation[0]);
static State goal = new State(0, pkg1Location[5], pkg2Location[4], truckLocation[3], planeLocation[2], cityLocation[1]);
static Action[] acts = {loadPlaneP1, loadPlaneP2, fly, unloadPlaneP1, unloadPlaneP2, loadTruckP1, loadTruckP2, drive, unloadTruckP1, unloadTruckP2 };
Node startNode = new Node(state, 0);
int[] costs = {loadPlaneP1.getActionCost(), loadPlaneP2.getActionCost(), fly.getActionCost(), unloadPlaneP1.getActionCost(), unloadPlaneP2.getActionCost(), loadTruckP1.getActionCost(), loadTruckP2.getActionCost(), drive.getActionCost(), unloadTruckP1.getActionCost(),unloadTruckP2.getActionCost()};
do{
if(nodeStatus != startNode) {
nodeStatus = startNode;
}
else {
nodeStatus = startNode;
}
if(nodeStatus == startNode) {
System.out.println("Old state parameters are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5());
for(int i = 0; i < acts.length; i++) {
if(acts[i].getActionCost() == getMinValue(costs)) {
System.out.println("PRE The first parameter is : " + acts[i].getActParameter1() + acts[i].name +" "+ acts[i].actionCost);
if(acts[i].loadPlaneP1Precondition() == true) {
System.out.println("POST The first parameter is : " + acts[i].getActParameter1());
System.out.println("Precondition satysfied" + " with action name: " + acts[i].name);
if(acts[i].getActParameter1() != state.getStateParameter1()) {
newState.setStateParameter1(acts[i].getActParameter1());
}
if(acts[i].getActParameter2() != state.getStateParameter2()) {
if(acts[i].getActParameter2() != State.pkg2Location[1]) {
newState.setStateParameter2(acts[i].getActParameter2());
}
}
if(acts[i].getActParameter3() != state.getStateParameter3()) {
newState.setStateParameter3(acts[i].getActParameter3());
}
if(acts[i].getActParameter4() != state.getStateParameter4()) {
newState.setStateParameter4(acts[i].getActParameter4());
}
if(acts[i].getActParameter5() != state.getStateParameter5()) {
newState.setStateParameter5(acts[i].getActParameter5());
}
acts[i].setActCost(100);
}
................................//checking other preconditions
Node child = new Node("Node "+ i, newState, startNode, acts[i].getActionCost(), acts[i].name);
startNode = child;
state = newState;
System.out.println("Costs array: "+ Arrays.toString(costs));
System.out.println("ActionID" +" " + i);
System.out.println("The action choosen is " + acts[i].name +" "+ acts[i].actionCost +" "+ acts[i].getActParameter1());
System.out.println("State parameters updated are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5());
System.out.println("The node created is : " + child.getNodeName());
}
}
}
}while(state == goal);
How can I print the result for each iteration?
At the end of your loop, you're checking while(state == goal)
.
Try something like while (state.equals(goal));
.
You should use the equals
method when comparing object instances (except when they're enumerations, which is not your case it seems).
By the way, you said "until the state is equal to the goal state" go ahead.
This would be translated into while (!state.equals(goal));
(not equal).
UPDATE:
Furthermore, in the beginning of your loop, you have the following code:
if(nodeStatus != startNode) {
nodeStatus = startNode;
}
else {
nodeStatus = startNode;
}
if(nodeStatus == startNode) {
First of all, also here you're comparing 2 objects by means of the equals sign instead of the equals method.
Furthermore, in either case you assign startNode
to nodeStatus
. The if-condition afterwards will always be true.