package cs437.som.network;

import cs437.som.SelfOrganizingMap;
import cs437.som.visualization.SOM2dPlotter;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

/**
 * A basic self-organizing map where the neurons are arranged by their weights.
 *
 * An simple example usage:
 *
 * <code>
 * <pre>
 * private static final int iterations = 500;
 *
 * public static void main(String[] args) {
 *     SelfOrganizingMap som = new BasicPlanarGridSOM(7, 2, iterations);
 *     Random r = new SecureRandom();
 *
 *     for (int i = 0; i < iterations; i++) {
 *         double[] in = {r.nextDouble() * 10, r.nextDouble() * 10};
 *         som.trainWith(in);
 *     }
 *
 *     // At this point, the SOM would be queried for it's BMU.
 * }
 * </pre>
 * </code>
 */
@SuppressWarnings({"UseOfSystemOutOrSystemErr"})
public class BasicPlanarSOM extends NetworkBase {

    public BasicPlanarSOM(int neuronCount, int inputVectorSize, int expectedIterations) {
        super(inputVectorSize, neuronCount, expectedIterations, neuronCount);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected double neuronDistance(int neuron0, int neuron1) {
        double sum = 0.0;
        for (int i = 0; i < inputVectorSize; i++) {
            double difference = weightMatrix[neuron0][i] - weightMatrix[neuron1][i];
            sum += difference * difference;
        }
        return Math.sqrt(sum);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected double neighborhoodWidth() {
        if (time < (expectedIterations / 5))
            return 1.0 * (1.0 - (time / (double) expectedIterations));
        return 0.0;
    }

    private static final int iterations = 3000;
    private static final int iterDelay = 10; /* ms */
    private static final double tenByTenStep = 10.1;
    private static final double nearnessOffest = 0.5;

    public static void main(String[] args) {
        SelfOrganizingMap som = new BasicPlanarSOM(100, 2, iterations);
        Random r = new SecureRandom();

        System.out.println("Before Training");
        print10x10Map(som);

        SOM2dPlotter plot = new SOM2dPlotter(som);
        for (int i = 0; i < iterations; i++) {
            double[] in = { r.nextDouble() * 10, r.nextDouble() * 10 };
            som.trainWith(in);
            plot.draw();
            try { Thread.sleep(iterDelay); } catch (InterruptedException ignored) { }
        }

        System.out.println("After training");
        print10x10Map(som);

        System.out.println("Nearby points");
        print10x10NearbyMap(som);
    }

    private static void print10x10Map(SelfOrganizingMap som) {
        System.out.println("  \t 1  2  3  4  5  6  7  8  9 10");

        for (double i = 1.0; i < tenByTenStep; i += 1.0) {
            System.out.print(String.format("%2d\t", (int)Math.round(i)));
            for (double j = 1.0; j < tenByTenStep; j += 1.0) {
                System.out.print(String.format("%2d ", som.getBestMatchingNeuron(new double[]{i, j})));
            }
            System.out.println();
        }
        System.out.println();
    }

    private static void print10x10NearbyMap(SelfOrganizingMap som) {
        Random r = new SecureRandom();

        System.out.println("  \t 1  2  3  4  5  6  7  8  9 10");

        for (double i = 1.0; i < tenByTenStep; i += 1.0) {
            System.out.print(String.format("%2d\t", (int)Math.round(i)));
            for (double j = 1.0; j < tenByTenStep; j += 1.0) {
                System.out.print(String.format("%2d ",
                        som.getBestMatchingNeuron(new double[]{i + r.nextDouble() - nearnessOffest, j + r.nextDouble() - nearnessOffest})));
            }
            System.out.println();
        }
        System.out.println();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "BasicPlanarSOM{weightMatrix=" + (weightMatrix == null ? null : Arrays.toString(weightMatrix)) + '}';
    }
}

@@ -32,166 +32,24 @@
 ort 
-cs437.som.SelfOrganizingMap;%0Aimport cs437.som.visualization.SOM2dPlotter;%0A%0Aimport java.security.SecureRandom;%0Aimport java.util.Arrays;%0Aimport java.util.Random
+java.util.Arrays
 ;%0A%0A/
@@ -646,57 +646,8 @@
  */%0A
-@SuppressWarnings(%7B%22UseOfSystemOutOrSystemErr%22%7D)%0A
 publ
@@ -1444,24 +1444,24 @@
 erations));%0A
+
         retu
@@ -1479,2121 +1479,8 @@
  %7D%0A%0A
-    private static final int iterations = 3000;%0A    private static final int iterDelay = 10; /* ms */%0A    private static final double tenByTenStep = 10.1;%0A    private static final double nearnessOffest = 0.5;%0A%0A    public static void main(String%5B%5D args) %7B%0A        SelfOrganizingMap som = new BasicPlanarSOM(100, 2, iterations);%0A        Random r = new SecureRandom();%0A%0A        System.out.println(%22Before Training%22);%0A        print10x10Map(som);%0A%0A        SOM2dPlotter plot = new SOM2dPlotter(som);%0A        for (int i = 0; i %3C iterations; i++) %7B%0A            double%5B%5D in = %7B r.nextDouble() * 10, r.nextDouble() * 10 %7D;%0A            som.trainWith(in);%0A            plot.draw();%0A            try %7B Thread.sleep(iterDelay); %7D catch (InterruptedException ignored) %7B %7D%0A        %7D%0A%0A        System.out.println(%22After training%22);%0A        print10x10Map(som);%0A%0A        System.out.println(%22Nearby points%22);%0A        print10x10NearbyMap(som);%0A    %7D%0A%0A    private static void print10x10Map(SelfOrganizingMap som) %7B%0A        System.out.println(%22  %5Ct 1  2  3  4  5  6  7  8  9 10%22);%0A%0A        for (double i = 1.0; i %3C tenByTenStep; i += 1.0) %7B%0A            System.out.print(String.format(%22%252d%5Ct%22, (int)Math.round(i)));%0A            for (double j = 1.0; j %3C tenByTenStep; j += 1.0) %7B%0A                System.out.print(String.format(%22%252d %22, som.getBestMatchingNeuron(new double%5B%5D%7Bi, j%7D)));%0A            %7D%0A            System.out.println();%0A        %7D%0A        System.out.println();%0A    %7D%0A%0A    private static void print10x10NearbyMap(SelfOrganizingMap som) %7B%0A        Random r = new SecureRandom();%0A%0A        System.out.println(%22  %5Ct 1  2  3  4  5  6  7  8  9 10%22);%0A%0A        for (double i = 1.0; i %3C tenByTenStep; i += 1.0) %7B%0A            System.out.print(String.format(%22%252d%5Ct%22, (int)Math.round(i)));%0A            for (double j = 1.0; j %3C tenByTenStep; j += 1.0) %7B%0A                System.out.print(String.format(%22%252d %22,%0A                        som.getBestMatchingNeuron(new double%5B%5D%7Bi + r.nextDouble() - nearnessOffest, j + r.nextDouble() - nearnessOffest%7D)));%0A            %7D%0A            System.out.println();%0A        %7D%0A        System.out.println();%0A    %7D%0A%0A
     

Remove main(..) from BasicPlanarSOM. BasicMapDemo has the same functionality.