Skip to content

Commit 41c35ce

Browse files
pivovaritmaibin
authored andcommitted
Hill-Climbing refactor (eugenp#2014)
* Rename test class * HC Refactor * refactor
1 parent e59ecbc commit 41c35ce

3 files changed

Lines changed: 22 additions & 58 deletions

File tree

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

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222
}
2323
}
2424

25-
public static void printEachStep(State state) {
25+
private static void printEachStep(State state) {
2626
List<Stack<String>> stackList = state.getState();
2727
System.out.println("----------------");
2828
stackList.forEach(stack -> {
@@ -33,23 +33,18 @@ public static void printEachStep(State state) {
3333
});
3434
}
3535

36-
public Stack<String> getStackWithValues(String[] blocks) {
37-
Stack<String> stack = new Stack<String>();
36+
private Stack<String> getStackWithValues(String[] blocks) {
37+
Stack<String> stack = new Stack<>();
3838
for (String block : blocks)
3939
stack.push(block);
4040
return stack;
4141
}
4242

4343
/**
4444
* This method prepares path from init state to goal state
45-
*
46-
* @param initStateStack
47-
* @param goalStateStack
48-
* @return
49-
* @throws Exception
5045
*/
5146
public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<String> goalStateStack) throws Exception {
52-
List<Stack<String>> initStateStackList = new ArrayList<Stack<String>>();
47+
List<Stack<String>> initStateStackList = new ArrayList<>();
5348
initStateStackList.add(initStateStack);
5449
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
5550
State initState = new State(initStateStackList, initStateHeuristics);
@@ -71,27 +66,21 @@ public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<
7166
}
7267
}
7368

74-
if (noStateFound)
75-
throw new Exception("No path found");
7669
return resultPath;
7770
}
7871

7972
/**
8073
* This method finds new state from current state based on goal and
8174
* heuristics
82-
*
83-
* @param currentState
84-
* @param goalStateStack
85-
* @return a next state
8675
*/
8776
public State findNextState(State currentState, Stack<String> goalStateStack) {
8877
List<Stack<String>> listOfStacks = currentState.getState();
8978
int currentStateHeuristics = currentState.getHeuristics();
9079

91-
Optional<State> newState = listOfStacks.stream()
80+
return listOfStacks.stream()
9281
.map(stack -> {
93-
State tempState = null;
94-
List<Stack<String>> tempStackList = new ArrayList<Stack<String>>(listOfStacks);
82+
State tempState;
83+
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
9584
String block = stack.pop();
9685
if (stack.size() == 0)
9786
tempStackList.remove(stack);
@@ -104,24 +93,17 @@ public State findNextState(State currentState, Stack<String> goalStateStack) {
10493
return tempState;
10594
})
10695
.filter(Objects::nonNull)
107-
.findFirst();
108-
109-
return newState.orElse(null);
96+
.findFirst()
97+
.orElse(null);
11098
}
11199

112100
/**
113101
* Operation to be applied on a state in order to find new states. This
114102
* operation pushes an element into a new stack
115-
*
116-
* @param currentStackList
117-
* @param block
118-
* @param currentStateHeuristics
119-
* @param goalStateStack
120-
* @return a new state
121103
*/
122104
private State pushElementToNewStack(List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
123105
State newState = null;
124-
Stack<String> newStack = new Stack<String>();
106+
Stack<String> newStack = new Stack<>();
125107
newStack.push(block);
126108

127109
currentStackList.add(newStack);
@@ -138,13 +120,6 @@ private State pushElementToNewStack(List<Stack<String>> currentStackList, String
138120
* Operation to be applied on a state in order to find new states. This
139121
* operation pushes an element into one of the other stacks to explore new
140122
* states
141-
*
142-
* @param stack
143-
* @param currentStackList
144-
* @param block
145-
* @param currentStateHeuristics
146-
* @param goalStateStack
147-
* @return a new state
148123
*/
149124
private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
150125

@@ -168,14 +143,10 @@ private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>
168143
/**
169144
* This method returns heuristics value for given state with respect to goal
170145
* state
171-
*
172-
* @param currentState
173-
* @param goalStateStack
174-
* @return
175146
*/
176147
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
177148

178-
Integer heuristicValue = 0;
149+
Integer heuristicValue;
179150
heuristicValue = currentState.stream()
180151
.mapToInt(stack -> {
181152
int stackHeuristics = 0;

algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
55
import java.util.Stack;
66

77
public class State {
8-
List<Stack<String>> state;
9-
int heuristics;
10-
11-
public State() {
12-
}
8+
private List<Stack<String>> state;
9+
private int heuristics;
1310

1411
public State(List<Stack<String>> state) {
1512
this.state = state;
1613
}
1714

18-
public State(List<Stack<String>> state, int heuristics) {
15+
State(List<Stack<String>> state, int heuristics) {
1916
this.state = state;
2017
this.heuristics = heuristics;
2118
}
2219

23-
public State(State state) {
20+
State(State state) {
2421
if (state != null) {
25-
this.state = new ArrayList<Stack<String>>();
22+
this.state = new ArrayList<>();
2623
for (Stack s : state.getState()) {
27-
Stack s1 = new Stack();
24+
Stack s1;
2825
s1 = (Stack) s.clone();
2926
this.state.add(s1);
3027
}
@@ -36,10 +33,6 @@ public List<Stack<String>> getState() {
3633
return state;
3734
}
3835

39-
public void setState(List<Stack<String>> state) {
40-
this.state = state;
41-
}
42-
4336
public int getHeuristics() {
4437
return heuristics;
4538
}

algorithms/src/test/java/algorithms/HillClimbingAlgorithmTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import static org.junit.Assert.assertTrue;
1515

1616
public class HillClimbingAlgorithmTest {
17-
Stack<String> initStack;
18-
Stack<String> goalStack;
17+
private Stack<String> initStack;
18+
private Stack<String> goalStack;
1919

2020
@Before
2121
public void initStacks() {
2222
String blockArr[] = { "B", "C", "D", "A" };
2323
String goalBlockArr[] = { "A", "B", "C", "D" };
24-
initStack = new Stack<String>();
24+
initStack = new Stack<>();
2525
for (String block : blockArr)
2626
initStack.push(block);
27-
goalStack = new Stack<String>();
27+
goalStack = new Stack<>();
2828
for (String block : goalBlockArr)
2929
goalStack.push(block);
3030
}
@@ -48,7 +48,7 @@ public void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
4848
@Test
4949
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
5050
HillClimbing hillClimbing = new HillClimbing();
51-
List<Stack<String>> initList = new ArrayList<Stack<String>>();
51+
List<Stack<String>> initList = new ArrayList<>();
5252
initList.add(initStack);
5353
State currentState = new State(initList);
5454
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));

0 commit comments

Comments
 (0)