如何在Java中使用for循环编写最短路径问题

我创建了一个动作数组,每个动作都有一个成本。之后,我实现了一个 for 循环来查找成本最低的操作。其次,我必须检查先决条件,看看可以采取哪些行动。

问题出在主逻辑中,因为当我打印操作名称和获得的成本时,显示的是loadPlaneP1(成本较低的那个),但我通过的参数getActParameter1()是unloadTruckP2(数组中最后一个)的参数。


我收到的输出是


Old state parameters are pkg1Location: lhr pkg2Location: lhr truckLocation: cdg planeLocation: lhr cityLocation:london

PRE The first parameter is : southloadPlaneP1

POST The first parameter is : south

Precondition satysfied with action name: loadPlaneP1

Costs array: [30, 40, 100, 50, 55, 60, 70, 70, 40]

ActionID 0

The action choosen is loadPlaneP130south

State parameters updated are pkg1Location: south pkg2Location: south truckLocation: south planeLocation: cdg cityLocation:paris

所以条件不满足,因为我得到的参数getActParameter1()与已有的不同loadPlaneP1。


为什么会发生这种情况?


慕婉清6462132
浏览 99回答 1
1回答

泛舟湖上清波郎朗

我在代码中看到的一个问题是 for 循环条件,如果实际成本不是最小成本,则会停止循环。因此,如果在第一次迭代中实际成本不等于最小成本,则永远不会迭代 for 循环。我没有试图找出你在 for 循环中做了什么。可能还有更多错误。for( int i = 0; i < acts.length -1; i++ )&nbsp;{&nbsp; &nbsp;if ( acts[i].getActionCost( ) == getMinValue( costs ) )&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp;System.out.println( "PRE The first parameter is : " +&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acts[i].getActParameter1() + acts[i].name );&nbsp; &nbsp; &nbsp;if ( acts[i].getActParameter1() == "plane" )&nbsp;&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "POST The first parameter is : " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acts[i].getActParameter1() );&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "Precondition satysfied with action name: " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acts[i].name );&nbsp; &nbsp; &nbsp; &nbsp; if ( acts[i].getActParameter1() != state.getStateParameter1() )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.setStateParameter1( acts[i].getActParameter1() );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(acts[i].getActParameter2() != state.getStateParameter2())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.setStateParameter2( acts[i].getActParameter2() );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if( acts[i].getActParameter3() != state.getStateParameter3() )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.setStateParameter3( acts[i].getActParameter3() );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if( acts[i].getActParameter4() != state.getStateParameter4() )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.setStateParameter4( acts[i].getActParameter4() );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if( acts[i].getActParameter5() != state.getStateParameter5() )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.setStateParameter5( acts[i].getActParameter5() );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Node child = new Node(state, startNode, acts[i].getActionCost());&nbsp; &nbsp; System.out.println( "Costs array: "+&nbsp; Arrays.toString( costs ) );&nbsp; &nbsp; System.out.println( "ActionID" +" " +&nbsp; i );&nbsp; &nbsp; System.out.println( "The action choosen is " + acts[i].name +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acts[i].actionCost + acts[i].getActParameter1() );&nbsp; &nbsp; System.out.println( "State parameters updated are " + "pkg1Location: " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.getStateParameter1() + " pkg2Location: " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.getStateParameter2() + " truckLocation: "+&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.getStateParameter3() + " planeLocation: " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.getStateParameter4() + " cityLocation:"+&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.getStateParameter5());&nbsp; }}&nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java