Exemplo n.º 1
0
        /**
         * Execute an action from the contingency plan
         *
         * @param percept a percept.
         * @return an action from the contingency plan.
         */
        public override IAction Execute(IPercept percept)
        {
            // check if goal state
            VacuumEnvironmentState state = (VacuumEnvironmentState)this
                                           .getPerceptToStateFunction()(percept);

            if (state.getLocationState(VacuumEnvironment.LOCATION_A) == VacuumEnvironment.LocationState.Clean &&
                state.getLocationState(VacuumEnvironment.LOCATION_B) == VacuumEnvironment.LocationState.Clean)
            {
                return(DynamicAction.NO_OP);
            }
            // check stack size
            if (this.stack.Size() < 1)
            {
                if (this.contingencyPlan.Size() < 1)
                {
                    return(DynamicAction.NO_OP);
                }
                else
                {
                    this.stack.Add(this.getContingencyPlan().Pop());
                }
            }
            // pop...
            object currentStep = this.stack.Peek();

            // push...
            if (currentStep is IAction)
            {
                return((IAction)this.stack.Pop());
            } // case: next step is a plan
            else if (currentStep is Plan)
            {
                Plan newPlan = (Plan)currentStep;
                if (newPlan.Size() > 0)
                {
                    this.stack.Add(newPlan.Pop());
                }
                else
                {
                    this.stack.Pop();
                }
                return(this.Execute(percept));
            } // case: next step is an if-then
            else if (currentStep is IfStateThenPlan)
            {
                IfStateThenPlan conditional = (IfStateThenPlan)this.stack.Pop();
                this.stack.Add(conditional.ifStateMatches(percept));
                return(this.Execute(percept));
            } // case: ignore next step if null
            else if (currentStep == null)
            {
                this.stack.Pop();
                return(this.Execute(percept));
            }
            else
            {
                throw new RuntimeException("Unrecognized contingency plan step.");
            }
        }
Exemplo n.º 2
0
 /**
  * Constructor which allows subclasses to define a vacuum environment with an arbitrary number
  * of squares. Two-dimensional grid environments can be defined by additionally overriding
  * {@link #getXDimension()} and {@link #getYDimension()}.
  */
 protected VacuumEnvironment(ICollection <string> locations, params LocationState[] locStates)
 {
     this.locations = locations;
     envState       = new VacuumEnvironmentState();
     for (int i = 0; i < locations.Size() && i < locStates.Length; ++i)
     {
         envState.setLocationState(locations.Get(i), locStates[i]);
     }
 }
Exemplo n.º 3
0
 public static bool testGoal(VacuumEnvironmentState state)
 {
     return(state.getLocationState(VacuumEnvironment.LOCATION_A) == VacuumEnvironment.LocationState.Clean &&
            state.getLocationState(VacuumEnvironment.LOCATION_B) == VacuumEnvironment.LocationState.Clean);
 }