use of aima.core.search.framework.problem.GeneralProblem in project aima-java by aimacode.
the class NQueensSearchDemo method startExperiment.
public void startExperiment(SearchForActions<NQueensBoard, QueenAction> search) {
search.addNodeListener(n -> notifyProgressTrackers(n.getState(), search.getMetrics()));
Problem<NQueensBoard, QueenAction> problem;
if (board.getNumberOfQueensOnBoard() == 0)
problem = new GeneralProblem<>(board, NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
else
problem = new GeneralProblem<>(board, NQueensFunctions::getCSFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
List<QueenAction> actions = search.findActions(problem);
for (QueenAction action : actions) board = NQueensFunctions.getResult(board, action);
notifyProgressTrackers(board, search.getMetrics());
}
use of aima.core.search.framework.problem.GeneralProblem 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());
}
use of aima.core.search.framework.problem.GeneralProblem 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"));
}
use of aima.core.search.framework.problem.GeneralProblem 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"));
}
use of aima.core.search.framework.problem.GeneralProblem in project aima-java by aimacode.
the class GreedyBestFirstSearchTest method testAIMA3eFigure3_23_using_GraphSearch.
@Test
public void testAIMA3eFigure3_23_using_GraphSearch() throws Exception {
Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania();
Problem<String, MoveToAction> problem = new GeneralProblem<>(SimplifiedRoadMapOfPartOfRomania.ARAD, MapFunctions.createActionsFunction(romaniaMap), MapFunctions.createResultFunction(), GoalTest.isEqual(SimplifiedRoadMapOfPartOfRomania.BUCHAREST), MapFunctions.createDistanceStepCostFunction(romaniaMap));
SearchForActions<String, MoveToAction> search = new GreedyBestFirstSearch<>(new GraphSearch<>(), MapFunctions.createSLDHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
SearchAgent<String, MoveToAction> agent = new SearchAgent<>(problem, search);
Assert.assertEquals("[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==Fagaras], Action[name==moveTo, location==Bucharest]]", agent.getActions().toString());
Assert.assertEquals(3, agent.getActions().size());
Assert.assertEquals("3", agent.getInstrumentation().getProperty("nodesExpanded"));
Assert.assertEquals("4", agent.getInstrumentation().getProperty("queueSize"));
Assert.assertEquals("5", agent.getInstrumentation().getProperty("maxQueueSize"));
}
Aggregations