Posts RSS Comments RSS 74 Posts and 827 Comments till now

Archive for the 'Computer Science' Category

Words With Friends AI

I ran across a game called “Words With Friends” these days on the IPhone.  It is very fun to play.  After playing several games, being a computer scientist, I automatically think about developing and artificial intelligence program for the game.   It is pretty challenging as there are a lot of words, and so many possible combinations to check, and for different directions.  I first implemented an exhaustive search, testing every word in the dictionary for every cell.   The result was miserable, one hour goes by and the computer only tested 20% of the words.

Admitted I was stomped.  There has to be a better way.  I have to add heuristics to the search algorithm.  So I mapped out an efficient and clear thought process, and implemented it again.  This time the results are better, took 10 minutes to go through all the words.  Of course, in computer science there are ways to cut processing time with increase in the use of memory.  So after a couple hacks, it can run on reasonable times.

Testing the AI against some opponents, I  kept on improving it with some simple strategies such as sacrificing points to reduce exposure to Double or Triple risks.  I think the AI is pretty good now.  I must say it was a fun project.

Click Here to use my Words With Friends Cheat

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

Next »