Search in sources :

Example 1 with UctSearch

use of suite.uct.UctSearch in project suite by stupidsing.

the class UctWeiqiMain method main.

public static void main(String[] args) {
    InputStreamReader isr = new InputStreamReader(System.in, Constants.charset);
    BufferedReader br = new BufferedReader(isr);
    DecimalFormat df = new DecimalFormat("0.000");
    int nThreads = Constants.nThreads;
    int nSimulations = 10000 * nThreads;
    int boundedTime = 30000;
    Weiqi.adjustSize(7);
    Board board = new Board();
    MovingGameSet gameSet = new MovingGameSet(board, startingPlayer);
    boolean auto = false;
    boolean quit = false;
    String status = "LET'S PLAY!";
    while (!quit) {
        GameSet gameSet1 = new GameSet(gameSet);
        UctVisitor<Coordinate> visitor = UctWeiqi.newVisitor(gameSet1);
        UctSearch<Coordinate> search = new UctSearch<>(visitor);
        search.setNumberOfThreads(nThreads);
        search.setNumberOfSimulations(nSimulations);
        search.setBoundedTime(boundedTime);
        if (auto || gameSet.getNextPlayer() == computerPlayer) {
            System.out.println("THINKING...");
            Stopwatch<Coordinate> sw = Stopwatch.of(search::search);
            Coordinate coord = sw.result;
            if (coord != null) {
                status = // 
                gameSet.getNextPlayer() + " " + // 
                coord + " " + // 
                df.format(search.getWinningChance()) + " " + sw.duration + "ms";
                gameSet.play(coord);
                if (auto)
                    display(gameSet, status);
            } else {
                System.out.println("I LOSE");
                quit = true;
            }
        }
        while (!auto && !quit && gameSet.getNextPlayer() == humanPlayer) try {
            display(gameSet, status);
            String line = br.readLine();
            if (line != null)
                switch(line) {
                    case "auto":
                        auto = true;
                        break;
                    case "load":
                        gameSet = loadGameSet(br);
                        break;
                    case "undo":
                        gameSet.undo();
                        gameSet.undo();
                        status = "AFTER UNDO:";
                        break;
                    default:
                        if (!String_.isBlank(line)) {
                            Pair<String, String> pos = String_.split2(line, ",");
                            int x = Integer.parseInt(pos.t0);
                            int y = Integer.parseInt(pos.t1);
                            gameSet.play(Coordinate.c(x, y));
                        }
                }
            else
                quit = true;
        } catch (Exception ex) {
            LogUtil.error(ex);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DecimalFormat(java.text.DecimalFormat) IOException(java.io.IOException) UctSearch(suite.uct.UctSearch) BufferedReader(java.io.BufferedReader)

Example 2 with UctSearch

use of suite.uct.UctSearch in project suite by stupidsing.

the class UctTest method testUctGame.

@Test
public void testUctGame() {
    new Profiler().profile(() -> {
        DecimalFormat df = new DecimalFormat("0.000");
        // 20000
        int nSimulations = 5000;
        int boundedTime = 300000;
        int seed = new Random().nextInt();
        System.out.println("RANDOM SEED = " + seed);
        ShuffleUtil.setSeed(seed);
        Board board = new Board();
        GameSet gameSet = new GameSet(board, Occupation.BLACK);
        while (true) {
            GameSet gameSet1 = new GameSet(gameSet);
            UctVisitor<Coordinate> visitor = UctWeiqi.newVisitor(gameSet1);
            UctSearch<Coordinate> search = new UctSearch<>(visitor);
            search.setNumberOfThreads(Constants.nThreads);
            search.setNumberOfSimulations(nSimulations);
            search.setBoundedTime(boundedTime);
            Stopwatch<Coordinate> timed = Stopwatch.of(search::search);
            Coordinate move = timed.result;
            if (move == null)
                break;
            Occupation player = gameSet.getNextPlayer();
            search.dumpPrincipalVariation();
            System.out.println(// 
            player + " " + // 
            move + " " + // 
            df.format(search.getWinningChance()) + " " + timed.duration + "ms");
            gameSet.play(move);
            UserInterface.display(gameSet);
        }
    });
}
Also used : DecimalFormat(java.text.DecimalFormat) UctSearch(suite.uct.UctSearch) Occupation(suite.weiqi.Weiqi.Occupation) Profiler(suite.sample.Profiler) Random(java.util.Random) Test(org.junit.Test)

Aggregations

DecimalFormat (java.text.DecimalFormat)2 UctSearch (suite.uct.UctSearch)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Random (java.util.Random)1 Test (org.junit.Test)1 Profiler (suite.sample.Profiler)1 Occupation (suite.weiqi.Weiqi.Occupation)1