use of aima.core.agent.Action in project aima-java by aimacode.
the class EightPuzzleFunctionsTest method testGenerateCorrect3Successors.
@Test
public void testGenerateCorrect3Successors() {
List<Action> actions = new ArrayList<>(EightPuzzleFunctions.getActions(board));
Assert.assertEquals(3, actions.size());
// test first successor
EightPuzzleBoard expectedFirst = new EightPuzzleBoard(new int[] { 1, 2, 0, 3, 4, 5, 6, 7, 8 });
EightPuzzleBoard actualFirst = (EightPuzzleBoard) EightPuzzleFunctions.getResult(board, actions.get(0));
Assert.assertEquals(expectedFirst, actualFirst);
Assert.assertEquals(EightPuzzleBoard.UP, actions.get(0));
// test second successor
EightPuzzleBoard expectedSecond = new EightPuzzleBoard(new int[] { 1, 2, 5, 3, 4, 8, 6, 7, 0 });
EightPuzzleBoard actualSecond = (EightPuzzleBoard) EightPuzzleFunctions.getResult(board, actions.get(1));
Assert.assertEquals(expectedSecond, actualSecond);
Assert.assertEquals(EightPuzzleBoard.DOWN, actions.get(1));
// test third successor
EightPuzzleBoard expectedThird = new EightPuzzleBoard(new int[] { 1, 2, 5, 3, 0, 4, 6, 7, 8 });
EightPuzzleBoard actualThird = (EightPuzzleBoard) EightPuzzleFunctions.getResult(board, actions.get(2));
Assert.assertEquals(expectedThird, actualThird);
Assert.assertEquals(EightPuzzleBoard.LEFT, actions.get(2));
}
use of aima.core.agent.Action in project aima-java by aimacode.
the class SimpleProblemSolvingAgent method execute.
// function SIMPLE-PROBLEM-SOLVING-AGENT(percept) returns an action
@Override
public Action execute(Percept p) {
Action action = NoOpAction.NO_OP;
// state <- UPDATE-STATE(state, percept)
updateState(p);
// if seq is empty then do
if (seq.isEmpty()) {
if (formulateGoalsIndefinitely || goalsFormulated < maxGoalsToFormulate) {
if (goalsFormulated > 0) {
notifyViewOfMetrics();
}
// goal <- FORMULATE-GOAL(state)
Object goal = formulateGoal();
goalsFormulated++;
// problem <- FORMULATE-PROBLEM(state, goal)
Problem<S, A> problem = formulateProblem(goal);
// seq <- SEARCH(problem)
seq.addAll(search(problem));
if (0 == seq.size()) {
// Unable to identify a path
seq.add(null);
}
} else {
// Agent no longer wishes to
// achieve any more goals
setAlive(false);
notifyViewOfMetrics();
}
}
if (seq.size() > 0) {
// action <- FIRST(seq)
// seq <- REST(seq)
action = seq.remove();
}
return action != null ? action : NoOpAction.NO_OP;
}
use of aima.core.agent.Action in project aima-java by aimacode.
the class TableDrivenAgentProgramTest method setUp.
@Before
public void setUp() {
Map<List<Percept>, Action> perceptSequenceActions = new HashMap<List<Percept>, Action>();
perceptSequenceActions.put(createPerceptSequence(new DynamicPercept("key1", "value1")), ACTION_1);
perceptSequenceActions.put(createPerceptSequence(new DynamicPercept("key1", "value1"), new DynamicPercept("key1", "value2")), ACTION_2);
perceptSequenceActions.put(createPerceptSequence(new DynamicPercept("key1", "value1"), new DynamicPercept("key1", "value2"), new DynamicPercept("key1", "value3")), ACTION_3);
agent = new MockAgent(new TableDrivenAgentProgram(perceptSequenceActions));
}
use of aima.core.agent.Action in project aima-java by aimacode.
the class EightPuzzleApp method simulate.
/** Starts the experiment. */
public void simulate() {
int strategyIdx = simPaneCtrl.getParamValueIndex(PARAM_STRATEGY);
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(board);
SearchForActions<EightPuzzleBoard, Action> search = SEARCH_ALGOS.get(strategyIdx);
List<Action> actions = search.findActions(problem);
for (Action action : actions) {
if (action == EightPuzzleBoard.UP)
board.moveGapUp();
else if (action == EightPuzzleBoard.DOWN)
board.moveGapDown();
else if (action == EightPuzzleBoard.LEFT)
board.moveGapLeft();
else if (action == EightPuzzleBoard.RIGHT)
board.moveGapRight();
Metrics m = new Metrics();
m.set("manhattanHeuristic", EightPuzzleFunctions.createManhattanHeuristicFunction().applyAsDouble(new Node<>(board)));
updateStateView(m);
if (CancelableThread.currIsCanceled())
break;
simPaneCtrl.waitAfterStep();
}
updateStateView(search.getMetrics());
}
use of aima.core.agent.Action in project aima-java by aimacode.
the class SimulatedAnnealingMaximumFinderApp method simulate.
/** Starts the experiment. */
@SuppressWarnings("unchecked")
public void simulate() {
List<Action> actions = new ArrayList<>(1);
actions.add(new DynamicAction("Move"));
Problem<Double, Action> problem = new GeneralProblem<>(getRandomState(), s -> actions, (s, a) -> getSuccessor(s), s -> false);
Function<Double, Double> func = (Function<Double, Double>) simPaneCtrl.getParamValue(PARAM_FUNC_SELECT);
Scheduler scheduler = new Scheduler(simPaneCtrl.getParamAsInt(PARAM_K), simPaneCtrl.getParamAsDouble(PARAM_LAMBDA), simPaneCtrl.getParamAsInt(PARAM_MAX_ITER));
search = new SimulatedAnnealingSearch<>(n -> 1 - func.apply(n.getState()), scheduler);
search.addNodeListener(n -> updateStateView(n.getState()));
search.findActions(problem);
updateStateView(search.getLastSearchState());
}
Aggregations