## Tuesday, 3 December 2013

### Evolution of Pseudo Randomness

"Anyone who attempts to generate random numbers by
deterministic means is, of course, living in a state of sin."
John Von Neumann

The GIF below shows the evolving outputs of Pseudo Random Number Generators (PRNGs) produced by the Genetic Program I implemented in C.

See the Wiki pages on Genetic Algorithms & ProgrammingEntropy and Koza's paper [1] for supporting information to this post.

Each PRNG in the population outputs 16384 "random" bits / 2048 bytes. Fitness (or "randomness") of a PRNG is measured using the Shannon Entropy equation for binary sub sequences of size $h$ from $1 \to N_{max} = 8$, across the 2Kb output. For each sub sequence of size $h$;

$E_{h} = - \sum_{j} P_{(hj)}\ \log_2\ P_{(hj)}$

therefore the total entropy of a PRNG output for binary sub sequences of size $h$ from $1 \to N_{max} = 8$;

$E_{total} = \sum_{h = 1}^{N_{max}} \left[ - \sum_{j} P_{(hj)}\ \log_2\ P_{(hj)} \right]$

Therefore in order to achieve maximum entropy for $E_h$, all probabilities for all $2^h$ binary sub sequences of length $h$ must be equal to $\frac{1}{2^h}$.

The maximum achievable entropy for sub seqences of sizes $1 \to 8$ can be calculated as;

$E_{max} = \sum_{i = 1}^{8} i = \frac{8 \cdot (8 + 1)}{2} = 36$

For this run the population is evolved over 39 Generations. The entropy is evolved rapidly to begin with and then fluctuates around 97~99%* until on the 39th generation it reaches the terminating entropy of 99.93% (or 35.97386 bits of entropy out of a maximum 36 bits). Each frame in the GIF shows the output of the fittest PRNG in the population at that generation.

May the lord have mercy on my soul!

* The implementation isn't driven by hill climbing.

[1] John R. Koza, Evolving a Computer Program to Generate Random Numbers Using the Genetic Programming Paradigm. Stanford University, 1991.

## Tuesday, 3 September 2013

### I'm Home v1.0.1 Android Application

I'm Home is a new Android Application from West Coast Labs which automatically starts up your computer when you arrive at home or work, and runs quietly and efficiently in the background of your Android device.

If you are someone who likes their PC to be on constantly so that you don't have to spend time waiting for you PC to turn on. Then this app is a smart alternative which will help you save on energy bills and do a little extra for the environment!

Alternately if you are someone who shuts their computer down when you leave your home/work and turn it back on when you return, then this app will help reduce the time in your day spent staring at loading screens, and you don't even need to take your phone out!

It works by using the Wake On Lan service built into almost all motherboards, and by sending magic packets to your computer when your android phone joins your WiFi network. Wake On Lan is only provided by ethernet adaptors so your PC will have to be connected to the router via ethernet. (This app will work on WiFi only to wake the PCs in sleep mode (i.e. when the WiFi card is still on)).

How To

Step 1) Simply configure your Windows PC/Mac/Linux machine using the tutorial in this link. The most important part is to ensure that Wake On Lan is enabled in your BIOS (usually that's all that's needed).
Step 2) Download the application on the Play Store here or using the QR code above.
Step 3) Configure the application as follows;

PC IP or Broadcast IP: This is the broadcast IP of your router or the specific IP of the PC. Note that putting the broadcast IP of the router is more reliable than the specific IP address especially if IP addresses are allocated dynamically on your router. As it says in the app, typically your Broadcast IP will be one of the following; 255.255.255.255, 192.168.0.255 or 255.255.255.0.

MAC: The MAC address of your computer. To find out, in Windows type "ipconfig /all" into a command prompt and look for "physical address" under your Ethernet Adaptor.

In Mac or Linux type "ifconfig" and look for "HWaddr" under your eth device.

Port: This is important for when the PC is in sleep mode instead of being completely turned off. (Must be filled out anyway, it will either be 9/7)

SSID: This is the name of the router to which your phone and computer are connected or connect to. You can use the button to auto fill this box with your current WiFi network's SSID.

The time slider is used to stop the application from sending packets for a certain amount of time after the last disconnect. This is useful if you briefly come back but don't want your computer started. Also I noticed that Android phones have a habit of quickly disconnecting and reconnecting from WiFi even when they are in range, in the middle of the night! So this feature also prevents your computer being started when your asleep.

The first checkbox stops your PC from waking up when you start your phone and connect to WiFi straight away. The second checkbox explains itself.

The disable from checkbox allows you to setup a time frame in which the app will be disabled and your PC will not be woken.

All the forms must be filled out when saving and the IP and MAC must be in the correct syntax.

The send test button sends magic packets using the configuration on the screen (so it doesn't need to be saved).

Cancel closes the configuration. The PC wakeup part of the app is started when the device connects to the SSID in the configuration.

Note: On some machines Wake On Lan only works after a clean shutdown. If you turn the power off and on at the switch or at the PSU then the ethernet adapter might not be turned back on again until you start the computer manually.

Edit 13/09/2013 - Version 1.0.1

## Tuesday, 4 December 2012

### Lego NXT & LeJOS Table Mapping & Navigation

So I did this mini project over the summer but it has been collecting e-dust ever since, and I thought a nice way to finish it off would be to write a small blog post.

The idea is that I wanted to make my NXT map and navigate a quadrilateral table after being placed at any point and angle on the table. I won't delve to deeply into details as the PDF covers the technical side and the code speaks for itself, but long story short it works quite nicely, and theoretically perfectly... But as is the way with the NXT and its various bendy Lego Meccano pieces, things are calibrated for an infinitesimally small time frame. In fact I probably spent at least just as long calibrating the NXT as I did programming and writing the PDF! The idea is that by following the PDF and using the different programs in the Git repo, it can be repeated on any NXT using only the light sensor.

The PDF can be downloaded here if you find the PDF viewer below a little small.

Cheers!
Phil

## Monday, 18 June 2012

### Parallel Processing on the Pi (Bramble)

Parallel processing on the Raspberry Pi is possible, thanks to the ultra portable MPICH2 (Message Passing Interface). I was keen to try this out as soon as I managed to get hold of two of these brilliant little computers (yes I'm a lucky boy). Here I'm going to show how I managed to get it all working and will display the results :) (Bramble was a name an ingenious Raspberry Pi forum member made up, not myself!)

There are three ways which you can install MPICH2 (in case one doesn't seem to work for you), compiling and installing from source, my .deb package then following the rest of the tutorial, or the Python script file. Installing from source takes a while on the little Pi when not cross compiling.

Install - Choose Method 1, 2 or 3

and then run it as root with the command;
sudo python install.py

Then follow the instructions on the screen entering all of the necessary info. The script will do a full install and configuration like the steps in the tutorial below. Do this for every node on your Bramble.

I have also written an RSA key script which sets up a secure public and private key between the Master and all of the Slave nodes so that you don't have to enter the password into SSH every time, but maintain a secure connection;

python rsa.py
You can run as root, and enter the username "root" for the other Slave(s) also for when you want to SSH as root.

Finally you can join onto the Testing phase of the tutorial if you want to, otherwise you are all setup!
Please let me know of any problems with the scripts and I will fix it :) Thanks!

2) From .deb package

De-package and install;
sudo dpkg -i mpich2_1.4.1p1-1_armel.deb

3) From source;

Now extract the files;
tar zxfv mpich2-1.4.1p1.tar.gz

Go into the source folder;
cd mpich2-1.4.1p1

Now we must run ./configure to configure the files ready for compilation;
sudo ./configure --disable-f77 --disable-fc

If you have a FORTRAN compiler then you can run it without "--disable-f77" and "--disable-fc".
If you do not, and you don't want to run FORTRAN programs on your cluster then make sure you do configure with them disabled as the error occurs a good 20 mins in and you have to start all over again :).

Now that you have configured without error, compile then install the compiled program;
sudo make
sudo make install

Setup

Now you should have MPICH2 installed on both (all) of your Raspberry Pis. On your Bramble, you are going to need one Master node and the rest are Slave nodes.
First create this file in both the pi and root user home directories,
touch ~/.rhosts
sudo touch /root/.rhosts

add the following lines to the normal user file (pi is the default username for both of my nodes);
Master pi
Slave pi

For the root user;
Master root
Slave root

Do this for every Pi/node on the Bramble.

Now edit the /etc/hosts file to contain the IP of each node and the master node like so;
192.168.0.14 Master
192.168.0.11 Slave

Edit /etc/hosts.allow to contain just "ALL+", or each IP address of the other nodes.

(To find out the IP on each node, type ifconfig)
Now edit /etc/securetty to contain the line to allow root access;

Create the file /etc/pam.d/rsh if it is not there already and put the following in;
auth       optional     /lib/security/pam_securetty.so
auth       sufficient   /lib/security/pam_env.so
auth       sufficient   /lib/security/pam_rhosts_auth.so
account    sufficient   /lib/security/pam_stack.so service=system-auth
session    sufficient   /lib/security/pam_stack.so service=system-auth

SSH Public Key Authentication - Optional

This section is optional but advised! The use of a public and private key for SSH will mean you won't have to type in the SSH password between the Master and the Slaves every time you want to run a program.

Use the Python script I have written;

python rsa.py

Or if it does not work properly, follow the instructions at this address http://steve.dynedge.co.uk/2012/05/30/logging-into-a-rasberry-pi-using-publicprivate-keys/
All thanks go to Steve Smith who wrote the tutorial!

Testing

Now you should be ready to rock and roll! Lets test the cluster out.
If you installed from source, you will have this file to test in the ~/mpich2-1.4.1p1/examples directory (you will need to compile it). If you installed from the .deb package then download this test file to each of the nodes in the same directory of each node (or even better a centralised dir, perhaps with NFS);

Extract the tar ball and make sure the permissions are set;
tar zxfv cp_test.tar.gz
sudo chmod -R 777 cpi_test

Enter the cpi_test directory. This cpi program is a little program written in C which outputs Pi to 16 decimal places and outputs run time. I think this program is quite apt given what is running this program :)
Now we have to setup a "machinefile". This tells MPICH2 where to delegate the processes to. Yes, more IP addresses to come! Create the machine file on the Master node;
touch machinefile

Now edit the file to contain the IP addresses of the node and a colon folowed by the number process to follow. For example my machine file looks like this;
192.168.0.14
192.168.0.11:2

This tells MPICH2 to pass the first process to my Master node at 192.168.0.14 and the next (second) process to my only slave node at 192.168.0.11. There is no process number after the first IP because empty is the same to the first. (i.e. 192.168.0.14 is the same as 192.168.0.14:1).

It's easier command wise if the machinefile and the program you are executing are in the same folder to reduce the length of the next command ;)

Now it's finally time to run our first program! Se sure that you are in the cpi_test folder and that the same folder is located in the same place in all the other nodes on the Bramble. Execute the following line;
mpiexec -f machinefile -n 100 ./cpi

-f shows mpiexec where the machinefile is. -n is the number of times to repeat the process. This is set at 100 to execute cpi 100 times so that we can see the difference in power easier
If it gives you an error about file not found, again make sure all the cpi files are in the same location on each node.

If the command executes successfully then you should get a record of the processes running and an output like;

pi is approximately 3.1415926544231274, Error is 0.0000000008333343
wall clock time = 0.728116

You can remove the Slave IPs from the machinefile to compare running this program on just the Master node.

My Results

I ran the previous command 10 times, with both nodes enables and again with just the Master and took an average both times. The average I got from both nodes was; 0.731 Seconds – with a minimum of around 0.6 and a limit of 0.8. The average I got from one node enabled was; 1.418 Seconds –with a minimum of 0.85 and a limit of 5.0.

Finish

You should now have your Bramble up and running!

I just compiled and created a deb file, and found the best way to set the program up for the Pi, all thanks go to the team who created the awesome and portable MPICH2, and any sources I used to make the tutorial :)

This shows a lot of other useful applications of MPICH2 including compiling across your Bramble, would be nice to have a compiler farm made of Pis for big projects :)

Phil

 MPICH2 on the RPi

## Friday, 15 June 2012

### MPI on the Pi

Understandably, configuring and compiling C programs on the Raspberry Pi is a process which requires a fair amount of patients. Especially when after 30 minutes ./configure (ing), you get this error:

configure: error: No Fortran 77 compiler found. If you don't need to build any Fortran programs, you can disable Fortran support using --disable-f77 and --disable-fc. If you do want to build Fortran programs, you need to install a Fortran compiler such as gfortran or ifort before you can proceed.

So inevitably;

pi@raspberrypi:~\$ sudo ./configure --disable-f77 --disable-fc

I'm sorry FORTRAN. I do plan to revisit you some day, but attempting to compile and install an MPI on two Raspberry Pi's* is on my agenda today :)

*unnecessary apostrophe, but it looks more attractive than without it...

## Sunday, 3 June 2012

### NXT Fibonacci

I have just received my Lego NXT Mindstorm! I immediatley flashed LeJOS on the brick, and before I got to work on some AI, I decided to create a little program which represented the Fibonacci sequence terms as bursts of the motors.
import lejos.nxt.*;

public class fib {

public static void main (String[] args) {

//Limit the sequence.
int fib[] = new int[100];

//First two terms in sequence are 1
fib[0] = fib[1] = 1;

for (int i = 2; i < fib.length; i++) {
//Calculate, and print next value in Fibonacci sequence
fib[i] = fib[i - 1] + fib[i - 2];
LCD.clear();
LCD.drawInt(fib[i], 6, 4);

//Move forward for the length of the Fibonacci number
Motor.C.forward();
Motor.A.forward();
try{
} catch (Exception e) {}

//Sleep for a tenth of a second.
Motor.C.stop();
Motor.A.stop();
try{
} catch (Exception e) {}
}
}
}


## Friday, 11 May 2012

### First post. A little coffee...

Declaring a variable as an "Integer" as opposed to an "int", gives the programmer the ability to set that value to "null".
class is_this_null {
public static void main (String[] args) {
Integer i = null;
if (i == null)
System.out.print ("This is null");
}
}