示例#1
0
		/// A positive iThinkFact is added to the \a State, and a negative one is removed from it.
		public void applyFact( iThinkState State )
		{
			if ( this.positive == false )
				State.delFact( this );
			else
				State.addFact( this );
		}
		/*! Returns a new iThinkState, applying only positive effects of this action */
		public iThinkState applyPositiveEffects( iThinkState State )
		{
			iThinkState NewState = new iThinkState( State );
			
			foreach ( iThinkFact effect in this.effects )
			{
				if (effect.getPositive())
					NewState.addFact(effect);
			}
			
			return NewState;
		}
示例#3
0
		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;
		}
示例#4
0
    public void writeInitState(iThinkState st)
    {
        // create a writer and open the file
            TextWriter tw = new StreamWriter("initState.txt");

            foreach (iThinkFact f in st.getFactList())
            {
            // write a line of text to the file
                tw.WriteLine(f.ToString());
            }

            // close the stream
            tw.Close();
    }
		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;
		}
		/*!
		 * Checks whether the action can be applied on iThinkState \a curState
		 * @param curState The state to be checked
		 * @returns A boolean value
		 */
		public bool isApplicable( iThinkState curState )
		{
			int counter = 0;

			foreach ( iThinkFact fact in preconditions )
			{
				//! TODO Get facts of wanted type/name only
				foreach ( iThinkFact checkFact in curState.getFactList() )
				{
					if ( fact.Equals(checkFact) )
						counter++;
				}
			}
			if ( counter == preconditions.Count ){
				//Debug.LogError("Found applicable! " + this.ToString());
				return true;
			}
			//if ( this.name.StartsWith("SFPSPlace") )
			//	Debug.Log("Unequal: " + counter + " of " + preconditions.Count + " - act: " + this.ToString());
			return false;
		}
示例#7
0
		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;
		}
示例#8
0
 public void setPlan(iThinkPlan plan)
 {
     state   = new iThinkState(plan.getState());
     actions = new List <iThinkAction>(plan.getPlanActions());
 }
		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;
		}
示例#10
0
 public iThinkPlan(iThinkState state, List <iThinkAction> actions)
 {
     this.state   = new iThinkState(state);
     this.actions = new List <iThinkAction>(actions);
 }
示例#11
0
		public iThinkPlan( iThinkState state, List<iThinkAction> actions )
		{
			this.state = new iThinkState( state );
			this.actions = new List<iThinkAction>( actions );
		}
示例#12
0
		public void setCurState(iThinkState state){ curState = state;}
示例#13
0
		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;
		}
示例#14
0
 public void setGoalState(iThinkState state)
 {
     goalState = state;
 }
		public virtual iThinkPlan SearchMethod(iThinkState GoalState, iThinkActionManager ActionManager, List<iThinkPlan> OpenStates, List<iThinkState> VisitedStates )
		{
			return null;
		}
示例#16
0
		public iThinkState( iThinkState state )
		{
			facts = new List<iThinkFact>( state.facts );
			heuristicCost = state.heuristicCost;
			name = state.name;
		}
示例#17
0
		public bool Equals(iThinkState state2)
		{
			foreach ( iThinkFact fact1 in this.facts )
			{
				bool check = false;

				foreach ( iThinkFact fact2 in state2.facts )
				{
					if ( fact1.Equals(fact2) )
						check = true;
				}

				if ( check == false )
					return false;
			}

			return true;
		}
示例#18
0
		public void setPlan( iThinkPlan plan ) {
			state = new iThinkState(plan.getState());
			actions = new List<iThinkAction>( plan.getPlanActions() );
		}
示例#19
0
		public void setState( iThinkState NewState ) { state = NewState; }
示例#20
0
		public iThinkPlan( iThinkPlan Step )
		{
			this.state = new iThinkState( Step.state );
			this.actions = new List<iThinkAction>( Step.actions );
		}
示例#21
0
 public void setState(iThinkState NewState)
 {
     state = NewState;
 }
示例#22
0
		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;
		}
示例#23
0
		public void setStartState(iThinkState state){ startState = state;}
示例#24
0
 public iThinkPlan()
 {
     this.state   = null;
     this.actions = new List <iThinkAction>();
 }
示例#25
0
		public void setGoalState(iThinkState state){ goalState = state;}
		/// Finds all actions applicable to the current \a State from the collection of available \a Actions
		public List<iThinkAction> getApplicable( iThinkState State, List<iThinkAction> Actions )
		{
			List<iThinkAction> ApplicableActions = new List<iThinkAction>();

			foreach ( iThinkAction action in Actions )
			{
				if ( action == null )
					break;

				if ( action.isApplicable( State ) )
					ApplicableActions.Add( action );
			}
			
			//foreach (iThinkAction a in ApplicableActions)
			//	UnityEngine.Debug.Log(a.ToStringLite());

			return ApplicableActions;
		}
示例#27
0
 public iThinkPlan(iThinkPlan Step)
 {
     this.state   = new iThinkState(Step.state);
     this.actions = new List <iThinkAction>(Step.actions);
 }
		/// Checks if goalState is a subset of curState
		static public bool compareStates( iThinkState curState, iThinkState goalState )
		{
			int counter = 0;
			if (curState != null) {
				foreach ( iThinkFact fact in goalState.getFactList() )
				{
					foreach ( iThinkFact check in curState.getFactList() )
					{
						if ( check == null )
							return false;
						else if ( check.Equals(fact) )
							counter++;
					}
				}
			}
			if ( counter == goalState.getFactList().Count )
				return true;
			return false;
		}
		public virtual iThinkPlan SearchMethodBounded(iThinkState GoalState, iThinkActionManager ActionManager, List<iThinkPlan> OpenStates, List<iThinkState> VisitedStates, double timelimit)
		{
			return null;
		}
示例#30
0
		public iThinkPlan()
		{
			this.state = null;
			this.actions = new List<iThinkAction>();
		}
示例#31
0
		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 virtual int hFunction( iThinkState nextState, iThinkState GoalState )
		{
			int counter = 0;
			foreach ( iThinkFact fact in nextState.getFactList() )
			{
				foreach ( iThinkFact goalFact in GoalState.getFactList() )
				{
					if ( fact.Equals( goalFact) )
					{
						counter++;
						break;
					}
				}
			}
			return counter;
		}