Posts RSS Comments RSS 72 Posts and 611 Comments till now

Archive for the 'Computer Science' Category

The Graphical Economy Tool

Since the U.S. economy has been very turbulent for the past couple of months, I decided to create a tool that will give me a better insight than the speculative commentaries by wall street “analysts”.   Since the government has a huge census database on inflation, employment, wages, etc, I decided to utilize such data and create intuitive graphs to draw insights on the economy.

After a weekend of programming, I finished making The Graphical Economy.

The line graphs will display the time series data, making it easier to spot interesting trends.   You are all welcome to use it and give me feedback.  But please understand that it may contain bugs and data issues.

Javascript Fun

Just came across a very neat Javascript trick.

1. Go to Google Images (http://images.google.com)

2. Search for “Apple” or anything that will give you results

3. Copy and Paste the following into the URL bar of your browser and hit Enter:

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI= document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5 ); void(0)

Left Brain or Right Brain

Today I came across a very interesting illusion:

If you see the above person turning clockwise, that means you are left-brain oriented and good at analytical endeavors (math, science, logic, etc)

If you see the above person turning counter-clockwise, that means you are right-brain oriented and good at artistical endeavors (arts, music, poetry, etc)

 
Thinking about being analytical and artistic, it suddenly becomes clear to me why good programmers are so rare.   I think everyone will agree that computer programming is an analytical endeavor.  However, in my opinion it also requires a lot of artistic thinking.   Instead of using different colors and paints, computer programmers use basic programming expressions to design complex data structures, threading models, object interactions, etc.
 
 

A programmer is not much different from an Architect.  While architects designs buildings, programmers design programs.  Architects uses tools such as different columns, materials, forms, spacial concepts, cultural symbols, etc.  Programmers users tools such as different languages, classes, data structures, algorithms, components, plugins, etc.  Computer language to a programmer is like ArchiCAD/AutoCAD for an architect.   The quality of the final product depends mostly on the design, instead of the tool. 
 
 

At the end, we are all artists trying to design and create a final masterpiece by brining together different primitive design components.

Robocode - Predictive Targeting Implementation

When I was building my bot, the most interesting aspect is implementing predictive targeting.   To tackle this problem, I first define two aspects I can use for prediction:  Velocity Change and Angular Change per turn.

Here is how I capture these two parameters:

angularChange = (targetHeading - lastTargetHeading) / turnsElapsed;
velocityChange = (targetVelocity - lastTargetVelocity) / turnsElapsed;

After capturing these two values, I can calculate an angle in addition to the current bearing such that the bullet shot from that new angle can arrive within 5 pixels of the target in some future turn.

Here is how I calculate future bullet and target positions at futuren turn:

double estTargetX = targetX;
double estTargetY = targetY;

for (double time = 1; time < 100; time++) {

  estTargetX = estTargetX + Math.sin(Math.toRadians((targetHeading + time * angularChange))) *(targetVelocity + time * velocityChange);
  estTargetY = estTargetY + Math.cos(Math.toRadians((targetHeading + time * angularChange))) * (targetVelocity + time * velocityChange);

  estBulletX = myX + Math.sin(Math.toRadians(myAngle)) * (time - timeTurnGun) * bulletSpeed;
  estBulletY = myY + Math.cos(Math.toRadians(myAngle)) * (time - timeTurnGun) * bulletSpeed;
}

Once the distance between the target and bullet is less than 5 pixels, I will turn the gun to myAngle and fire a shot.

I’ve attached my robocode file for reference.

My Robocode Bot

On Robocode & Strategy

Days ago I’ve stumpled upon a programming game called Robocode while I was reading up AI.   Basically it is a platform for programming intelligent tanks that fight with each other. 

http://robocode.sourceforge.net/

After playing with it for a while, I discover the following to be true for heads-up battles:

1. It is always better to move around, this avoids targets.

2. It is always better to predict the movement of the enemy and shoot accordingly.

3. It is always better to travel at unpredictable angles and speeds so your enemy cannot predict your future position.

Having this in mind, I started designing my robot to avoid bullets and predict targets.

As it turns out, my robot comes out to be pretty intelligent.

Through implementing my robocode, I realize the following:

1. Each action has a strategic value, which is:

(My Benefit - My Cost) - (Opponent Benefit - Opponent Cost).

Although looks simple, this equation entails the impacts on future actions.

2. Some actions have higher strategic values than other actions.

3. Given the rules of the game, some actions would always have higher strategic value than other actions.

4. A working strategy comprises of a sequence of high-value actions.

5. Theories are important in finding out what needs to be done. (In robocode’s case, I have used trigonometry extensively).

6. Good execution skills are important to carry out what needs to be done. (In robocode’s case, good execution is good programming in Java).

These six points can be applied to any strategic settings, be it chess or business.   Of course, the games can be much more complex than robocode.  But the principles of strategy are the same.