亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Java中使用for循環編寫最短路徑問題

如何在Java中使用for循環編寫最短路徑問題

慕婉清6462132 2024-01-17 16:34:29
我創建了一個動作數組,每個動作都有一個成本。之后,我實現了一個 for 循環來查找成本最低的操作。其次,我必須檢查先決條件,看看可以采取哪些行動。問題出在主邏輯中,因為當我打印操作名稱和獲得的成本時,顯示的是loadPlaneP1(成本較低的那個),但我通過的參數getActParameter1()是unloadTruckP2(數組中最后一個)的參數。我收到的輸出是Old state parameters are pkg1Location: lhr pkg2Location: lhr truckLocation: cdg planeLocation: lhr cityLocation:londonPRE The first parameter is : southloadPlaneP1POST The first parameter is : southPrecondition satysfied with action name: loadPlaneP1Costs array: [30, 40, 100, 50, 55, 60, 70, 70, 40]ActionID 0The action choosen is loadPlaneP130southState parameters updated are pkg1Location: south pkg2Location: south truckLocation: south planeLocation: cdg cityLocation:paris所以條件不滿足,因為我得到的參數getActParameter1()與已有的不同loadPlaneP1。為什么會發生這種情況?
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

我在代碼中看到的一個問題是 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());

  }

}   



查看完整回答
反對 回復 2024-01-17
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號