jeudi 31 octobre 2019

How to code a shortest path problem with for cycle in Java

I created an actions array and each one of this action has a cost. After this, I implement a for cycle to find the action with the lowest cost. Secondly, I have to check the preconditions to see which action is possible.

//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 };

The problem is in the main logic, because when I print the action name and the cost obtained, is showing "loadPlaneP1"(the one with lower cost) but the parameter that I get through getActParameter1() is the parameter of "unloadTruckP2"(the last one in the array). Moreover, if I change the cost to set another action cost to the lowest, the logic doesn't work.

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

                     };




                     }  

The output that I receive is

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

So the condition is not satysfied, because the parameter that I get from getActParameter1() is different from the one that loadPlaneP1 has. Why is this happening?

Aucun commentaire:

Enregistrer un commentaire