/*new progress function, where only the positive effects of actions will be applied */ public virtual iThinkPlan progressPositive(iThinkPlan Step, iThinkAction Action) { iThinkPlan NewStep = new iThinkPlan(Step); List <iThinkAction> curActions; curActions = NewStep.getPlanActions(); curActions.Add(Action); NewStep.setState(Action.applyPositiveEffects(Step.getState())); return(NewStep); }
public override iThinkPlan progress(iThinkPlan Step, iThinkAction Action) { iThinkPlan NewStep = new iThinkPlan(Step); List <iThinkAction> curActions; curActions = NewStep.getPlanActions(); curActions.Add(Action); NewStep.setState(Action.applyEffects(Step.getState())); return(NewStep); }
public override iThinkPlan SearchMethod(iThinkState GoalState, iThinkActionManager ActionManager, List <iThinkPlan> OpenStates, List <iThinkState> VisitedStates) { //Debug.LogWarning(GoalState.ToString()); //Debug.LogWarning(ActionManager.getActions().Count); //Debug.LogWarning(OpenStates.Count); //Debug.LogWarning(VisitedStates.Count); /*Debug.Log("Available Actions = " + totalActions); * foreach ( iThinkAction act in ActionManager.getActions() ) * Debug.Log(act.ToString());*/ int it = 0; iThinkPlan curStep, nextStep; iThinkState CurrentState; while (OpenStates.Count != 0) { List <iThinkAction> applicableActions = new List <iThinkAction>(); curStep = new iThinkPlan(OpenStates[0]); CurrentState = OpenStates[0].getState(); VisitedStates.Add(CurrentState); OpenStates.RemoveAt(0); //Debug.LogWarning( "Iteration #" + it + " - " + curStep.getState().ToString() ); applicableActions = getApplicable(CurrentState, ActionManager.getActions()); int count = 0; foreach (iThinkAction action in applicableActions) { //Debug.Log(action.ToString()); nextStep = progress(curStep, action); if (compareStates(nextStep.getState(), GoalState)) { Debug.Log("Found Plan (DepthFS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count); Plan.setPlan(nextStep); return(Plan); } if (!VisitedStates.Contains(nextStep.getState())) { OpenStates.Insert(count, nextStep); count++; } } ++it; } Debug.Log("Didn't find Plan (DepthFS) " + it); return(null); }
public override iThinkPlan SearchMethod( iThinkState GoalState, iThinkActionManager ActionManager, List<iThinkPlan> OpenStates, List<iThinkState> VisitedStates ) { //Debug.LogWarning(GoalState.ToString()); //Debug.LogWarning(ActionManager.getActions().Count); //Debug.LogWarning(OpenStates.Count); //Debug.LogWarning(VisitedStates.Count); /*Debug.Log("Available Actions = " + totalActions); foreach ( iThinkAction act in ActionManager.getActions() ) Debug.Log(act.ToString());*/ int it = 0; iThinkPlan curStep, nextStep; iThinkState CurrentState; while ( OpenStates.Count != 0 ) { List<iThinkAction> applicableActions = new List<iThinkAction>(); curStep = new iThinkPlan( OpenStates[0] ); CurrentState = OpenStates[0].getState(); VisitedStates.Add( CurrentState ); OpenStates.RemoveAt( 0 ); //Debug.LogWarning( "Iteration #" + it + " - " + curStep.getState().ToString() ); applicableActions = getApplicable( CurrentState, ActionManager.getActions() ); int count=0; foreach ( iThinkAction action in applicableActions ) { //Debug.Log(action.ToString()); nextStep = progress( curStep, action ); if ( compareStates( nextStep.getState(), GoalState ) ) { Debug.Log( "Found Plan (DepthFS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count ); Plan.setPlan( nextStep ); return Plan; } if( !VisitedStates.Contains(nextStep.getState())) { OpenStates.Insert( count, nextStep ); count++; } } ++it; } Debug.Log( "Didn't find Plan (DepthFS) " + it ); return null; }
/*public int hFunction(iThinkState nextState, iThinkState GoalState, int area) * { * return base.hFunction(nextState, GoalState); * }*/ public int hFunction(iThinkState nextState, iThinkState GoalState, int area) { //Debug.LogError(area); iThinkState tempState = new iThinkState(GoalState); iThinkState curState = new iThinkState(nextState); for (int i = 0; i < depth; i++) { List <int> areas = new List <int>(); foreach (iThinkFact f in curState.getFactList()) { if (f.getName() == "npc-at") { string name = f.getObj(0).name; int a = Convert.ToInt32(name.Substring(4)); areas.Add(a); } tempState.delFact(f); } if (tempState.getFactList().Count == 0) { return(i); } ///List<iThinkAction> applicableActions = getApplicable(curState, ((SimpleFPSActionManager)actionManager).getActions(-1) ); ///List<iThinkAction> applicableActions = getApplicable(curState, ((SimpleFPSActionManager)actionManager).getActions(area) ); List <iThinkAction> allApplicableActions = new List <iThinkAction>(); foreach (int k in areas) { List <iThinkAction> applicableActions = getApplicable(curState, ((SimpleFPSActionManager)actionManager).getActions(k)); allApplicableActions.InsertRange(0, applicableActions); } iThinkPlan curStep = new iThinkPlan(curState); foreach (iThinkAction act in allApplicableActions) { iThinkPlan nextStep = progressPositive(curStep, act); //iThinkPlan nextStep = progress(curStep, act); curStep = nextStep; } curState = curStep.getState(); } //in case the planning graph has #layers = depth, return -1, indicating that no solution can be found starting from current state) //consequenlty, the state will not be added in the fringe. return(-1); }
public bool forwardSearch( iThinkState InitialState, iThinkState GoalState, iThinkActionManager ActionManager) { iThinkPlan ReturnVal; _OpenStates.Clear(); _VisitedStates.Clear(); iThinkPlan step = new iThinkPlan( InitialState ); _OpenStates.Add( step ); _VisitedStates.Add( step.getState() ); ReturnVal = SearchMethod( GoalState, ActionManager, _OpenStates, _VisitedStates ); if ( ReturnVal == null ) return false; else if ( ReturnVal.hasPlan() ) return true; return false; }
public bool forwardSearchBounded(iThinkState InitialState, iThinkState GoalState, iThinkActionManager ActionManager, double timelimit, int retries = 3) { iThinkPlan ReturnVal; _OpenStates.Clear(); _VisitedStates.Clear(); iThinkPlan step = new iThinkPlan(InitialState); _OpenStates.Add(step); _VisitedStates.Add(step.getState()); ReturnVal = SearchMethodBounded(GoalState, ActionManager, _OpenStates, _VisitedStates, timelimit); if (ReturnVal == null) { return(false); } else if (compareStates(ReturnVal.getState(), GoalState)) { return(true); } return(false); }
public bool forwardSearch(iThinkState InitialState, iThinkState GoalState, iThinkActionManager ActionManager) { iThinkPlan ReturnVal; _OpenStates.Clear(); _VisitedStates.Clear(); iThinkPlan step = new iThinkPlan(InitialState); _OpenStates.Add(step); _VisitedStates.Add(step.getState()); ReturnVal = SearchMethod(GoalState, ActionManager, _OpenStates, _VisitedStates); if (ReturnVal == null) { return(false); } else if (ReturnVal.hasPlan()) { return(true); } return(false); }
public override iThinkPlan SearchMethod(iThinkState GoalState, iThinkActionManager ActionManager, List <iThinkPlan> OpenStates, List <iThinkState> VisitedStates) { int it = 0; actionManager = ActionManager; iThinkPlan curStep, nextStep; iThinkState CurrentState = null; List <iThinkPlan> stateList = null; nodesVisited++; while (OpenStates.Count != 0) { List <iThinkAction> applicableActions = new List <iThinkAction>(); stateList = new List <iThinkPlan>(); curStep = new iThinkPlan(OpenStates[0]); CurrentState = OpenStates[0].getState(); VisitedStates.Add(CurrentState); /* * for ( int i = 0 ; i < ((SimpleFPSActionManager)ActionManager).totalAreas ; i++ ) * foreach (iThinkAction a in ((SimpleFPSActionManager)ActionManager).getActions(i)) * a.ToStringLite(); */ OpenStates.RemoveAt(0); if (curStep.getActionCount() < depth) { applicableActions = getApplicable(curStep.getState(), ((SimpleFPSActionManager)ActionManager).getActions(Convert.ToInt32(curStep.getState().getFactList().Find(item => item.getName().Equals("npc-at")).getObj(0).name.Substring(4)))); foreach (iThinkAction action in applicableActions) { nextStep = progress(curStep, action); nodesVisited++; if (compareStates(nextStep.getState(), GoalState)) { nodesExpanded++; Debug.Log("Found Plan (A* FS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count + ", Nodes Expanded: " + nodesExpanded); Plan.setPlan(nextStep); repoFunct.completed = true; return(Plan); } if (!VisitedStates.Contains(nextStep.getState())) { int Cost = hFunction(nextStep.getState(), GoalState); Cost = Cost + nextStep.getPlanActions().Count; nextStep.getState().setCost(Cost); stateList.Add(nextStep); nodesExpanded++; } } OpenStates.AddRange(stateList); OpenStates.Sort(delegate(iThinkPlan obj1, iThinkPlan obj2) { if (obj1.getState().getCost() == obj2.getState().getCost()) { return(0); } else if (obj1.getState().getCost() < obj2.getState().getCost()) { return(-1); } else { return(1); } } ); ++it; } } Debug.Log("Didn't find Plan (A* FS)"); return(null); }
public iThinkPlanner() { Plan = new iThinkPlan(); _OpenStates = new List <iThinkPlan>(); _VisitedStates = new List <iThinkState>(); }
public void setPlan( iThinkPlan plan ) { state = new iThinkState(plan.getState()); actions = new List<iThinkAction>( plan.getPlanActions() ); }
public override iThinkPlan SearchMethod(iThinkState GoalState, iThinkActionManager ActionManager, List <iThinkPlan> OpenStates, List <iThinkState> VisitedStates) { int it = 0; iThinkPlan curStep, nextStep; iThinkState CurrentState = null; List <iThinkPlan> stateList = null; this.totalActions = ActionManager.getActions().Count; this.nodes += OpenStates.Count; /*Debug.Log("Available Actions = " + totalActions); * foreach ( iThinkAction act in ActionManager.getActions() ) * Debug.Log(act.ToString());*/ List <iThinkAction> applicableActions = new List <iThinkAction>(); stateList = new List <iThinkPlan>(); if (compareStates(OpenStates[0].getState(), GoalState)) { Debug.Log("Found Plan (BestFS) - Already at goal state!"); Plan.setPlan(OpenStates[0]); repoFunct.completed = true; return(Plan); } while (OpenStates.Count != 0) { if (it % 200 == 0 && it != 0) { //InitRepo(); this.repoOpenStates = new List <iThinkPlan>(OpenStates); this.repoVisitedStates = new List <iThinkState>(VisitedStates); this.repoStateList = new List <iThinkPlan>(stateList); this.repoCurrentState = new iThinkState(CurrentState); this.repoIterations = it; repoFunct.loadData = true; return(null); } else if (repoFunct.loadData) { OpenStates = this.repoOpenStates; VisitedStates = this.repoVisitedStates; stateList = this.repoStateList; CurrentState = this.repoCurrentState; it = this.repoIterations; repoFunct.loadData = false; //ClearRepo(); } else { stateList = new List <iThinkPlan>(); } curStep = new iThinkPlan(OpenStates[0]); CurrentState = OpenStates[0].getState(); //Debug.Log(GoalState.ToString()); //Debug.Log(CurrentState.ToString()); //Debug.LogWarning( "Iteration #" + it + " - " + curStep.getState().ToString() ); VisitedStates.Add(OpenStates[0].getState()); OpenStates.RemoveAt(0); applicableActions = getApplicable(CurrentState, ActionManager.getActions()); this.nodesExpanded++; this.totalApplicable += applicableActions.Count; foreach (iThinkAction action in applicableActions) { //Debug.Log(action.ToString()); nextStep = progress(curStep, action); if (compareStates(nextStep.getState(), GoalState)) { Debug.Log("Found Plan (BestFS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count); Plan.setPlan(nextStep); repoFunct.completed = true; return(Plan); } if (VisitedStates.Contains(nextStep.getState()) == false) { int Cost = hFunction(nextStep.getState(), GoalState); nextStep.getState().setCost(Cost); stateList.Add(nextStep); this.nodes++; this.totalActionsUsed++; } } OpenStates.AddRange(stateList); OpenStates.Sort(delegate(iThinkPlan obj1, iThinkPlan obj2) { if (obj1.getState().getCost() == obj2.getState().getCost()) { return(0); } else if (obj1.getState().getCost() > obj2.getState().getCost()) { return(-1); } else { return(1); } } ); ++it; } Debug.Log("Didn't find plan (BestFS) " + it); return(null); }
public iThinkPlanner() { Plan = new iThinkPlan(); _OpenStates = new List<iThinkPlan>(); _VisitedStates = new List<iThinkState>(); }
public override iThinkPlan SearchMethod( iThinkState GoalState, iThinkActionManager ActionManager, List<iThinkPlan> OpenStates, List<iThinkState> VisitedStates ) { int it = 0; DateTime n1 = DateTime.Now; iThinkPlan curStep, nextStep; iThinkState CurrentState; nodesVisited++; actionManager = ActionManager; List<iThinkAction> applicableActions; if ( compareStates( OpenStates[0].getState(), GoalState ) ) { Debug.Log( "Found Plan (HFS) - Already at goal state!" ); Plan.setPlan( OpenStates[0] ); repoFunct.completed=true; return Plan; } //Debug.Log("Available actions: " + ActionManager.getActions().Count); while ( OpenStates.Count != 0 ) { curStep = new iThinkPlan( OpenStates[0] ); CurrentState = OpenStates[0].getState(); VisitedStates.Add( CurrentState ); OpenStates.RemoveAt( 0 ); int curArea = Convert.ToInt32(curStep.getState().getFactList().Find(item => item.getName().Equals("npc-at")).getObj(0).name.Substring(4)); if (curStep.getActionCount() < depth) { List<iThinkPlan> successors = new List<iThinkPlan>(); applicableActions = getApplicable( CurrentState, ((SimpleFPSActionManager)ActionManager).getActions(-1) ); ///applicableActions = getApplicable( CurrentState, ((SimpleFPSActionManager)ActionManager).getActions(curArea) ); nodesExpanded++; foreach ( iThinkAction action in applicableActions ) { nextStep = progress( curStep, action ); if ( compareStates( nextStep.getState(), GoalState ) ) { nodesVisited++; Debug.Log( "Found Plan (H-DepthFS) " + nextStep.getActionCount() + " (Nodes: "+nodesVisited+"/"+nodesExpanded+")"); Plan.setPlan( nextStep ); return Plan; } //if ( !VisitedStates.Contains(nextStep.getState()) ) { int Cost = hFunction( nextStep.getState(), GoalState, curArea ); if (Cost != -1) { nextStep.getState().setCost( Cost ); successors.Add(nextStep); nodesVisited++; } //} } successors.Sort( delegate( iThinkPlan obj1, iThinkPlan obj2 ) { if ( obj1.getState().getCost() == obj2.getState().getCost() ) return 0; else if ( obj1.getState().getCost() < obj2.getState().getCost() ) return -1; else return 1; } ); OpenStates.InsertRange(0, successors); TimeSpan timediff = n1 - DateTime.Now; if ( timediff.TotalSeconds > 60 ) return OpenStates[0]; ++it; } else { Debug.LogWarning("Depth Sucks!"); } } Debug.Log( "Didn't find Plan (H-DepthFS)" ); return null; }
public override iThinkPlan SearchMethod( iThinkState GoalState, iThinkActionManager ActionManager, List<iThinkPlan> OpenStates, List<iThinkState> VisitedStates ) { int it = 0; iThinkPlan curStep, nextStep; iThinkState CurrentState = null; List<iThinkPlan> stateList = null; this.totalActions = ActionManager.getActions().Count; this.nodes += OpenStates.Count; /*Debug.Log("Available Actions = " + totalActions); foreach ( iThinkAction act in ActionManager.getActions() ) Debug.Log(act.ToString());*/ List<iThinkAction> applicableActions = new List<iThinkAction>(); stateList = new List<iThinkPlan>(); if ( compareStates( OpenStates[0].getState(), GoalState ) ) { Debug.Log( "Found Plan (BestFS) - Already at goal state!" ); Plan.setPlan( OpenStates[0] ); repoFunct.completed=true; return Plan; } while ( OpenStates.Count != 0 ) { if(it % 200 == 0 && it!=0){ //InitRepo(); this.repoOpenStates = new List<iThinkPlan>(OpenStates); this.repoVisitedStates = new List<iThinkState>(VisitedStates); this.repoStateList = new List<iThinkPlan>(stateList); this.repoCurrentState = new iThinkState(CurrentState); this.repoIterations = it; repoFunct.loadData = true; return null; }else if( repoFunct.loadData ){ OpenStates = this.repoOpenStates; VisitedStates = this.repoVisitedStates; stateList = this.repoStateList; CurrentState = this.repoCurrentState; it = this.repoIterations; repoFunct.loadData = false; //ClearRepo(); }else{ stateList = new List<iThinkPlan>(); } curStep = new iThinkPlan( OpenStates[0] ); CurrentState = OpenStates[0].getState(); //Debug.Log(GoalState.ToString()); //Debug.Log(CurrentState.ToString()); //Debug.LogWarning( "Iteration #" + it + " - " + curStep.getState().ToString() ); VisitedStates.Add( OpenStates[0].getState() ); OpenStates.RemoveAt( 0 ); applicableActions = getApplicable( CurrentState, ActionManager.getActions() ); this.nodesExpanded++; this.totalApplicable += applicableActions.Count; foreach ( iThinkAction action in applicableActions ) { //Debug.Log(action.ToString()); nextStep = progress( curStep, action ); if ( compareStates( nextStep.getState(), GoalState ) ) { Debug.Log( "Found Plan (BestFS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count ); Plan.setPlan( nextStep ); repoFunct.completed=true; return Plan; } if ( VisitedStates.Contains(nextStep.getState()) == false ) { int Cost = hFunction( nextStep.getState(), GoalState ); nextStep.getState().setCost( Cost ); stateList.Add( nextStep ); this.nodes ++; this.totalActionsUsed++; } } OpenStates.AddRange( stateList ); OpenStates.Sort( delegate( iThinkPlan obj1, iThinkPlan obj2 ) { if ( obj1.getState().getCost() == obj2.getState().getCost() ) return 0; else if ( obj1.getState().getCost() > obj2.getState().getCost() ) return -1; else return 1; } ); ++it; } Debug.Log( "Didn't find plan (BestFS) " + it ); return null; }
public override iThinkPlan SearchMethod(iThinkState GoalState, iThinkActionManager ActionManager, List <iThinkPlan> OpenStates, List <iThinkState> VisitedStates) { int it = 0; iThinkPlan curStep, nextStep; iThinkState CurrentState = null; List <iThinkPlan> stateList = null; while (OpenStates.Count != 0) { List <iThinkAction> applicableActions = new List <iThinkAction>(); stateList = new List <iThinkPlan>(); curStep = new iThinkPlan(OpenStates[0]); CurrentState = OpenStates[0].getState(); //VisitedStates.Add(CurrentState); OpenStates.RemoveAt(0); if (curStep.getActionCount() < depth) { applicableActions = getApplicable(CurrentState, ActionManager.getActions()); foreach (iThinkAction action in applicableActions) { nextStep = progress(curStep, action); if (compareStates(nextStep.getState(), GoalState)) { Debug.Log("Found Plan (A*-weak FS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count + ", Nodes Expanded: " + nodesExpanded); Plan.setPlan(nextStep); repoFunct.completed = true; return(Plan); } if (!VisitedStates.Contains(nextStep.getState())) { int Cost = hFunction(nextStep.getState(), GoalState); Cost = Cost + nextStep.getPlanActions().Count; nextStep.getState().setCost(Cost); stateList.Add(nextStep); } } OpenStates.AddRange(stateList); OpenStates.Sort(delegate(iThinkPlan obj1, iThinkPlan obj2) { if (obj1.getState().getCost() == obj2.getState().getCost()) { return(0); } else if (obj1.getState().getCost() < obj2.getState().getCost()) { return(-1); } else { return(1); } } ); ++it; } } Debug.Log("Didn't find Plan (A*-weak FS)"); return(null); }
public bool forwardSearchBounded( iThinkState InitialState, iThinkState GoalState, iThinkActionManager ActionManager, double timelimit, int retries = 3) { iThinkPlan ReturnVal; _OpenStates.Clear(); _VisitedStates.Clear(); iThinkPlan step = new iThinkPlan( InitialState ); _OpenStates.Add( step ); _VisitedStates.Add( step.getState() ); ReturnVal = SearchMethodBounded( GoalState, ActionManager, _OpenStates, _VisitedStates, timelimit ); if ( ReturnVal == null ) return false; else if ( compareStates(ReturnVal.getState(), GoalState ) ) return true; return false; }
public iThinkPlan( iThinkPlan Step ) { this.state = new iThinkState( Step.state ); this.actions = new List<iThinkAction>( Step.actions ); }
public void setPlan(iThinkPlan plan) { state = new iThinkState(plan.getState()); actions = new List <iThinkAction>(plan.getPlanActions()); }
public override iThinkPlan SearchMethod ( iThinkState GoalState, iThinkActionManager ActionManager, List<iThinkPlan> OpenStates, List<iThinkState> VisitedStates ) { int it = 0; actionManager = ActionManager; iThinkPlan curStep, nextStep; iThinkState CurrentState = null; List<iThinkPlan> stateList = null; nodesVisited++; while ( OpenStates.Count != 0 ) { List<iThinkAction> applicableActions = new List<iThinkAction>(); stateList = new List<iThinkPlan>(); curStep = new iThinkPlan( OpenStates[0] ); CurrentState = OpenStates[0].getState(); VisitedStates.Add(CurrentState); /* for ( int i = 0 ; i < ((SimpleFPSActionManager)ActionManager).totalAreas ; i++ ) foreach (iThinkAction a in ((SimpleFPSActionManager)ActionManager).getActions(i)) a.ToStringLite(); */ OpenStates.RemoveAt( 0 ); if (curStep.getActionCount() < depth) { applicableActions = getApplicable( curStep.getState(), ((SimpleFPSActionManager)ActionManager).getActions( Convert.ToInt32(curStep.getState().getFactList().Find(item => item.getName().Equals("npc-at")).getObj(0).name.Substring(4)))); foreach ( iThinkAction action in applicableActions ) { nextStep = progress( curStep, action ); nodesVisited++; if ( compareStates( nextStep.getState(), GoalState ) ) { nodesExpanded++; Debug.Log( "Found Plan (A* FS) after " + it + " iterations, of length " + nextStep.getPlanActions().Count + ", Nodes Expanded: " + nodesExpanded); Plan.setPlan( nextStep ); repoFunct.completed=true; return Plan; } if ( !VisitedStates.Contains(nextStep.getState()) ) { int Cost = hFunction( nextStep.getState(), GoalState ); Cost = Cost + nextStep.getPlanActions().Count; nextStep.getState().setCost( Cost ); stateList.Add( nextStep ); nodesExpanded++; } } OpenStates.AddRange( stateList ); OpenStates.Sort( delegate( iThinkPlan obj1, iThinkPlan obj2 ) { if ( obj1.getState().getCost() == obj2.getState().getCost() ) return 0; else if ( obj1.getState().getCost() < obj2.getState().getCost() ) return -1; else return 1; } ); ++it; } } Debug.Log( "Didn't find Plan (A* FS)" ); return null; }
/*new progress function, where only the positive effects of actions will be applied */ public virtual iThinkPlan progressPositive( iThinkPlan Step, iThinkAction Action ) { iThinkPlan NewStep = new iThinkPlan( Step ); List<iThinkAction> curActions; curActions = NewStep.getPlanActions(); curActions.Add( Action ); NewStep.setState( Action.applyPositiveEffects( Step.getState() ) ); return NewStep; }
public override iThinkPlan SearchMethod(iThinkState GoalState, iThinkActionManager ActionManager, List <iThinkPlan> OpenStates, List <iThinkState> VisitedStates) { int it = 0; nodesVisited = 0; nodesExpanded = 0; DateTime n1 = DateTime.Now; iThinkPlan curStep, nextStep; iThinkState CurrentState; nodesVisited++; actionManager = ActionManager; List <iThinkAction> applicableActions; if (compareStates(OpenStates[0].getState(), GoalState)) { Debug.Log("Found Plan (HFS) - Already at goal state!"); Plan.setPlan(OpenStates[0]); repoFunct.completed = true; return(Plan); } //Debug.Log("Available actions: " + ActionManager.getActions().Count); while (OpenStates.Count != 0) { curStep = new iThinkPlan(OpenStates[0]); CurrentState = OpenStates[0].getState(); VisitedStates.Add(CurrentState); OpenStates.RemoveAt(0); int curArea = Convert.ToInt32(curStep.getState().getFactList().Find(item => item.getName().Equals("npc-at")).getObj(0).name.Substring(4)); if (curStep.getActionCount() == depth) { DateTime n2 = DateTime.Now; TimeSpan interv = n2 - n1; interval = ((int)interv.TotalMilliseconds / 100) / (double)10; curStep.debugPrintPlan(); return(curStep); } if (curStep.getActionCount() < depth) { List <iThinkPlan> successors = new List <iThinkPlan>(); ///applicableActions = getApplicable( CurrentState, ((SimpleFPSActionManager)ActionManager).getActions(-1) ); applicableActions = getApplicable(CurrentState, ((SimpleFPSActionManager)ActionManager).getActions(curArea)); nodesExpanded++; foreach (iThinkAction action in applicableActions) { nextStep = progress(curStep, action); if (compareStates(nextStep.getState(), GoalState)) { nodesVisited++; Debug.Log("Found Plan (H-DepthFS) " + nextStep.getActionCount() + " (Nodes: " + nodesVisited + "/" + nodesExpanded + ")"); Plan.setPlan(nextStep); return(Plan); } if (!VisitedStates.Contains(nextStep.getState())) { int Cost = hFunction(nextStep.getState(), GoalState, curArea); if (Cost != -1) { nextStep.getState().setCost(Cost); successors.Add(nextStep); nodesVisited++; } } } successors.Sort(delegate(iThinkPlan obj1, iThinkPlan obj2) { if (obj1.getState().getCost() == obj2.getState().getCost()) { return(0); } else if (obj1.getState().getCost() < obj2.getState().getCost()) { return(-1); } else { return(1); } } ); OpenStates.InsertRange(0, successors); /*TimeSpan timediff = n1 - DateTime.Now; * if ( timediff.TotalSeconds > 60 ) * return OpenStates[0];*/ ++it; } else { } } Debug.Log("Didn't find Plan (H-DepthFS)"); return(null); }
public override iThinkPlan progress( iThinkPlan Step, iThinkAction Action ) { iThinkPlan NewStep = new iThinkPlan( Step ); List<iThinkAction> curActions; curActions = NewStep.getPlanActions(); curActions.Add( Action ); NewStep.setState( Action.applyEffects( Step.getState() ) ); return NewStep; }
public int hFunction(iThinkState nextState, iThinkState GoalState, int area) { //Debug.LogError(area); iThinkState tempState = new iThinkState(GoalState); iThinkState curState = new iThinkState(nextState); for (int i=0 ; i<depth ; i++) { List<int> areas = new List<int>(); foreach (iThinkFact f in curState.getFactList()) { if (f.getName() == "npc-at") { string name = f.getObj(0).name; int a = Convert.ToInt32(name.Substring(4)); areas.Add(a); } tempState.delFact(f); } if (tempState.getFactList().Count == 0) return i; ///List<iThinkAction> applicableActions = getApplicable(curState, ((SimpleFPSActionManager)actionManager).getActions(-1) ); ///List<iThinkAction> applicableActions = getApplicable(curState, ((SimpleFPSActionManager)actionManager).getActions(area) ); List<iThinkAction> allApplicableActions = new List<iThinkAction>(); foreach (int k in areas) { List<iThinkAction> applicableActions = getApplicable(curState, ((SimpleFPSActionManager)actionManager).getActions(k) ); allApplicableActions.InsertRange(0, applicableActions); } iThinkPlan curStep = new iThinkPlan(curState); foreach (iThinkAction act in allApplicableActions) { iThinkPlan nextStep = progressPositive(curStep, act); //iThinkPlan nextStep = progress(curStep, act); curStep = nextStep; } curState = curStep.getState(); } //in case the planning graph has #layers = depth, return -1, indicating that no solution can be found starting from current state) //consequenlty, the state will not be added in the fringe. return -1; }
public iThinkPlan(iThinkPlan Step) { this.state = new iThinkState(Step.state); this.actions = new List <iThinkAction>(Step.actions); }