- Implemented multiple users, including a selection dialog and automatic preference saving and loading.
- Integrated the ANN with the game. The network now predicts a user move, completely ignores it, and trains itself on the players actual move. This integration also included implementing two new functions. The first translates a board state to a boolean array to correspond with input nodes. The second translates a move to a boolean array to correspond with output nodes.
This commit is contained in:
@@ -17,30 +17,27 @@ import model.playerModel.node.SigmoidNode;
|
||||
|
||||
public class PlayerModel implements Serializable {
|
||||
|
||||
public static final String PLAYER_MODEL_PATH = "playerModel.dat"; // Path to
|
||||
// the
|
||||
// stored
|
||||
// player
|
||||
// model.
|
||||
public static final String DATA_FOLDER = "data/";
|
||||
public static final String PLAYER_MODEL_PREFIX = "playerModel";
|
||||
public static final String PLAYER_MODEL_SUFFIX = ".dat";
|
||||
public static final Random rand = new Random(); // Randomizer object.
|
||||
public static final boolean TRY_LOAD = true; // Set to false if any existing
|
||||
// player model should be
|
||||
// discarded and the next
|
||||
// game should begin a new
|
||||
// sequence.
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static boolean exists() {
|
||||
return (new File(PLAYER_MODEL_PATH)).exists();
|
||||
public static boolean exists(String playerName) {
|
||||
return (new File(getPlayerPath(playerName))).exists();
|
||||
}
|
||||
|
||||
public static PlayerModel load() {
|
||||
public static String getPlayerPath(String playerName) {
|
||||
return DATA_FOLDER + PLAYER_MODEL_PREFIX + "_" + playerName
|
||||
+ PLAYER_MODEL_SUFFIX;
|
||||
}
|
||||
|
||||
public static PlayerModel load(String path) {
|
||||
FileInputStream fin = null;
|
||||
ObjectInputStream oin = null;
|
||||
|
||||
try {
|
||||
fin = new FileInputStream(PLAYER_MODEL_PATH);
|
||||
fin = new FileInputStream(path);
|
||||
oin = new ObjectInputStream(fin);
|
||||
PlayerModel pm = (PlayerModel) oin.readObject();
|
||||
oin.close();
|
||||
@@ -54,29 +51,31 @@ public class PlayerModel implements Serializable {
|
||||
private final SigmoidNode[] hiddenLayer;
|
||||
private int highScoresAchieved = 0;
|
||||
|
||||
// One node for each tile-color combination, plus one for each upcoming
|
||||
// tile-color combination.
|
||||
// One node for each tile-color combination.
|
||||
private final InputNode[] inputNode = new InputNode[(Board.NUM_COLS
|
||||
* Board.NUM_ROWS * (Board.TileColor.values().length - 1))];
|
||||
|
||||
private final String name;
|
||||
|
||||
private int nextHighInGames = 0;
|
||||
|
||||
// One node for each tile plus four for the colors to be selected.
|
||||
// outputNode[0] is blue.
|
||||
// outputNode[1] is green.
|
||||
// outputNode[2] is red.
|
||||
// outputNode[3] is yellow.
|
||||
// outputNode[4] through outputNode[n] represent grid spaces. A true means
|
||||
// that the player is predicted to place on that tile.
|
||||
// They should be read from the top-left to bottom-right, across rows.
|
||||
// Ideally, the network should return only one true between 0 and 3 and only
|
||||
// one true between 4 and n, representing one color and the tile in which it
|
||||
// should be placed.
|
||||
/*
|
||||
* One node for each tile plus four for the colors to be selected.
|
||||
* outputNode[0] is blue. outputNode[1] is green. outputNode[2] is red.
|
||||
* outputNode[3] is yellow. outputNode[4] through outputNode[n] represent
|
||||
* grid spaces. A true means that the player is predicted to place on that
|
||||
* tile. They should be read from the top-left to bottom-right, across rows.
|
||||
* Ideally, the network should return only one true between 0 and 3 and only
|
||||
* one true between 4 and n, representing one color and the tile in which it
|
||||
* should be placed. See Referee.getMoveArray().
|
||||
*/
|
||||
private final SigmoidNode[] outputNode = new SigmoidNode[(Board.NUM_COLS * Board.NUM_ROWS) + 4];
|
||||
|
||||
private final ArrayList<GameLog> scores = new ArrayList<GameLog>();
|
||||
|
||||
public PlayerModel() {
|
||||
public PlayerModel(String nme) {
|
||||
name = nme;
|
||||
|
||||
hiddenLayer = new SigmoidNode[inputNode.length
|
||||
+ ((inputNode.length * 2) / 3)];
|
||||
|
||||
@@ -121,6 +120,18 @@ public class PlayerModel implements Serializable {
|
||||
return highScores;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getNumInputNodes() {
|
||||
return inputNode.length;
|
||||
}
|
||||
|
||||
public int getNumOutputNodes() {
|
||||
return outputNode.length;
|
||||
}
|
||||
|
||||
public boolean[] getPrediction(boolean[] input) {
|
||||
if (input.length == inputNode.length) {
|
||||
boolean[] prediction = new boolean[outputNode.length];
|
||||
@@ -187,13 +198,17 @@ public class PlayerModel implements Serializable {
|
||||
public boolean save() {
|
||||
FileOutputStream fout = null;
|
||||
ObjectOutputStream oout = null;
|
||||
String path = getPlayerPath(getName());
|
||||
try {
|
||||
fout = new FileOutputStream(PLAYER_MODEL_PATH);
|
||||
(new File(DATA_FOLDER)).mkdirs();
|
||||
|
||||
fout = new FileOutputStream(path);
|
||||
oout = new ObjectOutputStream(fout);
|
||||
oout.writeObject(this);
|
||||
oout.close();
|
||||
return true;
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user