Friday, October 31, 2008

Better Explained?

Cant help but post this, Check out http://betterexplained.com/. It has amazing new perspectives on simple issues...for starters, take a look at the pythagorean theorem. Im sure it'll amaze you as much as it did to me.

Wednesday, October 29, 2008

Reverse Engineering the Brain

Oh yes, you heard it right. It’s no longer a stuff of science fiction. On July 25, 2008, Intelligence Realm Inc. has completed a simulation of 100 billion neurons, the estimated size of the human brain. The simulation used distributed computing and involved over 3000 volunteers, 10000 processors, 180 TB of data and it lasted for a couple of months. This was the first simulation that bypassed the 100 billion level and used database files to store the data. The simulation is one of the first steps in a long-term project that is aiming to build a large-scale artificial intelligence by reverse engineering the brain.

So how are the neurons simulated? The concept of artificial neural networks (ANN) is used which can perform tasks like predicting stock market, hand writing recognition, face recognition and many more. A typical ANN hardly consists of 100 neurons. Imagine the potential of an ANN with 100 billion neurons. 'Google' on Artificial neural networks for more information...

So what’s the catch? There are many challenges involved to simulate an ANN at this degree.
1) Interconnection of neurons and their weights associated.
2) The processing power and storage capabilities.

The latter can be solved in an economical manner by using a distributed computing scheme similar to SETI (wherein, volunteers can share their processing power). As for the storage, with TB’s of hard disks coming up, that shouldn’t be much of an issue.

To solve the former, scientists have decided to mimic the connections and weights used in the brain. After all it is nothing less than marvelous. Take a look at www.brainmaps.org. They have created a brain map of many species. So far 92% of the human brain is mapped because of the complexity involved.

To get the gravity of this research, imagine the following scenario. A person is permanently blinded because of damage to visual cortex of the brain. Since, human brain mapping is available. A Nano sized chip could be made containing all the ANN’s required to process visual data. The beauty of this is that we don’t even have to understand how the information is processed. All we have to do is device a way for it to interact with the remainder of the brain by interconnecting the neurons of the chip with the real ones with a Nano surgery just as described by the brain map. By this a permanently blind person’s vision can be restored. This is because; the visual cortex neurons, connections and weights are mimicked exactly as it was in the real brain.

Seems far fetched? You’ll be in for a real surprise. Back in 1980’s, it was estimated that the number of computers to be used by the year 2000 would be about 100.

Understanding frameworks by example...2


Step 3: Check your code so far, revise and optimize


All might seem rosy at the moment but when you try out this code…you’ll notice that

1) Frame rates vary widely, some systems show FPS of up to 150 and some about 30
To ensure frame-rate consistency we’ll add ensureMinFPS() and ensureMaxFPS() functions. This can be done by skipping calls too render() function, also known as frame skipping.

That means we update status like 3 times but only draw it once onto the screen. The result is that the sprite may seem to be moving with large steps and may damage smoothness. But that’s the PC's problem nothing to do with us. Also we could add a convenience method getCurrentFPS(), just to check the performance.

2) For animating a moving object like a man, we’ll have to cycle through a set of images…
This requires some handling which is again redundant. So we’ll add AnimatedSprite class. This class may contain methods such as addFrame(Image img, long persistenceTime);

We’ll extend this class with Renderable.class so that its update and render methods can be utilized by the Game.class when registered with it.
@Override
public void update(long time)
{
Image curFrame = frames.get(curFrameIndex);
animTime += elapsedTime;

if(animTime >= curFrameduration)
{
curFrameIndex++;

//provides roll-over for animTime accurately...
animTime = animTime % curFrame.duration;

if(curFrameIndex == frames.size())
curFrameIndex = 0;
}
}
But sprites can move to...So we could use xSpeed, ySpeed and update them using:
xPos += xSpeed * (elapsedTime);
yPos += ySpeed * (elapsedTime);
this xPos and yPos could be utilized in render()
drawImage(getCurFrame() ,xPos,yPos);

Now re-run your code again. This time when you add sufficient renderable objects to your game, you’ll notice Overlapping.

i.e., the object added first is drawn first…
how will it seem if the graphics of a tree is being drawn after drawing man??


Forgive me for my crappy drawing…that’s the best I can come up with!
The problem occurs because in run() method of Game.class, we use:
for(int i=0 to numRenderable)
{
arrRenderable.get(i).update(curTime - prevTime);
arrRenderable.get(i).render();
}
avoid this we could modify addRenderableObject as
//image with smallest index is drawn 1st…
addRenderableObject (Renderable rend, int index)
{
arrRenderable.add(rend, index);
}
Now that the drawing order can be determined, overlapping won’t be a problem.

So far we have managed:
- Screen management, ensure min frame rate, a generic Renderable class that can be used with Game class followed by easier Animation handling.

Now imagine that you’re coding the game using THIS framework, suppose the game works on 500 images…you probably will have to load all the images @ startup and show a progress bar like loading or something…Now we’ll integrate this feature into this framework

Lets design a class called ResourceBox, that holds all the images.
public abstract class ResourceBox
{
Private HashMap hm = new HashMap();

//this is where you’ll load all the images…or ne other stuff
Public abstract void init();

Public void add(String id, Image img)
{
hm.put(id, img);
}

//methods such as remove, replace and so on…
//Include a method getImage(String Id)
}
We first create a ResourceBox object as:
ResourceBox rb = new ResourceBox()
{
@Override
Public void init()
{
//Load whatever resources you want…
//Ex – add(“man”, imgMan);
}
}
Remember the Game class…?
How do we register this object with the game class??
Game(ResourceBox rb)
{
//Copy this ref into a private variable within game class
}
This is how the game would run previously:
run()
{
Init();
Game loop
{

}
}
Here we plug in a bit of code:
run()
{
Init();
If(rb != null)
rb.init();
Game loop
{

}
}
Also we’ll add a utility method to game class as:
public ResourceBox getResourceBox();
This method is needed as we could use:
Image man = gameObj.getResourceBox().get(“man”);
Cool eh?

What about the progress bar??
We can’t provide an implementation as it would restrict the user from customizing/ making his own style of progress bar. More-over the user defined Loading screen or whatever must be shown when rb.init() method is called.

We’re gonna try some sort of an event listener kinda thingy. Here goes:

Include a method in ResourceBox.class
public void registerListener(Listener l);

//This is how Listener’s defined
Public class Listener
{
Public abstract void onInit(Graphics g);
}
First of all user’s gotta create an obj of Listener class for which you HAVE TO override the onInit() method
Listener l = new Listener()
{
@Override
Public void onInit(Graphics g)
{
//Draw your stuff on this graphics context…
//Ex = ((Graphics2D)g).do any thing();
}
}
Now we’ll also include a method getRegisteredListener() in ResourceBox.class
We now modify the game class run method as follows:
run()
{
Init();
if(rb != null)
{
Listener l = rb.getRegisteredListener();
If(l != null)
{
// Assuming we have some hypothetical screen management class
Graphics g = ScreenManager.getGraphics();
l.onInit(g);
}
rb.init();
g.dispose();// or clear…
}

Game loop
{

}
}
Now user can show a progress bar or…loading screen with his/her own graphics style…
So here’s a short summary,

- Abstract methods and stuff is absolutely integral in designing a good framework
(Primarily to give user the control of things, just as swing lets us control the shape of a button)

- You might need event listeners in case where your internal handling code needs to call something…like the onInit() we just discussed (swing provides action listeners etc…to a button which are automatically called on mouse click on that component, hope you can associate this with the onInit())

- Let your imagination run wild…experiment, try, you’ll eventually be able to write the top notch code. Okay, enough philosophy I suppose lets move further.

Go back to step 2:

The best way to add new feature(s) to your framework is like I said can be done by imagining the usage. So let’s start with some analysis...how do we integrate sounds? After all any game would suck without sounds…

In my next blog I’ll put up a nice UML diagram of the concepts so far and also discuss sound integration. As always, do comment and help me improve my posts.