Wednesday, October 29, 2008

Getting Fooled By Randomness



Reactions 

I ended up spending last one hour in debugging a program which runs perfectly while debugging but does not work as expected; when I run it normally. Bizarre...

Well, problem was the way in which I was generating random numbers. Look at the following snippet.


Random randomGen = new Random(System.currentTimeMillis());

Looks very simple and straightforward. But this line was part of a function. Which was structured as



fun()
{
for 1 to 1000
doRandom()
}
...

doRandom()
{
Random randomGen = new Random(System.currentTimeMillis());
number = randomGen.nextInt();
...
}


Has anyone reading it, figured out the problem with above code and why it will work fine while debugging?

If you have. Kudos.

The problem here is seed I am using i.e. System.currentTimeMillis().
  1. This seed will remain same for next 1/1000th of a second
  2. That's lots of time for a computer
  3. While debugging, every break point I place will result in delay and hence will lead to generation of different seed
Obvious solution is to make randomGen a class level variable. Modified code is like below



private Random _randomGen = new Random(System.currentTimeMillis());

...

doRandom()
{
number = _randomGen.nextInt();
...
}

Understanding Genetic Algorithms - 1 (Introduction)



Reactions 

Haha!! I know, I know. I still get nightmares about starting three part series on Memcached. Whose second and third part I never ended up writing, despite promising about it in multiples posts. I can only picture myself finishing that on a super lazy day, away from city, lying on a beach and that's not happening soon :).

So, I have been reading about Genetic Algorithms since last one week. Very fascinating subject and I will not write any theory on this as there are already well written information on this (how else would I learn about it? :)). See notes.

So, I be dividing the whole thought train in four posts. Yeah, here I go again :)
  1. Introduction: I intend to write my notes and some thoughts about Genetic Algorithms. I will skip most of the theory but following paragraphs can definitely serve as a starting point.
  2. API: I will primarily be writing a Java package for writing further code using this approach. Just interfaces which will serve as guide.
  3. PathFinder: Using the package written in second post. I will try solving an interesting problem of finding a path. There be tons of code :).
  4. Eight Queens: Well, everyone can recall this problem from childhood days. It will be a good candidate for testing power of this approach.
The Need

Programming is as much challenging a task as it is fun. From the very start, language designers have tried modeling various paradigms around real world. In the starting there was only machine code then came better instruction set then came functional programming and then object oriented programming. Advancement here is the increasing ability to model the code as near to real world; so it can aid in thinking and understanding for humans. But are we actually mimicking the real world while solving problems?

Genetic algorithms is a problem solving approach which tries to mix evolutionary biology with computer science. Idea is not to find an exact solution but an acceptable solution without actually writing the steps. Sounds magical? (Read the links in notes)

Idea is to

1) Create a population
2) Give it a goal
3) Give means to approach towards that goal but not the solution
4) Kill the population
5) Have some way to measure effectiveness of the ones who lived
6) Select the best
7) Mutate (optional)
8) Create new generation with some experience from the best in previous
9) Repeat the process until some generation solves the problem

That's how nature works. Right?

Algorithm Vs Genetic Algorithms


Term algorithm in "Genetic Algorithm" is misleading in a way. Algorithm translates into series of steps which transforms a given input into expected output. The keyword here is expected. I know precisely what output I will get if I pass (4, 6, 2, 2) to any sorting algorithm.

The problems solved using GA approach are of different nature. What if I want to simulate some situation whose outcome I don't know. War games? Affect of population increase on existent resources? Machine learning.

Applications
  • Simulation
  • Machine Evolution aka machines making other machines
  • Machine Learning
Notes:

http://en.wikipedia.org/wiki/Genetic_algorithm
http://www.ai-junkie.com/ga/intro/gat1.html

Saturday, October 11, 2008

Games Banks Plays - A Personal Account of Being Cheated



Reactions 

My father had always been cynical of private sector banks. And I had been critical of him on this as very word bank means trust. "I bank upon you". It is beyond my imagination that a bank holding trillions in money of millions of people will play cheap. After this incident, it will take a while before my "trust others" level reach to what it was.

A banker friend once told me that they are paid so well not for their skills but for their integrity. After some thought, I found him correct. But point then was their role in million dollar transactions.

Isn't the recent banking collapse proof enough that justified corporate greed in the end result in nothing but failure. Banks collapsed because they were allowed to leverage 33 times their cash reserve i.e. 3% default was enough to wipe them out. In their infinite greed banks ended up leveraging themselves this badly and result is known to everyone.

Coming to my story. I asked ICICI to send my amortization chart as I needed that for accounting purposes. To my great surprise my 20 year loan was now a 40 year loan!! Has anyone ever heard of a 40 year bank loan? But this was not it. For next 20 years I was only paying interest and after that my EMI would start including principal of what I thought I was paying. To simplify, I was scheduled to pay only interest for next 20 years.

Now my problem is bigger than this. How many people actually cares to check this? My lose is only of not paying four month's principal after discovering this. What about people who are not tech savvy and are trusting of banks? More I thought about this, more it sounds like a scam. Even if one in five people turned out to be ignorant (I am not sure if trusting is right word anymore) than... I am out of words.

Although, this change was made without any permission from me or even giving me any notification. I am asked to send a mail and wait for eight business days before correction (can't call it change) can be made. I am truly feeling sick. I request everyone who is reading this to spread the word.

I feel sorry for people who make their livelihood knowing well in advance that their decisions although will hoard money for the organization but will also end up conning someone. I feel sorry for those organizations who commend this behavior as I am certain they are not being built to last.

To clarify: Before others ask me if my loan is now 40 years. No. I got it rectified. But what about people who have no idea about these changes. Banks should be changing only the interest rates as they are floating and not the tenure. Not without customer knowledge at least.