gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:13:06 25 4
gpt4 key购买 nike

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

//Actions
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);
static Action fly = new Action("fly",pkg1Location[1], pkg2Location[1], truckLocation[0], planeLocation[1], cityLocation[0], 100);
static Action unloadPlaneP1 = new Action("unloadPlaneP1",pkg1Location[2], pkg2Location[1], truckLocation[0], planeLocation[2], cityLocation[1], 50);
static Action unloadPlaneP2 = new Action("unloadPlaneP2",pkg1Location[1], pkg2Location[2], truckLocation[0], planeLocation[2], cityLocation[1], 55);
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], 10);
static Action drive = new Action("drive",pkg1Location[3], pkg2Location[3], truckLocation[1], planeLocation[2], cityLocation[1], 70);
static Action unloadTruckP1 = new Action("unloadTruckP1", pkg1Location[5], pkg2Location[5], truckLocation[2], planeLocation[2], cityLocation[1], 40);
static Action unloadTruckP2 = new Action("unloadTruckP2",pkg1Location[4], pkg2Location[4], truckLocation[3], planeLocation[2], cityLocation[1], 43);

//Array
static Action[] acts = { loadPlaneP1, loadPlaneP2, fly, unloadPlaneP1, unloadPlaneP2, loadTruckP1, loadTruckP2, drive, unloadTruckP1, unloadTruckP2 };

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

此外,如果我更改成本以将另一个操作成本设置为最低,则逻辑将不起作用。

//Main logic
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; acts[i].getActionCost() == getMinValue(costs); i++) {
System.out.println("PRE The first parameter is : " + acts[i].getActParameter1() + acts[i].name);

if(acts[i].getActParameter1() == "plane") {
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()) {
state.setStateParameter1(acts[i].getActParameter1());
}

if(acts[i].getActParameter2() != state.getStateParameter2()) {
state.setStateParameter2(acts[i].getActParameter2());
}

if(acts[i].getActParameter3() != state.getStateParameter3()) {
state.setStateParameter3(acts[i].getActParameter3());
}

if(acts[i].getActParameter4() != state.getStateParameter4()) {
state.setStateParameter4(acts[i].getActParameter4());
}

if(acts[i].getActParameter5() != state.getStateParameter5()) {
state.setStateParameter5(acts[i].getActParameter5());
}
}

Node child = new Node(state, startNode, acts[i].getActionCost());

/* List<Action> removeList = new ArrayList<Action>(Arrays.asList(acts));
removeList.remove(Arrays.asList(i));
acts = removeList.toArray(acts);*/

//nextAction = acts[i];
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());
};

}

我收到的输出是

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的参数不同。

为什么会发生这种情况?

最佳答案

我在您的代码中看到的一个问题是 for 循环条件,如果实际成本不是最小成本,则会停止循环。因此,如果在第一次迭代中实际成本不等于最小成本,则永远不会迭代 for 循环。我没有试图找出你在 for 循环中做了什么。可能还有更多错误。

for( int i = 0; i < acts.length -1; i++ ) 
{
if ( acts[i].getActionCost( ) == getMinValue( costs ) )
{
System.out.println( "PRE The first parameter is : " +
acts[i].getActParameter1() + acts[i].name );

if ( acts[i].getActParameter1() == "plane" )
{
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() )
{
state.setStateParameter1( acts[i].getActParameter1() );
}

if(acts[i].getActParameter2() != state.getStateParameter2())
{
state.setStateParameter2( acts[i].getActParameter2() );
}

if( acts[i].getActParameter3() != state.getStateParameter3() )
{
state.setStateParameter3( acts[i].getActParameter3() );
}

if( acts[i].getActParameter4() != state.getStateParameter4() )
{
state.setStateParameter4( acts[i].getActParameter4() );
}

if( acts[i].getActParameter5() != state.getStateParameter5() )
{
state.setStateParameter5( acts[i].getActParameter5() );
}
}

Node child = new Node(state, startNode, acts[i].getActionCost());

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

关于java - 如何在Java中使用for循环编写最短路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com