From 923607ed3be59d824555b7eb58ec364fb24de93f Mon Sep 17 00:00:00 2001 From: Collin Barber Date: Mon, 21 Oct 2024 13:51:33 -0400 Subject: [PATCH 1/3] team collin completed submission --- .../java/com/buaisociety/pacman/Main.java | 6 ++--- .../com/buaisociety/pacman/Tournament.java | 2 +- .../entity/behavior/NeatPacmanBehavior.java | 21 +++++++++++++++++- .../entity/behavior/TournamentBehavior.java | 22 ++++++++++++++++++- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/buaisociety/pacman/Main.java b/core/src/main/java/com/buaisociety/pacman/Main.java index 29113e7..7d25997 100644 --- a/core/src/main/java/com/buaisociety/pacman/Main.java +++ b/core/src/main/java/com/buaisociety/pacman/Main.java @@ -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 { @@ -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 { diff --git a/core/src/main/java/com/buaisociety/pacman/Tournament.java b/core/src/main/java/com/buaisociety/pacman/Tournament.java index 697fa74..60484c2 100644 --- a/core/src/main/java/com/buaisociety/pacman/Tournament.java +++ b/core/src/main/java/com/buaisociety/pacman/Tournament.java @@ -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; diff --git a/core/src/main/java/com/buaisociety/pacman/entity/behavior/NeatPacmanBehavior.java b/core/src/main/java/com/buaisociety/pacman/entity/behavior/NeatPacmanBehavior.java index d3fd62f..1d541bf 100644 --- a/core/src/main/java/com/buaisociety/pacman/entity/behavior/NeatPacmanBehavior.java +++ b/core/src/main/java/com/buaisociety/pacman/entity/behavior/NeatPacmanBehavior.java @@ -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; @@ -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; } @@ -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 @@ -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; diff --git a/core/src/main/java/com/buaisociety/pacman/entity/behavior/TournamentBehavior.java b/core/src/main/java/com/buaisociety/pacman/entity/behavior/TournamentBehavior.java index 22cab50..93d532f 100644 --- a/core/src/main/java/com/buaisociety/pacman/entity/behavior/TournamentBehavior.java +++ b/core/src/main/java/com/buaisociety/pacman/entity/behavior/TournamentBehavior.java @@ -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; @@ -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(); From 48961417d85b3c11fa5edcc735badc55831d9708 Mon Sep 17 00:00:00 2001 From: Collin Barber Date: Mon, 21 Oct 2024 13:54:54 -0400 Subject: [PATCH 2/3] added model weights --- assets/saves/oct21-3/best-calculator-56.json | 540 +++++++++++++++++++ 1 file changed, 540 insertions(+) create mode 100644 assets/saves/oct21-3/best-calculator-56.json diff --git a/assets/saves/oct21-3/best-calculator-56.json b/assets/saves/oct21-3/best-calculator-56.json new file mode 100644 index 0000000..163ea17 --- /dev/null +++ b/assets/saves/oct21-3/best-calculator-56.json @@ -0,0 +1,540 @@ +{ + "isAddBias": true, + "inputs": [ + { + "id": 0, + "x": 0.1, + "incoming": [] + }, + { + "id": 1, + "x": 0.1, + "incoming": [] + }, + { + "id": 2, + "x": 0.1, + "incoming": [] + }, + { + "id": 3, + "x": 0.1, + "incoming": [] + }, + { + "id": 4, + "x": 0.1, + "incoming": [] + }, + { + "id": 5, + "x": 0.1, + "incoming": [] + } + ], + "hidden": [ + { + "id": 11, + "x": 0.49999997, + "incoming": [ + { + "fromId": 0, + "weight": 0.7641629, + "enabled": true + }, + { + "fromId": 3, + "weight": 0.80850387, + "enabled": true + } + ] + }, + { + "id": 16, + "x": 0.49999997, + "incoming": [ + { + "fromId": 1, + "weight": 2.001179, + "enabled": true + }, + { + "fromId": 0, + "weight": -0.46623152, + "enabled": true + } + ] + }, + { + "id": 18, + "x": 0.49999997, + "incoming": [ + { + "fromId": 2, + "weight": 0.61018574, + "enabled": true + }, + { + "fromId": 0, + "weight": -1.3347825, + "enabled": true + }, + { + "fromId": 4, + "weight": -0.42727387, + "enabled": true + }, + { + "fromId": 3, + "weight": -0.21809122, + "enabled": true + } + ] + }, + { + "id": 20, + "x": 0.49999997, + "incoming": [ + { + "fromId": 2, + "weight": -0.8673107, + "enabled": true + }, + { + "fromId": 0, + "weight": -1.0695026, + "enabled": true + }, + { + "fromId": 1, + "weight": -0.88185215, + "enabled": true + }, + { + "fromId": 4, + "weight": -0.4702679, + "enabled": true + } + ] + }, + { + "id": 22, + "x": 0.49999997, + "incoming": [ + { + "fromId": 1, + "weight": 0.8598641, + "enabled": true + }, + { + "fromId": 0, + "weight": -0.90188545, + "enabled": true + } + ] + }, + { + "id": 24, + "x": 0.49999997, + "incoming": [ + { + "fromId": 0, + "weight": -0.73111296, + "enabled": true + }, + { + "fromId": 3, + "weight": -1.2087523, + "enabled": true + }, + { + "fromId": 2, + "weight": 0.034289762, + "enabled": true + }, + { + "fromId": 1, + "weight": -0.14501403, + "enabled": true + }, + { + "fromId": 4, + "weight": 0.21784155, + "enabled": true + } + ] + }, + { + "id": 101, + "x": 0.59999996, + "incoming": [ + { + "fromId": 20, + "weight": 0.9046616, + "enabled": true + }, + { + "fromId": 0, + "weight": -1.465641E-4, + "enabled": true + } + ] + }, + { + "id": 32, + "x": 0.7, + "incoming": [ + { + "fromId": 18, + "weight": 0.01858487, + "enabled": true + }, + { + "fromId": 0, + "weight": -0.7905834, + "enabled": true + }, + { + "fromId": 1, + "weight": -0.15909714, + "enabled": true + }, + { + "fromId": 20, + "weight": 0.8015864, + "enabled": true + }, + { + "fromId": 101, + "weight": 1.0921924, + "enabled": true + } + ] + }, + { + "id": 41, + "x": 0.7, + "incoming": [ + { + "fromId": 11, + "weight": -0.438623, + "enabled": true + }, + { + "fromId": 0, + "weight": 1.1878761, + "enabled": true + }, + { + "fromId": 3, + "weight": -1.123704, + "enabled": true + }, + { + "fromId": 24, + "weight": -0.40908763, + "enabled": true + }, + { + "fromId": 18, + "weight": -0.9886239, + "enabled": true + }, + { + "fromId": 5, + "weight": -1.6581414, + "enabled": true + }, + { + "fromId": 22, + "weight": 0.61254334, + "enabled": true + } + ] + }, + { + "id": 64, + "x": 0.7, + "incoming": [ + { + "fromId": 20, + "weight": 0.1775468, + "enabled": true + }, + { + "fromId": 0, + "weight": 0.9171058, + "enabled": true + }, + { + "fromId": 24, + "weight": -0.08489804, + "enabled": true + } + ] + }, + { + "id": 80, + "x": 0.7, + "incoming": [ + { + "fromId": 24, + "weight": 1.1953843, + "enabled": true + }, + { + "fromId": 0, + "weight": 0.035830855, + "enabled": true + } + ] + }, + { + "id": 47, + "x": 0.79999995, + "incoming": [ + { + "fromId": 32, + "weight": -0.8200141, + "enabled": true + }, + { + "fromId": 0, + "weight": -1.0937632, + "enabled": true + }, + { + "fromId": 2, + "weight": -0.17210811, + "enabled": true + }, + { + "fromId": 18, + "weight": 0.093324095, + "enabled": true + } + ] + } + ], + "outputs": [ + { + "id": 6, + "x": 0.9, + "incoming": [ + { + "fromId": 0, + "weight": -0.10386026, + "enabled": true + }, + { + "fromId": 1, + "weight": 0.31315854, + "enabled": false + }, + { + "fromId": 2, + "weight": -0.48492256, + "enabled": false + }, + { + "fromId": 3, + "weight": -0.29395568, + "enabled": true + }, + { + "fromId": 4, + "weight": 0.37558234, + "enabled": true + }, + { + "fromId": 20, + "weight": -1.4620963, + "enabled": true + }, + { + "fromId": 16, + "weight": 2.2011852, + "enabled": true + } + ] + }, + { + "id": 7, + "x": 0.9, + "incoming": [ + { + "fromId": 0, + "weight": 0.32994458, + "enabled": false + }, + { + "fromId": 1, + "weight": -0.79549956, + "enabled": true + }, + { + "fromId": 2, + "weight": 0.039557755, + "enabled": true + }, + { + "fromId": 3, + "weight": -0.21629529, + "enabled": true + }, + { + "fromId": 4, + "weight": 0.5528336, + "enabled": true + }, + { + "fromId": 11, + "weight": 0.9048728, + "enabled": true + }, + { + "fromId": 41, + "weight": 0.061160706, + "enabled": true + }, + { + "fromId": 32, + "weight": -0.62862194, + "enabled": true + }, + { + "fromId": 47, + "weight": 1.1354114, + "enabled": true + } + ] + }, + { + "id": 8, + "x": 0.9, + "incoming": [ + { + "fromId": 0, + "weight": 0.04318148, + "enabled": true + }, + { + "fromId": 1, + "weight": -0.5736072, + "enabled": true + }, + { + "fromId": 2, + "weight": -0.54648906, + "enabled": true + }, + { + "fromId": 3, + "weight": 0.72467273, + "enabled": true + }, + { + "fromId": 4, + "weight": 0.3841963, + "enabled": true + }, + { + "fromId": 11, + "weight": 0.72472185, + "enabled": true + }, + { + "fromId": 5, + "weight": 0.7822476, + "enabled": true + }, + { + "fromId": 22, + "weight": 0.3034445, + "enabled": true + }, + { + "fromId": 20, + "weight": 1.2395077, + "enabled": true + }, + { + "fromId": 32, + "weight": 0.31442374, + "enabled": true + } + ] + }, + { + "id": 9, + "x": 0.9, + "incoming": [ + { + "fromId": 0, + "weight": -0.14869577, + "enabled": true + }, + { + "fromId": 1, + "weight": -0.008117929, + "enabled": true + }, + { + "fromId": 2, + "weight": -1.2438565, + "enabled": true + }, + { + "fromId": 3, + "weight": -0.59588987, + "enabled": true + }, + { + "fromId": 4, + "weight": 0.44294688, + "enabled": true + }, + { + "fromId": 18, + "weight": -0.34322798, + "enabled": false + }, + { + "fromId": 20, + "weight": -0.28999704, + "enabled": true + }, + { + "fromId": 32, + "weight": -1.1251336, + "enabled": true + }, + { + "fromId": 47, + "weight": -1.4046988, + "enabled": true + }, + { + "fromId": 24, + "weight": -0.41295922, + "enabled": false + }, + { + "fromId": 64, + "weight": -0.16639823, + "enabled": true + }, + { + "fromId": 80, + "weight": 0.82104206, + "enabled": true + }, + { + "fromId": 41, + "weight": -0.6041287, + "enabled": true + } + ] + } + ] +} From a8aa52c1ae14a2cea55e3e91591dbd1a68b36e3f Mon Sep 17 00:00:00 2001 From: Collin Barber Date: Tue, 22 Oct 2024 15:47:27 -0400 Subject: [PATCH 3/3] remove example calculator --- assets/saves/oct21-3/best-calculator-56.json | 540 ------------------- 1 file changed, 540 deletions(-) delete mode 100644 assets/saves/oct21-3/best-calculator-56.json diff --git a/assets/saves/oct21-3/best-calculator-56.json b/assets/saves/oct21-3/best-calculator-56.json deleted file mode 100644 index 163ea17..0000000 --- a/assets/saves/oct21-3/best-calculator-56.json +++ /dev/null @@ -1,540 +0,0 @@ -{ - "isAddBias": true, - "inputs": [ - { - "id": 0, - "x": 0.1, - "incoming": [] - }, - { - "id": 1, - "x": 0.1, - "incoming": [] - }, - { - "id": 2, - "x": 0.1, - "incoming": [] - }, - { - "id": 3, - "x": 0.1, - "incoming": [] - }, - { - "id": 4, - "x": 0.1, - "incoming": [] - }, - { - "id": 5, - "x": 0.1, - "incoming": [] - } - ], - "hidden": [ - { - "id": 11, - "x": 0.49999997, - "incoming": [ - { - "fromId": 0, - "weight": 0.7641629, - "enabled": true - }, - { - "fromId": 3, - "weight": 0.80850387, - "enabled": true - } - ] - }, - { - "id": 16, - "x": 0.49999997, - "incoming": [ - { - "fromId": 1, - "weight": 2.001179, - "enabled": true - }, - { - "fromId": 0, - "weight": -0.46623152, - "enabled": true - } - ] - }, - { - "id": 18, - "x": 0.49999997, - "incoming": [ - { - "fromId": 2, - "weight": 0.61018574, - "enabled": true - }, - { - "fromId": 0, - "weight": -1.3347825, - "enabled": true - }, - { - "fromId": 4, - "weight": -0.42727387, - "enabled": true - }, - { - "fromId": 3, - "weight": -0.21809122, - "enabled": true - } - ] - }, - { - "id": 20, - "x": 0.49999997, - "incoming": [ - { - "fromId": 2, - "weight": -0.8673107, - "enabled": true - }, - { - "fromId": 0, - "weight": -1.0695026, - "enabled": true - }, - { - "fromId": 1, - "weight": -0.88185215, - "enabled": true - }, - { - "fromId": 4, - "weight": -0.4702679, - "enabled": true - } - ] - }, - { - "id": 22, - "x": 0.49999997, - "incoming": [ - { - "fromId": 1, - "weight": 0.8598641, - "enabled": true - }, - { - "fromId": 0, - "weight": -0.90188545, - "enabled": true - } - ] - }, - { - "id": 24, - "x": 0.49999997, - "incoming": [ - { - "fromId": 0, - "weight": -0.73111296, - "enabled": true - }, - { - "fromId": 3, - "weight": -1.2087523, - "enabled": true - }, - { - "fromId": 2, - "weight": 0.034289762, - "enabled": true - }, - { - "fromId": 1, - "weight": -0.14501403, - "enabled": true - }, - { - "fromId": 4, - "weight": 0.21784155, - "enabled": true - } - ] - }, - { - "id": 101, - "x": 0.59999996, - "incoming": [ - { - "fromId": 20, - "weight": 0.9046616, - "enabled": true - }, - { - "fromId": 0, - "weight": -1.465641E-4, - "enabled": true - } - ] - }, - { - "id": 32, - "x": 0.7, - "incoming": [ - { - "fromId": 18, - "weight": 0.01858487, - "enabled": true - }, - { - "fromId": 0, - "weight": -0.7905834, - "enabled": true - }, - { - "fromId": 1, - "weight": -0.15909714, - "enabled": true - }, - { - "fromId": 20, - "weight": 0.8015864, - "enabled": true - }, - { - "fromId": 101, - "weight": 1.0921924, - "enabled": true - } - ] - }, - { - "id": 41, - "x": 0.7, - "incoming": [ - { - "fromId": 11, - "weight": -0.438623, - "enabled": true - }, - { - "fromId": 0, - "weight": 1.1878761, - "enabled": true - }, - { - "fromId": 3, - "weight": -1.123704, - "enabled": true - }, - { - "fromId": 24, - "weight": -0.40908763, - "enabled": true - }, - { - "fromId": 18, - "weight": -0.9886239, - "enabled": true - }, - { - "fromId": 5, - "weight": -1.6581414, - "enabled": true - }, - { - "fromId": 22, - "weight": 0.61254334, - "enabled": true - } - ] - }, - { - "id": 64, - "x": 0.7, - "incoming": [ - { - "fromId": 20, - "weight": 0.1775468, - "enabled": true - }, - { - "fromId": 0, - "weight": 0.9171058, - "enabled": true - }, - { - "fromId": 24, - "weight": -0.08489804, - "enabled": true - } - ] - }, - { - "id": 80, - "x": 0.7, - "incoming": [ - { - "fromId": 24, - "weight": 1.1953843, - "enabled": true - }, - { - "fromId": 0, - "weight": 0.035830855, - "enabled": true - } - ] - }, - { - "id": 47, - "x": 0.79999995, - "incoming": [ - { - "fromId": 32, - "weight": -0.8200141, - "enabled": true - }, - { - "fromId": 0, - "weight": -1.0937632, - "enabled": true - }, - { - "fromId": 2, - "weight": -0.17210811, - "enabled": true - }, - { - "fromId": 18, - "weight": 0.093324095, - "enabled": true - } - ] - } - ], - "outputs": [ - { - "id": 6, - "x": 0.9, - "incoming": [ - { - "fromId": 0, - "weight": -0.10386026, - "enabled": true - }, - { - "fromId": 1, - "weight": 0.31315854, - "enabled": false - }, - { - "fromId": 2, - "weight": -0.48492256, - "enabled": false - }, - { - "fromId": 3, - "weight": -0.29395568, - "enabled": true - }, - { - "fromId": 4, - "weight": 0.37558234, - "enabled": true - }, - { - "fromId": 20, - "weight": -1.4620963, - "enabled": true - }, - { - "fromId": 16, - "weight": 2.2011852, - "enabled": true - } - ] - }, - { - "id": 7, - "x": 0.9, - "incoming": [ - { - "fromId": 0, - "weight": 0.32994458, - "enabled": false - }, - { - "fromId": 1, - "weight": -0.79549956, - "enabled": true - }, - { - "fromId": 2, - "weight": 0.039557755, - "enabled": true - }, - { - "fromId": 3, - "weight": -0.21629529, - "enabled": true - }, - { - "fromId": 4, - "weight": 0.5528336, - "enabled": true - }, - { - "fromId": 11, - "weight": 0.9048728, - "enabled": true - }, - { - "fromId": 41, - "weight": 0.061160706, - "enabled": true - }, - { - "fromId": 32, - "weight": -0.62862194, - "enabled": true - }, - { - "fromId": 47, - "weight": 1.1354114, - "enabled": true - } - ] - }, - { - "id": 8, - "x": 0.9, - "incoming": [ - { - "fromId": 0, - "weight": 0.04318148, - "enabled": true - }, - { - "fromId": 1, - "weight": -0.5736072, - "enabled": true - }, - { - "fromId": 2, - "weight": -0.54648906, - "enabled": true - }, - { - "fromId": 3, - "weight": 0.72467273, - "enabled": true - }, - { - "fromId": 4, - "weight": 0.3841963, - "enabled": true - }, - { - "fromId": 11, - "weight": 0.72472185, - "enabled": true - }, - { - "fromId": 5, - "weight": 0.7822476, - "enabled": true - }, - { - "fromId": 22, - "weight": 0.3034445, - "enabled": true - }, - { - "fromId": 20, - "weight": 1.2395077, - "enabled": true - }, - { - "fromId": 32, - "weight": 0.31442374, - "enabled": true - } - ] - }, - { - "id": 9, - "x": 0.9, - "incoming": [ - { - "fromId": 0, - "weight": -0.14869577, - "enabled": true - }, - { - "fromId": 1, - "weight": -0.008117929, - "enabled": true - }, - { - "fromId": 2, - "weight": -1.2438565, - "enabled": true - }, - { - "fromId": 3, - "weight": -0.59588987, - "enabled": true - }, - { - "fromId": 4, - "weight": 0.44294688, - "enabled": true - }, - { - "fromId": 18, - "weight": -0.34322798, - "enabled": false - }, - { - "fromId": 20, - "weight": -0.28999704, - "enabled": true - }, - { - "fromId": 32, - "weight": -1.1251336, - "enabled": true - }, - { - "fromId": 47, - "weight": -1.4046988, - "enabled": true - }, - { - "fromId": 24, - "weight": -0.41295922, - "enabled": false - }, - { - "fromId": 64, - "weight": -0.16639823, - "enabled": true - }, - { - "fromId": 80, - "weight": 0.82104206, - "enabled": true - }, - { - "fromId": 41, - "weight": -0.6041287, - "enabled": true - } - ] - } - ] -}