-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemPancake.java
More file actions
87 lines (55 loc) · 2.8 KB
/
ProblemPancake.java
File metadata and controls
87 lines (55 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.HashSet;
import java.util.Set;
public class ProblemPancake extends Problem {
boolean goal_test(Object state) {
StatePancake pancakeState = (StatePancake) state;
//Final Result Should be a sorted array,
for (int i = 0; i < pancakeState.N - 1; i++) {
if (pancakeState.pancakeArray[i] > pancakeState.pancakeArray[i + 1]) {
return false; // It is proven that the array is not sorted.
}
}
return true;
}
Set<Object> getSuccessors(Object state) {
Set<Object> set = new HashSet<Object>();
StatePancake pancakeState = (StatePancake) state;
StatePancake successorState;
//We can flip Pancakes through Spatula which can be at any n-1 positions.(n-2,n-3....1)
for(int i=1;i<pancakeState.N;i++) {
successorState=new StatePancake(pancakeState);
for (int j = 0; j < (i / 2)+1; j++) {
//flip the array for every possible
int temp = successorState.pancakeArray[j];
successorState.pancakeArray[j] = successorState.pancakeArray[i - j];
successorState.pancakeArray[i - j] = temp;
}
set.add(successorState);
}
return set;
}
double step_cost(Object fromState, Object toState) {
return 1;
}
public double h(Object state) { return 0; }
public static void main(String[] args) throws Exception {
ProblemPancake problem = new ProblemPancake();
//Pass any N number of inputs
int[] pancakeArray = { 5,2,7,4,1,3,6};
problem.initialState = new StatePancake(pancakeArray);
Search search = new Search(problem);
System.out.println("\n\nQ6 c: Pancake sorting Problem - Uninformed Strategies\n");
System.out.println("BreadthFirstTreeSearch:\t\t" + search.BreadthFirstTreeSearch());
System.out.println("BreadthFirstGraphSearch:\t" + search.BreadthFirstGraphSearch());
System.out.println("\nDepthFirstTreeSearch:\t" + search.DepthFirstTreeSearch());
System.out.println("DepthFirstGraphSearch:\t" + search.DepthFirstGraphSearch());
System.out.println("\nUniformCostTreeSearch:\t" + search.UniformCostTreeSearch());
System.out.println("UniformCostGraphSearch:\t" + search.UniformCostGraphSearch());
System.out.println("\nIterativeDeepeningTreeSearch:\t" + search.IterativeDeepeningTreeSearch());
System.out.println("IterativeDeepeningGraphSearch:\t" + search.IterativeDeepeningGraphSearch());
System.out.println("\nGreedyBestFirstTreeSearch:\t" + search.GreedyBestFirstTreeSearch());
System.out.println("\nGreedyBestFirstGraphSearch:\t" + search.GreedyBestFirstGraphSearch());
System.out.println("\nAstarTreeSearch:\t" + search.AstarTreeSearch());
System.out.println("\nAstarGraphSearch:\t" + search.AstarGraphSearch());
}
}