Search in sources :

Example 21 with GeneralProblem

use of aima.core.search.framework.problem.GeneralProblem in project aima-java by aimacode.

the class IterativeDeepeningSearchTest method testIterativeDeepeningSearch.

@Test
public void testIterativeDeepeningSearch() {
    try {
        Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(8), NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
        SearchForActions<NQueensBoard, QueenAction> search = new IterativeDeepeningSearch<>();
        List<QueenAction> actions = search.findActions(problem);
        assertCorrectPlacement(actions);
        Assert.assertEquals("3656", search.getMetrics().get("nodesExpanded"));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception should not occur");
    }
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) QueenAction(aima.core.environment.nqueens.QueenAction) IterativeDeepeningSearch(aima.core.search.uninformed.IterativeDeepeningSearch) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) Test(org.junit.Test)

Example 22 with GeneralProblem

use of aima.core.search.framework.problem.GeneralProblem in project aima-java by aimacode.

the class UniformCostSearchTest method testUniformCostUnSuccesfulSearch.

@Test
public void testUniformCostUnSuccesfulSearch() throws Exception {
    Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(3), NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    SearchForActions<NQueensBoard, QueenAction> search = new UniformCostSearch<>();
    SearchAgent<NQueensBoard, QueenAction> agent = new SearchAgent<>(problem, search);
    List<Action> actions = agent.getActions();
    Assert.assertEquals(0, actions.size());
    Assert.assertEquals("6", agent.getInstrumentation().getProperty("nodesExpanded"));
    // Will be 0 as did not reach goal state.
    Assert.assertEquals("0", agent.getInstrumentation().getProperty("pathCost"));
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) Action(aima.core.agent.Action) QueenAction(aima.core.environment.nqueens.QueenAction) UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) QueenAction(aima.core.environment.nqueens.QueenAction) SearchAgent(aima.core.search.framework.SearchAgent) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 23 with GeneralProblem

use of aima.core.search.framework.problem.GeneralProblem in project aima-java by aimacode.

the class OsmAgentController method initAgents.

/** Creates new agents and adds them to the current environment. */
protected void initAgents(MessageLogger logger) {
    List<MapNode> markers = map.getOsmMap().getMarkers();
    if (markers.size() < 2) {
        logger.log("Error: Please set two markers with mouse-left.");
        return;
    }
    String[] locs = new String[markers.size()];
    for (int i = 0; i < markers.size(); i++) {
        MapNode node = markers.get(i);
        Point2D pt = new Point2D(node.getLon(), node.getLat());
        locs[i] = map.getNearestLocation(pt);
    }
    MapAgentFrame.SelectionState state = frame.getSelection();
    heuristic = createHeuristic(state.getIndex(MapAgentFrame.HEURISTIC_SEL), locs[1]);
    search = SearchFactory.getInstance().createSearch(state.getIndex(MapAgentFrame.SEARCH_SEL), state.getIndex(MapAgentFrame.Q_SEARCH_IMPL_SEL), heuristic);
    Agent agent = null;
    switch(state.getIndex(MapAgentFrame.AGENT_SEL)) {
        case 0:
            agent = new SimpleMapAgent(map, env, search, new String[] { locs[1] });
            break;
        case 1:
            Problem<String, MoveToAction> p = new BidirectionalMapProblem(map, null, locs[1]);
            OnlineSearchProblem<String, MoveToAction> osp = new GeneralProblem<>(null, p::getActions, null, p::testGoal, p::getStepCosts);
            agent = new LRTAStarAgent<>(osp, MapFunctions.createPerceptToStateFunction(), s -> heuristic.applyAsDouble(new Node<>(s)));
            break;
    }
    env.addAgent(agent, locs[0]);
}
Also used : DecimalFormat(java.text.DecimalFormat) MessageLogger(aima.gui.swing.framework.MessageLogger) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) LRTAStarAgent(aima.core.search.online.LRTAStarAgent) MapAdapter(aimax.osm.routing.MapAdapter) SearchForActions(aima.core.search.framework.SearchForActions) MapAgentFrame(aima.gui.swing.applications.agent.map.MapAgentFrame) ArrayList(java.util.ArrayList) Point2D(aima.core.util.math.geom.shapes.Point2D) AgentAppController(aima.gui.swing.framework.AgentAppController) List(java.util.List) Node(aima.core.search.framework.Node) OnlineSearchProblem(aima.core.search.framework.problem.OnlineSearchProblem) MapWayAttFilter(aimax.osm.data.MapWayAttFilter) ToDoubleFunction(java.util.function.ToDoubleFunction) MapNode(aimax.osm.data.entities.MapNode) Agent(aima.core.agent.Agent) SimulationThread(aima.gui.swing.framework.SimulationThread) aima.core.environment.map(aima.core.environment.map) Problem(aima.core.search.framework.problem.Problem) SearchFactory(aima.gui.util.SearchFactory) LRTAStarAgent(aima.core.search.online.LRTAStarAgent) Agent(aima.core.agent.Agent) MapNode(aimax.osm.data.entities.MapNode) aima.core.environment.map(aima.core.environment.map) Point2D(aima.core.util.math.geom.shapes.Point2D) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) MapAgentFrame(aima.gui.swing.applications.agent.map.MapAgentFrame)

Aggregations

GeneralProblem (aima.core.search.framework.problem.GeneralProblem)23 Test (org.junit.Test)20 GoalTest (aima.core.search.framework.problem.GoalTest)12 NQueensBoard (aima.core.environment.nqueens.NQueensBoard)11 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)11 QueenAction (aima.core.environment.nqueens.QueenAction)11 MoveToAction (aima.core.environment.map.MoveToAction)10 SearchAgent (aima.core.search.framework.SearchAgent)8 MapEnvironment (aima.core.environment.map.MapEnvironment)7 Action (aima.core.agent.Action)6 LRTAStarAgent (aima.core.search.online.LRTAStarAgent)4 OnlineDFSAgent (aima.core.search.online.OnlineDFSAgent)4 Map (aima.core.environment.map.Map)3 SimplifiedRoadMapOfPartOfRomania (aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania)3 DepthLimitedSearch (aima.core.search.uninformed.DepthLimitedSearch)3 UniformCostSearch (aima.core.search.uninformed.UniformCostSearch)3 ExtendableMap (aima.core.environment.map.ExtendableMap)2 Node (aima.core.search.framework.Node)2 Problem (aima.core.search.framework.problem.Problem)2 GreedyBestFirstSearch (aima.core.search.informed.GreedyBestFirstSearch)2