parent
920167f5b7
commit
4552b8088c
@ -5,7 +5,6 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class HillClimbing {
|
public class HillClimbing {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -79,6 +78,17 @@ public class HillClimbing {
|
|||||||
|
|
||||||
return listOfStacks.stream()
|
return listOfStacks.stream()
|
||||||
.map(stack -> {
|
.map(stack -> {
|
||||||
|
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<Stack<String>> listOfStacks, Stack<String> stack, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||||
State tempState;
|
State tempState;
|
||||||
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
|
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
|
||||||
String block = stack.pop();
|
String block = stack.pop();
|
||||||
@ -91,10 +101,6 @@ public class HillClimbing {
|
|||||||
if (tempState == null)
|
if (tempState == null)
|
||||||
stack.push(block);
|
stack.push(block);
|
||||||
return tempState;
|
return tempState;
|
||||||
})
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.findFirst()
|
|
||||||
.orElse(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,13 +132,7 @@ public class HillClimbing {
|
|||||||
Optional<State> newState = currentStackList.stream()
|
Optional<State> newState = currentStackList.stream()
|
||||||
.filter(stack -> stack != currentStack)
|
.filter(stack -> stack != currentStack)
|
||||||
.map(stack -> {
|
.map(stack -> {
|
||||||
stack.push(block);
|
return pushElementToStack(stack, block, currentStackList, currentStateHeuristics, goalStateStack);
|
||||||
int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack);
|
|
||||||
if (newStateHeuristics > currentStateHeuristics) {
|
|
||||||
return new State(currentStackList, newStateHeuristics);
|
|
||||||
}
|
|
||||||
stack.pop();
|
|
||||||
return null;
|
|
||||||
})
|
})
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.findFirst();
|
.findFirst();
|
||||||
@ -140,15 +140,37 @@ public class HillClimbing {
|
|||||||
return newState.orElse(null);
|
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<Stack<String>> currentStackList, int currentStateHeuristics, Stack<String> 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
|
* This method returns heuristics value for given state with respect to goal
|
||||||
* state
|
* state
|
||||||
*/
|
*/
|
||||||
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
||||||
|
|
||||||
Integer heuristicValue;
|
Integer heuristicValue;
|
||||||
heuristicValue = currentState.stream()
|
heuristicValue = currentState.stream()
|
||||||
.mapToInt(stack -> {
|
.mapToInt(stack -> {
|
||||||
|
return getHeuristicsValueForStack(stack, currentState, goalStateStack);
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
return heuristicValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns heuristics value for a particular stack
|
||||||
|
*/
|
||||||
|
public int getHeuristicsValueForStack(Stack<String> stack, List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
||||||
int stackHeuristics = 0;
|
int stackHeuristics = 0;
|
||||||
boolean isPositioneCorrect = true;
|
boolean isPositioneCorrect = true;
|
||||||
int goalStartIndex = 0;
|
int goalStartIndex = 0;
|
||||||
@ -162,9 +184,6 @@ public class HillClimbing {
|
|||||||
goalStartIndex++;
|
goalStartIndex++;
|
||||||
}
|
}
|
||||||
return stackHeuristics;
|
return stackHeuristics;
|
||||||
})
|
|
||||||
.sum();
|
|
||||||
return heuristicValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user