Search in sources :

Example 1 with UniformCostSearch

use of aima.core.search.uninformed.UniformCostSearch in project aima-java by aimacode.

the class UniformCostSearchTest method testUniformCostSuccesfulSearch.

@Test
public void testUniformCostSuccesfulSearch() throws Exception {
    Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(8), 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(8, actions.size());
    Assert.assertEquals("1965", agent.getInstrumentation().getProperty("nodesExpanded"));
    Assert.assertEquals("8.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 2 with UniformCostSearch

use of aima.core.search.uninformed.UniformCostSearch in project aima-java by aimacode.

the class SolutionTesterTest method testMultiGoalProblem.

@Test
public void testMultiGoalProblem() throws Exception {
    Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania();
    Problem<String, MoveToAction> problem = new GeneralProblem<String, MoveToAction>(SimplifiedRoadMapOfPartOfRomania.ARAD, MapFunctions.createActionsFunction(romaniaMap), MapFunctions.createResultFunction(), GoalTest.<String>isEqual(SimplifiedRoadMapOfPartOfRomania.BUCHAREST).or(GoalTest.isEqual(SimplifiedRoadMapOfPartOfRomania.HIRSOVA)), MapFunctions.createDistanceStepCostFunction(romaniaMap)) {

        @Override
        public boolean testSolution(Node<String, MoveToAction> node) {
            return testGoal(node.getState()) && node.getPathCost() > 550;
        // accept paths to goal only if their costs are above 550
        }
    };
    SearchForActions<String, MoveToAction> search = new UniformCostSearch<>(new GraphSearch<>());
    SearchAgent<String, MoveToAction> agent = new SearchAgent<>(problem, search);
    Assert.assertEquals("[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==RimnicuVilcea], Action[name==moveTo, location==Pitesti], Action[name==moveTo, location==Bucharest], Action[name==moveTo, location==Urziceni], Action[name==moveTo, location==Hirsova]]", agent.getActions().toString());
    Assert.assertEquals(6, agent.getActions().size());
    Assert.assertEquals("15", agent.getInstrumentation().getProperty("nodesExpanded"));
    Assert.assertEquals("1", agent.getInstrumentation().getProperty("queueSize"));
    Assert.assertEquals("5", agent.getInstrumentation().getProperty("maxQueueSize"));
}
Also used : UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) SimplifiedRoadMapOfPartOfRomania(aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania) SearchAgent(aima.core.search.framework.SearchAgent) Node(aima.core.search.framework.Node) Map(aima.core.environment.map.Map) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) MoveToAction(aima.core.environment.map.MoveToAction) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 3 with UniformCostSearch

use of aima.core.search.uninformed.UniformCostSearch in project aima-java by aimacode.

the class MapAgentTest method testNormalSearchGraphSearchMinFrontier.

@Test
public void testNormalSearchGraphSearchMinFrontier() {
    MapEnvironment me = new MapEnvironment(aMap);
    UniformCostSearch<String, MoveToAction> ucSearch = new UniformCostSearch<>(new GraphSearchReducedFrontier<>());
    SimpleMapAgent ma = new SimpleMapAgent(me.getMap(), me, ucSearch, new String[] { "D" });
    me.addAgent(ma, "A");
    me.addEnvironmentView(new TestEnvironmentView());
    me.stepUntilDone();
    Assert.assertEquals("CurrentLocation=In(A), Goal=In(D):Action[name==moveTo, location==C]:Action[name==moveTo, location==D]:METRIC[pathCost]=13.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=1:METRIC[nodesExpanded]=3:Action[name==NoOp]:", envChanges.toString());
}
Also used : UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) SimpleMapAgent(aima.core.environment.map.SimpleMapAgent) MapEnvironment(aima.core.environment.map.MapEnvironment) MoveToAction(aima.core.environment.map.MoveToAction) Test(org.junit.Test)

Example 4 with UniformCostSearch

use of aima.core.search.uninformed.UniformCostSearch in project aima-java by aimacode.

the class MapEnvironmentTest method testTwoAgentsSupported.

@Test
public void testTwoAgentsSupported() {
    SimpleMapAgent ma1 = new SimpleMapAgent(me.getMap(), me, new UniformCostSearch(), new String[] { "A" });
    SimpleMapAgent ma2 = new SimpleMapAgent(me.getMap(), me, new UniformCostSearch(), new String[] { "A" });
    me.addAgent(ma1, "A");
    me.addAgent(ma2, "A");
    me.executeAction(ma1, new MoveToAction("B"));
    me.executeAction(ma2, new MoveToAction("C"));
    Assert.assertEquals(me.getAgentLocation(ma1), "B");
    Assert.assertEquals(me.getAgentLocation(ma2), "C");
}
Also used : UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) SimpleMapAgent(aima.core.environment.map.SimpleMapAgent) MoveToAction(aima.core.environment.map.MoveToAction) Test(org.junit.Test)

Example 5 with UniformCostSearch

use of aima.core.search.uninformed.UniformCostSearch in project aima-java by aimacode.

the class MapEnvironmentTest method setUp.

@Before
public void setUp() {
    ExtendableMap aMap = new ExtendableMap();
    aMap.addBidirectionalLink("A", "B", 5.0);
    aMap.addBidirectionalLink("A", "C", 6.0);
    aMap.addBidirectionalLink("B", "C", 4.0);
    aMap.addBidirectionalLink("C", "D", 7.0);
    aMap.addUnidirectionalLink("B", "E", 14.0);
    me = new MapEnvironment(aMap);
    ma = new SimpleMapAgent(me.getMap(), me, new UniformCostSearch(), new String[] { "A" });
}
Also used : UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) SimpleMapAgent(aima.core.environment.map.SimpleMapAgent) MapEnvironment(aima.core.environment.map.MapEnvironment) ExtendableMap(aima.core.environment.map.ExtendableMap) Before(org.junit.Before)

Aggregations

UniformCostSearch (aima.core.search.uninformed.UniformCostSearch)6 Test (org.junit.Test)5 MoveToAction (aima.core.environment.map.MoveToAction)3 SimpleMapAgent (aima.core.environment.map.SimpleMapAgent)3 SearchAgent (aima.core.search.framework.SearchAgent)3 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)3 GoalTest (aima.core.search.framework.problem.GoalTest)3 Action (aima.core.agent.Action)2 MapEnvironment (aima.core.environment.map.MapEnvironment)2 NQueensBoard (aima.core.environment.nqueens.NQueensBoard)2 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)2 QueenAction (aima.core.environment.nqueens.QueenAction)2 ExtendableMap (aima.core.environment.map.ExtendableMap)1 Map (aima.core.environment.map.Map)1 SimplifiedRoadMapOfPartOfRomania (aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania)1 Node (aima.core.search.framework.Node)1 Before (org.junit.Before)1