Hill-Climbing refactor (#2014)
* Rename test class * HC Refactor * refactor
This commit is contained in:
parent
e59ecbc864
commit
41c35cea8b
|
@ -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();
|
List<Stack<String>> stackList = state.getState();
|
||||||
System.out.println("----------------");
|
System.out.println("----------------");
|
||||||
stackList.forEach(stack -> {
|
stackList.forEach(stack -> {
|
||||||
|
@ -33,8 +33,8 @@ public class HillClimbing {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stack<String> getStackWithValues(String[] blocks) {
|
private Stack<String> getStackWithValues(String[] blocks) {
|
||||||
Stack<String> stack = new Stack<String>();
|
Stack<String> stack = new Stack<>();
|
||||||
for (String block : blocks)
|
for (String block : blocks)
|
||||||
stack.push(block);
|
stack.push(block);
|
||||||
return stack;
|
return stack;
|
||||||
|
@ -42,14 +42,9 @@ public class HillClimbing {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method prepares path from init state to goal state
|
* 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 {
|
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);
|
initStateStackList.add(initStateStack);
|
||||||
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
|
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
|
||||||
State initState = new State(initStateStackList, initStateHeuristics);
|
State initState = new State(initStateStackList, initStateHeuristics);
|
||||||
|
@ -71,27 +66,21 @@ public class HillClimbing {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noStateFound)
|
|
||||||
throw new Exception("No path found");
|
|
||||||
return resultPath;
|
return resultPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method finds new state from current state based on goal and
|
* This method finds new state from current state based on goal and
|
||||||
* heuristics
|
* heuristics
|
||||||
*
|
|
||||||
* @param currentState
|
|
||||||
* @param goalStateStack
|
|
||||||
* @return a next state
|
|
||||||
*/
|
*/
|
||||||
public State findNextState(State currentState, Stack<String> goalStateStack) {
|
public State findNextState(State currentState, Stack<String> goalStateStack) {
|
||||||
List<Stack<String>> listOfStacks = currentState.getState();
|
List<Stack<String>> listOfStacks = currentState.getState();
|
||||||
int currentStateHeuristics = currentState.getHeuristics();
|
int currentStateHeuristics = currentState.getHeuristics();
|
||||||
|
|
||||||
Optional<State> newState = listOfStacks.stream()
|
return listOfStacks.stream()
|
||||||
.map(stack -> {
|
.map(stack -> {
|
||||||
State tempState = null;
|
State tempState;
|
||||||
List<Stack<String>> tempStackList = new ArrayList<Stack<String>>(listOfStacks);
|
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
|
||||||
String block = stack.pop();
|
String block = stack.pop();
|
||||||
if (stack.size() == 0)
|
if (stack.size() == 0)
|
||||||
tempStackList.remove(stack);
|
tempStackList.remove(stack);
|
||||||
|
@ -104,24 +93,17 @@ public class HillClimbing {
|
||||||
return tempState;
|
return tempState;
|
||||||
})
|
})
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.findFirst();
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
return newState.orElse(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Operation to be applied on a state in order to find new states. This
|
* Operation to be applied on a state in order to find new states. This
|
||||||
* operation pushes an element into a new stack
|
* 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) {
|
private State pushElementToNewStack(List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||||
State newState = null;
|
State newState = null;
|
||||||
Stack<String> newStack = new Stack<String>();
|
Stack<String> newStack = new Stack<>();
|
||||||
newStack.push(block);
|
newStack.push(block);
|
||||||
|
|
||||||
currentStackList.add(newStack);
|
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 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
|
* operation pushes an element into one of the other stacks to explore new
|
||||||
* states
|
* 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) {
|
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
|
* This method returns heuristics value for given state with respect to goal
|
||||||
* state
|
* state
|
||||||
*
|
|
||||||
* @param currentState
|
|
||||||
* @param goalStateStack
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
||||||
|
|
||||||
Integer heuristicValue = 0;
|
Integer heuristicValue;
|
||||||
heuristicValue = currentState.stream()
|
heuristicValue = currentState.stream()
|
||||||
.mapToInt(stack -> {
|
.mapToInt(stack -> {
|
||||||
int stackHeuristics = 0;
|
int stackHeuristics = 0;
|
||||||
|
|
|
@ -5,26 +5,23 @@ import java.util.List;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
public class State {
|
public class State {
|
||||||
List<Stack<String>> state;
|
private List<Stack<String>> state;
|
||||||
int heuristics;
|
private int heuristics;
|
||||||
|
|
||||||
public State() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public State(List<Stack<String>> state) {
|
public State(List<Stack<String>> state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public State(List<Stack<String>> state, int heuristics) {
|
State(List<Stack<String>> state, int heuristics) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.heuristics = heuristics;
|
this.heuristics = heuristics;
|
||||||
}
|
}
|
||||||
|
|
||||||
public State(State state) {
|
State(State state) {
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
this.state = new ArrayList<Stack<String>>();
|
this.state = new ArrayList<>();
|
||||||
for (Stack s : state.getState()) {
|
for (Stack s : state.getState()) {
|
||||||
Stack s1 = new Stack();
|
Stack s1;
|
||||||
s1 = (Stack) s.clone();
|
s1 = (Stack) s.clone();
|
||||||
this.state.add(s1);
|
this.state.add(s1);
|
||||||
}
|
}
|
||||||
|
@ -36,10 +33,6 @@ public class State {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setState(List<Stack<String>> state) {
|
|
||||||
this.state = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeuristics() {
|
public int getHeuristics() {
|
||||||
return heuristics;
|
return heuristics;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,17 +14,17 @@ import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class HillClimbingAlgorithmTest {
|
public class HillClimbingAlgorithmTest {
|
||||||
Stack<String> initStack;
|
private Stack<String> initStack;
|
||||||
Stack<String> goalStack;
|
private Stack<String> goalStack;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initStacks() {
|
public void initStacks() {
|
||||||
String blockArr[] = { "B", "C", "D", "A" };
|
String blockArr[] = { "B", "C", "D", "A" };
|
||||||
String goalBlockArr[] = { "A", "B", "C", "D" };
|
String goalBlockArr[] = { "A", "B", "C", "D" };
|
||||||
initStack = new Stack<String>();
|
initStack = new Stack<>();
|
||||||
for (String block : blockArr)
|
for (String block : blockArr)
|
||||||
initStack.push(block);
|
initStack.push(block);
|
||||||
goalStack = new Stack<String>();
|
goalStack = new Stack<>();
|
||||||
for (String block : goalBlockArr)
|
for (String block : goalBlockArr)
|
||||||
goalStack.push(block);
|
goalStack.push(block);
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ public class HillClimbingAlgorithmTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
|
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
|
||||||
HillClimbing hillClimbing = new HillClimbing();
|
HillClimbing hillClimbing = new HillClimbing();
|
||||||
List<Stack<String>> initList = new ArrayList<Stack<String>>();
|
List<Stack<String>> initList = new ArrayList<>();
|
||||||
initList.add(initStack);
|
initList.add(initStack);
|
||||||
State currentState = new State(initList);
|
State currentState = new State(initList);
|
||||||
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));
|
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));
|
||||||
|
|
Loading…
Reference in New Issue