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;
}
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");
}
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.");
}
}
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' ≤ t}
Set<Room> unvisited = kb.askUnvisitedRooms(t);
if (plan.isEmpty()) {
// plan <- PLAN-ROUTE(current, unvisited ∩ 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 ∩ 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;
}
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();
}
Aggregations