Search in sources :

Example 6 with Action

use of aima.core.agent.Action in project aima-java by aimacode.

the class PassiveTDAgent method isTerminal.

//
// PRIVATE METHODS
//
private boolean isTerminal(S s) {
    boolean terminal = false;
    Action a = pi.get(s);
    if (null == a || a.isNoOp()) {
        // No actions possible in state is considered terminal.
        terminal = true;
    }
    return terminal;
}
Also used : Action(aima.core.agent.Action)

Example 7 with Action

use of aima.core.agent.Action in project aima-java by aimacode.

the class ReinforcementAgent method execute.

@SuppressWarnings("unchecked")
@Override
public Action execute(Percept p) {
    if (p instanceof PerceptStateReward<?>) {
        Action a = execute((PerceptStateReward<S>) p);
        if (null == a) {
            a = NoOpAction.NO_OP;
            setAlive(false);
        }
        return a;
    }
    throw new IllegalArgumentException("Percept passed in must be a PerceptStateReward");
}
Also used : NoOpAction(aima.core.agent.impl.NoOpAction) Action(aima.core.agent.Action) PerceptStateReward(aima.core.learning.reinforcement.PerceptStateReward)

Example 8 with Action

use of aima.core.agent.Action in project aima-java by aimacode.

the class NondeterministicVacuumAgent method execute.

/**
	 * Execute an action from the contingency plan
	 * 
	 * @param percept a percept.
	 * @return an action from the contingency plan.
	 */
@Override
public Action execute(Percept percept) {
    // check if goal state
    VacuumEnvironmentState state = (VacuumEnvironmentState) this.getPerceptToStateFunction().apply(percept);
    if (state.getLocationState(VacuumEnvironment.LOCATION_A) == VacuumEnvironment.LocationState.Clean && state.getLocationState(VacuumEnvironment.LOCATION_B) == VacuumEnvironment.LocationState.Clean) {
        return NoOpAction.NO_OP;
    }
    // check stack size
    if (this.stack.size() < 1) {
        if (this.contingencyPlan.size() < 1) {
            return NoOpAction.NO_OP;
        } else {
            this.stack.push(this.getContingencyPlan().removeFirst());
        }
    }
    // pop...
    Object currentStep = this.stack.peek();
    // push...
    if (currentStep instanceof Action) {
        return (Action) this.stack.remove();
    } else // case: next step is a plan
    if (currentStep instanceof Plan) {
        Plan newPlan = (Plan) currentStep;
        if (newPlan.size() > 0) {
            this.stack.push(newPlan.removeFirst());
        } else {
            this.stack.remove();
        }
        return this.execute(percept);
    } else // case: next step is an if-then
    if (currentStep instanceof IfStateThenPlan) {
        IfStateThenPlan conditional = (IfStateThenPlan) this.stack.remove();
        this.stack.push(conditional.ifStateMatches(percept));
        return this.execute(percept);
    } else // case: ignore next step if null
    if (currentStep == null) {
        this.stack.remove();
        return this.execute(percept);
    } else {
        throw new RuntimeException("Unrecognized contingency plan step.");
    }
}
Also used : NoOpAction(aima.core.agent.impl.NoOpAction) Action(aima.core.agent.Action) IfStateThenPlan(aima.core.search.nondeterministic.IfStateThenPlan) Plan(aima.core.search.nondeterministic.Plan) IfStateThenPlan(aima.core.search.nondeterministic.IfStateThenPlan)

Example 9 with Action

use of aima.core.agent.Action in project aima-java by aimacode.

the class HybridWumpusAgent method execute.

/**
	 * function HYBRID-WUMPUS-AGENT(percept) returns an action<br>
	 * 
	 * @param percept
	 *            a list, [stench, breeze, glitter, bump, scream]
	 * 
	 * @return an action the agent should take.
	 */
@Override
public Action execute(Percept percept) {
    // TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
    kb.makePerceptSentence((AgentPercept) percept, t);
    // TELL the KB the temporal "physics" sentences for time t
    kb.tellTemporalPhysicsSentences(t);
    AgentPosition current = kb.askCurrentPosition(t);
    // safe <- {[x, y] : ASK(KB, OK<sup>t</sup><sub>x,y</sub>) = true}
    Set<Room> safe = kb.askSafeRooms(t);
    // if ASK(KB, Glitter<sup>t</sup>) = true then
    if (kb.askGlitter(t)) {
        // plan <- [Grab] + PLAN-ROUTE(current, {[1,1]}, safe) + [Climb]
        Set<Room> goals = new LinkedHashSet<>();
        goals.add(new Room(1, 1));
        plan.add(new Grab());
        plan.addAll(planRoute(current, goals, safe));
        plan.add(new Climb());
    }
    // if plan is empty then
    // unvisited <- {[x, y] : ASK(KB, L<sup>t'</sup><sub>x,y</sub>) = false
    // for all t' &le; t}
    Set<Room> unvisited = kb.askUnvisitedRooms(t);
    if (plan.isEmpty()) {
        // plan <- PLAN-ROUTE(current, unvisited &cap; safe, safe)
        plan.addAll(planRoute(current, SetOps.intersection(unvisited, safe), safe));
    }
    // if plan is empty and ASK(KB, HaveArrow<sup>t</sup>) = true then
    if (plan.isEmpty() && kb.askHaveArrow(t)) {
        // possible_wumpus <- {[x, y] : ASK(KB, ~W<sub>x,y</sub>) = false}
        Set<Room> possibleWumpus = kb.askPossibleWumpusRooms(t);
        // plan <- PLAN-SHOT(current, possible_wumpus, safe)
        plan.addAll(planShot(current, possibleWumpus, safe));
    }
    // if plan is empty then //no choice but to take a risk
    if (plan.isEmpty()) {
        // not_unsafe <- {[x, y] : ASK(KB, ~OK<sup>t</sup><sub>x,y</sub>) =
        // false}
        Set<Room> notUnsafe = kb.askNotUnsafeRooms(t);
        // plan <- PLAN-ROUTE(current, unvisited &cap; not_unsafe, safe)
        plan.addAll(planRoute(current, SetOps.intersection(unvisited, notUnsafe), safe));
    }
    // if plan is empty then
    if (plan.isEmpty()) {
        // plan PLAN-ROUTE(current, {[1,1]}, safe) + [Climb]
        Set<Room> start = new LinkedHashSet<>();
        start.add(new Room(1, 1));
        plan.addAll(planRoute(current, start, safe));
        plan.add(new Climb());
    }
    // action <- POP(plan)
    Action action = plan.remove();
    // TELL(KB, MAKE-ACTION-SENTENCE(action, t))
    kb.makeActionSentence(action, t);
    // t <- t+1
    t = t + 1;
    // return action
    return action;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Action(aima.core.agent.Action) Climb(aima.core.environment.wumpusworld.action.Climb) Grab(aima.core.environment.wumpusworld.action.Grab)

Example 10 with Action

use of aima.core.agent.Action in project aima-java by aimacode.

the class AbstractEnvironment method step.

/**
	 * Central template method for controlling agent simulation. The concrete
	 * behavior is determined by the primitive operations
	 * {@link #getPerceptSeenBy(Agent)}, {@link #executeAction(Agent, Action)},
	 * and {@link #createExogenousChange()}.
	 */
public void step() {
    for (Agent agent : agents) {
        if (agent.isAlive()) {
            Percept percept = getPerceptSeenBy(agent);
            Action anAction = agent.execute(percept);
            executeAction(agent, anAction);
            notifyEnvironmentViews(agent, percept, anAction);
        }
    }
    createExogenousChange();
}
Also used : Agent(aima.core.agent.Agent) Action(aima.core.agent.Action) Percept(aima.core.agent.Percept)

Aggregations

Action (aima.core.agent.Action)26 Test (org.junit.Test)12 SearchAgent (aima.core.search.framework.SearchAgent)8 NoOpAction (aima.core.agent.impl.NoOpAction)6 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)6 EightPuzzleBoard (aima.core.environment.eightpuzzle.EightPuzzleBoard)5 NQueensBoard (aima.core.environment.nqueens.NQueensBoard)5 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)5 QueenAction (aima.core.environment.nqueens.QueenAction)5 GoalTest (aima.core.search.framework.problem.GoalTest)5 ArrayList (java.util.ArrayList)4 DynamicAction (aima.core.agent.impl.DynamicAction)3 BidirectionalEightPuzzleProblem (aima.core.environment.eightpuzzle.BidirectionalEightPuzzleProblem)3 Agent (aima.core.agent.Agent)2 Percept (aima.core.agent.Percept)2 MoveToAction (aima.core.environment.map.MoveToAction)2 Climb (aima.core.environment.wumpusworld.action.Climb)2 Grab (aima.core.environment.wumpusworld.action.Grab)2 TurnLeft (aima.core.environment.wumpusworld.action.TurnLeft)2 GreedyBestFirstSearch (aima.core.search.informed.GreedyBestFirstSearch)2