【发布时间】:2019-11-04 13:55:42
【问题描述】:
我的目标是迭代我创建的节点搜索逻辑。为此,我实现了一个 do...while 条件。在 do{} 中,我从初始状态和初始节点中说“选择满足前提条件且成本较低的动作”。找到后,将初始状态和节点设置为新状态。 在 while() 中,我表示条件“直到状态等于目标状态”。
问题是当我运行它时,只打印第一次迭代的结果,但似乎仍在继续计算,没有停止。
这是代码
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);
如何打印每次迭代的结果?
【问题讨论】:
-
请阅读“如何创建minimal reproducible example”。然后使用edit 链接改进您的问题(不要通过 cmets 添加更多信息)。否则我们无法回答您的问题并为您提供帮助。除此之外:请花时间很好地格式化/缩进您的所有代码。您希望其他人利用他们的空闲时间来帮助您解决问题,因此请花 5 分钟的时间来提出易于阅读的输入。
-
我已经更新了代码标识并减少了代码,还需要什么?谢谢
-
它仍然无法重现。比如什么是nodeStatus。
-
我已经添加了所有的参数定义
-
对不起,你仍然有很多无用的换行符,你的缩进进一步向下仍然非常混乱。如前所述:请阅读该链接:我们需要能够理解导致您的问题的整个路径。例如,我们有关于您的代码失败的示例数据的 zero 信息。再说一遍:请阅读我给你的那个链接。小心点。
标签: java graph while-loop do-loops