my do ... while цикл заканчивается после второй итерации, даже если условие не удовлетворено . Мой проект состоит из двух циклов: первый первый - это for condition
, который из всех возможных действий проверяет, существует ли один с минимальной стоимостью, удовлетворяющий предварительным условиям. После этого цикл for должен быть повторен для повторного поиска во всем массиве действий. Для этого я реализовал условие do...while
. Это повторяет цикл for до тех пор, пока параметры, которые изменяет действие, не станут равными цели.
Проблема находится рядом с действием unloadPlaneP1
и unloadPlaneP2
. Если я устанавливаю стоимость действия в unloadPlaneP1 ниже, чем в unloadPlaneP2, цикл повторяется два раза и работает. Если я устанавливаю стоимость действия в unloadPlaneP2 ниже, чем в unloadPlaneP1, цикл должен выполняться 3 раза для достижения успеха, но он останавливается на второй итерации без выполнения условия.
public class Main {
private static String[] pkg1Location = {"lhr","plane", "cdg", "truck", "south","north"};
private static String[] pkg2Location = {"lhr","plane", "cdg", "truck", "south","north"};
private static String[] truckLocation = {"cdg", "driving", "north","south"};
private static String[] planeLocation = {"lhr","flying","cdg"};
private static String[] cityLocation = {"london", "paris"};
//actions with effects
static Action loadPlaneP1 = new Action("loadPlaneP1",pkg1Location[1], pkg2Location[0], truckLocation[0], planeLocation[0], cityLocation[0], 35);
static Action loadPlaneP2 = new Action("loadPlaneP2", pkg1Location[0], pkg2Location[1], truckLocation[0], planeLocation[0], cityLocation[0], 30);
static Action fly = new Action("fly",pkg1Location[1], pkg2Location[1], truckLocation[0], planeLocation[1], cityLocation[0], 40);
static Action unloadPlaneP1 = new Action("unloadPlaneP1",pkg1Location[2], pkg2Location[1], truckLocation[0], planeLocation[2], cityLocation[1], 55);
static Action unloadPlaneP2 = new Action("unloadPlaneP2",pkg1Location[1], pkg2Location[2], truckLocation[0], planeLocation[2], cityLocation[1], 45);
static Action loadTruckP1 = new Action("loadTruckP1",pkg1Location[3], pkg2Location[2], truckLocation[0], planeLocation[2], cityLocation[1], 60);
static Action loadTruckP2 = new Action("loadTruckP2",pkg1Location[2], pkg2Location[3], truckLocation[0], planeLocation[2], cityLocation[1], 70);
static Action drive = new Action("drive",pkg1Location[3], pkg2Location[3], truckLocation[1], planeLocation[2], cityLocation[1], 80);
static Action unloadTruckP1 = new Action("unloadTruckP1", pkg1Location[5], pkg2Location[3], truckLocation[2], planeLocation[2], cityLocation[1], 90);
static Action unloadTruckP2 = new Action("unloadTruckP2",pkg1Location[3], pkg2Location[4], truckLocation[3], planeLocation[2], cityLocation[1], 100);
static Action[] acts = {loadPlaneP1, loadPlaneP2, fly, unloadPlaneP1, unloadPlaneP2, loadTruckP1, loadTruckP2, drive, unloadTruckP1, unloadTruckP2 };
public static State state = new State(0, pkg1Location[0], pkg2Location[0], truckLocation[0], planeLocation[0], cityLocation[0]);
public static State goal = new State(0, pkg1Location[5], pkg2Location[4], truckLocation[3], planeLocation[2], cityLocation[1]);
private static Node node;
private static Node startNode = new Node("StartNode", state, node, 0, "");
private static Node nodeStatus;
public static void main(String args[]) throws InterruptedException {
State newState = new State(0, pkg1Location[0], pkg2Location[0], truckLocation[0], planeLocation[0], cityLocation[0]);
nodeStatus = startNode;
int[] costs = {loadPlaneP1.getActionCost(), loadPlaneP2.getActionCost(), fly.getActionCost(), unloadPlaneP1.getActionCost(), unloadPlaneP2.getActionCost(), loadTruckP1.getActionCost(), loadTruckP2.getActionCost(), drive.getActionCost(), unloadTruckP1.getActionCost(),unloadTruckP2.getActionCost()};
System.out.println("Old state parameters are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5()+ "\n");
do{
System.out.println("ARRIVA QUI?");
for(int i = 0; i < acts.length; i++) {
TimeUnit.SECONDS.sleep(1);
System.out.println("Costs array: "+ Arrays.toString(costs));
System.out.println("Action cost "+ acts[i].getActionCost());
System.out.println("Each run parameters " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5()+ "\n");
if(acts[i].getActionCost() == getMinValue(costs)) {
System.out.println("COST CHECK PASSED");
System.out.println("\n"+ "PRE The act first parameter is : " + acts[i].getActParameter1() + acts[i].name +" "+ acts[i].actionCost);
if(acts[i].loadPlaneP1Precondition() == true) {
System.out.println("POST The act first parameter is : " + acts[i].getActParameter1());
System.out.println("Precondition P1 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);
Node child = new Node("Node "+ i, newState, startNode, acts[i].getActionCost(), acts[i].name);
nodeStatus = child;
state = newState;
costs[i] = costs[i] + 100;
System.out.println("The node number "+ (i+1) + " is created.");
System.out.println("The action choosen is " + acts[i].name +". The cost is "+ acts[i].actionCost);
System.out.println("State parameters updated are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5()+ "\n");
//System.out.println("Costs array: "+ Arrays.toString(costs));
}
.......//other preconditions
if(acts[i].unloadPlaneP1Precondition() == 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[2]) {
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());
}
Node child = new Node("Node "+ i, newState, startNode, acts[i].getActionCost(), acts[i].name);
nodeStatus = child;
state = newState;
costs[i] = costs[i] + 100;
System.out.println("The node number "+ (i+1) + " is created.");
System.out.println("The action choosen is " + acts[i].name +". The cost is "+ acts[i].actionCost);
System.out.println("State parameters updated are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5()+ "\n");
//System.out.println("Costs array: "+ Arrays.toString(costs));
}
if(acts[i].unloadPlaneP2Precondition() == 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()) {*/
if(acts[i].getActParameter1() == State.pkg1Location[2]) {
newState.setStateParameter1(acts[i].getActParameter1());
}
//}
if(acts[i].getActParameter2() != state.getStateParameter2()) {
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());
}
Node child = new Node("Node "+ i, newState, startNode, acts[i].getActionCost(), acts[i].name);
nodeStatus = child;
state = newState;
costs[i] = costs[i] + 100;
System.out.println("The node number "+ (i+1) + " is created.");
System.out.println("The action choosen is " + acts[i].name +". The cost is "+ acts[i].actionCost);
System.out.println("State parameters updated are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5()+ "\n");
//System.out.println("Costs array: "+ Arrays.toString(costs));
}
}else {
System.out.println("COST CHECK NOT SATYSFIED" + " with action name: " + acts[i].name );
}
}System.out.println("\n");
System.out.println("Last run parameters are " + "pkg1Location: " + state.getStateParameter1() + " pkg2Location: " + state.getStateParameter2() + " truckLocation: "+ state.getStateParameter3() + " planeLocation: " + state.getStateParameter4() + " cityLocation:"+ state.getStateParameter5()+ "\n");
if(state.getStateParameter1().equals(goal.getStateParameter1())
&&state.getStateParameter2().equals(goal.getStateParameter2())
&&state.getStateParameter3().equals(goal.getStateParameter3())
&&state.getStateParameter4().equals(goal.getStateParameter4())
&&state.getStateParameter5().equals(goal.getStateParameter5())){
System.out.println("LAST WHILE SATYSFIED");
}else {
System.out.println("LAST WHILE NOT SATYSFIED");
}
}while(!state.getStateParameter1().equals(goal.getStateParameter1())
&&!state.getStateParameter2().equals(goal.getStateParameter2())
&&!state.getStateParameter3().equals(goal.getStateParameter3())
&&!state.getStateParameter4().equals(goal.getStateParameter4())
&&!state.getStateParameter5().equals(goal.getStateParameter5()));
}
}
public static int getMinValue(int[] numbers){
int minValue = numbers[0];
for(int i=1;i<numbers.length;i++){
if(numbers[i] < minValue){
minValue = numbers[i];
}
}
return minValue;
}
}
Почему не входит в третью итерацию?