如何为节点迭代设置 do...while

How to set do...while for a node iteration

我的目标是迭代我创建的节点搜索逻辑。为此,我实施了一个 do...while 条件。在 do{} 中,我从初始状态和初始节点说 "choose the action that satisfy the precondition with the lower cost"。找到后,将初始状态和节点设置为新的。 在 while() 中,我表达了条件 "until the state is equal to the goal state" go ahead.

问题是,当我 运行 这个时,只打印第一次迭代的结果,但似乎仍在继续计算,没有停止。

这是代码

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

如何打印每次迭代的结果?

在循环结束时,您正在检查 while(state == goal)
试试 while (state.equals(goal));.

您应该在比较对象实例时使用 equals 方法(除非它们是枚举,这似乎不是您的情况)。

对了,你说"until the state is equal to the goal state"继续
这将被翻译成 while (!state.equals(goal));(不等于)。

更新:
此外,在循环的开头,您有以下代码:

if(nodeStatus != startNode) {
               nodeStatus = startNode;
            }
             else {
               nodeStatus = startNode;
                  }


         if(nodeStatus == startNode) {

首先,在这里您也是通过等号而不是等号方法来比较 2 个对象。
此外,在任何一种情况下,您都将 startNode 分配给 nodeStatus。之后的 if 条件将始终为真。

您应该尝试 Robert Kock 的解决方案。

您需要检查:

  1. 如果状态 class 是由您创建的,您应该覆盖 equals 方法(这样您就不会比较对象的地址)。
  2. 并遵循 Robert Kock 的解决方案 while (!state.equals(goal));