Search in sources :

Example 1 with Map

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;
    }
}
Also used : Point2D(aima.core.util.math.geom.shapes.Point2D) Map(aima.core.environment.map.Map)

Example 2 with Map

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);
    }
}
Also used : Point2D(aima.core.util.math.geom.shapes.Point2D) ArrayList(java.util.ArrayList) Map(aima.core.environment.map.Map)

Example 3 with Map

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));
        }
    }
}
Also used : Point2D(aima.core.util.math.geom.shapes.Point2D) Map(aima.core.environment.map.Map)

Example 4 with Map

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));
        }
    }
}
Also used : Line(javafx.scene.shape.Line) Circle(javafx.scene.shape.Circle) Agent(aima.core.agent.Agent) Shape(javafx.scene.shape.Shape) Point2D(aima.core.util.math.geom.shapes.Point2D) Text(javafx.scene.text.Text) Map(aima.core.environment.map.Map) Font(javafx.scene.text.Font)

Example 5 with Map

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"));
}
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)

Aggregations

Map (aima.core.environment.map.Map)11 Point2D (aima.core.util.math.geom.shapes.Point2D)7 MoveToAction (aima.core.environment.map.MoveToAction)3 SimplifiedRoadMapOfPartOfRomania (aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania)3 SearchAgent (aima.core.search.framework.SearchAgent)3 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)3 GoalTest (aima.core.search.framework.problem.GoalTest)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Agent (aima.core.agent.Agent)2 GreedyBestFirstSearch (aima.core.search.informed.GreedyBestFirstSearch)2 Node (aima.core.search.framework.Node)1 UniformCostSearch (aima.core.search.uninformed.UniformCostSearch)1 Circle (javafx.scene.shape.Circle)1 Line (javafx.scene.shape.Line)1 Shape (javafx.scene.shape.Shape)1 Font (javafx.scene.text.Font)1 Text (javafx.scene.text.Text)1