Saturday, June 29, 2013

Simpletron: A Simple Computer\Microprocessor

Simpletron: A Simple Computer\Microprocessor

In my early days of learning Java I had been provided with "Java How to Program" by Deitel from my college library. I wondered the initial week as to why we had been given this book instead of the standard Sun Microsystems Press Series, "Core Java 2 Volume 1 Fundamentals" by Dr. Cay S. Horstmann and Gary Cornell. Now, I realize in hindsight that it was a good choice on behalf of the library for us, who had first time exposure to Java in our engineering curriculum.


I don't mean to say that Core Java Volume 1 Fundamentals is not a good book, but its for programmers (already drilled with the art of programming, looking to learn Java for the first time) and those with at least one programming language familiarity. It doesn't contain enough large/complex real life examples as in Java How to Program. Also, Java How to Program was a somewhat composite book with a brief introduction of Advanced concepts which has been separated by Sun Microsystems Press into Volume 2 Advanced Concepts. Deitel also contains lots and lots of exercises for practice to hone your Java skills to a very high degree of perfection.

Now, returning to the topic of this blog. The thickness/volume of this book initially made it quite and intimidating task to initiate the learning process.


After my initiation into Java and a few months of tinkering of code on Netbeans IDE (Yeah, one of the first mistakes I did as a beginner was to use an IDE. Should've used the Terminal/Command Prompt and I'd have become better quicker), I was introduced by a friend of mine to "Head First Java" from O'Reilly by Kathy Sierra & Bert Bates. It is an excellent book for beginners!

While cementing my concepts, juggling between the three books and two IDEs (I started using Eclipse. It is the most widely used IDE), I frequently returned to Java How to Program for examples and exercises and one day I stumbled upon question number 7.35 Special Section: Building Your Own Computer (Page Number 309). I was instantly intrigued by the question and support material it contained to create a program which I would in later classes and years will come to know as a Microprocessor. If I'd have realized it at that time, it may have had become a daunting task (It's true that, ignorance is bliss).

In a few weeks I had completed the project and then went on to add a mnemonic conversion facility. It was a very good experience, I'll admit.

So, without further delay. Here's the source code to my Simpletron.

Download Source Code

Oh! And do comment and critique.

Screen Shots

[Note: I've not posted other screen-shots due to their intimidating and disturbing nature, especially with a lot of doubles printed in 2D Arrays may look a lot like advanced mathematics. Trust me it's easier. Also, try my code after you've attempted to built your.]

Wednesday, June 26, 2013

Simmunity: A New Game of Life

Simmunity: A New Game of Life

Conway invented the Game of Life in 1970; A "zero-player"game which demonstrated that a complex and sophisticated system can be created by a simple set of rules on a cellular automaton given only its initial condition. It is one of the most amazing AI experiments I've studied.

I was inspired by an entry in the list of Under Graduate Research Projects by CS Department of the Princeton University, to build a variant of Conway's Game of Life to Simulate an Immunity System [which has been now removed in recent updates; someone may have done it or it may have become archaic, don't know].

I then began studying about Conway's Game of Life and it's extrapolated conclusions on Evolutionary Science. A simple program with a beautiful visual display with a nostalgic video-game style simulation, made me very happy and interested to do this project. It's graphic display would also be a good project to show-off to non-computer science people. And, thus began my journey.

On the way, I found a lot of knowledge missing in me and had to improvise to generate, share and visualize the Immunity System as a Game of Life. I did not have enough know how to build GUIs in Java (one of my strong and comfortable languages) and thus had to generate universe matrix in Java and then write them to text files in CSV(Comma Separated Values) format. This CSV was next read by a MATLAB program and visualized in a colored grid. The series of grids were saved as frames and animated to show each step of change in the universe step-by-step.

The Rules of Simmunity are:

  1. Regeneration Step: 10% RBC (Red Blood Cells) are regenerated in a random fashion in empty places (i.e occupied by Blood-Plasma) after every step.
  2. Regeneration Step: 10% WBC (White Blood Cells) are regenerated in a random fashion in empty places (i.e occupied by Blood-Plasma) after every step.
  3. Pathogen Spread Step: Pathogen (denoted by black in the graphics grid) eats and replaces (i.e.copies itself in) any one of the 8 neighboring adjacent cell if populated by an RBC.
  4. WBC Spread Step:  WBC eats and replaces (i.e.copies itself in) any one of the 8 neighboring adjacent cell if populated by Pathogen.
  5. Starvation Death Step:  When a Pathogen cell is not surrounded by any RBC for more than three steps/iterations the Pathogen dies to leave Plasma (empty cell) behind.
  6. Starvation Death Step:  When a WBC is not surrounded by any Pathogen cell for more than three steps/iterations the WBC dies to leave Plasma (empty cell) behind.
  7. Natural Death Step: A RBC dies after a few iterations simulating natural death of RBCs.

 The Outcomes:

The body/system/universe heals successfully or fights off the disease/infection and no pathogen is left in the system. The WBC and RBC attain maximum levels flooding the universe and then attain an equilibrium of death and regeneration.

 OR

The body/system/universe succumbs to the disease and the pathogen spreads to dominate the universe for the stipulated number of iterations. Note: The body/system/universe will recover in later steps when the pathogen starts dying out of starvation and regenerating WBC will strike back with a vengeance. Also the RBC count will increase by regenration.

OR

The fight is still going on and the stipulated number of steps by the user has to be increased.

Additions:

I later added the facility to choose the size of the universe (i.e. grid/matrix) and the number of steps to be iterated. Another MATLAB program, I wrote, would count the number of RBCs, WBCs, Blood-Plasma(water) and Pathogen(virus etc) and record its step by step statistics which would be displayed as a net graphical outcome of the set of rules applied to the system and observe outliers or deviations.

***Addendum***

 I recently re-run my Simmunity program and found a few of my folders misplaced from the Desktop which may cause trouble. Thus, I'm adding this note here;
"You need to create a Directory called SIMMUNITY on the Desktop with two sub-directories, Outputs and Universes Collection."
 The Outputs sub-directory will contain FirstStat.txt and SimData.txt files auto-generated by the Java AI program. These contain a CSV matrix for processing by Matlab M-Files. The Universes Collection sub-directory will contain a Random Universe.txt file that will contain the size of the simulated cellular universe (i.e the grid or matrix) for the Matlab M-Files to run loops accordingly and create a simulation. You do not need to create these text files.

Screen Shots: 




 Download Source Code:

*Comments/Critiques are welcomed and appreciated.