Hill-Climbing refactor (#2014)

* Rename test class

* HC Refactor

* refactor
This commit is contained in:
Grzegorz Piwowarek 2017-06-08 17:04:30 +02:00 committed by maibin
parent e59ecbc864
commit 41c35cea8b
3 changed files with 22 additions and 58 deletions

View File

@ -22,7 +22,7 @@ public class HillClimbing {
}
}
public static void printEachStep(State state) {
private static void printEachStep(State state) {
List<Stack<String>> stackList = state.getState();
System.out.println("----------------");
stackList.forEach(stack -> {
@ -33,8 +33,8 @@ public class HillClimbing {
});
}
public Stack<String> getStackWithValues(String[] blocks) {
Stack<String> stack = new Stack<String>();
private Stack<String> getStackWithValues(String[] blocks) {
Stack<String> stack = new Stack<>();
for (String block : blocks)
stack.push(block);
return stack;
@ -42,14 +42,9 @@ public class HillClimbing {
/**
* This method prepares path from init state to goal state
*
* @param initStateStack
* @param goalStateStack
* @return
* @throws Exception
*/
public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<String> goalStateStack) throws Exception {
List<Stack<String>> initStateStackList = new ArrayList<Stack<String>>();
List<Stack<String>> initStateStackList = new ArrayList<>();
initStateStackList.add(initStateStack);
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
State initState = new State(initStateStackList, initStateHeuristics);
@ -71,27 +66,21 @@ public class HillClimbing {
}
}
if (noStateFound)
throw new Exception("No path found");
return resultPath;
}
/**
* This method finds new state from current state based on goal and
* heuristics
*
* @param currentState
* @param goalStateStack
* @return a next state
*/
public State findNextState(State currentState, Stack<String> goalStateStack) {
List<Stack<String>> listOfStacks = currentState.getState();
int currentStateHeuristics = currentState.getHeuristics();
Optional<State> newState = listOfStacks.stream()
return listOfStacks.stream()
.map(stack -> {
State tempState = null;
List<Stack<String>> tempStackList = new ArrayList<Stack<String>>(listOfStacks);
State tempState;
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
String block = stack.pop();
if (stack.size() == 0)
tempStackList.remove(stack);
@ -104,24 +93,17 @@ public class HillClimbing {
return tempState;
})
.filter(Objects::nonNull)
.findFirst();
return newState.orElse(null);
.findFirst()
.orElse(null);
}
/**
* Operation to be applied on a state in order to find new states. This
* operation pushes an element into a new stack
*
* @param currentStackList
* @param block
* @param currentStateHeuristics
* @param goalStateStack
* @return a new state
*/
private State pushElementToNewStack(List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
State newState = null;
Stack<String> newStack = new Stack<String>();
Stack<String> newStack = new Stack<>();
newStack.push(block);
currentStackList.add(newStack);
@ -138,13 +120,6 @@ public class HillClimbing {
* Operation to be applied on a state in order to find new states. This
* operation pushes an element into one of the other stacks to explore new
* states
*
* @param stack
* @param currentStackList
* @param block
* @param currentStateHeuristics
* @param goalStateStack
* @return a new state
*/
private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
@ -168,14 +143,10 @@ public class HillClimbing {
/**
* This method returns heuristics value for given state with respect to goal
* state
*
* @param currentState
* @param goalStateStack
* @return
*/
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
Integer heuristicValue = 0;
Integer heuristicValue;
heuristicValue = currentState.stream()
.mapToInt(stack -> {
int stackHeuristics = 0;

View File

@ -5,26 +5,23 @@ import java.util.List;
import java.util.Stack;
public class State {
List<Stack<String>> state;
int heuristics;
public State() {
}
private List<Stack<String>> state;
private int heuristics;
public State(List<Stack<String>> state) {
this.state = state;
}
public State(List<Stack<String>> state, int heuristics) {
State(List<Stack<String>> state, int heuristics) {
this.state = state;
this.heuristics = heuristics;
}
public State(State state) {
State(State state) {
if (state != null) {
this.state = new ArrayList<Stack<String>>();
this.state = new ArrayList<>();
for (Stack s : state.getState()) {
Stack s1 = new Stack();
Stack s1;
s1 = (Stack) s.clone();
this.state.add(s1);
}
@ -36,10 +33,6 @@ public class State {
return state;
}
public void setState(List<Stack<String>> state) {
this.state = state;
}
public int getHeuristics() {
return heuristics;
}

View File

@ -14,17 +14,17 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class HillClimbingAlgorithmTest {
Stack<String> initStack;
Stack<String> goalStack;
private Stack<String> initStack;
private Stack<String> goalStack;
@Before
public void initStacks() {
String blockArr[] = { "B", "C", "D", "A" };
String goalBlockArr[] = { "A", "B", "C", "D" };
initStack = new Stack<String>();
initStack = new Stack<>();
for (String block : blockArr)
initStack.push(block);
goalStack = new Stack<String>();
goalStack = new Stack<>();
for (String block : goalBlockArr)
goalStack.push(block);
}
@ -48,7 +48,7 @@ public class HillClimbingAlgorithmTest {
@Test
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
HillClimbing hillClimbing = new HillClimbing();
List<Stack<String>> initList = new ArrayList<Stack<String>>();
List<Stack<String>> initList = new ArrayList<>();
initList.add(initStack);
State currentState = new State(initList);
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));