[Home]Chase-san/SelfOrganizingMap

Robo Home | Chase-san | Changes | Preferences | AllPages

Showing revision 1
package chase.s2.net;

public class SelfOrganizingMap {
	protected SelfOrganizedNode[] nodes;
	protected SelfOrganizedNode bmu;
	protected int[] lattice;
	protected int statBinIndexes;
	protected int weights;
	protected int timeIndex;
	
	/**
	 * Creates a self organizing map with a lattice size, stat indexes and
	 * number of node weights as stated.
	 * @param lattice - An array of integers, designating a shape of the lattice
	 * @param statIndexes - number of indexes in output array
	 * @param weights - the nubmer of weights found in a node
	 */
	public SelfOrganizingMap(int[] lattice, int statIndexes, int weights) {
		if(lattice.length < 1)
			throw new RuntimeException("Lattice requires atleast 1 dimension.");
		
		statBinIndexes = statIndexes;
		this.lattice = lattice;
		this.weights = weights;
		
		int latticeSize = 1;
		for(int i=0; i<lattice.length; i++) {
			latticeSize *= lattice[i];
		} //end for
		
		nodes = new SelfOrganizedNode[latticeSize];
		int[] vector = new int[lattice.length];
		
		for(int i=0; i<latticeSize; i++) {
			nodes[i] = new SelfOrganizedNode(vector, this);
			
			initialize(nodes[i], vector);
			
			vector[0]++;
			for(int j=1; j<lattice.length; j++) {
				if(vector[j-1] >= lattice[j-1]) {
					vector[j-1] = 0;
					vector[j]++;
				} //end if
			} //end for
		} //end for
	}
	
	//Notice: In desperate need of a better initialization!
	private void initialize(SelfOrganizedNode n, int[] position) {
		//Initializes each node randomly, not very well done
		for(int i=0; i<weights; i++)
			n.weights[i] = Math.random();

		//I have found that in a 2D map with equal dimensions
		//that if you set each weight outward from the center
		//at a distance of about a quarter, and then guassian
		//it from there with a .5 = dimension/4, that it works
		//alot better, but my code is far to complex and messy
		//to include here
	}
	
	public void updateMap(double[] input, int index) {
		timeIndex++;
		bmu = getBMU(input);
		for(int i=0; i<nodes.length;i++) {
			nodes[i].update(input, index);
		}
	}
	
	public SelfOrganizedNode getBMU(double[] input) {
		int bmu = 0;
		double smallestDifference = Double.POSITIVE_INFINITY;
		
		//there is no faster way as the data in the nodes changes constantly
		for(int i=0; i<nodes.length; i++) {
			double difference = nodes[i].difference(input);
			
			//small shortcut, as we need every ounce of speed
			if(difference < 0.002) return nodes[i];
			if(difference < smallestDifference) {
				bmu = i;
				smallestDifference = difference;
			}
		}
		
		return nodes[bmu];
	}
}

public class SelfOrganizedNode {
	protected SelfOrganizingMap map;
	protected int position[];
	protected double weights[];
	public float statBin[];
	
	protected SelfOrganizedNode(int[] latticePosition, SelfOrganizingMap parent) {
		position = latticePosition.clone();
		map = parent;
		statBin = new float[map.statBinIndexes];
		weights = new double[map.weights];
	}
	
	public void update(double[] input, int index) {
		double distance = distance(map.bmu);

		//Make this smarter, it should degrade over time, and it determines
		//The size of the area to update, it should be based on the physical
		//size of the map, not the number of nodes (easiest is the smallest dimension)
		//I find its best if it slowly decreases in size over about 100 to 1000 ticks/updates
		double variance = 10/map.timeIndex;
		double neighborhood = Math.exp(-(variance*variance*distance*distance)/2.0);
		
		//This makes the whole thing ALOT faster. Remove for precision or if
		//you wanna test how you bot handles turn skipping.
		if(neighborhood < 0.0002) return;
		
		//Much more can be done here, this should also be based on time
		//and it is, however if this is the bmu, then it will get changed
		//to the input, an undesirable thing later on. I never did this in
		//my prototype gun, I could of achieved greater precision here
		for(int i=0; i < map.weights; i++) {
			weights[i] += neighborhood*(input[i] - weights[i]);
		}
		

		//This is just the index updating, very important to multiply in the neighborhood
		//otherwise it gets very messy later on, not to much you can do here unlike other areas
		//.7 is purely a magic number I came up with, I find binIndexes / 30 works well too
		for(int i=0; i<map.statBinIndexes; i++) {
			int diff = Math.abs(index - i);
			float stat = (float)Math.exp(-(.7*.7*diff*diff)/2.0);
			statBin[i] += neighborhood*stat;

//			statBin[i] = KTools.rollingAvg(bin[0][i], neighborhood*input,
//					Math.min(update_num, 5), 100);
		}
	}
	
	protected float distance(SelfOrganizedNode n) {
		return distance(position, n.position);
	}
	
	protected double difference(double[] input) {
		return distance(weights, input);
	}
	
	public static final float distance(int[] p, int[] q) {
		float k, d = 0;
		for(int i=0; i<p.length; i++) {
			d += (k=((float)p[i]-(float)q[i]))*k; 
		}
		return d;
	}
	
	public static final double distance(double[] p, double[] q) {
		if(p == null || q == null) return Double.POSITIVE_INFINITY;
		double k, d = 0;
		for(int i=0; i<p.length; i++) {
			d += (k=(p[i]-q[i]))*k; 
		}
		return d;
	}
}

Comments

Yah, I realize I could do better for an example, but I really wanna see what everyone else can come up with for this, its a very basic SOM system, I have myself introduced my own distributed update and so on, this is just the data control. If you like I can add things into this, but I am not an expert programmer, and my methods are anything but pretty. --Chase-san

Robo Home | Chase-san | Changes | Preferences | AllPages
Edit revision 1 of this page | View other revisions | View current revision
Edited January 10, 2008 9:46 EST by Chase-san (diff)
Search: