public class ElasticNet { private static final int EPOCHS = 1000; private static final int NUM_CITIES = 5; private static final int NUM_NEURONS = NUM_CITIES * 2; private static final double NEAR = 0.05; private static final double MOMENTUM = 0.995; private static double city[][] = null; private static double neuronXY[][] = null; private static double weight[][] = null; private static double r[][]; private static double gridInterval = 0.0; private static double theta = 0.0; private static double phi = 0.0; private static void initialize() { city = new double[NUM_CITIES][2]; // [city index][0 = x, 1 = y] neuronXY = new double[NUM_NEURONS][2]; // [network size][0 = x, 1 = y] weight = new double[NUM_NEURONS][2]; // [network size][0 = x, 1 = y] r = new double[NUM_NEURONS][NUM_NEURONS]; theta = 0.5; phi = 0.5; city[0][0] = 7.0; city[0][1] = 2.0; city[1][0] = 12.0; city[1][1] = 5.0; city[2][0] = 9.0; city[2][1] = 10.0; city[3][0] = 5.0; city[3][1] = 10.0; city[4][0] = 2.0; city[4][1] = 5.0; for(int i = 0; i < NUM_NEURONS; i++) { neuronXY[i][0] = 0.5 + 0.5 * Math.cos(gridInterval); neuronXY[i][1] = 0.5 + 0.5 * Math.sin(gridInterval); gridInterval += Math.PI * 2.0 / (double)(NUM_NEURONS); weight[i][0] = Math.random(); weight[i][1] = Math.random(); } computeMatrix(theta); return; } private static void computeMatrix(double theta) { for(int i = 0; i < NUM_NEURONS; i++) { r[i][i] = 1.0; for(int j = i + 1; j < NUM_NEURONS; j++) { r[i][j] = Math.exp(-1.0 * (getDistance(i, j) * getDistance(i, j)) / (2.0 * Math.pow(theta, 2))); r[j][i] = r[i][j]; } } return; } private static double getDistance(int index, int index2) { double dx = neuronXY[index][0] - neuronXY[index2][0]; double dy = neuronXY[index][1] - neuronXY[index2][1]; return Math.sqrt(dx * dx + dy * dy); } private static int findMinimum(double location1, double location2) { double minimumDistance = 100000.0; int minimumIndex = -1; for(int i = 0; i < NUM_NEURONS; i++) { double distance = (Math.pow((location1 - weight[i][0]), 2)) + (Math.pow((location2 - weight[i][1]), 2)); if(distance < minimumDistance){ minimumDistance = distance; minimumIndex = i; } } return minimumIndex; } private static void algorithm() { int index = 0; int minimumIndex = 0; double loc1 = 0.0; double loc2 = 0.0; int count = 0; while(count < EPOCHS) { // Pick a city for random comparison index = (int)(Math.random() * NUM_CITIES); loc1 = city[index][0] + (Math.random() * NEAR) - NEAR / 2; loc2 = city[index][1] + (Math.random() * NEAR) - NEAR / 2; minimumIndex = findMinimum(loc1, loc2); // Update all weights. for(int i = 0; i < NUM_NEURONS; i++) { weight[i][0] += (phi * r[i][minimumIndex] * (loc1 - weight[i][0])); weight[i][1] += (phi * r[i][minimumIndex] * (loc2 - weight[i][1])); } phi *= MOMENTUM; theta *= MOMENTUM; computeMatrix(theta); printRing(); count++; } return; } private static void printRing() { for(int i = 0; i < NUM_NEURONS; i++){ String tempX = String.format("%.1f", weight[i][0]); String tempY = String.format("%.1f", weight[i][1]); System.out.print("(" + tempX + ", " + tempY + ") "); } System.out.print("\n"); return; } public static void main(String[] args) { initialize(); algorithm(); return; } }