use of aima.core.environment.map.Map in project aima-java by aimacode.
the class MapAgentView method paintTrack.
/** The track of the agent is visualized with red lines. */
private void paintTrack(java.awt.Graphics2D g2, Agent a) {
Map map = getMapEnv().getMap();
Point2D lastPt = null;
g2.setColor(Color.red);
for (String loc : getTrack(a)) {
Point2D pt = map.getPosition(loc);
if (pt != null && lastPt != null) {
g2.drawLine(x(pt), y(pt), x(lastPt), y(lastPt));
}
lastPt = pt;
}
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class ExtendedMapAgentView method paintLoc.
/** Displays a map location. */
protected void paintLoc(Graphics2D g2, String loc) {
Map map = getMapEnv().getMap();
Point2D pt = map.getPosition(loc);
if (pt != null) {
int x = x(pt);
int y = y(pt);
String info = "";
List<String> track = new ArrayList<String>();
if (!env.getAgents().isEmpty())
// show details only for track of first agent...
track = getTrack(env.getAgents().get(0));
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < track.size(); i++) if (track.get(i).equals(loc))
list.add(i + 1);
if (!list.isEmpty())
info = list.toString();
// }
if (scenario != null && scenario.getInitAgentLocation().equals(loc)) {
g2.setColor(Color.red);
g2.fillOval(x - 7, y - 7, 14, 14);
}
if (getAgentLocs().contains(loc)) {
g2.setColor(Color.red);
g2.fillOval(x - 4, y - 4, 8, 8);
}
// else
if (destinations != null && destinations.contains(loc))
g2.setColor(Color.green);
else if (track.contains(loc))
g2.setColor(Color.black);
else
g2.setColor(Color.gray);
g2.drawString(loc + info, x, y);
}
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class MapAgentView method paintMap.
/**
* Represents roads by lines and locations by name-labeled points.
*/
protected void paintMap(java.awt.Graphics2D g2) {
Map envMap = getMapEnv().getMap();
for (String l1 : envMap.getLocations()) {
Point2D pt1 = envMap.getPosition(l1);
List<String> linkedLocs = envMap.getPossibleNextLocations(l1);
for (String l2 : linkedLocs) {
Point2D pt2 = envMap.getPosition(l2);
g2.setColor(Color.lightGray);
g2.drawLine(x(pt1), y(pt1), x(pt2), y(pt2));
}
}
}
use of aima.core.environment.map.Map in project aima-java by aimacode.
the class MapEnvironmentViewCtrl method update.
protected void update() {
envStateView.getChildren().clear();
if (env != null) {
double scale = adjustTransform();
Map map = env.getMap();
// print connections
for (String loc1 : map.getLocations()) {
Point2D pt1 = map.getPosition(loc1);
for (String loc2 : map.getPossibleNextLocations(loc1)) {
Point2D pt2 = map.getPosition(loc2);
Shape line = new Line(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY());
line.setStroke(Color.LIGHTGRAY);
envStateView.getChildren().add(line);
}
}
// print track of first agent
if (!env.getAgents().isEmpty()) {
String aLoc = env.getAgentLocation(env.getAgents().get(0));
if (track.isEmpty() || !Objects.equals(track.get(track.size() - 1), aLoc))
track.add(aLoc);
for (int i = 1; i < track.size(); i++) {
Point2D pt1 = map.getPosition(track.get(i - 1));
Point2D pt2 = map.getPosition(track.get(i));
Shape line = new Line(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY());
line.setStroke(Color.RED);
line.setStrokeWidth(2);
envStateView.getChildren().add(line);
}
}
// print locations
for (String loc : map.getLocations()) {
Point2D point = map.getPosition(loc);
Text text = new Text(point.getX() + 10 / scale, point.getY(), loc);
text.setFont(new Font(12.0 / scale));
envStateView.getChildren().add(text);
envStateView.getChildren().add(new Circle(point.getX(), point.getY(), 2 / scale));
}
// print agent locations
for (Agent agent : env.getAgents()) {
String loc = env.getAgentLocation(agent);
if (loc != null) {
Point2D pt = map.getPosition(loc);
envStateView.getChildren().add(new Circle(pt.getX(), pt.getY(), 8 / scale, Color.RED));
}
}
// print goal
if (goal != null) {
Point2D pt = map.getPosition(goal);
envStateView.getChildren().add(new Circle(pt.getX(), pt.getY(), 6 / scale, Color.GREEN));
}
}
}
use of aima.core.environment.map.Map 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"));
}
Aggregations