001    package aima.search.map;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    import java.util.Set;
006    
007    import aima.basic.Percept;
008    import aima.search.framework.HeuristicFunction;
009    import aima.search.framework.Problem;
010    import aima.search.framework.Search;
011    import aima.search.framework.SimpleProblemSolvingAgent;
012    
013    /**
014     * A realization of the SimpleProblemSolvingAgent capable of navigating a MapEnvironment.
015     */
016    
017    /**
018     * @author Ciaran O'Reilly
019     * 
020     */
021    
022    public class MapAgent extends SimpleProblemSolvingAgent {
023            private MapEnvironment mapEnvironment = null;
024    
025            private Search search = null;
026    
027            private String currentLocation = null;
028    
029            private String[] goalTests = null;
030    
031            private int goalTestPos = 0;
032    
033            private HeuristicFunction heuristicFunction = null;
034    
035            public MapAgent(MapEnvironment mapEnvironment, Search search) {
036                    this.mapEnvironment = mapEnvironment;
037                    this.search = search;
038            }
039    
040            public MapAgent(MapEnvironment mapEnvironment, Search search,
041                            int maxGoalsToFormulate) {
042                    super(maxGoalsToFormulate);
043                    this.mapEnvironment = mapEnvironment;
044                    this.search = search;
045            }
046    
047            public MapAgent(MapEnvironment mapEnvironment, Search search,
048                            String[] goalTests) {
049                    super(goalTests.length);
050                    this.mapEnvironment = mapEnvironment;
051                    this.search = search;
052                    this.goalTests = new String[goalTests.length];
053                    System.arraycopy(goalTests, 0, this.goalTests, 0, goalTests.length);
054            }
055    
056            public HeuristicFunction getHeuristicFunction() {
057                    return heuristicFunction;
058            }
059    
060            public void setHeuristicFunction(HeuristicFunction heuristicFunction) {
061                    this.heuristicFunction = heuristicFunction;
062            }
063    
064            //
065            // PROTECTED METHODS
066            //
067            @Override
068            protected Object updateState(Percept p) {
069                    currentLocation = (String) p.getAttribute(DynAttributeNames.PERCEPT_IN);
070    
071                    return currentLocation;
072            }
073    
074            @Override
075            protected Object formulateGoal() {
076                    Object goal = null;
077                    if (null == goalTests) {
078                            goal = mapEnvironment.getMap().randomlyGenerateDestination();
079                    } else {
080                            goal = goalTests[goalTestPos];
081                            goalTestPos++;
082                    }
083                    mapEnvironment.updateViews("CurrentLocation=In(" + currentLocation
084                                    + "), Goal=In(" + goal + ")");
085    
086                    return goal;
087            }
088    
089            @Override
090            protected Problem formulateProblem(Object goal) {
091                    if (null == getHeuristicFunction()) {
092                            return new BidirectionalMapProblem(mapEnvironment.getMap(),
093                                            currentLocation, (String) goal);
094                    } else {
095                            return new BidirectionalMapProblem(mapEnvironment.getMap(),
096                                            currentLocation, (String) goal, getHeuristicFunction());
097                    }
098            }
099    
100            @Override
101            protected List<String> search(Problem problem) {
102                    List<String> actions = new ArrayList<String>();
103                    try {
104                            List sactions = search.search(problem);
105                            for (Object action : sactions) {
106                                    actions.add((String) action);
107                            }
108                    } catch (Exception ex) {
109                            ex.printStackTrace();
110                    }
111                    return actions;
112            }
113    
114            @Override
115            protected void notifyViewOfMetrics() {
116                    Set keys = search.getMetrics().keySet();
117                    for (Object key : keys) {
118                            mapEnvironment.updateViews("METRIC[" + key + "]="
119                                            + search.getMetrics().get((String) key));
120                    }
121            }
122    }