From 4552b8088c93a2dd670d1024f9d737c6b7e0e5f7 Mon Sep 17 00:00:00 2001 From: Parth Karia Date: Fri, 9 Jun 2017 18:27:29 +0530 Subject: [PATCH] BAEL-875 code refactoring (#2030) Hill-Climbing refactor --- .../algorithms/hillclimbing/HillClimbing.java | 87 +++++++++++-------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java b/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java index 69eaa0580b..d278418a87 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.Stack; -import java.util.stream.Collectors; public class HillClimbing { public static void main(String[] args) { @@ -79,24 +78,31 @@ public class HillClimbing { return listOfStacks.stream() .map(stack -> { - State tempState; - List> tempStackList = new ArrayList<>(listOfStacks); - String block = stack.pop(); - if (stack.size() == 0) - tempStackList.remove(stack); - tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack); - if (tempState == null) { - tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack); - } - if (tempState == null) - stack.push(block); - return tempState; + return applyOperationsOnState(listOfStacks, stack, currentStateHeuristics, goalStateStack); }) .filter(Objects::nonNull) .findFirst() .orElse(null); } + /** + * This method applies operations on the current state to get a new state + */ + public State applyOperationsOnState(List> listOfStacks, Stack stack, int currentStateHeuristics, Stack goalStateStack) { + State tempState; + List> tempStackList = new ArrayList<>(listOfStacks); + String block = stack.pop(); + if (stack.size() == 0) + tempStackList.remove(stack); + tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack); + if (tempState == null) { + tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack); + } + if (tempState == null) + stack.push(block); + return tempState; + } + /** * Operation to be applied on a state in order to find new states. This * operation pushes an element into a new stack @@ -126,45 +132,58 @@ public class HillClimbing { Optional newState = currentStackList.stream() .filter(stack -> stack != currentStack) .map(stack -> { - stack.push(block); - int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack); - if (newStateHeuristics > currentStateHeuristics) { - return new State(currentStackList, newStateHeuristics); - } - stack.pop(); - return null; + return pushElementToStack(stack, block, currentStackList, currentStateHeuristics, goalStateStack); }) .filter(Objects::nonNull) .findFirst(); return newState.orElse(null); } + + /** + * This method pushes a block to the stack and returns new state if its closer to goal + */ + private State pushElementToStack(Stack stack, String block, List> currentStackList, int currentStateHeuristics, Stack goalStateStack) { + stack.push(block); + int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack); + if (newStateHeuristics > currentStateHeuristics) { + return new State(currentStackList, newStateHeuristics); + } + stack.pop(); + return null; + } /** * This method returns heuristics value for given state with respect to goal * state */ public int getHeuristicsValue(List> currentState, Stack goalStateStack) { - Integer heuristicValue; heuristicValue = currentState.stream() .mapToInt(stack -> { - int stackHeuristics = 0; - boolean isPositioneCorrect = true; - int goalStartIndex = 0; - for (String currentBlock : stack) { - if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) { - stackHeuristics += goalStartIndex; - } else { - stackHeuristics -= goalStartIndex; - isPositioneCorrect = false; - } - goalStartIndex++; - } - return stackHeuristics; + return getHeuristicsValueForStack(stack, currentState, goalStateStack); }) .sum(); return heuristicValue; } + + /** + * This method returns heuristics value for a particular stack + */ + public int getHeuristicsValueForStack(Stack stack, List> currentState, Stack goalStateStack) { + int stackHeuristics = 0; + boolean isPositioneCorrect = true; + int goalStartIndex = 0; + for (String currentBlock : stack) { + if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) { + stackHeuristics += goalStartIndex; + } else { + stackHeuristics -= goalStartIndex; + isPositioneCorrect = false; + } + goalStartIndex++; + } + return stackHeuristics; + } }