Skip to content

Commit 869adac

Browse files
Ruediger.LundeRuediger.Lunde
authored andcommitted
StepCostFunction modified so that it also works for small distances between 0 and 1. Hope, that's ok...
1 parent 0f117e6 commit 869adac

File tree

9 files changed

+207
-26
lines changed

9 files changed

+207
-26
lines changed

src/aima/gui/applications/search/map/MapAgentFrame.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ class MapAgentView extends AgentView {
6161

6262
/** Clears the panel and draws the map and the tour history. */
6363
public void paint(java.awt.Graphics g) {
64+
super.paint(g);
6465
MapAgentModel maModel = (MapAgentModel) model;
65-
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
66-
g2.setColor(Color.white);
67-
g2.fillRect(0, 0, getWidth(), getHeight());
6866
if (maModel != null && !maModel.isEmpty()) {
67+
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
6968
adjustTransformation();
7069
paintMap(g2);
7170
paintTour(g2);

src/aima/gui/framework/AgentAppFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public SelectionState getSelection() {
125125
*/
126126
public void setAgentView(AbstractAgentView view) {
127127
agentView = view;
128-
centerPane.add(centerPane.LEFT, new JScrollPane(agentView));
128+
centerPane.add(centerPane.LEFT, agentView);
129129
}
130130

131131
/** Specifies how to distribute extra space when resizing the split pane. */
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}

src/aima/search/map/MapStepCostFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public Double calculateStepCost(Object fromCurrentState,
4040

4141
Double distance = map.getDistance(fromLoc, toLoc);
4242

43-
if (null == distance || distance < 0) {
43+
if (distance == null || distance <= 0) {
4444
return constantCost;
4545
}
4646

47-
return constantCost + new Double(distance);
47+
return new Double(distance);
4848
}
4949
}

src/aima/search/map/Point2D.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package aima.search.map;
2+
3+
/**
4+
* Simplified version of {@link java.awt.geom.Point}. We do not
5+
* want dependencies to presentation layer packages here.
6+
* @author R. Lunde
7+
*/
8+
public class Point2D {
9+
private double x;
10+
private double y;
11+
12+
public Point2D(double x, double y) {
13+
this.x = x;
14+
this.y = y;
15+
}
16+
17+
public double getX() {
18+
return x;
19+
}
20+
21+
public double getY() {
22+
return y;
23+
}
24+
25+
/**
26+
* Returns the Euclidean distance between a specified point
27+
* and this point.
28+
*/
29+
public double distance(Point2D pt) {
30+
double result = (pt.getX() - x) * (pt.getX() - x);
31+
result += (pt.getY() - y) * (pt.getY() - y);
32+
return Math.sqrt(result);
33+
}
34+
}

src/aima/test/search/map/MapAgentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void envChanged(String command) {
6363
me.stepUntilNoOp();
6464

6565
assertEquals(
66-
"CurrentLocation=In(A), Goal=In(D):C:D:METRIC[pathCost]=15.0:METRIC[maxQueueSize]=6:METRIC[queueSize]=1:METRIC[nodesExpanded]=3:NoOP:",
66+
"CurrentLocation=In(A), Goal=In(D):C:D:METRIC[pathCost]=13.0:METRIC[maxQueueSize]=6:METRIC[queueSize]=1:METRIC[nodesExpanded]=3:NoOP:",
6767
envChanges.toString());
6868
}
6969

src/aima/test/search/map/MapStepCostFunctionTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public void setUp() {
2525
}
2626

2727
public void testCosts() {
28-
assertEquals(new Double(6), mscf.calculateStepCost("A", "B", "Go"));
29-
assertEquals(new Double(7), mscf.calculateStepCost("A", "C", "Go"));
30-
assertEquals(new Double(5), mscf.calculateStepCost("B", "C", "Go"));
31-
assertEquals(new Double(8), mscf.calculateStepCost("C", "D", "Go"));
32-
assertEquals(new Double(15), mscf.calculateStepCost("B", "E", "Go"));
28+
assertEquals(new Double(5), mscf.calculateStepCost("A", "B", "Go"));
29+
assertEquals(new Double(6), mscf.calculateStepCost("A", "C", "Go"));
30+
assertEquals(new Double(4), mscf.calculateStepCost("B", "C", "Go"));
31+
assertEquals(new Double(7), mscf.calculateStepCost("C", "D", "Go"));
32+
assertEquals(new Double(14), mscf.calculateStepCost("B", "E", "Go"));
3333
//
34-
assertEquals(new Double(6), mscf.calculateStepCost("B", "A", "Go"));
35-
assertEquals(new Double(7), mscf.calculateStepCost("C", "A", "Go"));
36-
assertEquals(new Double(5), mscf.calculateStepCost("C", "B", "Go"));
37-
assertEquals(new Double(8), mscf.calculateStepCost("D", "C", "Go"));
34+
assertEquals(new Double(5), mscf.calculateStepCost("B", "A", "Go"));
35+
assertEquals(new Double(6), mscf.calculateStepCost("C", "A", "Go"));
36+
assertEquals(new Double(4), mscf.calculateStepCost("C", "B", "Go"));
37+
assertEquals(new Double(7), mscf.calculateStepCost("D", "C", "Go"));
3838
//
3939
assertEquals(new Double(1), mscf.calculateStepCost("X", "Z", "Go"));
4040
assertEquals(new Double(1), mscf.calculateStepCost("A", "Z", "Go"));

src/aima/test/search/searches/BidirectionalSearchTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void envChanged(String command) {
9898
me.stepUntilNoOp();
9999

100100
assertEquals(
101-
"CurrentLocation=In(A), Goal=In(B):B:METRIC[pathCost]=6.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=2:METRIC[nodesExpanded]=2:NoOP:",
101+
"CurrentLocation=In(A), Goal=In(B):B:METRIC[pathCost]=5.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=2:METRIC[nodesExpanded]=2:NoOP:",
102102
envChanges.toString());
103103

104104
assertEquals(
@@ -126,7 +126,7 @@ public void envChanged(String command) {
126126
me.stepUntilNoOp();
127127

128128
assertEquals(
129-
"CurrentLocation=In(A), Goal=In(C):B:C:METRIC[pathCost]=12.0:METRIC[maxQueueSize]=4:METRIC[queueSize]=4:METRIC[nodesExpanded]=4:NoOP:",
129+
"CurrentLocation=In(A), Goal=In(C):B:C:METRIC[pathCost]=10.0:METRIC[maxQueueSize]=4:METRIC[queueSize]=4:METRIC[nodesExpanded]=4:NoOP:",
130130
envChanges.toString());
131131

132132
assertEquals(
@@ -155,7 +155,7 @@ public void envChanged(String command) {
155155
me.stepUntilNoOp();
156156

157157
assertEquals(
158-
"CurrentLocation=In(A), Goal=In(D):B:C:D:METRIC[pathCost]=18.0:METRIC[maxQueueSize]=4:METRIC[queueSize]=4:METRIC[nodesExpanded]=4:NoOP:",
158+
"CurrentLocation=In(A), Goal=In(D):B:C:D:METRIC[pathCost]=15.0:METRIC[maxQueueSize]=4:METRIC[queueSize]=4:METRIC[nodesExpanded]=4:NoOP:",
159159
envChanges.toString());
160160

161161
assertEquals(
@@ -182,7 +182,7 @@ public void envChanged(String command) {
182182
me.stepUntilNoOp();
183183

184184
assertEquals(
185-
"CurrentLocation=In(A), Goal=In(B):B:METRIC[pathCost]=6.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=1:METRIC[nodesExpanded]=2:NoOP:",
185+
"CurrentLocation=In(A), Goal=In(B):B:METRIC[pathCost]=5.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=1:METRIC[nodesExpanded]=2:NoOP:",
186186
envChanges.toString());
187187

188188
assertEquals(
@@ -210,7 +210,7 @@ public void envChanged(String command) {
210210
me.stepUntilNoOp();
211211

212212
assertEquals(
213-
"CurrentLocation=In(A), Goal=In(C):B:C:METRIC[pathCost]=12.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=0:METRIC[nodesExpanded]=4:NoOP:",
213+
"CurrentLocation=In(A), Goal=In(C):B:C:METRIC[pathCost]=10.0:METRIC[maxQueueSize]=2:METRIC[queueSize]=0:METRIC[nodesExpanded]=4:NoOP:",
214214
envChanges.toString());
215215

216216
assertEquals(
@@ -240,7 +240,7 @@ public void envChanged(String command) {
240240
me.stepUntilNoOp();
241241

242242
assertEquals(
243-
"CurrentLocation=In(A), Goal=In(E):B:C:D:E:METRIC[pathCost]=24.0:METRIC[maxQueueSize]=4:METRIC[queueSize]=3:METRIC[nodesExpanded]=5:NoOP:",
243+
"CurrentLocation=In(A), Goal=In(E):B:C:D:E:METRIC[pathCost]=20.0:METRIC[maxQueueSize]=4:METRIC[queueSize]=3:METRIC[nodesExpanded]=5:NoOP:",
244244
envChanges.toString());
245245

246246
assertEquals(
@@ -354,7 +354,7 @@ public void envChanged(String command) {
354354
me.stepUntilNoOp();
355355

356356
assertEquals(
357-
"CurrentLocation=In(A), Goal=In(F):B:F:METRIC[pathCost]=12.0:METRIC[maxQueueSize]=5:METRIC[queueSize]=5:METRIC[nodesExpanded]=6:NoOP:",
357+
"CurrentLocation=In(A), Goal=In(F):B:F:METRIC[pathCost]=10.0:METRIC[maxQueueSize]=5:METRIC[queueSize]=5:METRIC[nodesExpanded]=6:NoOP:",
358358
envChanges.toString());
359359

360360
assertEquals(
@@ -387,7 +387,7 @@ public void envChanged(String command) {
387387
me.stepUntilNoOp();
388388

389389
assertEquals(
390-
"CurrentLocation=In(A), Goal=In(F):B:C:D:E:F:METRIC[pathCost]=30.0:METRIC[maxQueueSize]=6:METRIC[queueSize]=6:METRIC[nodesExpanded]=7:NoOP:",
390+
"CurrentLocation=In(A), Goal=In(F):B:C:D:E:F:METRIC[pathCost]=25.0:METRIC[maxQueueSize]=6:METRIC[queueSize]=6:METRIC[nodesExpanded]=7:NoOP:",
391391
envChanges.toString());
392392

393393
assertEquals(
@@ -423,7 +423,7 @@ public void envChanged(String command) {
423423
me.stepUntilNoOp();
424424

425425
assertEquals(
426-
"CurrentLocation=In(A), Goal=In(F):B:C:D:F:METRIC[pathCost]=24.0:METRIC[maxQueueSize]=9:METRIC[queueSize]=9:METRIC[nodesExpanded]=7:NoOP:",
426+
"CurrentLocation=In(A), Goal=In(F):B:C:D:F:METRIC[pathCost]=20.0:METRIC[maxQueueSize]=9:METRIC[queueSize]=9:METRIC[nodesExpanded]=7:NoOP:",
427427
envChanges.toString());
428428

429429
assertEquals(

src/aima/test/search/searches/RecursiveBestFirstSearchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void envChanged(String command) {
7979
me.stepUntilNoOp();
8080

8181
assertEquals(
82-
"CurrentLocation=In(Arad), Goal=In(Bucharest):Sibiu:RimnicuVilcea:Pitesti:Bucharest:METRIC[pathCost]=422.0:METRIC[maxRecursiveDepth]=4:METRIC[nodesExpanded]=6:NoOP:",
82+
"CurrentLocation=In(Arad), Goal=In(Bucharest):Sibiu:RimnicuVilcea:Pitesti:Bucharest:METRIC[pathCost]=418.0:METRIC[maxRecursiveDepth]=4:METRIC[nodesExpanded]=6:NoOP:",
8383
envChanges.toString());
8484
}
8585
}

0 commit comments

Comments
 (0)