[BAEL-9551] - Splitted algorithms into 4 modules
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* Finite state machine.
|
||||
*/
|
||||
public interface FiniteStateMachine {
|
||||
|
||||
/**
|
||||
* Follow a transition, switch the state of the machine.
|
||||
* @param c Char.
|
||||
* @return A new finite state machine with the new state.
|
||||
*/
|
||||
FiniteStateMachine switchState(final CharSequence c);
|
||||
|
||||
/**
|
||||
* Is the current state a final one?
|
||||
* @return true or false.
|
||||
*/
|
||||
boolean canStop();
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* Default implementation of a finite state machine.
|
||||
* This class is immutable and thread-safe.
|
||||
*/
|
||||
public final class RtFiniteStateMachine implements FiniteStateMachine {
|
||||
|
||||
/**
|
||||
* Current state.
|
||||
*/
|
||||
private State current;
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
* @param initial Initial state of this machine.
|
||||
*/
|
||||
public RtFiniteStateMachine(final State initial) {
|
||||
this.current = initial;
|
||||
}
|
||||
|
||||
public FiniteStateMachine switchState(final CharSequence c) {
|
||||
return new RtFiniteStateMachine(this.current.transit(c));
|
||||
}
|
||||
|
||||
public boolean canStop() {
|
||||
return this.current.isFinal();
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* State in a finite state machine.
|
||||
*/
|
||||
public final class RtState implements State {
|
||||
|
||||
private List<Transition> transitions;
|
||||
private boolean isFinal;
|
||||
|
||||
public RtState() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public RtState(final boolean isFinal) {
|
||||
this.transitions = new ArrayList<>();
|
||||
this.isFinal = isFinal;
|
||||
}
|
||||
|
||||
public State transit(final CharSequence c) {
|
||||
return transitions
|
||||
.stream()
|
||||
.filter(t -> t.isPossible(c))
|
||||
.map(Transition::state)
|
||||
.findAny()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Input not accepted: " + c));
|
||||
}
|
||||
|
||||
public boolean isFinal() {
|
||||
return this.isFinal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public State with(Transition tr) {
|
||||
this.transitions.add(tr);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
|
||||
/**
|
||||
* Transition in finite state machine.
|
||||
*/
|
||||
public final class RtTransition implements Transition {
|
||||
|
||||
private String rule;
|
||||
private State next;
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
* @param rule Rule that a character has to meet
|
||||
* in order to get to the next state.
|
||||
* @param next Next state.
|
||||
*/
|
||||
public RtTransition (String rule, State next) {
|
||||
this.rule = rule;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public State state() {
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public boolean isPossible(CharSequence c) {
|
||||
return this.rule.equalsIgnoreCase(String.valueOf(c));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* State. Part of a finite state machine.
|
||||
*/
|
||||
public interface State {
|
||||
|
||||
/**
|
||||
* Add a Transition to this state.
|
||||
* @param tr Given transition.
|
||||
* @return Modified State.
|
||||
*/
|
||||
State with(final Transition tr);
|
||||
|
||||
/**
|
||||
* Follow one of the transitions, to get
|
||||
* to the next state.
|
||||
* @param c Character.
|
||||
* @return State.
|
||||
* @throws IllegalStateException if the char is not accepted.
|
||||
*/
|
||||
State transit(final CharSequence c);
|
||||
|
||||
/**
|
||||
* Can the automaton stop on this state?
|
||||
* @return true or false
|
||||
*/
|
||||
boolean isFinal();
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* Transition in a finite State machine.
|
||||
*/
|
||||
public interface Transition {
|
||||
|
||||
/**
|
||||
* Is the transition possible with the given character?
|
||||
* @param c char.
|
||||
* @return true or false.
|
||||
*/
|
||||
boolean isPossible(final CharSequence c);
|
||||
|
||||
/**
|
||||
* The state to which this transition leads.
|
||||
* @return State.
|
||||
*/
|
||||
State state();
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.algorithms.binarysearch;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class BinarySearch {
|
||||
|
||||
public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
|
||||
|
||||
int index = Integer.MAX_VALUE;
|
||||
|
||||
while (low <= high) {
|
||||
|
||||
int mid = (low + high) / 2;
|
||||
|
||||
if (sortedArray[mid] < key) {
|
||||
low = mid + 1;
|
||||
} else if (sortedArray[mid] > key) {
|
||||
high = mid - 1;
|
||||
} else if (sortedArray[mid] == key) {
|
||||
index = mid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
|
||||
|
||||
int middle = (low + high) / 2;
|
||||
if (high < low) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (key == sortedArray[middle]) {
|
||||
return middle;
|
||||
} else if (key < sortedArray[middle]) {
|
||||
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
|
||||
} else {
|
||||
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
|
||||
int index = Arrays.binarySearch(sortedArray, key);
|
||||
return index;
|
||||
}
|
||||
|
||||
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
|
||||
int index = Collections.binarySearch(sortedList, key);
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
package com.baeldung.algorithms.hillclimbing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Stack;
|
||||
|
||||
public class HillClimbing {
|
||||
public static void main(String[] args) {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
String blockArr[] = { "B", "C", "D", "A" };
|
||||
Stack<String> startState = hillClimbing.getStackWithValues(blockArr);
|
||||
String goalBlockArr[] = { "A", "B", "C", "D" };
|
||||
Stack<String> goalState = hillClimbing.getStackWithValues(goalBlockArr);
|
||||
try {
|
||||
List<State> solutionSequence = hillClimbing.getRouteWithHillClimbing(startState, goalState);
|
||||
solutionSequence.forEach(HillClimbing::printEachStep);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printEachStep(State state) {
|
||||
List<Stack<String>> stackList = state.getState();
|
||||
System.out.println("----------------");
|
||||
stackList.forEach(stack -> {
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.println(stack.pop());
|
||||
}
|
||||
System.out.println(" ");
|
||||
});
|
||||
}
|
||||
|
||||
private Stack<String> getStackWithValues(String[] blocks) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
for (String block : blocks)
|
||||
stack.push(block);
|
||||
return stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method prepares path from init state to goal state
|
||||
*/
|
||||
public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<String> goalStateStack) throws Exception {
|
||||
List<Stack<String>> initStateStackList = new ArrayList<>();
|
||||
initStateStackList.add(initStateStack);
|
||||
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
|
||||
State initState = new State(initStateStackList, initStateHeuristics);
|
||||
|
||||
List<State> resultPath = new ArrayList<>();
|
||||
resultPath.add(new State(initState));
|
||||
|
||||
State currentState = initState;
|
||||
boolean noStateFound = false;
|
||||
while (!currentState.getState()
|
||||
.get(0)
|
||||
.equals(goalStateStack) || noStateFound) {
|
||||
noStateFound = true;
|
||||
State nextState = findNextState(currentState, goalStateStack);
|
||||
if (nextState != null) {
|
||||
noStateFound = false;
|
||||
currentState = nextState;
|
||||
resultPath.add(new State(nextState));
|
||||
}
|
||||
}
|
||||
|
||||
return resultPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method finds new state from current state based on goal and
|
||||
* heuristics
|
||||
*/
|
||||
public State findNextState(State currentState, Stack<String> goalStateStack) {
|
||||
List<Stack<String>> listOfStacks = currentState.getState();
|
||||
int currentStateHeuristics = currentState.getHeuristics();
|
||||
|
||||
return listOfStacks.stream()
|
||||
.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;
|
||||
List<Stack<String>> 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
|
||||
*/
|
||||
private State pushElementToNewStack(List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||
State newState = null;
|
||||
Stack<String> newStack = new Stack<>();
|
||||
newStack.push(block);
|
||||
|
||||
currentStackList.add(newStack);
|
||||
int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack);
|
||||
if (newStateHeuristics > currentStateHeuristics) {
|
||||
newState = new State(currentStackList, newStateHeuristics);
|
||||
} else {
|
||||
currentStackList.remove(newStack);
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||
|
||||
Optional<State> newState = currentStackList.stream()
|
||||
.filter(stack -> stack != currentStack)
|
||||
.map(stack -> {
|
||||
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<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
|
||||
* state
|
||||
*/
|
||||
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
||||
Integer heuristicValue;
|
||||
heuristicValue = currentState.stream()
|
||||
.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;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.algorithms.hillclimbing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
public class State {
|
||||
private List<Stack<String>> state;
|
||||
private int heuristics;
|
||||
|
||||
public State(List<Stack<String>> state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
State(List<Stack<String>> state, int heuristics) {
|
||||
this.state = state;
|
||||
this.heuristics = heuristics;
|
||||
}
|
||||
|
||||
State(State state) {
|
||||
if (state != null) {
|
||||
this.state = new ArrayList<>();
|
||||
for (Stack s : state.getState()) {
|
||||
Stack s1;
|
||||
s1 = (Stack) s.clone();
|
||||
this.state.add(s1);
|
||||
}
|
||||
this.heuristics = state.getHeuristics();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Stack<String>> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public int getHeuristics() {
|
||||
return heuristics;
|
||||
}
|
||||
|
||||
public void setHeuristics(int heuristics) {
|
||||
this.heuristics = heuristics;
|
||||
}
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
package com.baeldung.algorithms.kthlargest;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class FindKthLargest {
|
||||
|
||||
public int findKthLargestBySorting(Integer[] arr, int k) {
|
||||
Arrays.sort(arr);
|
||||
int targetIndex = arr.length - k;
|
||||
return arr[targetIndex];
|
||||
}
|
||||
|
||||
public int findKthLargestBySortingDesc(Integer[] arr, int k) {
|
||||
Arrays.sort(arr, Collections.reverseOrder());
|
||||
return arr[k - 1];
|
||||
}
|
||||
|
||||
public int findKthElementByQuickSelect(Integer[] arr, int left, int right, int k) {
|
||||
if (k >= 0 && k <= right - left + 1) {
|
||||
int pos = partition(arr, left, right);
|
||||
if (pos - left == k) {
|
||||
return arr[pos];
|
||||
}
|
||||
if (pos - left > k) {
|
||||
return findKthElementByQuickSelect(arr, left, pos - 1, k);
|
||||
}
|
||||
return findKthElementByQuickSelect(arr, pos + 1, right, k - pos + left - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int findKthElementByQuickSelectWithIterativePartition(Integer[] arr, int left, int right, int k) {
|
||||
if (k >= 0 && k <= right - left + 1) {
|
||||
int pos = partitionIterative(arr, left, right);
|
||||
if (pos - left == k) {
|
||||
return arr[pos];
|
||||
}
|
||||
if (pos - left > k) {
|
||||
return findKthElementByQuickSelectWithIterativePartition(arr, left, pos - 1, k);
|
||||
}
|
||||
return findKthElementByQuickSelectWithIterativePartition(arr, pos + 1, right, k - pos + left - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int partition(Integer[] arr, int left, int right) {
|
||||
int pivot = arr[right];
|
||||
Integer[] leftArr;
|
||||
Integer[] rightArr;
|
||||
|
||||
leftArr = IntStream.range(left, right)
|
||||
.filter(i -> arr[i] < pivot)
|
||||
.map(i -> arr[i])
|
||||
.boxed()
|
||||
.toArray(Integer[]::new);
|
||||
|
||||
rightArr = IntStream.range(left, right)
|
||||
.filter(i -> arr[i] > pivot)
|
||||
.map(i -> arr[i])
|
||||
.boxed()
|
||||
.toArray(Integer[]::new);
|
||||
|
||||
int leftArraySize = leftArr.length;
|
||||
System.arraycopy(leftArr, 0, arr, left, leftArraySize);
|
||||
arr[leftArraySize + left] = pivot;
|
||||
System.arraycopy(rightArr, 0, arr, left + leftArraySize + 1, rightArr.length);
|
||||
|
||||
return left + leftArraySize;
|
||||
}
|
||||
|
||||
private int partitionIterative(Integer[] arr, int left, int right) {
|
||||
int pivot = arr[right], i = left;
|
||||
for (int j = left; j <= right - 1; j++) {
|
||||
if (arr[j] <= pivot) {
|
||||
swap(arr, i, j);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
swap(arr, i, right);
|
||||
return i;
|
||||
}
|
||||
|
||||
public int findKthElementByRandomizedQuickSelect(Integer[] arr, int left, int right, int k) {
|
||||
if (k >= 0 && k <= right - left + 1) {
|
||||
int pos = randomPartition(arr, left, right);
|
||||
if (pos - left == k) {
|
||||
return arr[pos];
|
||||
}
|
||||
if (pos - left > k) {
|
||||
return findKthElementByRandomizedQuickSelect(arr, left, pos - 1, k);
|
||||
}
|
||||
return findKthElementByRandomizedQuickSelect(arr, pos + 1, right, k - pos + left - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int randomPartition(Integer arr[], int left, int right) {
|
||||
int n = right - left + 1;
|
||||
int pivot = (int) (Math.random() * n);
|
||||
swap(arr, left + pivot, right);
|
||||
return partition(arr, left, right);
|
||||
}
|
||||
|
||||
private void swap(Integer[] arr, int n1, int n2) {
|
||||
int temp = arr[n2];
|
||||
arr[n2] = arr[n1];
|
||||
arr[n1] = temp;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.linesintersection;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LinesIntersectionService {
|
||||
|
||||
public Optional<Point> calculateIntersectionPoint(double m1, double b1, double m2, double b2) {
|
||||
|
||||
if (m1 == m2) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
double x = (b2 - b1) / (m1 - m2);
|
||||
double y = m1 * x + b1;
|
||||
|
||||
Point point = new Point();
|
||||
point.setLocation(x, y);
|
||||
return Optional.of(point);
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package com.baeldung.algorithms.mcts.montecarlo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Board;
|
||||
import com.baeldung.algorithms.mcts.tree.Node;
|
||||
import com.baeldung.algorithms.mcts.tree.Tree;
|
||||
|
||||
public class MonteCarloTreeSearch {
|
||||
|
||||
private static final int WIN_SCORE = 10;
|
||||
private int level;
|
||||
private int opponent;
|
||||
|
||||
public MonteCarloTreeSearch() {
|
||||
this.level = 3;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
private int getMillisForCurrentLevel() {
|
||||
return 2 * (this.level - 1) + 1;
|
||||
}
|
||||
|
||||
public Board findNextMove(Board board, int playerNo) {
|
||||
long start = System.currentTimeMillis();
|
||||
long end = start + 60 * getMillisForCurrentLevel();
|
||||
|
||||
opponent = 3 - playerNo;
|
||||
Tree tree = new Tree();
|
||||
Node rootNode = tree.getRoot();
|
||||
rootNode.getState().setBoard(board);
|
||||
rootNode.getState().setPlayerNo(opponent);
|
||||
|
||||
while (System.currentTimeMillis() < end) {
|
||||
// Phase 1 - Selection
|
||||
Node promisingNode = selectPromisingNode(rootNode);
|
||||
// Phase 2 - Expansion
|
||||
if (promisingNode.getState().getBoard().checkStatus() == Board.IN_PROGRESS)
|
||||
expandNode(promisingNode);
|
||||
|
||||
// Phase 3 - Simulation
|
||||
Node nodeToExplore = promisingNode;
|
||||
if (promisingNode.getChildArray().size() > 0) {
|
||||
nodeToExplore = promisingNode.getRandomChildNode();
|
||||
}
|
||||
int playoutResult = simulateRandomPlayout(nodeToExplore);
|
||||
// Phase 4 - Update
|
||||
backPropogation(nodeToExplore, playoutResult);
|
||||
}
|
||||
|
||||
Node winnerNode = rootNode.getChildWithMaxScore();
|
||||
tree.setRoot(winnerNode);
|
||||
return winnerNode.getState().getBoard();
|
||||
}
|
||||
|
||||
private Node selectPromisingNode(Node rootNode) {
|
||||
Node node = rootNode;
|
||||
while (node.getChildArray().size() != 0) {
|
||||
node = UCT.findBestNodeWithUCT(node);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private void expandNode(Node node) {
|
||||
List<State> possibleStates = node.getState().getAllPossibleStates();
|
||||
possibleStates.forEach(state -> {
|
||||
Node newNode = new Node(state);
|
||||
newNode.setParent(node);
|
||||
newNode.getState().setPlayerNo(node.getState().getOpponent());
|
||||
node.getChildArray().add(newNode);
|
||||
});
|
||||
}
|
||||
|
||||
private void backPropogation(Node nodeToExplore, int playerNo) {
|
||||
Node tempNode = nodeToExplore;
|
||||
while (tempNode != null) {
|
||||
tempNode.getState().incrementVisit();
|
||||
if (tempNode.getState().getPlayerNo() == playerNo)
|
||||
tempNode.getState().addScore(WIN_SCORE);
|
||||
tempNode = tempNode.getParent();
|
||||
}
|
||||
}
|
||||
|
||||
private int simulateRandomPlayout(Node node) {
|
||||
Node tempNode = new Node(node);
|
||||
State tempState = tempNode.getState();
|
||||
int boardStatus = tempState.getBoard().checkStatus();
|
||||
|
||||
if (boardStatus == opponent) {
|
||||
tempNode.getParent().getState().setWinScore(Integer.MIN_VALUE);
|
||||
return boardStatus;
|
||||
}
|
||||
while (boardStatus == Board.IN_PROGRESS) {
|
||||
tempState.togglePlayer();
|
||||
tempState.randomPlay();
|
||||
boardStatus = tempState.getBoard().checkStatus();
|
||||
}
|
||||
|
||||
return boardStatus;
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.algorithms.mcts.montecarlo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Board;
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Position;
|
||||
|
||||
public class State {
|
||||
private Board board;
|
||||
private int playerNo;
|
||||
private int visitCount;
|
||||
private double winScore;
|
||||
|
||||
public State() {
|
||||
board = new Board();
|
||||
}
|
||||
|
||||
public State(State state) {
|
||||
this.board = new Board(state.getBoard());
|
||||
this.playerNo = state.getPlayerNo();
|
||||
this.visitCount = state.getVisitCount();
|
||||
this.winScore = state.getWinScore();
|
||||
}
|
||||
|
||||
public State(Board board) {
|
||||
this.board = new Board(board);
|
||||
}
|
||||
|
||||
Board getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
void setBoard(Board board) {
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
int getPlayerNo() {
|
||||
return playerNo;
|
||||
}
|
||||
|
||||
void setPlayerNo(int playerNo) {
|
||||
this.playerNo = playerNo;
|
||||
}
|
||||
|
||||
int getOpponent() {
|
||||
return 3 - playerNo;
|
||||
}
|
||||
|
||||
public int getVisitCount() {
|
||||
return visitCount;
|
||||
}
|
||||
|
||||
public void setVisitCount(int visitCount) {
|
||||
this.visitCount = visitCount;
|
||||
}
|
||||
|
||||
double getWinScore() {
|
||||
return winScore;
|
||||
}
|
||||
|
||||
void setWinScore(double winScore) {
|
||||
this.winScore = winScore;
|
||||
}
|
||||
|
||||
public List<State> getAllPossibleStates() {
|
||||
List<State> possibleStates = new ArrayList<>();
|
||||
List<Position> availablePositions = this.board.getEmptyPositions();
|
||||
availablePositions.forEach(p -> {
|
||||
State newState = new State(this.board);
|
||||
newState.setPlayerNo(3 - this.playerNo);
|
||||
newState.getBoard().performMove(newState.getPlayerNo(), p);
|
||||
possibleStates.add(newState);
|
||||
});
|
||||
return possibleStates;
|
||||
}
|
||||
|
||||
void incrementVisit() {
|
||||
this.visitCount++;
|
||||
}
|
||||
|
||||
void addScore(double score) {
|
||||
if (this.winScore != Integer.MIN_VALUE)
|
||||
this.winScore += score;
|
||||
}
|
||||
|
||||
void randomPlay() {
|
||||
List<Position> availablePositions = this.board.getEmptyPositions();
|
||||
int totalPossibilities = availablePositions.size();
|
||||
int selectRandom = (int) (Math.random() * totalPossibilities);
|
||||
this.board.performMove(this.playerNo, availablePositions.get(selectRandom));
|
||||
}
|
||||
|
||||
void togglePlayer() {
|
||||
this.playerNo = 3 - this.playerNo;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.algorithms.mcts.montecarlo;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.algorithms.mcts.tree.Node;
|
||||
|
||||
public class UCT {
|
||||
|
||||
public static double uctValue(int totalVisit, double nodeWinScore, int nodeVisit) {
|
||||
if (nodeVisit == 0) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
return (nodeWinScore / (double) nodeVisit) + 1.41 * Math.sqrt(Math.log(totalVisit) / (double) nodeVisit);
|
||||
}
|
||||
|
||||
static Node findBestNodeWithUCT(Node node) {
|
||||
int parentVisit = node.getState().getVisitCount();
|
||||
return Collections.max(
|
||||
node.getChildArray(),
|
||||
Comparator.comparing(c -> uctValue(parentVisit, c.getState().getWinScore(), c.getState().getVisitCount())));
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
package com.baeldung.algorithms.mcts.tictactoe;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Board {
|
||||
int[][] boardValues;
|
||||
int totalMoves;
|
||||
|
||||
public static final int DEFAULT_BOARD_SIZE = 3;
|
||||
|
||||
public static final int IN_PROGRESS = -1;
|
||||
public static final int DRAW = 0;
|
||||
public static final int P1 = 1;
|
||||
public static final int P2 = 2;
|
||||
|
||||
public Board() {
|
||||
boardValues = new int[DEFAULT_BOARD_SIZE][DEFAULT_BOARD_SIZE];
|
||||
}
|
||||
|
||||
public Board(int boardSize) {
|
||||
boardValues = new int[boardSize][boardSize];
|
||||
}
|
||||
|
||||
public Board(int[][] boardValues) {
|
||||
this.boardValues = boardValues;
|
||||
}
|
||||
|
||||
public Board(int[][] boardValues, int totalMoves) {
|
||||
this.boardValues = boardValues;
|
||||
this.totalMoves = totalMoves;
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
int boardLength = board.getBoardValues().length;
|
||||
this.boardValues = new int[boardLength][boardLength];
|
||||
int[][] boardValues = board.getBoardValues();
|
||||
int n = boardValues.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int m = boardValues[i].length;
|
||||
for (int j = 0; j < m; j++) {
|
||||
this.boardValues[i][j] = boardValues[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void performMove(int player, Position p) {
|
||||
this.totalMoves++;
|
||||
boardValues[p.getX()][p.getY()] = player;
|
||||
}
|
||||
|
||||
public int[][] getBoardValues() {
|
||||
return boardValues;
|
||||
}
|
||||
|
||||
public void setBoardValues(int[][] boardValues) {
|
||||
this.boardValues = boardValues;
|
||||
}
|
||||
|
||||
public int checkStatus() {
|
||||
int boardSize = boardValues.length;
|
||||
int maxIndex = boardSize - 1;
|
||||
int[] diag1 = new int[boardSize];
|
||||
int[] diag2 = new int[boardSize];
|
||||
|
||||
for (int i = 0; i < boardSize; i++) {
|
||||
int[] row = boardValues[i];
|
||||
int[] col = new int[boardSize];
|
||||
for (int j = 0; j < boardSize; j++) {
|
||||
col[j] = boardValues[j][i];
|
||||
}
|
||||
|
||||
int checkRowForWin = checkForWin(row);
|
||||
if(checkRowForWin!=0)
|
||||
return checkRowForWin;
|
||||
|
||||
int checkColForWin = checkForWin(col);
|
||||
if(checkColForWin!=0)
|
||||
return checkColForWin;
|
||||
|
||||
diag1[i] = boardValues[i][i];
|
||||
diag2[i] = boardValues[maxIndex - i][i];
|
||||
}
|
||||
|
||||
int checkDia1gForWin = checkForWin(diag1);
|
||||
if(checkDia1gForWin!=0)
|
||||
return checkDia1gForWin;
|
||||
|
||||
int checkDiag2ForWin = checkForWin(diag2);
|
||||
if(checkDiag2ForWin!=0)
|
||||
return checkDiag2ForWin;
|
||||
|
||||
if (getEmptyPositions().size() > 0)
|
||||
return IN_PROGRESS;
|
||||
else
|
||||
return DRAW;
|
||||
}
|
||||
|
||||
private int checkForWin(int[] row) {
|
||||
boolean isEqual = true;
|
||||
int size = row.length;
|
||||
int previous = row[0];
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (previous != row[i]) {
|
||||
isEqual = false;
|
||||
break;
|
||||
}
|
||||
previous = row[i];
|
||||
}
|
||||
if(isEqual)
|
||||
return previous;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void printBoard() {
|
||||
int size = this.boardValues.length;
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
System.out.print(boardValues[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Position> getEmptyPositions() {
|
||||
int size = this.boardValues.length;
|
||||
List<Position> emptyPositions = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (boardValues[i][j] == 0)
|
||||
emptyPositions.add(new Position(i, j));
|
||||
}
|
||||
}
|
||||
return emptyPositions;
|
||||
}
|
||||
|
||||
public void printStatus() {
|
||||
switch (this.checkStatus()) {
|
||||
case P1:
|
||||
System.out.println("Player 1 wins");
|
||||
break;
|
||||
case P2:
|
||||
System.out.println("Player 2 wins");
|
||||
break;
|
||||
case DRAW:
|
||||
System.out.println("Game Draw");
|
||||
break;
|
||||
case IN_PROGRESS:
|
||||
System.out.println("Game In Progress");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.mcts.tictactoe;
|
||||
|
||||
public class Position {
|
||||
int x;
|
||||
int y;
|
||||
|
||||
public Position() {
|
||||
}
|
||||
|
||||
public Position(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.algorithms.mcts.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.algorithms.mcts.montecarlo.State;
|
||||
|
||||
public class Node {
|
||||
State state;
|
||||
Node parent;
|
||||
List<Node> childArray;
|
||||
|
||||
public Node() {
|
||||
this.state = new State();
|
||||
childArray = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Node(State state) {
|
||||
this.state = state;
|
||||
childArray = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Node(State state, Node parent, List<Node> childArray) {
|
||||
this.state = state;
|
||||
this.parent = parent;
|
||||
this.childArray = childArray;
|
||||
}
|
||||
|
||||
public Node(Node node) {
|
||||
this.childArray = new ArrayList<>();
|
||||
this.state = new State(node.getState());
|
||||
if (node.getParent() != null)
|
||||
this.parent = node.getParent();
|
||||
List<Node> childArray = node.getChildArray();
|
||||
for (Node child : childArray) {
|
||||
this.childArray.add(new Node(child));
|
||||
}
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public Node getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Node parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public List<Node> getChildArray() {
|
||||
return childArray;
|
||||
}
|
||||
|
||||
public void setChildArray(List<Node> childArray) {
|
||||
this.childArray = childArray;
|
||||
}
|
||||
|
||||
public Node getRandomChildNode() {
|
||||
int noOfPossibleMoves = this.childArray.size();
|
||||
int selectRandom = (int) (Math.random() * noOfPossibleMoves);
|
||||
return this.childArray.get(selectRandom);
|
||||
}
|
||||
|
||||
public Node getChildWithMaxScore() {
|
||||
return Collections.max(this.childArray, Comparator.comparing(c -> {
|
||||
return c.getState().getVisitCount();
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.mcts.tree;
|
||||
|
||||
public class Tree {
|
||||
Node root;
|
||||
|
||||
public Tree() {
|
||||
root = new Node();
|
||||
}
|
||||
|
||||
public Tree(Node root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
public Node getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
public void setRoot(Node root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
public void addChild(Node parent, Node child) {
|
||||
parent.getChildArray().add(child);
|
||||
}
|
||||
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.middleelementlookup;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MiddleElementLookup {
|
||||
|
||||
public static Optional<String> findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
if (linkedList == null || linkedList.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(linkedList.get((linkedList.size() - 1) / 2));
|
||||
}
|
||||
|
||||
public static Optional<String> findMiddleElementFromHead(Node head) {
|
||||
if (head == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// calculate the size of the list
|
||||
Node current = head;
|
||||
int size = 1;
|
||||
while (current.hasNext()) {
|
||||
current = current.next();
|
||||
size++;
|
||||
}
|
||||
|
||||
// iterate till the middle element
|
||||
current = head;
|
||||
for (int i = 0; i < (size - 1) / 2; i++) {
|
||||
current = current.next();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(current.data());
|
||||
}
|
||||
|
||||
public static Optional<String> findMiddleElementFromHead1PassRecursively(Node head) {
|
||||
if (head == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
MiddleAuxRecursion middleAux = new MiddleAuxRecursion();
|
||||
findMiddleRecursively(head, middleAux);
|
||||
return Optional.ofNullable(middleAux.middle.data());
|
||||
}
|
||||
|
||||
private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAux) {
|
||||
if (node == null) {
|
||||
// reached the end
|
||||
middleAux.length = middleAux.length / 2;
|
||||
return;
|
||||
}
|
||||
middleAux.length++;
|
||||
findMiddleRecursively(node.next(), middleAux);
|
||||
|
||||
if (middleAux.length == 0) {
|
||||
// found the middle
|
||||
middleAux.middle = node;
|
||||
}
|
||||
|
||||
middleAux.length--;
|
||||
}
|
||||
|
||||
public static Optional<String> findMiddleElementFromHead1PassIteratively(Node head) {
|
||||
if (head == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Node slowPointer = head;
|
||||
Node fastPointer = head;
|
||||
|
||||
while (fastPointer.hasNext() && fastPointer.next()
|
||||
.hasNext()) {
|
||||
fastPointer = fastPointer.next()
|
||||
.next();
|
||||
slowPointer = slowPointer.next();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(slowPointer.data());
|
||||
}
|
||||
|
||||
private static class MiddleAuxRecursion {
|
||||
Node middle;
|
||||
int length = 0;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.middleelementlookup;
|
||||
|
||||
public class Node {
|
||||
private Node next;
|
||||
private String data;
|
||||
|
||||
public Node(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
public Node next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(Node next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
class GameOfBones {
|
||||
static List<Integer> getPossibleStates(int noOfBonesInHeap) {
|
||||
return IntStream.rangeClosed(1, 3).boxed()
|
||||
.map(i -> noOfBonesInHeap - i)
|
||||
.filter(newHeapCount -> newHeapCount >= 0)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MiniMax {
|
||||
private Tree tree;
|
||||
|
||||
public Tree getTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
public void constructTree(int noOfBones) {
|
||||
tree = new Tree();
|
||||
Node root = new Node(noOfBones, true);
|
||||
tree.setRoot(root);
|
||||
constructTree(root);
|
||||
}
|
||||
|
||||
private void constructTree(Node parentNode) {
|
||||
List<Integer> listofPossibleHeaps = GameOfBones.getPossibleStates(parentNode.getNoOfBones());
|
||||
boolean isChildMaxPlayer = !parentNode.isMaxPlayer();
|
||||
listofPossibleHeaps.forEach(n -> {
|
||||
Node newNode = new Node(n, isChildMaxPlayer);
|
||||
parentNode.addChild(newNode);
|
||||
if (newNode.getNoOfBones() > 0) {
|
||||
constructTree(newNode);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean checkWin() {
|
||||
Node root = tree.getRoot();
|
||||
checkWin(root);
|
||||
return root.getScore() == 1;
|
||||
}
|
||||
|
||||
private void checkWin(Node node) {
|
||||
List<Node> children = node.getChildren();
|
||||
boolean isMaxPlayer = node.isMaxPlayer();
|
||||
children.forEach(child -> {
|
||||
if (child.getNoOfBones() == 0) {
|
||||
child.setScore(isMaxPlayer ? 1 : -1);
|
||||
} else {
|
||||
checkWin(child);
|
||||
}
|
||||
});
|
||||
Node bestChild = findBestChild(isMaxPlayer, children);
|
||||
node.setScore(bestChild.getScore());
|
||||
}
|
||||
|
||||
private Node findBestChild(boolean isMaxPlayer, List<Node> children) {
|
||||
Comparator<Node> byScoreComparator = Comparator.comparing(Node::getScore);
|
||||
|
||||
return children.stream()
|
||||
.max(isMaxPlayer ? byScoreComparator : byScoreComparator.reversed())
|
||||
.orElseThrow(NoSuchElementException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Node {
|
||||
private int noOfBones;
|
||||
private boolean isMaxPlayer;
|
||||
private int score;
|
||||
private List<Node> children;
|
||||
|
||||
public Node(int noOfBones, boolean isMaxPlayer) {
|
||||
this.noOfBones = noOfBones;
|
||||
this.isMaxPlayer = isMaxPlayer;
|
||||
children = new ArrayList<>();
|
||||
}
|
||||
|
||||
int getNoOfBones() {
|
||||
return noOfBones;
|
||||
}
|
||||
|
||||
boolean isMaxPlayer() {
|
||||
return isMaxPlayer;
|
||||
}
|
||||
|
||||
int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
List<Node> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
void addChild(Node newNode) {
|
||||
children.add(newNode);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
public class Tree {
|
||||
private Node root;
|
||||
|
||||
Tree() {
|
||||
}
|
||||
|
||||
Node getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
void setRoot(Node root) {
|
||||
this.root = root;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
/**
|
||||
* Constants used by the Multi-swarm optimization algorithms.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Constants {
|
||||
|
||||
/**
|
||||
* The inertia factor encourages a particle to continue moving in its
|
||||
* current direction.
|
||||
*/
|
||||
public static final double INERTIA_FACTOR = 0.729;
|
||||
|
||||
/**
|
||||
* The cognitive weight encourages a particle to move toward its historical
|
||||
* best-known position.
|
||||
*/
|
||||
public static final double COGNITIVE_WEIGHT = 1.49445;
|
||||
|
||||
/**
|
||||
* The social weight encourages a particle to move toward the best-known
|
||||
* position found by any of the particle’s swarm-mates.
|
||||
*/
|
||||
public static final double SOCIAL_WEIGHT = 1.49445;
|
||||
|
||||
/**
|
||||
* The global weight encourages a particle to move toward the best-known
|
||||
* position found by any particle in any swarm.
|
||||
*/
|
||||
public static final double GLOBAL_WEIGHT = 0.3645;
|
||||
|
||||
/**
|
||||
* Upper bound for the random generation. We use it to reduce the
|
||||
* computation time since we can rawly estimate it.
|
||||
*/
|
||||
public static final int PARTICLE_UPPER_BOUND = 10000000;
|
||||
|
||||
/**
|
||||
* Private constructor for utility class.
|
||||
*/
|
||||
private Constants() {
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
/**
|
||||
* Interface for a fitness function, used to decouple the main algorithm logic
|
||||
* from the specific problem solution.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public interface FitnessFunction {
|
||||
|
||||
/**
|
||||
* Returns the fitness of a particle given its position.
|
||||
*
|
||||
* @param particlePosition
|
||||
* the position of the particle
|
||||
* @return the fitness of the particle
|
||||
*/
|
||||
public double getFitness(long[] particlePosition);
|
||||
|
||||
}
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents a collection of {@link Swarm}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Multiswarm {
|
||||
|
||||
/**
|
||||
* The swarms managed by this multiswarm.
|
||||
*/
|
||||
private Swarm[] swarms;
|
||||
|
||||
/**
|
||||
* The best position found within all the {@link #swarms}.
|
||||
*/
|
||||
private long[] bestPosition;
|
||||
|
||||
/**
|
||||
* The best fitness score found within all the {@link #swarms}.
|
||||
*/
|
||||
private double bestFitness = Double.NEGATIVE_INFINITY;
|
||||
|
||||
/**
|
||||
* A random generator.
|
||||
*/
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* The fitness function used to determine how good is a particle.
|
||||
*/
|
||||
private FitnessFunction fitnessFunction;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiswarm.
|
||||
*
|
||||
* @param numSwarms
|
||||
* the number of {@link #swarms}
|
||||
* @param particlesPerSwarm
|
||||
* the number of particle for each {@link #swarms}
|
||||
* @param fitnessFunction
|
||||
* the {@link #fitnessFunction}
|
||||
*/
|
||||
public Multiswarm(int numSwarms, int particlesPerSwarm, FitnessFunction fitnessFunction) {
|
||||
this.fitnessFunction = fitnessFunction;
|
||||
this.swarms = new Swarm[numSwarms];
|
||||
for (int i = 0; i < numSwarms; i++) {
|
||||
swarms[i] = new Swarm(particlesPerSwarm);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main loop of the algorithm. Iterates all particles of all
|
||||
* {@link #swarms}. For each particle, computes the new fitness and checks
|
||||
* if a new best position has been found among itself, the swarm and all the
|
||||
* swarms and finally updates the particle position and speed.
|
||||
*/
|
||||
public void mainLoop() {
|
||||
for (Swarm swarm : swarms) {
|
||||
for (Particle particle : swarm.getParticles()) {
|
||||
|
||||
long[] particleOldPosition = particle.getPosition().clone();
|
||||
|
||||
// Calculate the particle fitness.
|
||||
particle.setFitness(fitnessFunction.getFitness(particleOldPosition));
|
||||
|
||||
// Check if a new best position has been found for the particle
|
||||
// itself, within the swarm and the multiswarm.
|
||||
if (particle.getFitness() > particle.getBestFitness()) {
|
||||
particle.setBestFitness(particle.getFitness());
|
||||
particle.setBestPosition(particleOldPosition);
|
||||
|
||||
if (particle.getFitness() > swarm.getBestFitness()) {
|
||||
swarm.setBestFitness(particle.getFitness());
|
||||
swarm.setBestPosition(particleOldPosition);
|
||||
|
||||
if (swarm.getBestFitness() > bestFitness) {
|
||||
bestFitness = swarm.getBestFitness();
|
||||
bestPosition = swarm.getBestPosition().clone();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Updates the particle position by adding the speed to the
|
||||
// actual position.
|
||||
long[] position = particle.getPosition();
|
||||
long[] speed = particle.getSpeed();
|
||||
|
||||
position[0] += speed[0];
|
||||
position[1] += speed[1];
|
||||
|
||||
// Updates the particle speed.
|
||||
speed[0] = getNewParticleSpeedForIndex(particle, swarm, 0);
|
||||
speed[1] = getNewParticleSpeedForIndex(particle, swarm, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a new speed for a given particle of a given swarm on a given
|
||||
* axis. The new speed is computed using the formula:
|
||||
*
|
||||
* <pre>
|
||||
* ({@link Constants#INERTIA_FACTOR} * {@link Particle#getSpeed()}) +
|
||||
* (({@link Constants#COGNITIVE_WEIGHT} * random(0,1)) * ({@link Particle#getBestPosition()} - {@link Particle#getPosition()})) +
|
||||
* (({@link Constants#SOCIAL_WEIGHT} * random(0,1)) * ({@link Swarm#getBestPosition()} - {@link Particle#getPosition()})) +
|
||||
* (({@link Constants#GLOBAL_WEIGHT} * random(0,1)) * ({@link #bestPosition} - {@link Particle#getPosition()}))
|
||||
* </pre>
|
||||
*
|
||||
* @param particle
|
||||
* the particle whose new speed needs to be computed
|
||||
* @param swarm
|
||||
* the swarm which contains the particle
|
||||
* @param index
|
||||
* the index of the particle axis whose speeds needs to be
|
||||
* computed
|
||||
* @return the new speed of the particle passed on the given axis
|
||||
*/
|
||||
private int getNewParticleSpeedForIndex(Particle particle, Swarm swarm, int index) {
|
||||
return (int) ((Constants.INERTIA_FACTOR * particle.getSpeed()[index])
|
||||
+ (randomizePercentage(Constants.COGNITIVE_WEIGHT)
|
||||
* (particle.getBestPosition()[index] - particle.getPosition()[index]))
|
||||
+ (randomizePercentage(Constants.SOCIAL_WEIGHT)
|
||||
* (swarm.getBestPosition()[index] - particle.getPosition()[index]))
|
||||
+ (randomizePercentage(Constants.GLOBAL_WEIGHT)
|
||||
* (bestPosition[index] - particle.getPosition()[index])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random number between 0 and the value passed as argument.
|
||||
*
|
||||
* @param value
|
||||
* the value to randomize
|
||||
* @return a random value between 0 and the one passed as argument
|
||||
*/
|
||||
private double randomizePercentage(double value) {
|
||||
return random.nextDouble() * value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestPosition}.
|
||||
*
|
||||
* @return the {@link #bestPosition}
|
||||
*/
|
||||
public long[] getBestPosition() {
|
||||
return bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestFitness}.
|
||||
*
|
||||
* @return the {@link #bestFitness}
|
||||
*/
|
||||
public double getBestFitness() {
|
||||
return bestFitness;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(bestFitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(bestPosition);
|
||||
result = prime * result + ((fitnessFunction == null) ? 0 : fitnessFunction.hashCode());
|
||||
result = prime * result + ((random == null) ? 0 : random.hashCode());
|
||||
result = prime * result + Arrays.hashCode(swarms);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Multiswarm other = (Multiswarm) obj;
|
||||
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
|
||||
return false;
|
||||
if (!Arrays.equals(bestPosition, other.bestPosition))
|
||||
return false;
|
||||
if (fitnessFunction == null) {
|
||||
if (other.fitnessFunction != null)
|
||||
return false;
|
||||
} else if (!fitnessFunction.equals(other.fitnessFunction))
|
||||
return false;
|
||||
if (random == null) {
|
||||
if (other.random != null)
|
||||
return false;
|
||||
} else if (!random.equals(other.random))
|
||||
return false;
|
||||
if (!Arrays.equals(swarms, other.swarms))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Multiswarm [swarms=" + Arrays.toString(swarms) + ", bestPosition=" + Arrays.toString(bestPosition)
|
||||
+ ", bestFitness=" + bestFitness + ", random=" + random + ", fitnessFunction=" + fitnessFunction + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Represents a particle, the basic component of a {@link Swarm}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Particle {
|
||||
|
||||
/**
|
||||
* The current position of this particle.
|
||||
*/
|
||||
private long[] position;
|
||||
|
||||
/**
|
||||
* The speed of this particle.
|
||||
*/
|
||||
private long[] speed;
|
||||
|
||||
/**
|
||||
* The fitness of this particle for the current position.
|
||||
*/
|
||||
private double fitness;
|
||||
|
||||
/**
|
||||
* The best position found by this particle.
|
||||
*/
|
||||
private long[] bestPosition;
|
||||
|
||||
/**
|
||||
* The best fitness found by this particle.
|
||||
*/
|
||||
private double bestFitness = Double.NEGATIVE_INFINITY;
|
||||
|
||||
/**
|
||||
* Instantiates a new Particle.
|
||||
*
|
||||
* @param initialPosition
|
||||
* the initial {@link #position}
|
||||
* @param initialSpeed
|
||||
* the initial {@link #speed}
|
||||
*/
|
||||
public Particle(long[] initialPosition, long[] initialSpeed) {
|
||||
this.position = initialPosition;
|
||||
this.speed = initialSpeed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #position}.
|
||||
*
|
||||
* @return the {@link #position}
|
||||
*/
|
||||
public long[] getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #speed}.
|
||||
*
|
||||
* @return the {@link #speed}
|
||||
*/
|
||||
public long[] getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #fitness}.
|
||||
*
|
||||
* @return the {@link #fitness}
|
||||
*/
|
||||
public double getFitness() {
|
||||
return fitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestPosition}.
|
||||
*
|
||||
* @return the {@link #bestPosition}
|
||||
*/
|
||||
public long[] getBestPosition() {
|
||||
return bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestFitness}.
|
||||
*
|
||||
* @return the {@link #bestFitness}
|
||||
*/
|
||||
public double getBestFitness() {
|
||||
return bestFitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #position}.
|
||||
*
|
||||
* @param position
|
||||
* the new {@link #position}
|
||||
*/
|
||||
public void setPosition(long[] position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #speed}.
|
||||
*
|
||||
* @param speed
|
||||
* the new {@link #speed}
|
||||
*/
|
||||
public void setSpeed(long[] speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #fitness}.
|
||||
*
|
||||
* @param fitness
|
||||
* the new {@link #fitness}
|
||||
*/
|
||||
public void setFitness(double fitness) {
|
||||
this.fitness = fitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestPosition}.
|
||||
*
|
||||
* @param bestPosition
|
||||
* the new {@link #bestPosition}
|
||||
*/
|
||||
public void setBestPosition(long[] bestPosition) {
|
||||
this.bestPosition = bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestFitness}.
|
||||
*
|
||||
* @param bestFitness
|
||||
* the new {@link #bestFitness}
|
||||
*/
|
||||
public void setBestFitness(double bestFitness) {
|
||||
this.bestFitness = bestFitness;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(bestFitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(bestPosition);
|
||||
temp = Double.doubleToLongBits(fitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(position);
|
||||
result = prime * result + Arrays.hashCode(speed);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Particle other = (Particle) obj;
|
||||
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
|
||||
return false;
|
||||
if (!Arrays.equals(bestPosition, other.bestPosition))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(fitness) != Double.doubleToLongBits(other.fitness))
|
||||
return false;
|
||||
if (!Arrays.equals(position, other.position))
|
||||
return false;
|
||||
if (!Arrays.equals(speed, other.speed))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Particle [position=" + Arrays.toString(position) + ", speed=" + Arrays.toString(speed) + ", fitness="
|
||||
+ fitness + ", bestPosition=" + Arrays.toString(bestPosition) + ", bestFitness=" + bestFitness + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents a collection of {@link Particle}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Swarm {
|
||||
|
||||
/**
|
||||
* The particles of this swarm.
|
||||
*/
|
||||
private Particle[] particles;
|
||||
|
||||
/**
|
||||
* The best position found within the particles of this swarm.
|
||||
*/
|
||||
private long[] bestPosition;
|
||||
|
||||
/**
|
||||
* The best fitness score found within the particles of this swarm.
|
||||
*/
|
||||
private double bestFitness = Double.NEGATIVE_INFINITY;
|
||||
|
||||
/**
|
||||
* A random generator.
|
||||
*/
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* Instantiates a new Swarm.
|
||||
*
|
||||
* @param numParticles
|
||||
* the number of particles of the swarm
|
||||
*/
|
||||
public Swarm(int numParticles) {
|
||||
particles = new Particle[numParticles];
|
||||
for (int i = 0; i < numParticles; i++) {
|
||||
long[] initialParticlePosition = { random.nextInt(Constants.PARTICLE_UPPER_BOUND),
|
||||
random.nextInt(Constants.PARTICLE_UPPER_BOUND) };
|
||||
long[] initialParticleSpeed = { random.nextInt(Constants.PARTICLE_UPPER_BOUND),
|
||||
random.nextInt(Constants.PARTICLE_UPPER_BOUND) };
|
||||
particles[i] = new Particle(initialParticlePosition, initialParticleSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #particles}.
|
||||
*
|
||||
* @return the {@link #particles}
|
||||
*/
|
||||
public Particle[] getParticles() {
|
||||
return particles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestPosition}.
|
||||
*
|
||||
* @return the {@link #bestPosition}
|
||||
*/
|
||||
public long[] getBestPosition() {
|
||||
return bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestFitness}.
|
||||
*
|
||||
* @return the {@link #bestFitness}
|
||||
*/
|
||||
public double getBestFitness() {
|
||||
return bestFitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestPosition}.
|
||||
*
|
||||
* @param bestPosition
|
||||
* the new {@link #bestPosition}
|
||||
*/
|
||||
public void setBestPosition(long[] bestPosition) {
|
||||
this.bestPosition = bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestFitness}.
|
||||
*
|
||||
* @param bestFitness
|
||||
* the new {@link #bestFitness}
|
||||
*/
|
||||
public void setBestFitness(double bestFitness) {
|
||||
this.bestFitness = bestFitness;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(bestFitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(bestPosition);
|
||||
result = prime * result + Arrays.hashCode(particles);
|
||||
result = prime * result + ((random == null) ? 0 : random.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Swarm other = (Swarm) obj;
|
||||
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
|
||||
return false;
|
||||
if (!Arrays.equals(bestPosition, other.bestPosition))
|
||||
return false;
|
||||
if (!Arrays.equals(particles, other.particles))
|
||||
return false;
|
||||
if (random == null) {
|
||||
if (other.random != null)
|
||||
return false;
|
||||
} else if (!random.equals(other.random))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Swarm [particles=" + Arrays.toString(particles) + ", bestPosition=" + Arrays.toString(bestPosition)
|
||||
+ ", bestFitness=" + bestFitness + ", random=" + random + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.algorithms.string;
|
||||
|
||||
public class EnglishAlphabetLetters {
|
||||
|
||||
public static boolean checkStringForAllTheLetters(String input) {
|
||||
boolean[] visited = new boolean[26];
|
||||
|
||||
int index = 0;
|
||||
|
||||
for (int id = 0; id < input.length(); id++) {
|
||||
if ('a' <= input.charAt(id) && input.charAt(id) <= 'z') {
|
||||
index = input.charAt(id) - 'a';
|
||||
} else if ('A' <= input.charAt(id) && input.charAt(id) <= 'Z') {
|
||||
index = input.charAt(id) - 'A';
|
||||
}
|
||||
visited[index] = true;
|
||||
}
|
||||
|
||||
for (int id = 0; id < 26; id++) {
|
||||
if (!visited[id]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkStringForAllLetterUsingStream(String input) {
|
||||
long c = input.toLowerCase().chars().filter(ch -> ch >= 'a' && ch <= 'z').distinct().count();
|
||||
return c == 26;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
checkStringForAllLetterUsingStream("intit");
|
||||
}
|
||||
}
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
package com.baeldung.algorithms.string.search;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
public class StringSearchAlgorithms {
|
||||
public static long getBiggerPrime(int m) {
|
||||
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(m) + 1, new Random());
|
||||
return prime.longValue();
|
||||
}
|
||||
|
||||
public static long getLowerPrime(long number) {
|
||||
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(number) - 1, new Random());
|
||||
return prime.longValue();
|
||||
}
|
||||
|
||||
private static int getNumberOfBits(final int number) {
|
||||
return Integer.SIZE - Integer.numberOfLeadingZeros(number);
|
||||
}
|
||||
|
||||
private static int getNumberOfBits(final long number) {
|
||||
return Long.SIZE - Long.numberOfLeadingZeros(number);
|
||||
}
|
||||
|
||||
public static int simpleTextSearch(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length;
|
||||
int textSize = text.length;
|
||||
|
||||
int i = 0;
|
||||
|
||||
while ((i + patternSize) <= textSize) {
|
||||
int j = 0;
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j += 1;
|
||||
if (j >= patternSize)
|
||||
return i;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int RabinKarpMethod(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length; // m
|
||||
int textSize = text.length; // n
|
||||
|
||||
long prime = getBiggerPrime(patternSize);
|
||||
|
||||
long r = 1;
|
||||
for (int i = 0; i < patternSize - 1; i++) {
|
||||
r *= 2;
|
||||
r = r % prime;
|
||||
}
|
||||
|
||||
long[] t = new long[textSize];
|
||||
t[0] = 0;
|
||||
|
||||
long pfinger = 0;
|
||||
|
||||
for (int j = 0; j < patternSize; j++) {
|
||||
t[0] = (2 * t[0] + text[j]) % prime;
|
||||
pfinger = (2 * pfinger + pattern[j]) % prime;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
boolean passed = false;
|
||||
|
||||
int diff = textSize - patternSize;
|
||||
for (i = 0; i <= diff; i++) {
|
||||
if (t[i] == pfinger) {
|
||||
passed = true;
|
||||
for (int k = 0; k < patternSize; k++) {
|
||||
if (text[i + k] != pattern[k]) {
|
||||
passed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (passed) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
if (i < diff) {
|
||||
long value = 2 * (t[i] - r * text[i]) + text[i + patternSize];
|
||||
t[i + 1] = ((value % prime) + prime) % prime;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
public static int KnuthMorrisPrattSearch(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length; // m
|
||||
int textSize = text.length; // n
|
||||
|
||||
int i = 0, j = 0;
|
||||
|
||||
int[] shift = KnuthMorrisPrattShift(pattern);
|
||||
|
||||
while ((i + patternSize) <= textSize) {
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j += 1;
|
||||
if (j >= patternSize)
|
||||
return i;
|
||||
}
|
||||
|
||||
if (j > 0) {
|
||||
i += shift[j - 1];
|
||||
j = Math.max(j - shift[j - 1], 0);
|
||||
} else {
|
||||
i++;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int[] KnuthMorrisPrattShift(char[] pattern) {
|
||||
int patternSize = pattern.length;
|
||||
|
||||
int[] shift = new int[patternSize];
|
||||
shift[0] = 1;
|
||||
|
||||
int i = 1, j = 0;
|
||||
|
||||
while ((i + j) < patternSize) {
|
||||
if (pattern[i + j] == pattern[j]) {
|
||||
shift[i + j] = i;
|
||||
j++;
|
||||
} else {
|
||||
if (j == 0)
|
||||
shift[i] = i + 1;
|
||||
|
||||
if (j > 0) {
|
||||
i = i + shift[j - 1];
|
||||
j = Math.max(j - shift[j - 1], 0);
|
||||
} else {
|
||||
i = i + 1;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return shift;
|
||||
}
|
||||
|
||||
public static int BoyerMooreHorspoolSimpleSearch(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length;
|
||||
int textSize = text.length;
|
||||
|
||||
int i = 0, j = 0;
|
||||
|
||||
while ((i + patternSize) <= textSize) {
|
||||
j = patternSize - 1;
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j--;
|
||||
if (j < 0)
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int BoyerMooreHorspoolSearch(char[] pattern, char[] text) {
|
||||
|
||||
int shift[] = new int[256];
|
||||
|
||||
for (int k = 0; k < 256; k++) {
|
||||
shift[k] = pattern.length;
|
||||
}
|
||||
|
||||
for (int k = 0; k < pattern.length - 1; k++) {
|
||||
shift[pattern[k]] = pattern.length - 1 - k;
|
||||
}
|
||||
|
||||
int i = 0, j = 0;
|
||||
|
||||
while ((i + pattern.length) <= text.length) {
|
||||
j = pattern.length - 1;
|
||||
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j -= 1;
|
||||
if (j < 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
i = i + shift[text[i + pattern.length - 1]];
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,12 @@
|
||||
S ########
|
||||
# #
|
||||
# ### ## #
|
||||
# # # #
|
||||
# # # # #
|
||||
# ## #####
|
||||
# # #
|
||||
# # # # #
|
||||
##### ####
|
||||
# # E
|
||||
# # # #
|
||||
##########
|
||||
@@ -0,0 +1,22 @@
|
||||
S ##########################
|
||||
# # # #
|
||||
# # #### ############### #
|
||||
# # # # # #
|
||||
# # #### # # ###############
|
||||
# # # # # # #
|
||||
# # # #### ### ########### #
|
||||
# # # # # #
|
||||
# ################## #
|
||||
######### # # # # #
|
||||
# # #### # ####### # #
|
||||
# # ### ### # # # # #
|
||||
# # ## # ##### # #
|
||||
##### ####### # # # # #
|
||||
# # ## ## #### # #
|
||||
# ##### ####### # #
|
||||
# # ############
|
||||
####### ######### # #
|
||||
# # ######## #
|
||||
# ####### ###### ## # E
|
||||
# # # ## #
|
||||
############################
|
||||
Reference in New Issue
Block a user