Skip to content

Commit be06366

Browse files
committed
SearchForStates added.
1 parent 218d7bd commit be06366

File tree

56 files changed

+2257
-2308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2257
-2308
lines changed
Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
package aima.core.environment.eightpuzzle;
2-
3-
import aima.core.search.framework.HeuristicFunction;
4-
import aima.core.util.datastructure.XYLocation;
5-
6-
/**
7-
* @author Ravi Mohan
8-
*
9-
*/
10-
public class ManhattanHeuristicFunction implements HeuristicFunction {
11-
12-
public double h(Object state) {
13-
EightPuzzleBoard board = (EightPuzzleBoard) state;
14-
int retVal = 0;
15-
for (int i = 1; i < 9; i++) {
16-
XYLocation loc = board.getLocationOf(i);
17-
retVal += evaluateManhattanDistanceOf(i, loc);
18-
}
19-
return retVal;
20-
21-
}
22-
23-
public int evaluateManhattanDistanceOf(int i, XYLocation loc) {
24-
int retVal = -1;
25-
int xpos = loc.getXCoOrdinate();
26-
int ypos = loc.getYCoOrdinate();
27-
switch (i) {
28-
29-
case 1:
30-
retVal = Math.abs(xpos - 0) + Math.abs(ypos - 1);
31-
break;
32-
case 2:
33-
retVal = Math.abs(xpos - 0) + Math.abs(ypos - 2);
34-
break;
35-
case 3:
36-
retVal = Math.abs(xpos - 1) + Math.abs(ypos - 0);
37-
break;
38-
case 4:
39-
retVal = Math.abs(xpos - 1) + Math.abs(ypos - 1);
40-
break;
41-
case 5:
42-
retVal = Math.abs(xpos - 1) + Math.abs(ypos - 2);
43-
break;
44-
case 6:
45-
retVal = Math.abs(xpos - 2) + Math.abs(ypos - 0);
46-
break;
47-
case 7:
48-
retVal = Math.abs(xpos - 2) + Math.abs(ypos - 1);
49-
break;
50-
case 8:
51-
retVal = Math.abs(xpos - 2) + Math.abs(ypos - 2);
52-
break;
53-
54-
}
55-
return retVal;
56-
}
1+
package aima.core.environment.eightpuzzle;
2+
3+
import aima.core.search.framework.evalfunc.HeuristicFunction;
4+
import aima.core.util.datastructure.XYLocation;
5+
6+
/**
7+
* @author Ravi Mohan
8+
*
9+
*/
10+
public class ManhattanHeuristicFunction implements HeuristicFunction {
11+
12+
public double h(Object state) {
13+
EightPuzzleBoard board = (EightPuzzleBoard) state;
14+
int retVal = 0;
15+
for (int i = 1; i < 9; i++) {
16+
XYLocation loc = board.getLocationOf(i);
17+
retVal += evaluateManhattanDistanceOf(i, loc);
18+
}
19+
return retVal;
20+
21+
}
22+
23+
public int evaluateManhattanDistanceOf(int i, XYLocation loc) {
24+
int retVal = -1;
25+
int xpos = loc.getXCoOrdinate();
26+
int ypos = loc.getYCoOrdinate();
27+
switch (i) {
28+
29+
case 1:
30+
retVal = Math.abs(xpos - 0) + Math.abs(ypos - 1);
31+
break;
32+
case 2:
33+
retVal = Math.abs(xpos - 0) + Math.abs(ypos - 2);
34+
break;
35+
case 3:
36+
retVal = Math.abs(xpos - 1) + Math.abs(ypos - 0);
37+
break;
38+
case 4:
39+
retVal = Math.abs(xpos - 1) + Math.abs(ypos - 1);
40+
break;
41+
case 5:
42+
retVal = Math.abs(xpos - 1) + Math.abs(ypos - 2);
43+
break;
44+
case 6:
45+
retVal = Math.abs(xpos - 2) + Math.abs(ypos - 0);
46+
break;
47+
case 7:
48+
retVal = Math.abs(xpos - 2) + Math.abs(ypos - 1);
49+
break;
50+
case 8:
51+
retVal = Math.abs(xpos - 2) + Math.abs(ypos - 2);
52+
break;
53+
54+
}
55+
return retVal;
56+
}
5757
}
Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
package aima.core.environment.eightpuzzle;
2-
3-
import aima.core.search.framework.HeuristicFunction;
4-
import aima.core.util.datastructure.XYLocation;
5-
6-
/**
7-
* @author Ravi Mohan
8-
*
9-
*/
10-
public class MisplacedTilleHeuristicFunction implements HeuristicFunction {
11-
12-
public double h(Object state) {
13-
EightPuzzleBoard board = (EightPuzzleBoard) state;
14-
return getNumberOfMisplacedTiles(board);
15-
}
16-
17-
private int getNumberOfMisplacedTiles(EightPuzzleBoard board) {
18-
int numberOfMisplacedTiles = 0;
19-
if (!(board.getLocationOf(0).equals(new XYLocation(0, 0)))) {
20-
numberOfMisplacedTiles++;
21-
}
22-
if (!(board.getLocationOf(1).equals(new XYLocation(0, 1)))) {
23-
numberOfMisplacedTiles++;
24-
}
25-
if (!(board.getLocationOf(2).equals(new XYLocation(0, 2)))) {
26-
numberOfMisplacedTiles++;
27-
}
28-
if (!(board.getLocationOf(3).equals(new XYLocation(1, 0)))) {
29-
numberOfMisplacedTiles++;
30-
}
31-
if (!(board.getLocationOf(4).equals(new XYLocation(1, 1)))) {
32-
numberOfMisplacedTiles++;
33-
}
34-
if (!(board.getLocationOf(5).equals(new XYLocation(1, 2)))) {
35-
numberOfMisplacedTiles++;
36-
}
37-
if (!(board.getLocationOf(6).equals(new XYLocation(2, 0)))) {
38-
numberOfMisplacedTiles++;
39-
}
40-
if (!(board.getLocationOf(7).equals(new XYLocation(2, 1)))) {
41-
numberOfMisplacedTiles++;
42-
}
43-
if (!(board.getLocationOf(8).equals(new XYLocation(2, 2)))) {
44-
numberOfMisplacedTiles++;
45-
}
46-
// Subtract the gap position from the # of misplaced tiles
47-
// as its not actually a tile (see issue 73).
48-
if (numberOfMisplacedTiles > 0) {
49-
numberOfMisplacedTiles--;
50-
}
51-
return numberOfMisplacedTiles;
52-
}
1+
package aima.core.environment.eightpuzzle;
2+
3+
import aima.core.search.framework.evalfunc.HeuristicFunction;
4+
import aima.core.util.datastructure.XYLocation;
5+
6+
/**
7+
* @author Ravi Mohan
8+
*
9+
*/
10+
public class MisplacedTilleHeuristicFunction implements HeuristicFunction {
11+
12+
public double h(Object state) {
13+
EightPuzzleBoard board = (EightPuzzleBoard) state;
14+
return getNumberOfMisplacedTiles(board);
15+
}
16+
17+
private int getNumberOfMisplacedTiles(EightPuzzleBoard board) {
18+
int numberOfMisplacedTiles = 0;
19+
if (!(board.getLocationOf(0).equals(new XYLocation(0, 0)))) {
20+
numberOfMisplacedTiles++;
21+
}
22+
if (!(board.getLocationOf(1).equals(new XYLocation(0, 1)))) {
23+
numberOfMisplacedTiles++;
24+
}
25+
if (!(board.getLocationOf(2).equals(new XYLocation(0, 2)))) {
26+
numberOfMisplacedTiles++;
27+
}
28+
if (!(board.getLocationOf(3).equals(new XYLocation(1, 0)))) {
29+
numberOfMisplacedTiles++;
30+
}
31+
if (!(board.getLocationOf(4).equals(new XYLocation(1, 1)))) {
32+
numberOfMisplacedTiles++;
33+
}
34+
if (!(board.getLocationOf(5).equals(new XYLocation(1, 2)))) {
35+
numberOfMisplacedTiles++;
36+
}
37+
if (!(board.getLocationOf(6).equals(new XYLocation(2, 0)))) {
38+
numberOfMisplacedTiles++;
39+
}
40+
if (!(board.getLocationOf(7).equals(new XYLocation(2, 1)))) {
41+
numberOfMisplacedTiles++;
42+
}
43+
if (!(board.getLocationOf(8).equals(new XYLocation(2, 2)))) {
44+
numberOfMisplacedTiles++;
45+
}
46+
// Subtract the gap position from the # of misplaced tiles
47+
// as its not actually a tile (see issue 73).
48+
if (numberOfMisplacedTiles > 0) {
49+
numberOfMisplacedTiles--;
50+
}
51+
return numberOfMisplacedTiles;
52+
}
5353
}

aima-core/src/main/java/aima/core/environment/map/MapAgent.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import aima.core.agent.State;
1111
import aima.core.agent.impl.DynamicPercept;
1212
import aima.core.agent.impl.DynamicState;
13-
import aima.core.search.framework.EvaluationFunction;
14-
import aima.core.search.framework.HeuristicFunctionFactory;
1513
import aima.core.search.framework.ProblemSolvingAgent;
16-
import aima.core.search.framework.Search;
14+
import aima.core.search.framework.SearchForActions;
15+
import aima.core.search.framework.evalfunc.EvaluationFunction;
16+
import aima.core.search.framework.evalfunc.HeuristicFunctionFactory;
1717
import aima.core.search.framework.problem.Problem;
1818
import aima.core.search.informed.BestFirstSearch;
1919
import aima.core.search.informed.HeuristicEvaluationFunction;
@@ -38,32 +38,32 @@ public class MapAgent extends ProblemSolvingAgent {
3838

3939
// possibly null...
4040
protected EnvironmentViewNotifier notifier = null;
41-
private Search search = null;
41+
private SearchForActions search = null;
4242
private HeuristicFunctionFactory hfFactory;
4343

44-
public MapAgent(Map map, Search search, String goal) {
44+
public MapAgent(Map map, SearchForActions search, String goal) {
4545
this.map = map;
4646
this.search = search;
4747
goals.add(goal);
4848
}
4949

50-
public MapAgent(Map map, Search search, String goal, EnvironmentViewNotifier notifier) {
50+
public MapAgent(Map map, SearchForActions search, String goal, EnvironmentViewNotifier notifier) {
5151
this(map, search, goal);
5252
this.notifier = notifier;
5353
}
5454

55-
public MapAgent(Map map, Search search, List<String> goals) {
55+
public MapAgent(Map map, SearchForActions search, List<String> goals) {
5656
this.map = map;
5757
this.search = search;
5858
this.goals.addAll(goals);
5959
}
6060

61-
public MapAgent(Map map, Search search, List<String> goals, EnvironmentViewNotifier notifier) {
61+
public MapAgent(Map map, SearchForActions search, List<String> goals, EnvironmentViewNotifier notifier) {
6262
this(map, search, goals);
6363
this.notifier = notifier;
6464
}
6565

66-
public MapAgent(Map map, Search search, List<String> goals, EnvironmentViewNotifier notifier,
66+
public MapAgent(Map map, SearchForActions search, List<String> goals, EnvironmentViewNotifier notifier,
6767
HeuristicFunctionFactory hfFactory) {
6868
this(map, search, goals, notifier);
6969
this.hfFactory = hfFactory;

aima-core/src/main/java/aima/core/environment/map/MapFunctionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import aima.core.agent.Action;
88
import aima.core.agent.Percept;
99
import aima.core.agent.impl.DynamicPercept;
10-
import aima.core.search.framework.HeuristicFunction;
1110
import aima.core.search.framework.PerceptToStateFunction;
11+
import aima.core.search.framework.evalfunc.HeuristicFunction;
1212
import aima.core.search.framework.problem.ActionsFunction;
1313
import aima.core.search.framework.problem.ResultFunction;
1414
import aima.core.util.math.geom.shapes.Point2D;

aima-core/src/main/java/aima/core/environment/map/SimpleMapAgent.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
import aima.core.agent.State;
1111
import aima.core.agent.impl.DynamicPercept;
1212
import aima.core.agent.impl.DynamicState;
13-
import aima.core.search.framework.Search;
13+
import aima.core.search.framework.SearchForActions;
1414
import aima.core.search.framework.SimpleProblemSolvingAgent;
1515
import aima.core.search.framework.problem.Problem;
1616

1717
/**
1818
* Note: This implementation should be used with one predefined goal only or
19-
* with uninformed search. As the heuristic of the used search algorithm is never
20-
* changed, estimates for the second (or randomly created goal) will be wrong.
19+
* with uninformed search. As the heuristic of the used search algorithm is
20+
* never changed, estimates for the second (or randomly created goal) will be
21+
* wrong.
2122
*
2223
* @author Ciaran O'Reilly
2324
* @author Ruediger Lunde
@@ -30,29 +31,29 @@ public class SimpleMapAgent extends SimpleProblemSolvingAgent {
3031

3132
// possibly null...
3233
private EnvironmentViewNotifier notifier = null;
33-
private Search search = null;
34+
private SearchForActions search = null;
3435
private String[] goals = null;
3536
private int goalTestPos = 0;
3637

37-
public SimpleMapAgent(Map map, EnvironmentViewNotifier notifier, Search search) {
38+
public SimpleMapAgent(Map map, EnvironmentViewNotifier notifier, SearchForActions search) {
3839
this.map = map;
3940
this.notifier = notifier;
4041
this.search = search;
4142
}
4243

43-
public SimpleMapAgent(Map map, EnvironmentViewNotifier notifier, Search search, int maxGoalsToFormulate) {
44+
public SimpleMapAgent(Map map, EnvironmentViewNotifier notifier, SearchForActions search, int maxGoalsToFormulate) {
4445
super(maxGoalsToFormulate);
4546
this.map = map;
4647
this.notifier = notifier;
4748
this.search = search;
4849
}
4950

50-
public SimpleMapAgent(Map map, EnvironmentViewNotifier notifier, Search search, String[] goals) {
51+
public SimpleMapAgent(Map map, EnvironmentViewNotifier notifier, SearchForActions search, String[] goals) {
5152
this(map, search, goals);
5253
this.notifier = notifier;
5354
}
5455

55-
public SimpleMapAgent(Map map, Search search, String[] goals) {
56+
public SimpleMapAgent(Map map, SearchForActions search, String[] goals) {
5657
super(goals.length);
5758
this.map = map;
5859
this.search = search;
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package aima.core.environment.nqueens;
2-
3-
import aima.core.search.framework.HeuristicFunction;
4-
5-
/**
6-
* Estimates the distance to goal by the number of attacking pairs of queens on
7-
* the board.
8-
*
9-
* @author R. Lunde
10-
*/
11-
public class AttackingPairsHeuristic implements HeuristicFunction {
12-
13-
public double h(Object state) {
14-
NQueensBoard board = (NQueensBoard) state;
15-
return board.getNumberOfAttackingPairs();
16-
}
1+
package aima.core.environment.nqueens;
2+
3+
import aima.core.search.framework.evalfunc.HeuristicFunction;
4+
5+
/**
6+
* Estimates the distance to goal by the number of attacking pairs of queens on
7+
* the board.
8+
*
9+
* @author R. Lunde
10+
*/
11+
public class AttackingPairsHeuristic implements HeuristicFunction {
12+
13+
public double h(Object state) {
14+
NQueensBoard board = (NQueensBoard) state;
15+
return board.getNumberOfAttackingPairs();
16+
}
1717
}

0 commit comments

Comments
 (0)