Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes MCTS and adapts Tester to it #181

Merged
merged 20 commits into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/src/main/java/Bamboo/controller/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import Bamboo.controller.heuristics.Heuristic;
import Bamboo.model.Game;
import Bamboo.model.GameWithoutGUI;

import java.awt.Color;

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/Bamboo/controller/AgentFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Bamboo.controller.nNet.NeuralNetwork;
import Bamboo.controller.random.Random;

import java.awt.*;
import java.awt.Color;
import java.io.IOException;

public class AgentFactory
Expand Down
40 changes: 26 additions & 14 deletions app/src/main/java/Bamboo/controller/MCTS/MCTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
import Bamboo.controller.heuristics.Heuristic;
import Bamboo.controller.heuristics.Uniform;
import Bamboo.model.Game;
import Bamboo.model.GameWithoutGUI;

import java.awt.Color;

public class MCTS implements Agent
{
private Color colour;
private NodeMCTS root;
public Mutable<Integer> iterations = new Mutable<>(10000);
private int iter = 10000;
private Node root;
private boolean testing = false;
public Mutable<Float> c = new Mutable<>(0.5f);
public Mutable<Integer> iterations = new Mutable<>(200);
public Mutable<Float> c = new Mutable<>(1f);
public Mutable<Heuristic> heuristic = new Mutable<>(new Uniform());

public MCTS(Color colour)
Expand All @@ -44,19 +42,32 @@ public boolean isHuman()
@Override
public Vector getNextMove(Game game)
{
if(game instanceof GameWithoutGUI)
iter = iterations.get();
Node lastMove = null;
if(root != null)
lastMove = root.selectChild(game.getPreviousMove());

root = new NodeMCTS(game, null, game.getCurrentPlayer().getColor(), null);
if(lastMove != null)
root = lastMove;
else
root = new Node(game.getGrid(), game.getCurrentPlayer().getColor(), null, null);
UCB.C = c.get();
int mutableValue = 0;
if(testing)
mutableValue = Math.round((float)(Number)iterations.get());
int iter = testing ? mutableValue : iterations.get();
for(int i = 0; i < iter; i++)
{
NodeMCTS next = root.select();
next.heuristic = heuristic.get();
root.expand(next);
next.backProp(next.playout());
Node n = root.select();
if(n != null)
{
n.playout();
n.backprop();
}
}
NodeMCTS bestMove = root.selectBest();
return bestMove.getMove();

root = root.bestSelect();
root.pruneAbove();
return root.move();
}

@Override
Expand All @@ -77,6 +88,7 @@ public Mutable<Integer> getIterations() {

@Override
public Mutable<Float> getC() {
testing = true;
return this.c;
}

Expand Down
172 changes: 172 additions & 0 deletions app/src/main/java/Bamboo/controller/MCTS/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package Bamboo.controller.MCTS;

import Bamboo.controller.Vector;
import Bamboo.controller.heuristics.Heuristic;
import Bamboo.controller.heuristics.OuterWeighted;
import Bamboo.model.Grid;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Stack;

public class Node
{
private int visits = 1;
private int plays = 0;
private int wins = 0;
private Color colour;
private Grid grid;
private Vector move;
private Node parent;
private Heuristic heuristic;
private ArrayList<Node> children;
private Stack<Vector> unexplored;

public Node(Grid grid, Color colour, Vector move, Node parent)
{
this.grid = grid.copy();
this.colour = colour;
this.move = move;
this.parent = parent;
heuristic = new OuterWeighted();
unexplored = grid.getRemainingMovesStack();
children = new ArrayList<>();
}

public Node select()
{
visits++;
Node node;

Vector v = nextLegalMove();
if(v != null)
{
Color c = toggleColour(colour);
node = new Node(grid, c, v, this);
children.add(node);
}

else
{
node = UCBSelect();
}

return node;
}

/**
* Plays the game randomly until termination
* @return if win -> 1,
* else -> 0
*/
public void playout()
{
plays++;

Color startingColour = colour;
Color currentColor = colour;

while (!grid.isFinished(currentColor))
{
Vector v = heuristic.getNextMove(grid, currentColor);
grid.setTile(v, currentColor);
currentColor = toggleColour(currentColor);
}

if(currentColor != startingColour)
wins++;
}

public void backprop()
{
if (parent != null)
{
parent.update(plays, wins, visits);
parent.backprop();
}
}

public Node bestSelect()
{
Node best = null;
for(Node child: children)
{
if(best == null)
best = child;

if(child.wins > best.wins)
best = child;
}
return best;
}

public Node UCBSelect()
{
Node best = null;
double bestUCB = 0;
for(Node child: children)
{
if(best == null)
best = child;

double childUCB = UCB.calculate(wins, plays, child.visits, visits);
if(childUCB > bestUCB)
{
best = child;
bestUCB = childUCB;
}
}
return best;
}

public Node selectChild(Vector v)
{
for(Node child: children)
{
if(child.move().equals(v))
return child;
}
return null;
}

public void pruneAbove()
{
parent = null;
}

public Vector move()
{
return move;
}

private void update(int plays, int wins, int visits)
{
this.plays += plays;
this.wins += wins;
this.visits += visits;
}

private Vector nextLegalMove()
{
while (!unexplored.empty())
{
Vector v = unexplored.pop();
if(grid.isLegalMove(v, colour))
return v;
}
return null;
}

private Color toggleColour(Color colour)
{
if(colour == Color.BLUE)
return Color.RED;
return Color.BLUE;
}

@Override
public String toString()
{
return "Children: " + children.size() + " Visits: " + visits + " Plays: " + plays + " Wins: " + wins;
}
}
Loading