55import java .util .Objects ;
66import java .util .Optional ;
77import java .util .Stack ;
8- import java .util .stream .Collectors ;
98
109public class HillClimbing {
1110 public static void main (String [] args ) {
@@ -22,7 +21,7 @@ public static void main(String[] args) {
2221 }
2322 }
2423
25- public static void printEachStep (State state ) {
24+ private static void printEachStep (State state ) {
2625 List <Stack <String >> stackList = state .getState ();
2726 System .out .println ("----------------" );
2827 stackList .forEach (stack -> {
@@ -33,23 +32,18 @@ public static void printEachStep(State state) {
3332 });
3433 }
3534
36- public Stack <String > getStackWithValues (String [] blocks ) {
37- Stack <String > stack = new Stack <String >();
35+ private Stack <String > getStackWithValues (String [] blocks ) {
36+ Stack <String > stack = new Stack <>();
3837 for (String block : blocks )
3938 stack .push (block );
4039 return stack ;
4140 }
4241
4342 /**
4443 * This method prepares path from init state to goal state
45- *
46- * @param initStateStack
47- * @param goalStateStack
48- * @return
49- * @throws Exception
5044 */
5145 public List <State > getRouteWithHillClimbing (Stack <String > initStateStack , Stack <String > goalStateStack ) throws Exception {
52- List <Stack <String >> initStateStackList = new ArrayList <Stack < String > >();
46+ List <Stack <String >> initStateStackList = new ArrayList <>();
5347 initStateStackList .add (initStateStack );
5448 int initStateHeuristics = getHeuristicsValue (initStateStackList , goalStateStack );
5549 State initState = new State (initStateStackList , initStateHeuristics );
@@ -71,57 +65,51 @@ public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<
7165 }
7266 }
7367
74- if (noStateFound )
75- throw new Exception ("No path found" );
7668 return resultPath ;
7769 }
7870
7971 /**
8072 * This method finds new state from current state based on goal and
8173 * heuristics
82- *
83- * @param currentState
84- * @param goalStateStack
85- * @return a next state
8674 */
8775 public State findNextState (State currentState , Stack <String > goalStateStack ) {
8876 List <Stack <String >> listOfStacks = currentState .getState ();
8977 int currentStateHeuristics = currentState .getHeuristics ();
9078
91- Optional < State > newState = listOfStacks .stream ()
79+ return listOfStacks .stream ()
9280 .map (stack -> {
93- State tempState = null ;
94- List <Stack <String >> tempStackList = new ArrayList <Stack <String >>(listOfStacks );
95- String block = stack .pop ();
96- if (stack .size () == 0 )
97- tempStackList .remove (stack );
98- tempState = pushElementToNewStack (tempStackList , block , currentStateHeuristics , goalStateStack );
99- if (tempState == null ) {
100- tempState = pushElementToExistingStacks (stack , tempStackList , block , currentStateHeuristics , goalStateStack );
101- }
102- if (tempState == null )
103- stack .push (block );
104- return tempState ;
81+ return applyOperationsOnState (listOfStacks , stack , currentStateHeuristics , goalStateStack );
10582 })
10683 .filter (Objects ::nonNull )
107- .findFirst ();
108-
109- return newState .orElse (null );
84+ .findFirst ()
85+ .orElse (null );
11086 }
11187
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+
112106 /**
113107 * Operation to be applied on a state in order to find new states. This
114108 * 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
121109 */
122110 private State pushElementToNewStack (List <Stack <String >> currentStackList , String block , int currentStateHeuristics , Stack <String > goalStateStack ) {
123111 State newState = null ;
124- Stack <String > newStack = new Stack <String >();
112+ Stack <String > newStack = new Stack <>();
125113 newStack .push (block );
126114
127115 currentStackList .add (newStack );
@@ -138,62 +126,64 @@ private State pushElementToNewStack(List<Stack<String>> currentStackList, String
138126 * Operation to be applied on a state in order to find new states. This
139127 * operation pushes an element into one of the other stacks to explore new
140128 * states
141- *
142- * @param stack
143- * @param currentStackList
144- * @param block
145- * @param currentStateHeuristics
146- * @param goalStateStack
147- * @return a new state
148129 */
149130 private State pushElementToExistingStacks (Stack currentStack , List <Stack <String >> currentStackList , String block , int currentStateHeuristics , Stack <String > goalStateStack ) {
150131
151132 Optional <State > newState = currentStackList .stream ()
152133 .filter (stack -> stack != currentStack )
153134 .map (stack -> {
154- stack .push (block );
155- int newStateHeuristics = getHeuristicsValue (currentStackList , goalStateStack );
156- if (newStateHeuristics > currentStateHeuristics ) {
157- return new State (currentStackList , newStateHeuristics );
158- }
159- stack .pop ();
160- return null ;
135+ return pushElementToStack (stack , block , currentStackList , currentStateHeuristics , goalStateStack );
161136 })
162137 .filter (Objects ::nonNull )
163138 .findFirst ();
164139
165140 return newState .orElse (null );
166141 }
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+ }
167155
168156 /**
169157 * This method returns heuristics value for given state with respect to goal
170158 * state
171- *
172- * @param currentState
173- * @param goalStateStack
174- * @return
175159 */
176160 public int getHeuristicsValue (List <Stack <String >> currentState , Stack <String > goalStateStack ) {
177-
178- Integer heuristicValue = 0 ;
161+ Integer heuristicValue ;
179162 heuristicValue = currentState .stream ()
180163 .mapToInt (stack -> {
181- int stackHeuristics = 0 ;
182- boolean isPositioneCorrect = true ;
183- int goalStartIndex = 0 ;
184- for (String currentBlock : stack ) {
185- if (isPositioneCorrect && currentBlock .equals (goalStateStack .get (goalStartIndex ))) {
186- stackHeuristics += goalStartIndex ;
187- } else {
188- stackHeuristics -= goalStartIndex ;
189- isPositioneCorrect = false ;
190- }
191- goalStartIndex ++;
192- }
193- return stackHeuristics ;
164+ return getHeuristicsValueForStack (stack , currentState , goalStateStack );
194165 })
195166 .sum ();
196167 return heuristicValue ;
197168 }
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+ }
198188
199189}
0 commit comments