12.18.2009

The transition from C# to VB

For not having written any visual basic code in about a year, the transition from C# back to VB has been a slightly rough one.  I noticed an endless barrage of squiggly lines indicating syntax errors have plagued my code.  VB's compile-time syntax checking in Visual Studio didn't help either.  It let me be even more aware of my failure to comply with VB's rules.

The biggest offender was in my variable declaration.  Yes!  In the variable declaration!  I shall declare all my variables with a Dim.

I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
All your base are belong to us.

The point here is, like that of spoken languages, computer languages too need to be learned and perpetually practiced. Just like that violin you quit playing the moment you got accepted to a good university.  Don't neglect it or you may never get it back.

Ok, that's being overly dramatic.  Fine.  But, honestly, going from language to language is more of a nuisance than anything else unless you program in all of them everyday.  I quickly got over the Dim thing. I got used to the absence of the symbols in the beloved C-Style language: { } [ ] ; . I didn't like the fact that I had to google everything from "VB ternary operators" to "VB linq to sql examples."  I'm pretty good with Linq in C#, but in VB it seems so foreign to me. Man, I wish I saved my search history. But, in the end everything got done... with a 30% drop in productivity. Just kidding.

As for the long standing C# vs VB debate, here's my view.  Who cares?  For those of you who share the same passion as I do, we can have a language preference but that doesn't mean should refuse to work on projects written in a different languages.  Besides the fact that we might get fired, it's just not right.  We programmers are taught to learn to how learn.  Our programming languages course showed us how to adapt to other languages.  Where's the fun in exploring new ways to the do same thing?  So what if C# developers get paid more than their VB counterparts on average? Wait, I do.

12.16.2009

Backpropagation in Neural Networks

Backpropagation is a form of Supervised Training which teaches a Neural Network how to work and operate.  The training is done prior to using the network and works only for feed-forward networks.  There are many other ways to train a Neural Network, even in unsupervised ways, but Backpropagation is a widely popular training method because of its "learn by example" applicability in many real world cases.  This kind of network operates on the premise that given an input, it will produce the known and "correct" output.  This is analogous to training a cell phone to recognize your voice and how you pronounce certain words.  So you can train a network with inputs and what their corresponding outputs should be.  You can't train a network, however, to decipher what your cat's mood is at any given point.  Maybe someday?

Let's look into the well known XOR example.  We all know what this is right?  The bitwise Exclusive Or produces a known and correct output given two inputs as shown below.  Download the source code / demo for the Exclusive OR (XOR) problem.
Input data  Output data
(1, 1)          (0)
(1, 0)          (1)
(0, 1)          (1)
(0, 0)          (0)


In Neural Networks, neurons have weighted inputs, activation function and an output.  The input layer in this example has two elements (one for each bit).  The hidden layer calculates values based on the forumula: f(Sum(inputs * weight)).  The weight here is initialized to small random values, let's say between -1 to 1, with a mean of 0.  This produces an output value and since we know what the expected output value is, we calculate this difference and call it the error.  Then, this error is backpropagated to the hidden layer and the input layer where by the weights are adjusted so that each time the same input pattern is presented to the network, the output will be a little closer to the expected output.  The goal of training is to minimize this error a little bit during each iteration, aka epoch.  Here's a snippet from my powerpoint presentation that sums this process up.  

     -For each input-output pattern
Evaluate output
Calculate the error between output and expected output
Adjust weights in the output layer
*Do the same for the hidden layer(s)

You're probably asking the question - why do we need a Neural Network to give us the answer of an XOR operation.  We don't.  It is for theoretical and teaching purposes.  Now, the real uses of this is technique is widely seen in the AI of video games.  Here is a statement that caught my eye when researching this topic.
An agent was trained in Quake III by to collect items, engage in combat, and navigate a map. The controller was a neural network that learned by backpropagation on pre-recorded demos of human players, using the player’s weapon information and location as inputs.
*From Backpropagation without Human Supervision for Visual Control in Quake II by Matt Parker and Bobby D. Bryant

11.04.2009

malloc vs calloc vs realloc

char* malloc(sizeOf)
Returns a pointer in the heap with the specified size. One major difference is that it does not initialize the memory.

char* calloc(numElements, sizeOfElement)
Returns a pointer in the heap with the specified size for a number of elements - usually for an array.

char* realloc(ptr, newSize)
Returns a pointer in the heap after growing or shrinking a block a memory that was allocated by using malloc, calloc or realloc.

void free(ptr)
No return value. Deallocates memory previous allocated by malloc, calloc or realloc. ptr is unchanged.

11.03.2009

Processes do not share Global Variables



In the C code above, what are the values at line 13 and 18?

At line 13, the value is 30.
At line 18, the value is 10.

Why is this you ask? It's because the fork system call produces a new child process which does not share global variables with its parent process. Each process has its own code section and the data region. The run-time stack is copied for each process, thus they are not the same variable. Threads, on the other hand, do share global variables. Great. Now you tell me.

Can you think of a way to get around this limitation?

10.21.2009

File transfer using SFTP in C#

Before we delve into how we accomplish this, let's first find some common ground on what S-F-T-P means. In wikipedia, you'll get a longer than expected list of links for this acronym. The technology we want to utilize is SSH File Transfer Protocol. It is alternately known as Secure File Transfer Protocol, but to avoid confusion (hopefully) we will refer to it as SFTP henceforth. Please don't get it confused with "FTP over SSH" as it is a completely different protocol. SFTP runs on TCP port 22, the port commonly used for Secure Shell - SSH. "FTP over SSH" uses the standard TCP ports 20/21.

At this point, your boss asks you to send a file over to a vendor's [S]FTP server. After you do some research and figure out that he really means SSH File Transfer Protocol, the battle is half won. You can simply send that file using most ftp programs available. I had no idea. I personally use CoreFTP and was pleasantly surprised to find a nice little SSH/SFTP checkbox in the bottom right corner of the connection screen.



Now you want an api. SharpSSH is such a library that does SFTP along with a host of other things. Download demos and source files here.

Here is how simple it is to encrypt a file by using PGP.  I decided to go with the open source route.  Gnu Privacy Guard provides a simple wrapper class that makes it easy to encrypt/decrypt messages:

StreamReader reader = new StreamReader(readFromFile);
StreamWriter writer = new StreamWriter(writeToFile);
string output = "";

// Begin encryption
GnuPGWrapper wrapper = new GnuPGWrapper();

wrapper.homedirectory = "C:\\gnupg";
wrapper.passphrase = "";
wrapper.originator = "";
wrapper.recipient = "blah@blah.com";
wrapper.command = Commands.Encrypt;
wrapper.verbose = VerboseLevel.VeryVerbose;
wrapper.ExecuteCommand(reader.ReadToEnd(), out output);

writer.Write(output);

8.12.2009

Video Game Programming

The following is a description of one of the courses I will be taking in my final semester of graduate school.
CMP 717: Video Game Programming. 4 hours, 4 credits.
General game architecture, asynchronous input, animated sprites, action oriented a.i., collision detection, scrolling, sound clips, 3D Graphics. Student projects involving development of several video games, both individually and in teams.
PREREQ: CMP 338 and a strong foundation in object oriented programming techniques.
COREQ: MAT 226
NOTE: Students should expect to devote a great deal of time working both individually and in teams to produce several video games written in Java. This is a “Programming Intensive” course.
Notice the text in bold. I can't wait....

7.06.2009

Stop H*Commerce

Stop H*Commerce. Go there. Please.

It's a documentary revealing the business of hacking. It gives an inside look into the lives of a white-hat hacker and a victim of the Nigerian email scam. You know, the one about a lawyer or banker having no way of transferring a large sum of money and he needs YOUR help. This scam is also known as the 419 scam whose name four-one-nine was given from the Nigerian penal code for scamming.

H*commerce (Hacker Commerce) is a term coined for an industry unlike any industry that happens all around the world. The people involved are hackers and thieves and what they trade is your data. It can be anything, but the most popular item on the black market menu is called the "dump" - the data on the magnetic strip on the back of credit cards. Read all about it here.