Merge changes on dev

This commit is contained in:
lieght
2026-01-16 14:45:48 +01:00
parent d02c7bd095
commit 95e96583ec
9 changed files with 54 additions and 48 deletions

View File

@@ -1,9 +1,9 @@
package org.toop; package org.toop;
import org.toop.app.App; import org.toop.app.App;
import org.toop.framework.gameFramework.model.player.Player; import org.toop.framework.game.games.reversi.BitboardReversi;
import org.toop.game.games.reversi.BitboardReversi; import org.toop.framework.game.players.ArtificialPlayer;
import org.toop.game.players.ArtificialPlayer;
import org.toop.game.players.ai.MCTSAI; import org.toop.game.players.ai.MCTSAI;
import org.toop.game.players.ai.RandomAI; import org.toop.game.players.ai.RandomAI;
import org.toop.game.players.ai.mcts.MCTSAI1; import org.toop.game.players.ai.mcts.MCTSAI1;
@@ -11,19 +11,24 @@ import org.toop.game.players.ai.mcts.MCTSAI2;
import org.toop.game.players.ai.mcts.MCTSAI3; import org.toop.game.players.ai.mcts.MCTSAI3;
import org.toop.game.players.ai.mcts.MCTSAI4; import org.toop.game.players.ai.mcts.MCTSAI4;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class Main { public final class Main {
static void main(String[] args) { static void main(String[] args) {
// App.run(args); ExecutorService executor = Executors.newFixedThreadPool(1);
testMCTS(25);
executor.execute(() -> testMCTS(25));
App.run(args);
} }
private static void testMCTS(int games) { private static void testMCTS(int games) {
var versions = new ArtificialPlayer[5]; var versions = new ArtificialPlayer[5];
versions[0] = new ArtificialPlayer<>(new RandomAI<BitboardReversi>(), "Random AI"); versions[0] = new ArtificialPlayer(new RandomAI(), "Random AI");
versions[1] = new ArtificialPlayer<>(new MCTSAI1<BitboardReversi>(1000), "MCTS V1 AI"); versions[1] = new ArtificialPlayer(new MCTSAI1(1000), "MCTS V1 AI");
versions[2] = new ArtificialPlayer<>(new MCTSAI2<BitboardReversi>(1000), "MCTS V2 AI"); versions[2] = new ArtificialPlayer(new MCTSAI2(1000), "MCTS V2 AI");
versions[3] = new ArtificialPlayer<>(new MCTSAI3<BitboardReversi>(10, 10), "MCTS V3 AI"); versions[3] = new ArtificialPlayer(new MCTSAI3(10, 10), "MCTS V3 AI");
versions[4] = new ArtificialPlayer<>(new MCTSAI4<BitboardReversi>(10, 10), "MCTS V4 AI"); versions[4] = new ArtificialPlayer(new MCTSAI4(10, 10), "MCTS V4 AI");
for (int i = 0; i < versions.length; i++) { for (int i = 0; i < versions.length; i++) {
for (int j = i + 1; j < versions.length; j++) { for (int j = i + 1; j < versions.length; j++) {
@@ -35,12 +40,13 @@ public final class Main {
} }
} }
private static void testAI(int games, ArtificialPlayer<BitboardReversi>[] ais) { private static void testAI(int games, ArtificialPlayer[] ais) {
int wins = 0; int wins = 0;
int ties = 0; int ties = 0;
for (int i = 0; i < games; i++) { for (int i = 0; i < games; i++) {
final BitboardReversi match = new BitboardReversi(ais); final BitboardReversi match = new BitboardReversi();
match.init(ais);
while (!match.isTerminal()) { while (!match.isTerminal()) {
final int currentAI = match.getCurrentTurn(); final int currentAI = match.getCurrentTurn();
@@ -48,7 +54,7 @@ public final class Main {
match.play(move); match.play(move);
if (ais[currentAI].getAi() instanceof MCTSAI<?> mcts) { if (ais[currentAI].getAi() instanceof MCTSAI mcts) {
final int lastIterations = mcts.getLastIterations(); final int lastIterations = mcts.getLastIterations();
System.out.printf("iterations %s: %d\n", ais[currentAI].getName(), lastIterations); System.out.printf("iterations %s: %d\n", ais[currentAI].getName(), lastIterations);
} }

View File

@@ -20,8 +20,7 @@ import org.toop.framework.networking.connection.clients.TournamentNetworkingClie
import org.toop.framework.networking.connection.events.NetworkEvents; import org.toop.framework.networking.connection.events.NetworkEvents;
import org.toop.framework.networking.connection.types.NetworkingConnector; import org.toop.framework.networking.connection.types.NetworkingConnector;
import org.toop.framework.networking.server.gateway.NettyGatewayServer; import org.toop.framework.networking.server.gateway.NettyGatewayServer;
import org.toop.framework.game.players.LocalPlayer; import org.toop.game.players.ai.mcts.MCTSAI3;
import org.toop.game.players.ai.MCTSAI3;
import org.toop.local.AppContext; import org.toop.local.AppContext;
import java.util.Arrays; import java.util.Arrays;
@@ -211,7 +210,7 @@ public final class Server {
Player[] players = new Player[2]; Player[] players = new Player[2];
players[userStartingTurn] = new ArtificialPlayer(new MCTSAI3(1000), user); players[userStartingTurn] = new ArtificialPlayer(new MCTSAI3(1000, Runtime.getRuntime().availableProcessors()), user);
players[opponentStartingTurn] = new OnlinePlayer(response.opponent()); players[opponentStartingTurn] = new OnlinePlayer(response.opponent());
switch (type) { switch (type) {

View File

@@ -4,6 +4,7 @@ import javafx.application.Platform;
import org.toop.app.GameInformation; import org.toop.app.GameInformation;
import org.toop.app.gameControllers.ReversiBitController; import org.toop.app.gameControllers.ReversiBitController;
import org.toop.app.gameControllers.TicTacToeBitController; import org.toop.app.gameControllers.TicTacToeBitController;
import org.toop.framework.game.players.LocalPlayer;
import org.toop.framework.gameFramework.controller.GameController; import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.model.player.Player; import org.toop.framework.gameFramework.model.player.Player;
import org.toop.framework.game.players.ArtificialPlayer; import org.toop.framework.game.players.ArtificialPlayer;
@@ -12,13 +13,9 @@ import org.toop.app.widget.complex.PlayerInfoWidget;
import org.toop.app.widget.complex.ViewWidget; import org.toop.app.widget.complex.ViewWidget;
import org.toop.app.widget.popup.ErrorPopup; import org.toop.app.widget.popup.ErrorPopup;
import org.toop.app.widget.tutorial.*; import org.toop.app.widget.tutorial.*;
import org.toop.game.players.ai.MCTSAI1;
import org.toop.game.players.ai.MCTSAI2;
import org.toop.game.players.ai.MCTSAI3;
import org.toop.game.players.ai.MCTSAI4;
import org.toop.game.players.ai.MCTSAI5;
import org.toop.game.players.ai.MiniMaxAI; import org.toop.game.players.ai.MiniMaxAI;
import org.toop.game.players.ai.RandomAI; import org.toop.game.players.ai.mcts.MCTSAI1;
import org.toop.game.players.ai.mcts.MCTSAI3;
import org.toop.local.AppContext; import org.toop.local.AppContext;
import javafx.geometry.Pos; import javafx.geometry.Pos;
@@ -56,7 +53,7 @@ public class LocalMultiplayerView extends ViewWidget {
if (information.players[0].isHuman) { if (information.players[0].isHuman) {
players[0] = new LocalPlayer(information.players[0].name); players[0] = new LocalPlayer(information.players[0].name);
} else { } else {
players[0] = new ArtificialPlayer(new MCTSAI(100), "MCTS AI"); players[0] = new ArtificialPlayer(new MCTSAI1(100), "MCTS AI");
} }
if (information.players[1].isHuman) { if (information.players[1].isHuman) {
players[1] = new LocalPlayer(information.players[1].name); players[1] = new LocalPlayer(information.players[1].name);
@@ -85,12 +82,12 @@ public class LocalMultiplayerView extends ViewWidget {
players[0] = new LocalPlayer(information.players[0].name); players[0] = new LocalPlayer(information.players[0].name);
} else { } else {
// players[0] = new ArtificialPlayer(new RandomAI<BitboardReversi>(), "Random AI"); // players[0] = new ArtificialPlayer(new RandomAI<BitboardReversi>(), "Random AI");
players[0] = new ArtificialPlayer(new MCTSAI3(50), "MCTS V3 AI"); players[0] = new ArtificialPlayer(new MCTSAI3(50, Runtime.getRuntime().availableProcessors()), "MCTS V3 AI");
} }
if (information.players[1].isHuman) { if (information.players[1].isHuman) {
players[1] = new LocalPlayer(information.players[1].name); players[1] = new LocalPlayer(information.players[1].name);
} else { } else {
players[1] = new ArtificialPlayer(new MCTSAI(50), "MCTS V1 AI"); players[1] = new ArtificialPlayer(new MCTSAI1(50), "MCTS V1 AI");
} }
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) { if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
new ShowEnableTutorialWidget( new ShowEnableTutorialWidget(

View File

@@ -57,4 +57,8 @@ public class ArtificialPlayer extends AbstractPlayer {
public ArtificialPlayer deepCopy() { public ArtificialPlayer deepCopy() {
return new ArtificialPlayer(this); return new ArtificialPlayer(this);
} }
public AI getAi() {
return ai;
}
} }

View File

@@ -5,8 +5,8 @@ import org.toop.framework.gameFramework.model.player.AbstractAI;
import java.util.Random; import java.util.Random;
public class MCTSAI2 extends AbstractAI { public abstract class MCTSAI extends AbstractAI {
private static class Node { protected static class Node {
public TurnBasedGame state; public TurnBasedGame state;
public long move; public long move;
@@ -181,7 +181,7 @@ public class MCTSAI2 extends AbstractAI {
return mostVisitedChild; return mostVisitedChild;
} }
protected Node findOrResetRoot(Node root, T game) { protected Node findOrResetRoot(Node root, TurnBasedGame game) {
if (root == null) { if (root == null) {
return new Node(game.deepCopy()); return new Node(game.deepCopy());
} }

View File

@@ -3,22 +3,22 @@ package org.toop.game.players.ai.mcts;
import org.toop.framework.gameFramework.model.game.TurnBasedGame; import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.game.players.ai.MCTSAI; import org.toop.game.players.ai.MCTSAI;
public class MCTSAI1<T extends TurnBasedGame<T>> extends MCTSAI<T> { public class MCTSAI1 extends MCTSAI {
public MCTSAI1(int milliseconds) { public MCTSAI1(int milliseconds) {
super(milliseconds); super(milliseconds);
} }
public MCTSAI1(MCTSAI1<T> other) { public MCTSAI1(MCTSAI1 other) {
super(other); super(other);
} }
@Override @Override
public MCTSAI1<T> deepCopy() { public MCTSAI1 deepCopy() {
return new MCTSAI1<>(this); return new MCTSAI1(this);
} }
@Override @Override
public long getMove(T game) { public long getMove(TurnBasedGame game) {
final Node root = new Node(game, null, 0L); final Node root = new Node(game, null, 0L);
final long endTime = System.nanoTime() + milliseconds * 1_000_000L; final long endTime = System.nanoTime() + milliseconds * 1_000_000L;

View File

@@ -3,7 +3,7 @@ package org.toop.game.players.ai.mcts;
import org.toop.framework.gameFramework.model.game.TurnBasedGame; import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.game.players.ai.MCTSAI; import org.toop.game.players.ai.MCTSAI;
public class MCTSAI2<T extends TurnBasedGame<T>> extends MCTSAI<T> { public class MCTSAI2 extends MCTSAI {
private Node root; private Node root;
public MCTSAI2(int milliseconds) { public MCTSAI2(int milliseconds) {
@@ -12,19 +12,19 @@ public class MCTSAI2<T extends TurnBasedGame<T>> extends MCTSAI<T> {
this.root = null; this.root = null;
} }
public MCTSAI2(MCTSAI2<T> other) { public MCTSAI2(MCTSAI2 other) {
super(other); super(other);
this.root = other.root; this.root = other.root;
} }
@Override @Override
public MCTSAI2<T> deepCopy() { public MCTSAI2 deepCopy() {
return new MCTSAI2<>(this); return new MCTSAI2(this);
} }
@Override @Override
public long getMove(T game) { public long getMove(TurnBasedGame game) {
root = findOrResetRoot(root, game); root = findOrResetRoot(root, game);
final long endTime = System.nanoTime() + milliseconds * 1_000_000L; final long endTime = System.nanoTime() + milliseconds * 1_000_000L;

View File

@@ -10,7 +10,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
public class MCTSAI3<T extends TurnBasedGame<T>> extends MCTSAI<T> { public class MCTSAI3 extends MCTSAI {
private final int threads; private final int threads;
public MCTSAI3(int milliseconds, int threads) { public MCTSAI3(int milliseconds, int threads) {
@@ -19,19 +19,19 @@ public class MCTSAI3<T extends TurnBasedGame<T>> extends MCTSAI<T> {
this.threads = threads; this.threads = threads;
} }
public MCTSAI3(MCTSAI3<T> other) { public MCTSAI3(MCTSAI3 other) {
super(other); super(other);
this.threads = other.threads; this.threads = other.threads;
} }
@Override @Override
public MCTSAI3<T> deepCopy() { public MCTSAI3 deepCopy() {
return new MCTSAI3<>(this); return new MCTSAI3(this);
} }
@Override @Override
public long getMove(T game) { public long getMove(TurnBasedGame game) {
final ExecutorService pool = Executors.newFixedThreadPool(threads); final ExecutorService pool = Executors.newFixedThreadPool(threads);
final long endTime = System.nanoTime() + milliseconds * 1_000_000L; final long endTime = System.nanoTime() + milliseconds * 1_000_000L;

View File

@@ -10,7 +10,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
public class MCTSAI4<T extends TurnBasedGame<T>> extends MCTSAI<T> { public class MCTSAI4 extends MCTSAI {
private final int threads; private final int threads;
private final Node[] threadRoots; private final Node[] threadRoots;
@@ -21,7 +21,7 @@ public class MCTSAI4<T extends TurnBasedGame<T>> extends MCTSAI<T> {
this.threadRoots = new Node[threads]; this.threadRoots = new Node[threads];
} }
public MCTSAI4(MCTSAI4<T> other) { public MCTSAI4(MCTSAI4 other) {
super(other); super(other);
this.threads = other.threads; this.threads = other.threads;
@@ -29,12 +29,12 @@ public class MCTSAI4<T extends TurnBasedGame<T>> extends MCTSAI<T> {
} }
@Override @Override
public MCTSAI4<T> deepCopy() { public MCTSAI4 deepCopy() {
return new MCTSAI4<>(this); return new MCTSAI4(this);
} }
@Override @Override
public long getMove(T game) { public long getMove(TurnBasedGame game) {
for (int i = 0; i < threads; i++) { for (int i = 0; i < threads; i++) {
threadRoots[i] = findOrResetRoot(threadRoots[i], game); threadRoots[i] = findOrResetRoot(threadRoots[i], game);
} }