Search in sources :

Example 1 with Action

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));
}
Also used : Action(aima.core.agent.Action) EightPuzzleBoard(aima.core.environment.eightpuzzle.EightPuzzleBoard) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with Action

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;
}
Also used : NoOpAction(aima.core.agent.impl.NoOpAction) Action(aima.core.agent.Action)

Example 3 with Action

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));
}
Also used : MockAgent(aima.test.core.unit.agent.impl.MockAgent) NoOpAction(aima.core.agent.impl.NoOpAction) Action(aima.core.agent.Action) DynamicAction(aima.core.agent.impl.DynamicAction) DynamicPercept(aima.core.agent.impl.DynamicPercept) Percept(aima.core.agent.Percept) DynamicPercept(aima.core.agent.impl.DynamicPercept) HashMap(java.util.HashMap) TableDrivenAgentProgram(aima.core.agent.impl.aprog.TableDrivenAgentProgram) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.junit.Before)

Example 4 with Action

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());
}
Also used : Action(aima.core.agent.Action) Metrics(aima.core.search.framework.Metrics) Node(aima.core.search.framework.Node)

Example 5 with Action

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());
}
Also used : Color(javafx.scene.paint.Color) java.util(java.util) IntegrableApplication(aima.gui.fx.framework.IntegrableApplication) Canvas(javafx.scene.canvas.Canvas) Action(aima.core.agent.Action) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) SimulationPaneBuilder(aima.gui.fx.framework.SimulationPaneBuilder) Function(java.util.function.Function) Platform(javafx.application.Platform) Scheduler(aima.core.search.local.Scheduler) Parameter(aima.gui.fx.framework.Parameter) Paint(javafx.scene.paint.Paint) SimulationPaneCtrl(aima.gui.fx.framework.SimulationPaneCtrl) FunctionPlotterCtrl(aima.gui.fx.views.FunctionPlotterCtrl) BorderPane(javafx.scene.layout.BorderPane) DynamicAction(aima.core.agent.impl.DynamicAction) Problem(aima.core.search.framework.problem.Problem) SimulatedAnnealingSearch(aima.core.search.local.SimulatedAnnealingSearch) Pane(javafx.scene.layout.Pane) Function(java.util.function.Function) Action(aima.core.agent.Action) DynamicAction(aima.core.agent.impl.DynamicAction) Scheduler(aima.core.search.local.Scheduler) DynamicAction(aima.core.agent.impl.DynamicAction) GeneralProblem(aima.core.search.framework.problem.GeneralProblem)

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