// executeplan.c // // Version Date Author Description // -------------------------------------------------------------------------- // 0.01 29/07/01 BWW Execute the current plan #include #include "planner.h" #include "funcprototypes.h" extern Plan_Struct Plan; extern UnmetPreconditions_Struct UnmetPreconditions; extern Orderings_Struct Orderings; //extern AvailablePrecons_Struct AvailablePrecons; //extern AvailableActions_Struct AvailableActions; extern ActionEffects_Struct ActionEffects; extern CausalLinks_Struct CausalLinks; extern SearchGoalNodes_Struct SearchGoalNodes; extern Agent_Struct Agent; extern Route_Struct Route; extern Key_Struct Key; extern Door_Struct Door[]; extern CausalLinks_Struct CausalLinks; extern Orderings_Struct Orderings; extern Metrics_Struct Metrics; extern int startx, starty,destinationx, destinationy; // -------------------------------------------------------------------------- // Select the next ActionEffect to execute // -------------------------------------------------------------------------- // Description: Select the next action effect from the plan, considering // which of the outstanding preconditions are available to be // addressed. // // Parameters: None // // Returns: None void GetNextActionEffect() { static AvailablePrecons_Struct AvailablePrecons; static AvailableActions_Struct AvailableActions; int index, index2, count, lowestcost, selectedindex; // Initialise the AvailablePrecons structure. All preconditions are initially available for (index=0;index 0) { // Select the best route SelectRoute(); // Set up the Plan attributes Plan.duration = Route.cost[1]; // printf("Route No. steps: %d\n", Route.numsteps); // printf("Route Step 0 X: %d Y: %d Cost: %d\n", Route.x[0], Route.y[0],Route.cost[0]); // printf("Route Step 1 X: %d Y: %d Cost: %d\n", Route.x[1], Route.y[1],Route.cost[1]); // printf("Route Step 2 X: %d Y: %d Cost: %d\n", Route.x[2], Route.y[2],Route.cost[2]); // printf("Route Step 3 X: %d Y: %d Cost: %d\n", Route.x[3], Route.y[3],Route.cost[3]); // printf("Route Step 4 X: %d Y: %d Cost: %d\n", Route.x[4], Route.y[4],Route.cost[4]); // printf("Route Step 5 X: %d Y: %d Cost: %d\n", Route.x[5], Route.y[5],Route.cost[5]); // printf("Route Step 6 X: %d Y: %d Cost: %d\n", Route.x[6], Route.y[6],Route.cost[6]); // printf("Route Step 7 X: %d Y: %d Cost: %d\n", Route.x[7], Route.y[7],Route.cost[7]); // printf("Route Step 8 X: %d Y: %d Cost: %d\n", Route.x[8], Route.y[8],Route.cost[8]); // printf("Route Step 9 X: %d Y: %d Cost: %d\n", Route.x[9], Route.y[9],Route.cost[9]); Plan.currentstep = 1; } else { Plan.status=FAILEDPLAN; } } else { Plan.duration = ActionEffects.duration[selectedindex]; } } } } // -------------------------------------------------------------------------- // Initialise plan execution parameters // -------------------------------------------------------------------------- // Description: Initialise the parameters that will be used during the // execution of the plan // // Parameters: None // // Returns: None void InitExecution() { Plan.status = NOPLAN; Plan.actioneffectindex = -1; Plan.duration = 0; Plan.goalachieved = false; } // -------------------------------------------------------------------------- // Execute the plan // -------------------------------------------------------------------------- // Description: Execute the current plan // // Parameters: None // // Returns: None void ExecutePlan() { int index, index2, count; // Check for a valid plan, if there isn't one, then create one if ((Plan.status==NOPLAN)||(Plan.status==FAILEDPLAN)) { InitExecution(); BuildPlan(); Plan.status=PLAN; // Capture metrics Metrics.p_plannumber++; Metrics.p_numpreconditions = UnmetPreconditions.numpreconditions; Metrics.p_numactioneffects = ActionEffects.numeffects; Metrics.p_numcausallinks = CausalLinks.numcausallinks; Metrics.p_numorderings = Orderings.numorderings; Metrics.p_totplannumber++ ; Metrics.p_totnumpreconditions += UnmetPreconditions.numpreconditions; Metrics.p_totnumactioneffects += ActionEffects.numeffects; Metrics.p_totnumcausallinks += CausalLinks.numcausallinks; Metrics.p_totnumorderings += Orderings.numorderings; MetricPlan(); } // Check for a current ActionEffect, get the next if there isn't one if (Plan.actioneffectindex==-1) { GetNextActionEffect(); } // Check to see that we still have a valid plan and excute the current step if (Plan.status == PLAN) { // Decrement the counter that indicates the length of time an actioneffect // takes to apply. Endeavour to apply the effects of the action to the // environment if it has reached zero. Plan.duration--; if (Plan.duration == 0) { // Check whether the current ActionEffect is a MOVETO. If it is then it has // multiple parts that need to be applied before it is considered complete if (ActionEffects.effect[Plan.actioneffectindex].condition == EFFECT_MOVETO) { // If the location is inaccessible, then the plan has failed. Otherwise move there if (CheckLocationForAccess(Route.x[Plan.currentstep],Route.y[Plan.currentstep],true)==0) { Plan.status = FAILEDPLAN; Metrics.e_preconditionsfailed++; } else { Agent.x = Route.x[Plan.currentstep]; Agent.y = Route.y[Plan.currentstep]; // If the agent has the key then set it's location as well if (Agent.gotkey==true) { Key.x = Agent.x; Key.y = Agent.y; } } // Imcrement the current location along the route. Check for the end and get the duration // of the next step Plan.currentstep++; if (Plan.currentstep == Route.numsteps) { // Plan.actioneffectindex = -1; } else { Plan.duration = Route.cost[Plan.currentstep]; } } // Check again that the duration is still zero to see whether we have // reached the end of the MOVETO. MOVETO may also have failed, so also // check for plan failure if ((Plan.duration == 0)&&(Plan.status==PLAN)) { Metrics.e_preconditionsmet++; // Apply the effects of the action to the environment switch(ActionEffects.effect[Plan.actioneffectindex].condition) { case EFFECT_MOVETO: break; case EFFECT_UNLOCK: Door[ActionEffects.effect[Plan.actioneffectindex].value1].lockstatus=DOORUNLOCKED; break; case EFFECT_OPEN: Door[ActionEffects.effect[Plan.actioneffectindex].value1].doorstatus=DOOROPEN; break; case EFFECT_PICKUP: Agent.gotkey=true; break; case EFFECT_PLAN: break; case EFFECT_WAIT: break; } // Update the Causal Links for (index=0;index