?? jshop2.java
字號:
PlanStepInfo newStep = new PlanStepInfo(); newStep.planFound = true; planStepList.add(newStep); numPlans++; return true; } } //-- This array of size 4 will store the atoms and protections that are //-- deleted from and added to the current state of the world as a result //-- of an operator being applied. This information is used in case a //-- backtrack happens over that operator to store the state of the world //-- to what it was before the backtracked operator was applied. v.delAdd = new Vector[4]; //-- To iterate over the tasks we have the option to achieve right now. v.e = v.t0.iterator(); //-- For each of the tasks that we have the option to achieve right now, while (v.e.hasNext()) { //-- Find the next option. v.tl = (TaskList)v.e.next(); v.t = v.tl.getTask(); //-- Create a TRYING step for the list of plan steps PlanStepInfo newStep = new PlanStepInfo(); newStep.action = "TRYING"; newStep.state = state.getState(); newStep.taskAtom = v.t; planStepList.add(newStep); //-- If that task is primitive, if (v.t.isPrimitive()) { //-- Remove the task from the task list, by replacing it with an empty //-- task list. v.tl.replace(TaskList.empty); //-- Find all the operators that achieve this primitive task. v.o = domain.ops[v.t.getHead().getHead()]; //-- For each of these operators, for (v.j = 0; v.j < v.o.length; v.j++) { //-- Find the binding that unifies the head of the operator with the //-- task. v.binding = v.o[v.j].unify(v.t.getHead()); //-- If there is such bindings, if (v.binding != null) { //-- Get the iterator that iterates over all the bindings that can //-- satisfy the precondition for this operator. v.p = v.o[v.j].getIterator(v.binding, 0); //-- For each such binding, while ((v.nextB = v.p.nextBinding()) != null) { //-- Merge the two bindings. Term.merge(v.nextB, v.binding); //-- If the operator is applicable, apply it, and, if (v.o[v.j].apply(v.nextB, state, v.delAdd)) { //-- Add the instance of the operator that achieved this task //-- to the beginning of the plan, remembering how much it //-- cost. double cost = currentPlan.addOperator(v.o[v.j], v.nextB); //-- Create a STATECHANGED step for the list of plan steps newStep = new PlanStepInfo(); newStep.action = "STATECHANGED"; newStep.taskAtom = v.t; newStep.delAdd = v.delAdd; newStep.operatorInstance = v.o[v.j].getHead().applySubstitution(v.nextB).toString(JSHOP2.getDomain().getPrimitiveTasks()); planStepList.add(newStep); //-- Recursively call the same function to achieve the //-- remaining tasks. If a plan is found for the remaining //-- tasks and we have found the maximum number of plans we are //-- allowed, return true. if (findPlanHelper(tasks) && plans.size() >= planNo) return true; //-- Remove the operator from the current plan. currentPlan.removeOperator(cost); } //-- Undo the changes that were the result of applying this //-- operator, because we are backtracking here. state.undo(v.delAdd); } } } //-- Insert the task we chose to achieve first back where it was, //-- because we couldn't achieve it. v.tl.undo(); } //-- If that task is compound, else { //-- Find all the methods that decompose this compound task. v.m = domain.methods[v.t.getHead().getHead()]; //-- For each of these methods, for (v.j = 0; v.j < v.m.length; v.j++) { //-- Find the binding that unifies the head of the method with the //-- task. v.binding = v.m[v.j].unify(v.t.getHead()); //-- If there is such binding, if (v.binding != null) { //-- Initially, precondition of no branch of this method has //-- already been satisfied, so set this variable to false. v.found = false; //-- Iterate on all the branches of this method. note the use of //-- 'v.found' in the condition for the 'for' loop. It is there //-- because of the semantics of the method branches in JSHOP2: //-- Second branch is considered only when there is no binding for //-- the first branch, the third branch is considered only when //-- there is no binding for the first and second branches, etc. for (v.k = 0; (v.k < v.m[v.j].getSubs().length) && !v.found; v.k++) { //-- Get the iterator that iterates over all the bindings that //-- can satisfy the precondition for this branch of this method. v.p = v.m[v.j].getIterator(v.binding, v.k); //-- For each such binding, while ((v.nextB = v.p.nextBinding()) != null) { //-- Merge the two bindings. Term.merge(v.nextB, v.binding); //-- Replace the decomposed task in task list with its //-- decomposition according to this branch of this method. v.tl.replace(v.m[v.j].getSubs()[v.k].bind(v.nextB)); //-- Create a REDUCED step for the list of plan steps newStep = new PlanStepInfo(); newStep.action = "REDUCED"; newStep.taskAtom = v.t; newStep.children = v.tl.subtasks; newStep.ordered = v.m[v.j].getSubs()[v.k].isOrdered(); newStep.method = v.m[v.j].getLabel(v.k); planStepList.add(newStep); //-- Recursively call the same function to achieve the //-- remaining tasks, but make the function choose its next //-- tasks to achieve to be the substasks of the task we just //-- decomposed, till an operator is seen and applied, or this //-- whole task is achieved without seeing an operator (i.e., //-- this task was decomposed to an empty task list). if (findPlanHelper(v.tl) && plans.size() >= planNo) //-- A full plan is found, return true. return true; //-- The further branches of this method must NOT be considered //-- even if this branch fails because there has been at least //-- one satisfier for this branch of the method. Set this //-- variable to true to prevent the further branches of this //-- method from being considered. v.found = true; //-- Undo the changes in the task list, because this particular //-- decomposition failed. v.tl.undo(); } } } } } //-- Create a BACKTRACKING step for the list of plan steps newStep = new PlanStepInfo(); newStep.action = "BACKTRACKING"; newStep.taskAtom = v.t; planStepList.add(newStep); } //-- Return false, because all the options were tried and none worked. return false; } /** This function returns the planning domain. * * @return * the current planning domain. */ public static Domain getDomain() { return domain; } /** This function returns the current state of the world. * * @return * the current state of the world. */ public static State getState() { return state; } /** This function is used to initialize the planning algorithm. * * @param domainIn * the planning domain. * @param stateIn * the initial state of the world. */ public static void initialize(Domain domainIn, State stateIn) { domain = domainIn; state = stateIn; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -