- Updated the Node interface and its implementations to include a strength() function. This is a double value between 0 and 1 representing the percent activation.

- Updated the PlayerModel to fix a bug in the high score tracking.
- Created HighScoreDialog.java, which displays the high scores from a PlayerModel. In the future, it will have a "New Game" button so that it can be used to transition between games.
This commit is contained in:
Marshall
2012-04-26 13:04:41 -04:00
parent 0cfe26faf1
commit 24096b50a8
5 changed files with 104 additions and 14 deletions

View File

@@ -6,4 +6,5 @@ public interface Node {
void learn(boolean correct); void learn(boolean correct);
double strength();
} }

View File

@@ -21,9 +21,18 @@ public class PlayerModel {
private int nextHighInGames = 0; private int nextHighInGames = 0;
// One node for each tile. // One node for each tile plus four for the colors to be selected.
private final SigmoidNode[] outputNode = new SigmoidNode[Board.NUM_COLS // outputNode[0] is blue.
* Board.NUM_ROWS]; // 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.
private final SigmoidNode[] outputNode = new SigmoidNode[(Board.NUM_COLS * Board.NUM_ROWS) + 4];
public PlayerModel() { public PlayerModel() {
for (int i = 0; i < highScore.length; i++) { for (int i = 0; i < highScore.length; i++) {
@@ -54,6 +63,10 @@ public class PlayerModel {
} }
} }
public int[] getHighScores() {
return highScore;
}
public boolean[] getPrediction(boolean[] input) { public boolean[] getPrediction(boolean[] input) {
if (input.length == inputNode.length) { if (input.length == inputNode.length) {
boolean[] prediction = new boolean[outputNode.length]; boolean[] prediction = new boolean[outputNode.length];
@@ -118,12 +131,11 @@ public class PlayerModel {
else { else {
loop: for (int i = 0; i < highScore.length; i++) { loop: for (int i = 0; i < highScore.length; i++) {
if (score > highScore[i]) { if (score > highScore[i]) {
for (int j = i; j < highScore.length - 1; j++) { for (int j = highScore.length - 2; j >= i; j--) {
highScore[j + 1] = highScore[j]; highScore[j + 1] = highScore[j];
} }
highScore[i] = score; highScore[i] = score;
break loop; break loop;
} }
} }

View File

@@ -19,4 +19,9 @@ public class InputNode implements Node {
public void setStimulation(boolean active) { public void setStimulation(boolean active) {
dendrite = active; dendrite = active;
} }
@Override
public double strength() {
return axon() ? 1 : 0;
}
} }

View File

@@ -4,7 +4,6 @@ import java.util.Hashtable;
import model.playerModel.Node; import model.playerModel.Node;
public class SigmoidNode implements Node { public class SigmoidNode implements Node {
// Training rate. // Training rate.
@@ -23,14 +22,7 @@ public class SigmoidNode implements Node {
@Override @Override
public boolean axon() { public boolean axon() {
double sum = 0; return strength() > THRESHOLD;
for (Node n : dendrites.keySet()) {
sum += n.axon() ? dendrites.get(n) : 0;
}
return (activation(sum) > THRESHOLD);
} }
@Override @Override
@@ -44,6 +36,17 @@ public class SigmoidNode implements Node {
} }
} }
@Override
public double strength() {
double sum = 0;
for (Node n : dendrites.keySet()) {
sum += n.axon() ? dendrites.get(n) : 0;
}
return activation(sum);
}
private double activation(double sum) { private double activation(double sum) {
// Sigmoid function: // Sigmoid function:
// 1/(1+(e^(-bt))) // 1/(1+(e^(-bt)))

View File

@@ -0,0 +1,69 @@
package view;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import model.playerModel.PlayerModel;
public class HighScoreDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
JFrame x = new JFrame();
x.setDefaultCloseOperation(EXIT_ON_CLOSE);
x.setVisible(true);
PlayerModel pm = new PlayerModel();
pm.logGame(100);
pm.logGame(90);
pm.logGame(80);
pm.logGame(70);
pm.logGame(60);
pm.logGame(50);
pm.logGame(40);
pm.logGame(30);
pm.logGame(20);
pm.logGame(10);
pm.logGame(98);
pm.logGame(105);
new HighScoreDialog(x, pm);
}
public HighScoreDialog(JFrame owner, PlayerModel pm) {
super(owner, "Game over!", true);
String text = "<html>";
text += "<b>Game Over!</b>";
text += "<br><br>";
text += "<em>High Scores:</em>";
text += "<br><ol>";
int[] highScores = pm.getHighScores();
loop: for (int i = 0; i < highScores.length; i++) {
if (highScores[i] == -1) {
break loop;
}
else {
text += "<li>" + highScores[i] + "</li>";
}
}
text += "</ol></html>";
JLabel label = new JLabel(text);
label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.add(label);
this.pack();
this.setResizable(false);
this.setVisible(true);
}
}