|
| 1 | +package aima.search.map; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Hashtable; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import aima.util.LabeledGraph; |
| 8 | +import aima.util.Util; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements a map with locations, distance labeled links between the |
| 12 | + * locations, straight line distances, and 2d-placement positions of locations. |
| 13 | + * Locations are represented by strings and travel distances by integer values. |
| 14 | + * Locations and links can be added dynamically and removed after creation. This |
| 15 | + * enables to read maps from file or to modify them with respect to newly |
| 16 | + * obtained knowledge. |
| 17 | + * |
| 18 | + * @author R. Lunde |
| 19 | + */ |
| 20 | +public class ExtendableMap implements Map { |
| 21 | + |
| 22 | + /** |
| 23 | + * Stores map data. Locations are represented as vertices and connections |
| 24 | + * (links) as directed edges labeled with corresponding travel distances. |
| 25 | + */ |
| 26 | + private final LabeledGraph<String, Double> links; |
| 27 | + |
| 28 | + /** Stores xy-coordinates for each location. */ |
| 29 | + private final Hashtable<String, Point2D> locationPositions; |
| 30 | + |
| 31 | + /** Creates an empty map. */ |
| 32 | + public ExtendableMap() { |
| 33 | + links = new LabeledGraph<String, Double>(); |
| 34 | + locationPositions = new Hashtable<String, Point2D>(); |
| 35 | + } |
| 36 | + |
| 37 | + /** Removes everything. */ |
| 38 | + public void clear() { |
| 39 | + links.clear(); |
| 40 | + locationPositions.clear(); |
| 41 | + } |
| 42 | + |
| 43 | + /** Clears all connections but keeps location position informations. */ |
| 44 | + public void clearLinks() { |
| 45 | + links.clear(); |
| 46 | + } |
| 47 | + |
| 48 | + /** Returns a list of all locations. */ |
| 49 | + public List<String> getLocations() { |
| 50 | + return links.getVertexLabels(); |
| 51 | + } |
| 52 | + |
| 53 | + /** Checks whether the given string is the name of a location. */ |
| 54 | + public boolean isLocation(String str) { |
| 55 | + return links.isVertexLabel(str); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Answers to the question: Where can I get, following one of the |
| 60 | + * connections starting at the specified location? |
| 61 | + */ |
| 62 | + public List<String> getLocationsLinkedTo(String fromLocation) { |
| 63 | + List<String> result = links.getSuccessors(fromLocation); |
| 64 | + Collections.sort(result); |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the travel distance between the two specified locations if they |
| 70 | + * are linked by a connection and null otherwise. |
| 71 | + */ |
| 72 | + public Double getDistance(String fromLocation, String toLocation) { |
| 73 | + return links.get(fromLocation, toLocation); |
| 74 | + } |
| 75 | + |
| 76 | + /** Adds a one-way connection to the map. */ |
| 77 | + public void addUnidirectionalLink(String fromLocation, String toLocation, |
| 78 | + Double distance) { |
| 79 | + links.set(fromLocation, toLocation, distance); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Adds a connection which can be traveled in both direction. Internally, |
| 84 | + * such a connection is represented as two one-way connections. |
| 85 | + */ |
| 86 | + public void addBidirectionalLink(String fromLocation, String toLocation, |
| 87 | + Double distance) { |
| 88 | + links.set(fromLocation, toLocation, distance); |
| 89 | + links.set(toLocation, fromLocation, distance); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns a location which is selected by random. |
| 94 | + */ |
| 95 | + public String randomlyGenerateDestination() { |
| 96 | + return Util.selectRandomlyFromList(getLocations()); |
| 97 | + } |
| 98 | + |
| 99 | + /** Removes a one-way connection. */ |
| 100 | + public void removeUnidirectionalLink(String fromLocation, String toLocation) { |
| 101 | + links.remove(fromLocation, toLocation); |
| 102 | + } |
| 103 | + |
| 104 | + /** Removes the two corresponding one-way connections. */ |
| 105 | + public void removeBidirectionalLink(String fromLocation, String toLocation) { |
| 106 | + links.remove(fromLocation, toLocation); |
| 107 | + links.remove(toLocation, fromLocation); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Defines the position of a location as with respect to an orthogonal |
| 112 | + * coordinate system. |
| 113 | + */ |
| 114 | + public void setPosition(String loc, double x, double y) { |
| 115 | + locationPositions.put(loc, new Point2D(x, y)); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Defines the position of a location within the map. Using this method, one |
| 120 | + * location should be selected as reference position (<code>dist=0</code> |
| 121 | + * and <code>dir=0</code>) and all the other location should be placed |
| 122 | + * relative to it. |
| 123 | + * |
| 124 | + * @param loc |
| 125 | + * location name |
| 126 | + * @param dist |
| 127 | + * distance to a reference position |
| 128 | + * @param dir |
| 129 | + * bearing (compass direction) in which the location is seen from |
| 130 | + * the reference position |
| 131 | + */ |
| 132 | + public void setDistAndDirToRefLocation(String loc, double dist, int dir) { |
| 133 | + Point2D coords = new Point2D( |
| 134 | + -Math.sin(dir * Math.PI / 180.0) * dist, |
| 135 | + Math.cos(dir * Math.PI / 180.0) * dist); |
| 136 | + links.addVertex(loc); |
| 137 | + locationPositions.put(loc, coords); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns an array with two integers describing the the position of the |
| 142 | + * specified location. |
| 143 | + */ |
| 144 | + public Point2D getPosition(String loc) { |
| 145 | + return locationPositions.get(loc); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments