Skip to content

Commit 4552b80

Browse files
parthkariamaibin
authored andcommitted
BAEL-875 code refactoring (eugenp#2030)
Hill-Climbing refactor
1 parent 920167f commit 4552b80

1 file changed

Lines changed: 53 additions & 34 deletions

File tree

‎algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java‎

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Objects;
66
import java.util.Optional;
77
import java.util.Stack;
8-
import java.util.stream.Collectors;
98

109
public class HillClimbing {
1110
public static void main(String[] args) {
@@ -79,24 +78,31 @@ public State findNextState(State currentState, Stack<String> goalStateStack) {
7978

8079
return listOfStacks.stream()
8180
.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);
9482
})
9583
.filter(Objects::nonNull)
9684
.findFirst()
9785
.orElse(null);
9886
}
9987

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+
100106
/**
101107
* Operation to be applied on a state in order to find new states. This
102108
* operation pushes an element into a new stack
@@ -126,45 +132,58 @@ private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>
126132
Optional<State> newState = currentStackList.stream()
127133
.filter(stack -> stack != currentStack)
128134
.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);
136136
})
137137
.filter(Objects::nonNull)
138138
.findFirst();
139139

140140
return newState.orElse(null);
141141
}
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+
}
142155

143156
/**
144157
* This method returns heuristics value for given state with respect to goal
145158
* state
146159
*/
147160
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
148-
149161
Integer heuristicValue;
150162
heuristicValue = currentState.stream()
151163
.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);
165165
})
166166
.sum();
167167
return heuristicValue;
168168
}
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+
}
169188

170189
}

0 commit comments

Comments
 (0)