update mcts

This commit is contained in:
ramollia
2026-01-16 15:55:25 +01:00
parent c4b9378128
commit 3e4a343c4e
4 changed files with 15 additions and 15 deletions

View File

@@ -1,6 +1,5 @@
package org.toop; package org.toop;
import org.toop.app.App; import org.toop.app.App;
import org.toop.framework.game.games.reversi.BitboardReversi; import org.toop.framework.game.games.reversi.BitboardReversi;
import org.toop.framework.game.players.ArtificialPlayer; import org.toop.framework.game.players.ArtificialPlayer;
@@ -16,13 +15,13 @@ import java.util.concurrent.Executors;
public final class Main { public final class Main {
static void main(String[] args) { static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(() -> testMCTS(25));
App.run(args); App.run(args);
// final ExecutorService executor = Executors.newFixedThreadPool(1);
// executor.execute(() -> testAIs(25));
} }
private static void testMCTS(int games) { private static void testAIs(int games) {
var versions = new ArtificialPlayer[5]; var versions = new ArtificialPlayer[5];
versions[0] = new ArtificialPlayer(new RandomAI(), "Random AI"); versions[0] = new ArtificialPlayer(new RandomAI(), "Random AI");
versions[1] = new ArtificialPlayer(new MCTSAI1(1000), "MCTS V1 AI"); versions[1] = new ArtificialPlayer(new MCTSAI1(1000), "MCTS V1 AI");
@@ -35,12 +34,12 @@ public final class Main {
final int playerIndex1 = i % versions.length; final int playerIndex1 = i % versions.length;
final int playerIndex2 = j % versions.length; final int playerIndex2 = j % versions.length;
testAI(games, new ArtificialPlayer[] { versions[playerIndex1], versions[playerIndex2]}); testAIVSAI(games, new ArtificialPlayer[] { versions[playerIndex1], versions[playerIndex2]});
} }
} }
} }
private static void testAI(int games, ArtificialPlayer[] ais) { private static void testAIVSAI(int games, ArtificialPlayer[] ais) {
int wins = 0; int wins = 0;
int ties = 0; int ties = 0;
@@ -53,11 +52,6 @@ public final class Main {
final long move = ais[currentAI].getMove(match); final long move = ais[currentAI].getMove(match);
match.play(move); match.play(move);
if (ais[currentAI].getAi() instanceof MCTSAI mcts) {
final int lastIterations = mcts.getLastIterations();
System.out.printf("iterations %s: %d\n", ais[currentAI].getName(), lastIterations);
}
} }
if (match.getWinner() < 0) { if (match.getWinner() < 0) {

View File

@@ -16,6 +16,7 @@ import org.toop.app.widget.tutorial.*;
import org.toop.game.players.ai.MiniMaxAI; import org.toop.game.players.ai.MiniMaxAI;
import org.toop.game.players.ai.mcts.MCTSAI1; import org.toop.game.players.ai.mcts.MCTSAI1;
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.local.AppContext; import org.toop.local.AppContext;
import javafx.geometry.Pos; import javafx.geometry.Pos;
@@ -82,12 +83,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, Runtime.getRuntime().availableProcessors()), "MCTS V3 AI"); players[0] = new ArtificialPlayer(new MCTSAI4(500, 4), "MCTS V4 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 MCTSAI1(50), "MCTS V1 AI"); players[1] = new ArtificialPlayer(new MCTSAI1(500), "MCTS V1 AI");
} }
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) { if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
new ShowEnableTutorialWidget( new ShowEnableTutorialWidget(

View File

@@ -92,10 +92,14 @@ public abstract class MCTSAI extends AbstractAI {
public MCTSAI(int milliseconds) { public MCTSAI(int milliseconds) {
this.milliseconds = milliseconds; this.milliseconds = milliseconds;
this.lastIterations = 0;
} }
public MCTSAI(MCTSAI other) { public MCTSAI(MCTSAI other) {
this.milliseconds = other.milliseconds; this.milliseconds = other.milliseconds;
this.lastIterations = other.lastIterations;
} }
public int getLastIterations() { public int getLastIterations() {

View File

@@ -41,7 +41,8 @@ public class MCTSAI3 extends MCTSAI {
tasks.add(() -> { tasks.add(() -> {
final Node localRoot = new Node(game.deepCopy()); final Node localRoot = new Node(game.deepCopy());
while (Float.isNaN(localRoot.solved) && System.nanoTime() < endTime) { // while (Float.isNaN(localRoot.solved) && System.nanoTime() < endTime) {
while (System.nanoTime() < endTime) {
Node leaf = selection(localRoot); Node leaf = selection(localRoot);
leaf = expansion(leaf); leaf = expansion(leaf);
final float value = simulation(leaf); final float value = simulation(leaf);