Software Programming

Kunuk Nykjaer

Archive for September 2010

Simple debug framework example in Java

leave a comment »

Just a simple your own quick and dirty debug/log framework.
Ability to turn on/off debug prints and setting debugging level.
Implemented in Java

D.java

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author Kunuk Nykjaer
 * DebugFramework
 */

public class D {
	private static boolean debug = true;
	public final static int DEBUG_LEVEL_FATAL = 7;
	public final static int DEBUG_LEVEL_ERROR = 6;
	public final static int DEBUG_LEVEL_WARNING = 5;
	public final static int DEBUG_LEVEL_INFO = 4;
	public final static int DEBUG_LEVEL_FINE = 3;
	public final static int DEBUG_LEVEL_FINER = 2;
	public final static int DEBUG_LEVEL_FINEST = 1;
	private static int debugLevel = DEBUG_LEVEL_INFO; // default
	private static long begin = System.currentTimeMillis();	
	private final static NumberFormat nf = new DecimalFormat("#.##");
	
	public static void setDebugLevel(int level)
	{
		debugLevel = level;
	}
	public static void setDebug(boolean b)
	{
		debug = b;
	}
	public static void resetTime()
	{
		begin = System.currentTimeMillis();
	}
	
	
	public static void debug(Object o)
	{
		debug(o, DEBUG_LEVEL_INFO); //default
	}
	public static void debug(Object o, int level)
	{
		
		if(debug && debugLevel <= level){
			double secs = (System.currentTimeMillis() - begin) / 1000.0;
			String time =  nf.format(secs) + " sec: ";
			System.out.println("debug "+time +o);
		}
	}
}

Test.java

public class Test {
		public static void main(String[] args) {

			D.setDebug(true);
			D.resetTime();

			String one = "one";
			String two = "two";
			String three = "three";	

			D.setDebugLevel(D.DEBUG_LEVEL_INFO);
			System.out.println("-- DEBUG_LEVEL_INFO");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);	
			
			D.setDebugLevel(D.DEBUG_LEVEL_WARNING);
			System.out.println("-- DEBUG_LEVEL_WARNING");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);

			D.setDebugLevel(D.DEBUG_LEVEL_ERROR);
			System.out.println("-- DEBUG_LEVEL_ERROR");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);	

			// turn off debug
			D.setDebug(false);
			System.out.println("-- DEBUG set to false");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);
		}		
}

Result:

– DEBUG_LEVEL_INFO
debug 0 sec: one
debug 0 sec: two
debug 0 sec: three
– DEBUG_LEVEL_WARNING
debug 0 sec: two
debug 0 sec: three
– DEBUG_LEVEL_ERROR
debug 0 sec: three
– DEBUG set to false

Written by kunuk Nykjaer

September 28, 2010 at 12:50 am

Posted in Framework, Java

Closest pair in a plane example with Java – O(nlogn)

with 3 comments

Implementation: Java
Time: O(nlogn)

Closest pair in a plane
Divide and conquer version

Ashish Sharma
Rengakrishnan Subramanian
November 28, 2001
http://www.cis.ksu.edu/~subbu/Papers/Closest pair.pdf

Updated:
An update was been provided for a found bug. It is unknown currently whether the fix is sufficient (probably not).
A bug was found in the merging part where the mergePlanes function don’t have all the needed points to correctly calculate the minimum distance.
As I see it after re-reading the paper, I have not implemented mergePlanes correctly. As a lazy writer would say: I leave it as an exercise for you to implement it correctly.

ClosestPair.java

import java.util.*;

/**
 * @author Kunuk Nykjaer
 * updated version 1.1 after a bug was found
 * Divide and conquer implementation
 */

public class ClosestPair {
	public static void main(String[] args) throws Exception {
		
		// Load your own data for testing		
		P[] points = new P[] { new P(2, 7), new P(4, 13), new P(5, 7),new P(10, 5),
				new P(13, 9), new P(15, 5), new P(17, 7), new P(19, 10), 
				new P(22, 7), new P(25, 10), new P(29, 14),	new P(30, 2) };
				
		
		Arrays.sort(points, xComparator); // sort by x, then y
					
		P[] closest = findClosest(points);		
		P[] closestx = findMinDist( findMinDistNeighbor(points),closest );		
				
		for (P p : closestx)
			System.out.println(p);
		System.out.println("dist: "+distance(closestx[0],closestx[1]));					
	}

	/**
	 * Find min distance for neightbors in sorted by x-coord point list
	 * Fix divide problem where information is lost in algorithm
	 * @param ps
	 * @return
	 * 
	 * O(n)
	 */
	static P[] findMinDistNeighbor(P[] ps) {
		double minDist = Double.MAX_VALUE;
		P[] pMin = new P[]{new P(0,0),new P(Double.MAX_VALUE,Double.MAX_VALUE)};
		if(ps.length<4)
			return pMin;
		
		for (int i = 0; i < ps.length-3; i++){
			P p1 = ps[i];
			P p2 = ps[i+1];
			P p3 = ps[i+2];
			P p4 = ps[i+3];
			double dist1 = distance(p1,p2);
			double dist2 = distance(p1,p3);
			double dist3 = distance(p1,p4);	
			if(dist1<minDist){ // update				
				minDist = dist1; 
				pMin = new P[] {p1,p2};
			}
			if(dist2<minDist){ // update				
				minDist = dist2; 
				pMin = new P[] {p1,p3};
			}
			if(dist3<minDist){ // update				
				minDist = dist3; 
				pMin = new P[] {p1,p4};
			}
		}
		return pMin;
	}
	
	
	static P[] findMinDist(P[] p1,P[] p2) {
		double d1 = distance(p1[0],p1[1]);
		double d2 = distance(p2[0],p2[1]);
		return d1 < d2 ? p1 : p2;
	}
	
	/**
	 * Closest pair O(nlogn) 
	 * Ashish Sharma 
	 * Rengakrishnan Subramanian 
	 * November 28, 2001 
	 * http://www.cis.ksu.edu/~subbu/Papers/Closest%20pair.pdf
	 * @throws Exception 
	 */
	static P[] findClosest(P[] ps) throws Exception {
		// ps must be sorted in x, then y
		int n = ps.length;

		if (n <= 3){
			return shortest(ps);	
		}			
		else {
			int left = n / 2;
			int right = n / 2 + n % 2;

			// the set datas
			P[] Pleft = new P[left];
			P[] Pright = new P[right];
			P[] Pleftmin, Prightmin, Pclosest;

			for (int i = 0; i < left; i++)
				Pleft[i] = ps[i];
			for (int i = 0; i < right; i++)
				Pright[i] = ps[i + left];

			Pleftmin = findClosest(Pleft);
			Prightmin = findClosest(Pright);
			Pclosest = mergePlanes(Pleftmin, Prightmin);
			return Pclosest;
		}
	}

	static P[] mergePlanes(P[] p1, P[] p2) throws Exception {
		
		if(p1.length>2 || p2.length>2)
			throw new Exception("Invalid state in mergePlanes");
				
		double d1 = distance(p1[0],p1[1]);
		double d2 = distance(p2[0],p2[1]);
		double D = d1 < d2 ? d1 : d2; // delta
		
		// minimum
		P[] pMin = d1 < d2 ? p1 : p2; // default either in left or right sub-plane
				
		// examine for possible min dist where
		// one point is in left sub-plane and one point is in right sub-plane
		for (int i = 0; i < p1.length; i++) {
			for (int j = 0; j < p2.length; j++) {
				P pi = p1[i];
				P pj = p2[j];
				if (pi.equals(pj)) 
					continue;

				double xi = p1[i].getX();
				double xj = p2[j].getX();
				double yi = p1[i].getY();
				double yj = p2[j].getY();

				if (xi < xj + D && yi + D > yj && yj > yi - D) {
					if ( distance(pi,pj) < D) {
						return new P[]{ pi, pj };
					}
				}
			}
		}		
		
		// either both points were in left or right sub-plane
		return pMin;			
	}

	// O(n^2) naive version of closest pair
	static P[] shortest(P[] ps) {
		P p1 = null;
		P p2 = null;

		double distance = Double.MAX_VALUE;
		for (int i = 0; i < ps.length; i++) {
			for (int j = 0; j < i; j++) {
				if (i == j)
					continue;
				P ptemp1 = ps[i];
				P ptemp2 = ps[j];
				if (ptemp1.equals(ptemp2))
					continue;

				double newDistance = distance(ptemp1, ptemp2);
				if (newDistance < distance) {
					// update
					distance = newDistance;
					p1 = ptemp1;
					p2 = ptemp2;
				}
			}
		}
		P[] points = new P[]{ p1, p2};		
		return points;
	}

	static P[] union(P[] ps1, P[] ps2) {
		P[] ps = new P[ps1.length + ps2.length];
		for (int i = 0; i < ps1.length; i++)
			ps[i] = ps1[i];
		for (int i = 0; i < ps2.length; i++)
			ps[i + ps1.length] = ps2[i];
		return ps;
	}

	static double distance(P p1, P p2) {
		return p1.distance(p2); // Java api, Euclidean dist
	}

	static final Comparator<P> xComparator = new Comparator<P>() {
		@Override
		public int compare(P a, P b) {
			if (a.x < b.x) {
				return -1;
			}
			if (a.x > b.x) {
				return 1;
			}
			// if equal, sort by y
			if (a.y < b.y) {
				return -1;
			}
			if (a.y > b.y) {
				return 1;
			}
			return 0;
		}
	};	
}

P.java

import java.awt.geom.Point2D;

/**
 * @author Kunuk Nykjaer
 */

public class P extends Point2D.Double {
		public String name;	
		public P(double x, double y, String name)
		{
			super(x,y);
			this.name = name;		
		}	
		public P(double x, double y)
		{
			this(x,y,x+"_"+y);	
		}
		
		public String show()
		{
			int i = (int)(Math.round(x));
			int j = (int)(Math.round(y));				
			return name+" ["+i+";"+j+"]";		
		}	
		
		@Override
		public String toString()
		{
			return "("+x+";"+y+")";
		}
		
		@Override
		public boolean equals(Object o)
		{
			P p = (P)o;
			return this.name.equals(p.name);
		}
	}

Result:

(15.0;5.0)
(17.0;7.0)
dist: 2.8284271247461903

Written by kunuk Nykjaer

September 28, 2010 at 12:09 am

Genetic Algorithm example with Java

with 53 comments

Simple Genetic algorithm example.

The population starts with some random fitness strength, after some generations the algorithm should produce a population which has a stronger fitness strength. The max value possible here is 10.

Replacement strategy: elitism 10% i.e. top 10% parent survives each generation.
Steady state (only replace parent if child is better at least as good).
Selection strategy: Tournament method.
Find 4 random in population not same
Let 2 fight, and 2 fight
The winners makes 2 children

Click show source below for the Java code
GA.java

import java.util.*;

/**
 * @author Kunuk Nykjaer
 */

public class GA {

	static long BEGIN;
	static final boolean _DEBUG = true;
	LinkedList<Candidate> population = new LinkedList<Candidate>();
	final Random rand;
	
	final int populationSize = 10;
	final int parentUsePercent = 10;


	public GA() {
		rand = new Random();
		for (int i = 0; i < populationSize; i++) {
			Candidate c = new Candidate();
			c.random();				
			population.add(c);
		}

		Collections.sort(population); // sort method
		System.out.println("Init population sorted");
		print();
	}

	void print() {
		System.out.println("-- print");
		for (Candidate c : population) {
			System.out.println(c);
		}
	}

		
	/**
	 * Selection strategy: Tournament method
	 * Replacement strategy: elitism 10% and steady state		
	 * find 4 random in population not same
	 * let 2 fight, and 2 fight
	 * the winners makes 2 children	
	 */
	void produceNextGen() {
		LinkedList<Candidate> newpopulation = new LinkedList<Candidate>();
				
		while (newpopulation.size() < populationSize * (1.0-(parentUsePercent/100.0)) ) {
			int size = population.size();
			int i = rand.nextInt(size);
			int j, k, l;
			j = k = l = i;
			while (j == i)
				j = rand.nextInt(size);
			while (k == i || k == j)
				k = rand.nextInt(size);
			while (l == i || l == j || k == l)
				l = rand.nextInt(size);

			Candidate c1 = population.get(i);
			Candidate c2 = population.get(j);
			Candidate c3 = population.get(k);
			Candidate c4 = population.get(l);
						
			int f1 = c1.fitness();
			int f2 = c2.fitness();
			int f3 = c3.fitness();
			int f4 = c4.fitness();

			Candidate w1, w2;

			if (f1 > f2)
				w1 = c1;		
			else 
				w1 = c2;			
			if (f3 > f4)
				w2 = c3;			
			else  
				w2 = c4;
						
			Candidate child1, child2;
			
			
			// Method one-point crossover random pivot
			// int pivot = rand.nextInt(Candidate.SIZE-2) + 1; // cut interval is 1 .. size-1		
			//child1 = newChild(w1,w2,pivot);
			//child2 = newChild(w2,w1,pivot);
						
			// Method uniform crossover
			Candidate[] childs = newChilds(w1,w2);
			child1 = childs[0];
			child2 = childs[1];
			
			double mutatePercent = 0.01;
			boolean m1 = rand.nextFloat() <= mutatePercent;
			boolean m2 = rand.nextFloat() <= mutatePercent;
			
			if(m1)
				mutate(child1);
			if(m2)
				mutate(child2);
									
			boolean isChild1Good = child1.fitness() >= w1.fitness();
			boolean isChild2Good = child2.fitness() >= w2.fitness();
			
			newpopulation.add( isChild1Good ? child1 : w1);
			newpopulation.add( isChild2Good ? child2 : w2);
		}
						
		// add top percent parent		
		int j = (int)(populationSize*parentUsePercent/100.0);
		for (int i = 0; i < j; i++) {
			newpopulation.add( population.get(i));
		}				
				
		population=newpopulation;	
		Collections.sort(population);
	}
	
	// one-point crossover random pivot
	Candidate newChild(Candidate c1, Candidate c2, int pivot)
	{		
		Candidate child = new Candidate();
		
		for (int i = 0; i < pivot; i++) {
			child.genotype[i] = c1.genotype[i];
		}
		for (int j = pivot; j < Candidate.SIZE; j++) {
			child.genotype[j] = c2.genotype[j];
		}		
		
		return child;		
	}

	// Uniform crossover
	Candidate[] newChilds(Candidate c1, Candidate c2)
	{		
		Candidate child1 = new Candidate();
		Candidate child2 = new Candidate();
					
		for (int i = 0; i < Candidate.SIZE; i++) {
			boolean b = rand.nextFloat() >= 0.5;
			if(b){						
				child1.genotype[i] = c1.genotype[i];
				child2.genotype[i] = c2.genotype[i];
			}
			else
			{
				child1.genotype[i] = c2.genotype[i];
				child2.genotype[i] = c1.genotype[i];
			}
		}
				
		
		return new Candidate[]{child1,child2} ;		
	}	
	
	
	void mutate(Candidate c) {
		int i = rand.nextInt(Candidate.SIZE);
		c.genotype[i] = !c.genotype[i]; // flip
	}

	public static void main(String[] args) {
		BEGIN = System.currentTimeMillis();

		GA ga = new GA();
		ga.run();

		long END = System.currentTimeMillis();
		System.out.println("Time: " + (END - BEGIN) / 1000.0 + " sec.");
	}

	void run() {
		final int maxSteps = 50000;
		int count = 0;

		while (count < maxSteps) {
			count++;
			produceNextGen();
		}
	
		System.out.println("\nResult");
		print();
	}

	public class Candidate implements Comparable<Candidate> {
		public static final int SIZE = 10;
		public boolean[] genotype;

		public Candidate() {
			genotype = new boolean[SIZE];			
		}
		
		void random()
		{
			for (int i = 0; i < genotype.length; i++) {
				genotype[i] = 0.5 > rand.nextFloat();
			}
		}
				
		private String gene()
		{
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < genotype.length; i++) {				
				sb.append(genotype[i] == true ? 1 : 0);
			}
			return sb.toString();
		}
		
		int fitness() {
			int sum = 0;
			for (int i = 0; i < genotype.length; i++) {
				if (genotype[i])
					sum++;
			}
			return sum;
		}

		public int compareTo(Candidate o) {
			int f1 = this.fitness();
			int f2 = o.fitness();

			if (f1 < f2)
				return 1;
			else if (f1 > f2)
				return -1;
			else
				return 0;
		}
		
		
		@Override
		public String toString()
		{
			return "gene="+gene()+" fit="+fitness();
		}
	}
}


Init population sorted
– print
gene=0001111101 fit=6
gene=1110001011 fit=6
gene=1010001111 fit=6
gene=0011010101 fit=5
gene=0011011001 fit=5
gene=0011111000 fit=5
gene=1101000010 fit=4
gene=1100010001 fit=4
gene=1001100010 fit=4
gene=0010100001 fit=3

Result
– print
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
gene=1111111111 fit=10
Time: 0.536 sec.

Written by kunuk Nykjaer

September 27, 2010 at 11:12 pm

Q-learning example with Java

with 3 comments

Q-learning is a technique for letting the AI learn by itself by giving it reward or punishment.

This example shows the Q-learning used for path finding. A robot learns where it should go from any state.

The robot starts at a random place, it keeps memory of the score while it explores the area, whenever it reaches the goal, we repeat with a new random start. After enough repetitions the score values will be stationary (convergence).

In this example the action outcome is deterministic (transition probability is 1) and the action selection is random. The score values are calculated by the Q-learning algorithm Q(s,a).
The image shows the states (A,B,C,D,E,F), possible actions from the states and the reward given.

q-learn1

Result Q*(s,a)
q-learn2

Policy Π*(s)
q-learn3

Click show source below for the Java code
Qlearning.java

import java.text.DecimalFormat;
import java.util.Random;

/**
 * @author Kunuk Nykjaer
 */
public class Qlearning {
	final DecimalFormat df = new DecimalFormat("#.##");

	// path finding
	final double alpha = 0.1;
	final double gamma = 0.9;


// states A,B,C,D,E,F
// e.g. from A we can go to B or D
// from C we can only go to C 
// C is goal state, reward 100 when B->C or F->C
// 
// _______
// |A|B|C|
// |_____|
// |D|E|F|
// |_____|
//

	final int stateA = 0;
	final int stateB = 1;
	final int stateC = 2;
	final int stateD = 3;
	final int stateE = 4;
	final int stateF = 5;

	final int statesCount = 6;
	final int[] states = new int[]{stateA,stateB,stateC,stateD,stateE,stateF};

	// http://en.wikipedia.org/wiki/Q-learning
	// http://people.revoledu.com/kardi/tutorial/ReinforcementLearning/Q-Learning.htm

	// Q(s,a)= Q(s,a) + alpha * (R(s,a) + gamma * Max(next state, all actions) - Q(s,a))

	int[][] R = new int[statesCount][statesCount]; // reward lookup
	double[][] Q = new double[statesCount][statesCount]; // Q learning

	int[] actionsFromA = new int[] { stateB, stateD };
	int[] actionsFromB = new int[] { stateA, stateC, stateE };
	int[] actionsFromC = new int[] { stateC };
	int[] actionsFromD = new int[] { stateA, stateE };
	int[] actionsFromE = new int[] { stateB, stateD, stateF };
	int[] actionsFromF = new int[] { stateC, stateE };
	int[][] actions = new int[][] { actionsFromA, actionsFromB, actionsFromC,
			actionsFromD, actionsFromE, actionsFromF };

	String[] stateNames = new String[] { "A", "B", "C", "D", "E", "F" };

	public Qlearning() {
		init();
	}

	public void init() {		
		R[stateB][stateC] = 100; // from b to c
		R[stateF][stateC] = 100; // from f to c		
	}

	public static void main(String[] args) {
		long BEGIN = System.currentTimeMillis();

		Qlearning obj = new Qlearning();

		obj.run();
		obj.printResult();
		obj.showPolicy();

		long END = System.currentTimeMillis();
		System.out.println("Time: " + (END - BEGIN) / 1000.0 + " sec.");
	}

	void run() {
		/*
		 1. Set parameter , and environment reward matrix R 
		 2. Initialize matrix Q as zero matrix 
		 3. For each episode: Select random initial state 
			Do while not reach goal state o 
				Select one among all possible actions for the current state o 
				Using this possible action, consider to go to the next state o 
				Get maximum Q value of this next state based on all possible actions o 
				Compute o Set the next state as the current state
		 */

		// For each episode
		Random rand = new Random();
		for (int i = 0; i < 1000; i++) { // train episodes
			// Select random initial state
			int state = rand.nextInt(statesCount);
			while (state != stateC) // goal state
			{
				// Select one among all possible actions for the current state
				int[] actionsFromState = actions[state];
				
				// Selection strategy is random in this example
				int index = rand.nextInt(actionsFromState.length);
				int action = actionsFromState[index];

				// Action outcome is set to deterministic in this example
				// Transition probability is 1
				int nextState = action; // data structure

				// Using this possible action, consider to go to the next state
				double q = Q(state, action);
				double maxQ = maxQ(nextState);
				int r = R(state, action);

				double value = q + alpha * (r + gamma * maxQ - q);
				setQ(state, action, value);

				// Set the next state as the current state
				state = nextState;
			}
		}
	}

	double maxQ(int s) {
		int[] actionsFromState = actions[s];
		double maxValue = Double.MIN_VALUE;
		for (int i = 0; i < actionsFromState.length; i++) {
			int nextState = actionsFromState[i];
			double value = Q[s][nextState];

			if (value > maxValue)
				maxValue = value;
		}
		return maxValue;
	}

	// get policy from state
	int policy(int state) {
		int[] actionsFromState = actions[state];
		double maxValue = Double.MIN_VALUE;
		int policyGotoState = state; // default goto self if not found
		for (int i = 0; i < actionsFromState.length; i++) {
			int nextState = actionsFromState[i];
			double value = Q[state][nextState];

			if (value > maxValue) {
				maxValue = value;
				policyGotoState = nextState;
			}
		}
		return policyGotoState;
	}

	double Q(int s, int a) {
		return Q[s][a];
	}

	void setQ(int s, int a, double value) {
		Q[s][a] = value;
	}

	int R(int s, int a) {
		return R[s][a];
	}

	void printResult() {
		System.out.println("Print result");
		for (int i = 0; i < Q.length; i++) {
			System.out.print("out from " + stateNames[i] + ":  ");
			for (int j = 0; j < Q[i].length; j++) {
				System.out.print(df.format(Q[i][j]) + " ");
			}
			System.out.println();
		}
	}

	// policy is maxQ(states)
	void showPolicy() {
		System.out.println("\nshowPolicy");
		for (int i = 0; i < states.length; i++) {
			int from = states[i];
			int to =  policy(from);
			System.out.println("from "+stateNames[from]+" goto "+stateNames[to]);
		}			
	}
}

Print result
out from A:  0 90 0 72,9 0 0
out from B:  81 0 100 0 81 0
out from C:  0 0 0 0 0 0
out from D:  81 0 0 0 81 0
out from E:  0 90 0 72,9 0 90
out from F:  0 0 100 0 81 0

showPolicy
from a goto B
from b goto C
from c goto C
from d goto A
from e goto B
from f goto C
Time: 0.025 sec.

ConvergenceCon

The next post about q-learning can be read here
http://kunuk.wordpress.com/2012/01/14/q-learning-framework-example-with-c/

Written by kunuk Nykjaer

September 24, 2010 at 12:42 pm

Follow

Get every new post delivered to your Inbox.