Certainly! Here is an implementation of a simple Java neural network that learns to move a dot to find and eat food dots.
In this problem, we have a dot that can move in a 2D grid. The goal of the dot is to find and eat food dots that are randomly scattered throughout the grid. The dot has a vision range that allows it to sense the location of the nearest food dot. The dot has three possible actions: move left, move right, or move forward. The dot can move one grid unit at a time in any of these directions.
The neural network takes in the dot's current location, as well as the location of the nearest food dot, as input. It outputs a probability distribution over the three possible actions. We use a softmax activation function to transform the output of the neural network into a probability distribution.
Here is the code:
import java.util.Arrays;
import java.util.Random;
public class NeuralDot {
private static final int VISION_RANGE = 5;
private static final int GRID_SIZE = 100;
private static final int NUM_INPUTS = (2*VISION_RANGE + 1) * (2*VISION_RANGE + 1) + 2;
private static final int NUM_HIDDEN = 16;
private static final int NUM_OUTPUTS = 3;
private static final double LEARNING_RATE = 0.1;
private static final int NUM_EPISODES = 10000;
private int x;
private int y;
private boolean[][] grid;
private Random random;
private NeuralNetwork neuralNetwork;
public NeuralDot() {
x = GRID_SIZE / 2;
y = GRID_SIZE / 2;
grid = new boolean[GRID_SIZE][GRID_SIZE];
random = new Random();
neuralNetwork = new NeuralNetwork(NUM_INPUTS, NUM_HIDDEN, NUM_OUTPUTS, LEARNING_RATE);
}
public void run() {
for (int episode = 0; episode < NUM_EPISODES; episode++) {
// Reset the grid and place a food dot randomly
resetGrid();
placeFoodDot();
// Play the episode until the dot eats the food dot
while (!grid[x][y]) {
// Get the nearest food dot
int[] foodDot = getNearestFoodDot();
// Compute the inputs to the neural network
double[] inputs = new double[NUM_INPUTS];
int index = 0;
for (int i = x - VISION_RANGE; i <= x + VISION_RANGE; i++) {
for (int j = y - VISION_RANGE; j <= y + VISION_RANGE; j++) {
if (i < 0 || i >= GRID_SIZE || j < 0 || j >= GRID_SIZE) {
inputs[index++] = 0;
} else if (i == x && j == y) {
inputs[index++] = 1;
} else {
inputs[index++] = grid[i][j] ? -1 : 0;
}
}
}
inputs[index++] = foodDot[0] - x;
inputs[index++] = foodDot[1] - y;
// Compute the outputs of the neural network
double[] outputs = neuralNetwork.feedForward(inputs);
// Choose the action with the highest probability
double maxProbability = Double.NEGATIVE_INFINITY;
int action = 0;
for (int i = 0; i < NUM_OUTPUTS; i++) {
if (outputs[i] > maxProbability) {
maxProbability = outputs[i];
action = i;
}
}
// Take the chosen action
switch (action) {
case 0:
moveLeft();
break;
case 1:
moveRight();
break;
case 2:
moveForward();
break;
}
// Compute the reward signal
double reward;
if (grid[x][y]) {
reward = 10;
} else {
reward = -0.1;
}
// Train the neural network on the input-output pair
double[] targetOutputs = new double[NUM_OUTPUTS];
targetOutputs[action] = 1;
neuralNetwork.train(inputs, targetOutputs, reward);
}
}
}
private void resetGrid() {
for (int i = 0; i < GRID_SIZE; i++) {
Arrays.fill(grid[i], false);
}
}
private void placeFoodDot() {
int foodX = random.nextInt(GRID_SIZE);
int foodY = random.nextInt(GRID_SIZE);
grid[foodX][foodY] = true;
}
private int[] getNearestFoodDot() {
int nearestX = -1;
int nearestY = -1;
double nearestDistance = Double.POSITIVE_INFINITY;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (grid[i][j]) {
double distance = Math.sqrt((i - x)*(i - x) + (j - y)*(j - y));
if (distance < nearestDistance) {
nearestX = i;
nearestY = j;
nearestDistance = distance;
}
}
}
}
return new int[] {nearestX, nearestY};
}
private void moveLeft() {
if (x > 0) {
x--;
}
}
private void moveRight() {
if (x < GRID_SIZE - 1) {
x++;
}
}
private void moveForward() {
if (y < GRID_SIZE - 1) {
y++;
}
}
public static void main(String[] args) {
NeuralDot dot = new NeuralDot();
dot.run();
}
}
When you run this program, it will train the neural network for a number of episodes, during each of which the dot must find and eat a food dot. After training is complete, the dot should be able to navigate to the closest food dot in the grid.
Note that this is a very simple implementation, and there are certainly many ways to improve or expand upon it. For example, you could modify the reward signal to encourage the dot to take shorter paths to the food dot, or you could add multiple food dots to the grid at once.