Implementation Assignment
Implementation Assignment
CMPS715
PURPOSE:
Starting first phase of a project that describes a generic algorithm to search for a solution path
in a well formulated search problem. The algorithm is independent of any particular search
strategy and any particular search problem.
IDEA:
The intuitive idea behind the generic search algorithm, given a start node /initial state, and a set of
goal states/nodes, is to incrementally explore paths from the start nodes. However, we do not need
to maintain a tree or graph structure of the explored nodes in order to avoid additional space and
time complexities. We need to find a solution ; i.e. the sequence of actions or transitions that
take us from the initial state to the goal state depending on the search strategy.
search
problem solution
algorithm
The choice of
strategy can be very important as
to how quickly the problem will be solved, if indeed the problem will be solved at all.
In the first phase you are required to start with creating an abstraction of the various search
algorithms and use it to solve a single specific problem, the 8-puzzle problem. Different tests will
be made with different problem difficulties. The search algorithms will be then benchmarked and
compared.
The 8-puzzle problem is a puzzle popularized by Sam Loyd in the 1870s. It is played on a 3-by-3
grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the
blocks so that they are in order.
A state is a representation of a physical configuration of the 8-puzzle board and this may be
considered as a 1x9 array filled with 0, 1, 2, …, 7 or 8, where 0 represents the blank square. A
representation with a matrix of 3x3 is also correct. If you have another suggestion, propose it and
explain your choice.
As for the 1x9 array representation, the given initial state is represented by {7,2,4,5,0,6,8,3,1}.
The given goal state is represented by {0,1,2,3,4,5,6,7,8}.
A successor function: ( state*action--> state) returns the set of states reachable from a specific
state by applying the possible actions. For example, for the state {2, 8, 3, 1, 6, 4, 7, 0, 5},the
function returns the following successors:{2, 8, 3, 1, 0, 4, 7, 6, 5}, {2, 8, 3, 1, 6, 4, 0, 7, 5} and
{2, 8, 3, 1, 6, 4, 7, 5, 0}. These successors correspond to the actions UP, LEFT and RIGHT
respectively, the action DOWN being un-applicable on this state.
MAIN VIEW
We will start with implementing the interfaces and classes for the formulated 8-puzzle problem
then we will move on in parts 2 and 3 of phase I to implement different search algorithms.
PART 1
PROBLEM IMPLEMENTATION
- private Object state the state in the state space to which the node corresponds;
- private Node parent the node in the search tree that generated this node
- private int pathCost the cost of the path from the initial state to the node, as
indicated by the parent pointers.
- private Action action the action that was applied to the parent to generate the node
- constructor that sets the values for the four private member variables
- get... four methods that return the values of the four private member variables
2. Create an interface Action (similar to abstract class it contains only method prototypes
with no implementation here) where given a particular state s it returns the set of actions that
can be executed in s
@Override
public Object apply(Object state) {
int[] s = (int[]) state;
int blankPosition=0;
int newBlankPosition=0;
// you have to implement it
// should specify explicitly how to apply the UP DOWN LEFT RIGHT actions according to the
position of the "0" tile i.e. blank square
//if this action is the UP action, then...
if (name.equals("UP")) {
// then ...getIndex of 0, and subtract 3 from it
}
}
//Assume first that the cost per action is 1
@Override
public int getCostPerAction(){return 1};
4. Create an interface SuccessorFunction containing
Given a particular state s, this method returns the set of possible actions that can be executed for
a given state. We say that each of these actions is applicable in this state.
This method should specify explicitly the set of possible applicable actions given a specific state
for the 8-puzzle problem
- Breadth-First,
- Depth-First,
- Iterative Depth, and
- Uniform Cost, or Lowest Cost First.
- The node data structure: state, path from the root state, the total cost of the path, …
- The queue data structure, which is roughly speaking the bag of nodes to be explored at a
given moment.
Clearly, the queues used by the various search algorithms behave differently in terms of how
they push and pop the nodes, which influences the order in which the nodes are visited.
Specifically, the Uniform Cost algorithm uses a priority queue. The priority function is based on
the cost function g that associates to each state the number of moves made so far to get to the
state.
Search algorithm
Input
Formulated problem p: It should get the Start node (initial state) and check
iteratively for the fulfillment of the goal; i.e. goal state(s)
Output
path from Start node to a node for which goal is true
or false if there are no solution paths
PART 2
2. Create a class BFS to implement the SearchAlgorithm interface as the first specific
uninformed brute-force search method
The uninformed search algorithms should be called to work on problems with various
difficulties, i.e. initial states that are at various distances from the solution.
8 4 862 43 463 4 8
METRICS
- The total number of explored nodes. This is interpreted as a time complexity indicator.
- The maximum queue size, i.e. the maximum number of nodes that existed at the same
time in the queue waiting to be explored. This is interpreted as a space complexity
indicator.
- The sequence of actions allowing to reach the goal from the initial state
- The length of the path from the initial state to the solution, i.e. the length of the sequence
of actions allowing to reach the goal.
First, upgrade the search algorithms architecture to allow considering heuristics. The heuristic
function should be a problem dependent separate entity that is able to evaluate any state of the
problem.
We already defined a SEARCH NODE of the game to be a board, the number of moves made to
reach the board, and the previous search node. First, insert the initial search node (the initial board,
0 moves, and a null previous search node) into a priority queue. Then, delete from the priority
queue the search node with the minimum priority, and insert onto the priority queue all neighboring
search nodes (those that can be reached in one move from the dequeued search node). Repeat this
procedure until the search node dequeued corresponds to a goal board. The success of this approach
hinges on the choice of PRIORITY FUNCTION for a search node. We consider two priority
functions:
- HAMMING PRIORITY FUNCTION. A natural heuristic for a given state is the number
of blocks in the wrong position. Intuitively, states with a small number of blocks in the
wrong position are close to the goal state, and we prefer a search node that have been
reached using a small number of moves.
- MANHATTAN PRIORITY FUNCTION. A better heuristic value for a given state is the
sum of the Manhattan distances (sum of the vertical and horizontal distance) from the
blocks to their goal positions, plus the number of moves made so far to get to the state
(search node).
For example, the Hamming and Manhattan priorities of the initial search node below are 5 and
10, respectively.
8 1 3 1 2 3 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
4 2 4 5 6 ---------------------- ----------------------
7 6 5 7 8 1 1 0 0 1 1 0 1 1 2 0 0 2 2 0 3
IMPLEMENTATION
1 2
3 4 5
6 7 8 goal
OR the
1 2 3
goal
4 5 6
7 8
This will affect the hamming and manhattan distance calculations. Target Xs and Ys will differ
}
- public int calcManhattanDistance (Object currentState)
{ int manhattan = 0;
int [] state = Arrays.copyOf((int[]) currentState,
((int[])currentState).length);