Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions core/src/main/java/com/buaisociety/pacman/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public void create() {

public @NotNull Neat createNeat() {
// Change this to true/false as needed, if you want to load from file
if (false) {
if (true) {
// TODO: Change this to the exact file you want to load
File exactFile = new File("saves" + File.separator + "oct26-4" + File.separator + "generation-51.json");
File exactFile = new File("saves" + File.separator + "oct21-1" + File.separator + "generation-19.json");
// load exactFile contents to string
String json;
try {
Expand All @@ -115,7 +115,7 @@ public void create() {
}
NeatImpl impl = NeatImpl.fromJson(json);
// modify this as needed
//impl.updateNodeCounts(8, 4); // Add 4 new inputs
impl.updateNodeCounts(5, 4); // Add 4 new inputs
//impl.updateClients(200); // have 200 pacman games at once
return impl;
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/buaisociety/pacman/Tournament.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Tournament extends ApplicationAdapter {
*/
public Behavior setupBehavior() {
// TODO: Choose your best client here
File file = new File("saves" + File.separator + "oct20-4" + File.separator + "best-calculator-127.json");
File file = new File("saves" + File.separator + "oct21-3" + File.separator + "best-calculator-56.json");
if (!file.exists()) {
System.err.println("Could not find the file: " + file.getAbsolutePath());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.ThreadLocalRandom;

public class NeatPacmanBehavior implements Behavior {

private final @NotNull Client client;
Expand All @@ -21,6 +23,9 @@ public class NeatPacmanBehavior implements Behavior {
// specific pools of points instead of subtracting from all.
private int scoreModifier = 0;

private int numberUpdatesSinceLastScore = 0;
private int lastScore = 0;

public NeatPacmanBehavior(@NotNull Client client) {
this.client = client;
}
Expand All @@ -39,7 +44,18 @@ public Direction getDirection(@NotNull Entity entity) {
}

// SPECIAL TRAINING CONDITIONS
// TODO: Make changes here to help with your training...

int newScore = pacman.getMaze().getLevelManager().getScore();
if (newScore > lastScore) {
lastScore = newScore;
numberUpdatesSinceLastScore = 0;
}

if (numberUpdatesSinceLastScore++ > 60 * 10) {
pacman.kill();
return Direction.UP;
}

// END OF SPECIAL TRAINING CONDITIONS

// We are going to use these directions a lot for different inputs. Get them all once for clarity and brevity
Expand All @@ -54,11 +70,14 @@ public Direction getDirection(@NotNull Entity entity) {
boolean canMoveRight = pacman.canMove(right);
boolean canMoveBehind = pacman.canMove(behind);

float randomNumber = ThreadLocalRandom.current().nextFloat();

float[] outputs = client.getCalculator().calculate(new float[]{
canMoveForward ? 1f : 0f,
canMoveLeft ? 1f : 0f,
canMoveRight ? 1f : 0f,
canMoveBehind ? 1f : 0f,
randomNumber
}).join();

int index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.ThreadLocalRandom;

public class TournamentBehavior implements Behavior {

private final Calculator calculator;
Expand Down Expand Up @@ -48,9 +50,27 @@ public Direction getDirection(@NotNull Entity entity) {
// --- END OF DO NOT REMOVE ---

// TODO: Put all your code for info into the neural network here
// We are going to use these directions a lot for different inputs. Get them all once for clarity and brevity
Direction forward = pacman.getDirection();
Direction left = pacman.getDirection().left();
Direction right = pacman.getDirection().right();
Direction behind = pacman.getDirection().behind();

// Input nodes 1, 2, 3, and 4 show if the pacman can move in the forward, left, right, and behind directions
boolean canMoveForward = pacman.canMove(forward);
boolean canMoveLeft = pacman.canMove(left);
boolean canMoveRight = pacman.canMove(right);
boolean canMoveBehind = pacman.canMove(behind);

float randomNumber = ThreadLocalRandom.current().nextFloat();


float[] inputs = new float[] {
// TODO: Add your inputs here
canMoveForward ? 1f : 0f,
canMoveLeft ? 1f : 0f,
canMoveRight ? 1f : 0f,
canMoveBehind ? 1f : 0f,
randomNumber
};
float[] outputs = calculator.calculate(inputs).join();

Expand Down