use of aima.core.environment.map.Map 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"));
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class GreedyBestFirstSearchTest method testAIMA3eFigure3_23.
@Test
public void testAIMA3eFigure3_23() 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 TreeSearch<>(), 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("6", agent.getInstrumentation().getProperty("queueSize"));
Assert.assertEquals("7", agent.getInstrumentation().getProperty("maxQueueSize"));
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class ExtendedMapAgentView method paintMap.
/**
* Represents roads by lines and locations by name-labeled points.
*/
protected void paintMap(java.awt.Graphics2D g2) {
Map envMap = getMapEnv().getMap();
Map aMap = (agentMap != null) ? agentMap : envMap;
List<Roadblock> roadblocks = new ArrayList<Roadblock>();
for (String l1 : envMap.getLocations()) {
Point2D pt1 = envMap.getPosition(l1);
List<String> linkedLocs = envMap.getPossibleNextLocations(l1);
for (String l2 : aMap.getPossibleNextLocations(l1)) if (!linkedLocs.contains(l2))
linkedLocs.add(l2);
for (String l2 : linkedLocs) {
Point2D pt2 = envMap.getPosition(l2);
g2.setColor(Color.lightGray);
g2.drawLine(x(pt1), y(pt1), x(pt2), y(pt2));
boolean blockedInEnv = !envMap.getPossibleNextLocations(l2).contains(l1);
boolean blockedInAgent = !aMap.getPossibleNextLocations(l2).contains(l1);
roadblocks.add(new Roadblock(pt1, pt2, blockedInEnv, blockedInAgent));
if (blockedInEnv && blockedInAgent) {
boolean blockedInEnvOtherDir = !envMap.getPossibleNextLocations(l1).contains(l2);
boolean blockedInAgentOtherDir = !aMap.getPossibleNextLocations(l1).contains(l2);
roadblocks.add(new Roadblock(pt2, pt1, blockedInEnvOtherDir, blockedInAgentOtherDir));
}
}
}
for (Roadblock block : roadblocks) paintRoadblock(g2, block);
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class MapAgentView method adjustTransformation.
/**
* Adjusts offsets and scale so that the whole map fits on the view without
* scrolling.
*/
private void adjustTransformation() {
Map map = getMapEnv().getMap();
List<String> locs = map.getLocations();
// adjust coordinates relative to the left upper corner of the graph
// area
double minX = Double.POSITIVE_INFINITY;
double minY = Double.POSITIVE_INFINITY;
double maxX = Double.NEGATIVE_INFINITY;
double maxY = Double.NEGATIVE_INFINITY;
for (String loc : locs) {
Point2D xy = map.getPosition(loc);
if (xy.getX() < minX)
minX = xy.getX();
if (xy.getY() < minY)
minY = xy.getY();
if (xy.getX() > maxX)
maxX = xy.getX();
if (xy.getY() > maxY)
maxY = xy.getY();
}
this.setBorder(20, 20, 20, 100);
adjustTransformation(minX, minY, maxX, maxY);
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class MapAgentView method paintComponent.
/**
* Clears the panel, displays the map, the current agent locations, and the
* track of the first agent.
*/
@Override
public void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
if (env != null) {
Map map = getMapEnv().getMap();
if (!map.getLocations().isEmpty()) {
// safety first!
updateTracks();
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
adjustTransformation();
paintMap(g2);
for (Agent a : env.getAgents()) paintTrack(g2, a);
for (String loc : map.getLocations()) paintLoc(g2, loc);
}
}
}
Aggregations