Skip to content

Commit a1a9ec6

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 3f3b599 + df3b013 commit a1a9ec6

100 files changed

Lines changed: 1694 additions & 327 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

algorithms/src/main/java/com/baeldung/automata/FiniteStateMachine.java renamed to algorithms/src/main/java/com/baeldung/algorithms/automata/FiniteStateMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.automata;
1+
package com.baeldung.algorithms.automata;
22

33
/**
44
* Finite state machine.

algorithms/src/main/java/com/baeldung/automata/RtFiniteStateMachine.java renamed to algorithms/src/main/java/com/baeldung/algorithms/automata/RtFiniteStateMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.automata;
1+
package com.baeldung.algorithms.automata;
22

33
/**
44
* Default implementation of a finite state machine.

algorithms/src/main/java/com/baeldung/automata/RtState.java renamed to algorithms/src/main/java/com/baeldung/algorithms/automata/RtState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.automata;
1+
package com.baeldung.algorithms.automata;
22

33
import java.util.ArrayList;
44
import java.util.List;

algorithms/src/main/java/com/baeldung/automata/RtTransition.java renamed to algorithms/src/main/java/com/baeldung/algorithms/automata/RtTransition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.automata;
1+
package com.baeldung.algorithms.automata;
22

33

44
/**

algorithms/src/main/java/com/baeldung/automata/State.java renamed to algorithms/src/main/java/com/baeldung/algorithms/automata/State.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.automata;
1+
package com.baeldung.algorithms.automata;
22

33
/**
44
* State. Part of a finite state machine.

algorithms/src/main/java/com/baeldung/automata/Transition.java renamed to algorithms/src/main/java/com/baeldung/algorithms/automata/Transition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.automata;
1+
package com.baeldung.algorithms.automata;
22

33
/**
44
* Transition in a finite State machine.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.baeldung.algorithms.mcts.montecarlo;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.algorithms.mcts.tictactoe.Board;
6+
import com.baeldung.algorithms.mcts.tree.Node;
7+
import com.baeldung.algorithms.mcts.tree.Tree;
8+
9+
public class MonteCarloTreeSearch {
10+
11+
private static final int WIN_SCORE = 10;
12+
private int level;
13+
private int oponent;
14+
15+
public MonteCarloTreeSearch() {
16+
this.level = 3;
17+
}
18+
19+
public int getLevel() {
20+
return level;
21+
}
22+
23+
public void setLevel(int level) {
24+
this.level = level;
25+
}
26+
27+
private int getMillisForCurrentLevel() {
28+
return 2 * (this.level - 1) + 1;
29+
}
30+
31+
public Board findNextMove(Board board, int playerNo) {
32+
long start = System.currentTimeMillis();
33+
long end = start + 60 * getMillisForCurrentLevel();
34+
35+
oponent = 3 - playerNo;
36+
Tree tree = new Tree();
37+
Node rootNode = tree.getRoot();
38+
rootNode.getState().setBoard(board);
39+
rootNode.getState().setPlayerNo(oponent);
40+
41+
while (System.currentTimeMillis() < end) {
42+
// Phase 1 - Selection
43+
Node promisingNode = selectPromisingNode(rootNode);
44+
// Phase 2 - Expansion
45+
if (promisingNode.getState().getBoard().checkStatus() == Board.IN_PROGRESS)
46+
expandNode(promisingNode);
47+
48+
// Phase 3 - Simulation
49+
Node nodeToExplore = promisingNode;
50+
if (promisingNode.getChildArray().size() > 0) {
51+
nodeToExplore = promisingNode.getRandomChildNode();
52+
}
53+
int playoutResult = simulateRandomPlayout(nodeToExplore);
54+
// Phase 4 - Update
55+
backPropogation(nodeToExplore, playoutResult);
56+
}
57+
58+
Node winnerNode = rootNode.getChildWithMaxScore();
59+
tree.setRoot(winnerNode);
60+
return winnerNode.getState().getBoard();
61+
}
62+
63+
private Node selectPromisingNode(Node rootNode) {
64+
Node node = rootNode;
65+
while (node.getChildArray().size() != 0) {
66+
node = UCT.findBestNodeWithUCT(node);
67+
}
68+
return node;
69+
}
70+
71+
private void expandNode(Node node) {
72+
List<State> possibleStates = node.getState().getAllPossibleStates();
73+
possibleStates.forEach(state -> {
74+
Node newNode = new Node(state);
75+
newNode.setParent(node);
76+
newNode.getState().setPlayerNo(node.getState().getOpponent());
77+
node.getChildArray().add(newNode);
78+
});
79+
}
80+
81+
private void backPropogation(Node nodeToExplore, int playerNo) {
82+
Node tempNode = nodeToExplore;
83+
while (tempNode != null) {
84+
tempNode.getState().incrementVisit();
85+
if (tempNode.getState().getPlayerNo() == playerNo)
86+
tempNode.getState().addScore(WIN_SCORE);
87+
tempNode = tempNode.getParent();
88+
}
89+
}
90+
91+
private int simulateRandomPlayout(Node node) {
92+
Node tempNode = new Node(node);
93+
State tempState = tempNode.getState();
94+
int boardStatus = tempState.getBoard().checkStatus();
95+
96+
if (boardStatus == oponent) {
97+
tempNode.getParent().getState().setWinScore(Integer.MIN_VALUE);
98+
return boardStatus;
99+
}
100+
while (boardStatus == Board.IN_PROGRESS) {
101+
tempState.togglePlayer();
102+
tempState.randomPlay();
103+
boardStatus = tempState.getBoard().checkStatus();
104+
}
105+
106+
return boardStatus;
107+
}
108+
109+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.baeldung.algorithms.mcts.montecarlo;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.baeldung.algorithms.mcts.tictactoe.Board;
7+
import com.baeldung.algorithms.mcts.tictactoe.Position;
8+
9+
public class State {
10+
private Board board;
11+
private int playerNo;
12+
private int visitCount;
13+
private double winScore;
14+
15+
public State() {
16+
board = new Board();
17+
}
18+
19+
public State(State state) {
20+
this.board = new Board(state.getBoard());
21+
this.playerNo = state.getPlayerNo();
22+
this.visitCount = state.getVisitCount();
23+
this.winScore = state.getWinScore();
24+
}
25+
26+
public State(Board board) {
27+
this.board = new Board(board);
28+
}
29+
30+
Board getBoard() {
31+
return board;
32+
}
33+
34+
void setBoard(Board board) {
35+
this.board = board;
36+
}
37+
38+
int getPlayerNo() {
39+
return playerNo;
40+
}
41+
42+
void setPlayerNo(int playerNo) {
43+
this.playerNo = playerNo;
44+
}
45+
46+
int getOpponent() {
47+
return 3 - playerNo;
48+
}
49+
50+
public int getVisitCount() {
51+
return visitCount;
52+
}
53+
54+
public void setVisitCount(int visitCount) {
55+
this.visitCount = visitCount;
56+
}
57+
58+
double getWinScore() {
59+
return winScore;
60+
}
61+
62+
void setWinScore(double winScore) {
63+
this.winScore = winScore;
64+
}
65+
66+
public List<State> getAllPossibleStates() {
67+
List<State> possibleStates = new ArrayList<>();
68+
List<Position> availablePositions = this.board.getEmptyPositions();
69+
availablePositions.forEach(p -> {
70+
State newState = new State(this.board);
71+
newState.setPlayerNo(3 - this.playerNo);
72+
newState.getBoard().performMove(newState.getPlayerNo(), p);
73+
possibleStates.add(newState);
74+
});
75+
return possibleStates;
76+
}
77+
78+
void incrementVisit() {
79+
this.visitCount++;
80+
}
81+
82+
void addScore(double score) {
83+
if (this.winScore != Integer.MIN_VALUE)
84+
this.winScore += score;
85+
}
86+
87+
void randomPlay() {
88+
List<Position> availablePositions = this.board.getEmptyPositions();
89+
int totalPossibilities = availablePositions.size();
90+
int selectRandom = (int) (Math.random() * ((totalPossibilities - 1) + 1));
91+
this.board.performMove(this.playerNo, availablePositions.get(selectRandom));
92+
}
93+
94+
void togglePlayer() {
95+
this.playerNo = 3 - this.playerNo;
96+
}
97+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.algorithms.mcts.montecarlo;
2+
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
import com.baeldung.algorithms.mcts.tree.Node;
8+
9+
public class UCT {
10+
11+
public static double uctValue(int totalVisit, double nodeWinScore, int nodeVisit) {
12+
if (nodeVisit == 0) {
13+
return Integer.MAX_VALUE;
14+
}
15+
return (nodeWinScore / (double) nodeVisit) + 1.41 * Math.sqrt(Math.log(totalVisit) / (double) nodeVisit);
16+
}
17+
18+
static Node findBestNodeWithUCT(Node node) {
19+
int parentVisit = node.getState().getVisitCount();
20+
return Collections.max(
21+
node.getChildArray(),
22+
Comparator.comparing(c -> uctValue(parentVisit, c.getState().getWinScore(), c.getState().getVisitCount())));
23+
}
24+
}

0 commit comments

Comments
 (0)