|
5 | 5 | import java.util.Objects; |
6 | 6 | import java.util.Optional; |
7 | 7 | import java.util.Stack; |
8 | | -import java.util.stream.Collectors; |
9 | 8 |
|
10 | 9 | public class HillClimbing { |
11 | 10 | public static void main(String[] args) { |
@@ -79,24 +78,31 @@ public State findNextState(State currentState, Stack<String> goalStateStack) { |
79 | 78 |
|
80 | 79 | return listOfStacks.stream() |
81 | 80 | .map(stack -> { |
82 | | - State tempState; |
83 | | - List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks); |
84 | | - String block = stack.pop(); |
85 | | - if (stack.size() == 0) |
86 | | - tempStackList.remove(stack); |
87 | | - tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack); |
88 | | - if (tempState == null) { |
89 | | - tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack); |
90 | | - } |
91 | | - if (tempState == null) |
92 | | - stack.push(block); |
93 | | - return tempState; |
| 81 | + return applyOperationsOnState(listOfStacks, stack, currentStateHeuristics, goalStateStack); |
94 | 82 | }) |
95 | 83 | .filter(Objects::nonNull) |
96 | 84 | .findFirst() |
97 | 85 | .orElse(null); |
98 | 86 | } |
99 | 87 |
|
| 88 | + /** |
| 89 | + * This method applies operations on the current state to get a new state |
| 90 | + */ |
| 91 | + public State applyOperationsOnState(List<Stack<String>> listOfStacks, Stack<String> stack, int currentStateHeuristics, Stack<String> goalStateStack) { |
| 92 | + State tempState; |
| 93 | + List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks); |
| 94 | + String block = stack.pop(); |
| 95 | + if (stack.size() == 0) |
| 96 | + tempStackList.remove(stack); |
| 97 | + tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack); |
| 98 | + if (tempState == null) { |
| 99 | + tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack); |
| 100 | + } |
| 101 | + if (tempState == null) |
| 102 | + stack.push(block); |
| 103 | + return tempState; |
| 104 | + } |
| 105 | + |
100 | 106 | /** |
101 | 107 | * Operation to be applied on a state in order to find new states. This |
102 | 108 | * operation pushes an element into a new stack |
@@ -126,45 +132,58 @@ private State pushElementToExistingStacks(Stack currentStack, List<Stack<String> |
126 | 132 | Optional<State> newState = currentStackList.stream() |
127 | 133 | .filter(stack -> stack != currentStack) |
128 | 134 | .map(stack -> { |
129 | | - stack.push(block); |
130 | | - int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack); |
131 | | - if (newStateHeuristics > currentStateHeuristics) { |
132 | | - return new State(currentStackList, newStateHeuristics); |
133 | | - } |
134 | | - stack.pop(); |
135 | | - return null; |
| 135 | + return pushElementToStack(stack, block, currentStackList, currentStateHeuristics, goalStateStack); |
136 | 136 | }) |
137 | 137 | .filter(Objects::nonNull) |
138 | 138 | .findFirst(); |
139 | 139 |
|
140 | 140 | return newState.orElse(null); |
141 | 141 | } |
| 142 | + |
| 143 | + /** |
| 144 | + * This method pushes a block to the stack and returns new state if its closer to goal |
| 145 | + */ |
| 146 | + private State pushElementToStack(Stack stack, String block, List<Stack<String>> currentStackList, int currentStateHeuristics, Stack<String> goalStateStack) { |
| 147 | + stack.push(block); |
| 148 | + int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack); |
| 149 | + if (newStateHeuristics > currentStateHeuristics) { |
| 150 | + return new State(currentStackList, newStateHeuristics); |
| 151 | + } |
| 152 | + stack.pop(); |
| 153 | + return null; |
| 154 | + } |
142 | 155 |
|
143 | 156 | /** |
144 | 157 | * This method returns heuristics value for given state with respect to goal |
145 | 158 | * state |
146 | 159 | */ |
147 | 160 | public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) { |
148 | | - |
149 | 161 | Integer heuristicValue; |
150 | 162 | heuristicValue = currentState.stream() |
151 | 163 | .mapToInt(stack -> { |
152 | | - int stackHeuristics = 0; |
153 | | - boolean isPositioneCorrect = true; |
154 | | - int goalStartIndex = 0; |
155 | | - for (String currentBlock : stack) { |
156 | | - if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) { |
157 | | - stackHeuristics += goalStartIndex; |
158 | | - } else { |
159 | | - stackHeuristics -= goalStartIndex; |
160 | | - isPositioneCorrect = false; |
161 | | - } |
162 | | - goalStartIndex++; |
163 | | - } |
164 | | - return stackHeuristics; |
| 164 | + return getHeuristicsValueForStack(stack, currentState, goalStateStack); |
165 | 165 | }) |
166 | 166 | .sum(); |
167 | 167 | return heuristicValue; |
168 | 168 | } |
| 169 | + |
| 170 | + /** |
| 171 | + * This method returns heuristics value for a particular stack |
| 172 | + */ |
| 173 | + public int getHeuristicsValueForStack(Stack<String> stack, List<Stack<String>> currentState, Stack<String> goalStateStack) { |
| 174 | + int stackHeuristics = 0; |
| 175 | + boolean isPositioneCorrect = true; |
| 176 | + int goalStartIndex = 0; |
| 177 | + for (String currentBlock : stack) { |
| 178 | + if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) { |
| 179 | + stackHeuristics += goalStartIndex; |
| 180 | + } else { |
| 181 | + stackHeuristics -= goalStartIndex; |
| 182 | + isPositioneCorrect = false; |
| 183 | + } |
| 184 | + goalStartIndex++; |
| 185 | + } |
| 186 | + return stackHeuristics; |
| 187 | + } |
169 | 188 |
|
170 | 189 | } |
0 commit comments