Skip to content

Commit ef74004

Browse files
authored
Algorithms refactor (eugenp#2136)
1 parent 1b0d5f0 commit ef74004

11 files changed

Lines changed: 124 additions & 126 deletions

File tree

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.

algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
public class MonteCarloTreeSearch {
1010

11-
static final int WIN_SCORE = 10;
12-
int level;
13-
int oponent;
11+
private static final int WIN_SCORE = 10;
12+
private int level;
13+
private int oponent;
1414

1515
public MonteCarloTreeSearch() {
1616
this.level = 3;

algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/State.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import com.baeldung.algorithms.mcts.tictactoe.Position;
88

99
public class State {
10-
Board board;
11-
int playerNo;
12-
int visitCount;
13-
double winScore;
10+
private Board board;
11+
private int playerNo;
12+
private int visitCount;
13+
private double winScore;
1414

1515
public State() {
1616
board = new Board();
@@ -27,23 +27,23 @@ public State(Board board) {
2727
this.board = new Board(board);
2828
}
2929

30-
public Board getBoard() {
30+
Board getBoard() {
3131
return board;
3232
}
3333

34-
public void setBoard(Board board) {
34+
void setBoard(Board board) {
3535
this.board = board;
3636
}
3737

38-
public int getPlayerNo() {
38+
int getPlayerNo() {
3939
return playerNo;
4040
}
4141

42-
public void setPlayerNo(int playerNo) {
42+
void setPlayerNo(int playerNo) {
4343
this.playerNo = playerNo;
4444
}
4545

46-
public int getOpponent() {
46+
int getOpponent() {
4747
return 3 - playerNo;
4848
}
4949

@@ -55,11 +55,11 @@ public void setVisitCount(int visitCount) {
5555
this.visitCount = visitCount;
5656
}
5757

58-
public double getWinScore() {
58+
double getWinScore() {
5959
return winScore;
6060
}
6161

62-
public void setWinScore(double winScore) {
62+
void setWinScore(double winScore) {
6363
this.winScore = winScore;
6464
}
6565

@@ -75,23 +75,23 @@ public List<State> getAllPossibleStates() {
7575
return possibleStates;
7676
}
7777

78-
public void incrementVisit() {
78+
void incrementVisit() {
7979
this.visitCount++;
8080
}
8181

82-
public void addScore(double score) {
82+
void addScore(double score) {
8383
if (this.winScore != Integer.MIN_VALUE)
8484
this.winScore += score;
8585
}
8686

87-
public void randomPlay() {
87+
void randomPlay() {
8888
List<Position> availablePositions = this.board.getEmptyPositions();
8989
int totalPossibilities = availablePositions.size();
9090
int selectRandom = (int) (Math.random() * ((totalPossibilities - 1) + 1));
9191
this.board.performMove(this.playerNo, availablePositions.get(selectRandom));
9292
}
9393

94-
public void togglePlayer() {
94+
void togglePlayer() {
9595
this.playerNo = 3 - this.playerNo;
9696
}
9797
}

algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
import com.baeldung.algorithms.mcts.tree.Node;
88

99
public class UCT {
10-
final static double C = 1.41;
1110

1211
public static double uctValue(int totalVisit, double nodeWinScore, int nodeVisit) {
13-
if (nodeVisit == 0)
12+
if (nodeVisit == 0) {
1413
return Integer.MAX_VALUE;
15-
return ((double) nodeWinScore / (double) nodeVisit) + 1.41 * Math.sqrt(Math.log(totalVisit) / (double) nodeVisit);
14+
}
15+
return (nodeWinScore / (double) nodeVisit) + 1.41 * Math.sqrt(Math.log(totalVisit) / (double) nodeVisit);
1616
}
1717

18-
public static Node findBestNodeWithUCT(Node node) {
18+
static Node findBestNodeWithUCT(Node node) {
1919
int parentVisit = node.getState().getVisitCount();
20-
List<Node> childNodes = node.getChildArray();
21-
return Collections.max(childNodes, Comparator.comparing(c -> {
22-
double score = uctValue(parentVisit, c.getState().getWinScore(), c.getState().getVisitCount());
23-
return score;
24-
}));
20+
return Collections.max(
21+
node.getChildArray(),
22+
Comparator.comparing(c -> uctValue(parentVisit, c.getState().getWinScore(), c.getState().getVisitCount())));
2523
}
2624
}

algorithms/src/test/java/algorithms/RtFiniteStateMachineLongRunningUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package algorithms;
22

3-
import com.baeldung.automata.*;
3+
import com.baeldung.algorithms.automata.*;
44
import org.junit.Test;
55

66
import static org.junit.Assert.assertTrue;

0 commit comments

Comments
 (0)