Wednesday, 18 July 2012

Colouring in: bored-student-in-the-early-hours-of-the-morning style

You remember the fun of colouring in don't you? A sheet of paper printed with some monochrome pattern was given to you by your parents to keep you quiet for a couple hours. You steadily coloured in each block with care and attention, pretending each one filled in was another area of Metropolis that had fallen to the pandemic... Oh, that was just me? Perhaps that goes some way to explaining why I am the sort of person who writes a programming blog at 3am.

Seeing as I am writing this at 3am I really should write about something. It would appear that I have written a very good introduction to a post about colouring in, which is particularly fortuitous as I have a very good program to write about that does exactly that.

What I have done is altered my last program so that instead of using a square box it uses any user inputted shape. To input a shape you first need to create a boundary of black cells, ensuring that it is closed, i.e. every black cell is touching at least two others horizontally, vertically or diagonally. You then need to set a spawn point by entering any characters in any one cell in the centre of your shape. In this rendition every ant is spawned at one cell for simplicity. So suppose I entered this:

A heart turns out to be an easy bit image.
Here the '<3' is my spawn point. If I then run my code with one ant it produces this:


The only thing cuter would be a kitten. I am not doing a kitten.

I am going to admit that it took me several runs to get this because the colours are still on random, but my romantic side refused to have anything put a red heart. Alright, pink. I'm not quite romantic enough to spend 30 minutes of non-sleeping for my computer to happen to pick brilliant crimson out of the sixteen million possible colours allowed by my code.

31,

mathmo

Thursday, 12 July 2012

Art for maths sake


gif maker
Sampling every 100, 50, 25, 10, 5, 1 steps.

This is the explanation for the images:
"First order of business was to fix my program from last post. Obviously the 22.8GB variable had to go. I managed to replace it with two 20KB variables, but in exchange for  that I have to spend a lot longer outputting. As per usual the program runs the entire simulation before outputting anything. When it gets to the visual output stage I have a counter which goes from 2 to 100000 with a step size of n. Every time the counter increments I change the colour of the cell that the ant is currently in to the colour it is meant to be at that turn according to the pre-processed simulation. That means that to get the full output I had to use a step size of 1, which is particularly processor-intensive but works. I then thought to myself "what would happen if I didn't use a step size of 1?". That resulted in the images above."
 I quite like the variety of styles in the images above, in particular:
  • The sparsity of the 100-step image.
  • The polka-dot of the 10-step image.
  • The suspiciously like a colour-coded topological map 1-step image. (I wonder if I could alter Langton's Ant to produce a randomly generated landscape...)
If you have an opinion please comment below, I want to pursue these further and it would be great to have some idea of what looks best!

Oh and seeing as someone managed to solve my last number pattern (it was the differences of consecutive 5th powers) I have come up with a new one!

3,

mathmo

P.S. This is a bloody difficult one, I will be impressed if someone gets it!

Sunday, 8 July 2012

My computer can't quite remember that much

I was planning to have a new post for you tonight, but some exceedingly bad programming on my part has scuppered that plan. More on that in a minute.

I have written a program that, if it was run on say your average supercomputer, would run very similarly to my initial multiple ant program except with a twist. When an ant lands on a coloured cell the cell won't revert to being white, it will instead turn a darker shade of the colour it currently is. To ensure this doesn't result in ants going in circles I have written it so that the cell alternates between being classed as 'coloured' or 'white' in the classical Langton's Ant sense.

I came up with this particular idea on a bus journey a couple days ago, when I realised that some of the patterns that have been cropping up are in their own way quite beautiful. I think this new iteration will result in some spectacular images.

A quick update on the developing maturity of my code:

  • I have rediscovered For...Next loops. I am now saving about 5 minutes on each bit of code, as I don't need to go back at the end and find the counter=counter+1 that I have missed out. For...Next loops work by taking a number equal to some specified lower bound, running a piece of code with that number and then incrementing the number until the number equals some specified upper bound. Do While loops take a number and keep running the code while the number meets some condition, say being less than or equal to some specified upper bound. So they are virtually the same, except in a Do While loop you need to manually increment the number each time the code runs, which is a little bit tiresome when you have twenty of them.
  • I have moved all of my colours into Hex, meaning I can get a much bigger range of colours. Read bigger as 300,000 times bigger.


And now finally we get to the reason I don't have anything juicy for you tonight...

The code I wrote tried to create a 22.8GB variable. Yes, a variable nearly 4 times larger than Skyrim. AND it was trying to create it in RAM. The thing is, I know exactly how to get around this as I did it that way in all of my previous code. I just happened to try something new and it has backfired spectacularly. The error was so big that instead of bringing up a basic error message Windows took control, fired off an error report to whomever those actually go to and restarted Excel. That has never happened to me before.

I will sort this all out tomorrow, by which time my computer will either have calmed down or comitted itself.

4651,

mathmo


Sunday, 1 July 2012

Football, football, chimpanzee-riding-a-segway, football

I have finished my challenge! With ten minutes remaining until I go to the pub, I have typed the last of my 200~ lines of code... I don't have time to upload it, but tomorrow I will be uploading a GIF of the game in action. Come on Italy!

Update:

Admittedly this IS rather later than planned, but it turns out I was being pretty optimistic when I said I was 'finished'. Fifty-additional-lines-of-code optimistic; including a timer, a visual output and a complete re-haul of what I am naively calling my 'engine'. Note: the ball is the black dot that spends most of the game flashing into and out of existence, and when it is in existence thinking "Oh, this patch of grass again".


I apologise for the points where the entire board goes blank, that is when the moment that the screen recorder takes an image syncs up with when I clear the board before placing the ants at their next position. As you see in the message box at the end the game was a nil-nil draw, and that was with 51 players on each side. Clearly there is no need to replace Manchester United with giant robotic ants. Yet.

My game has some simplified rules:

  1. Players start randomly positioned around their own half.
  2. The goalies are only only allowed to move up and down their goal line (orange).
  3. When a player runs into the ball it is kicked in a straight line of length ranging between 10 and 30 cells. The ball stops if it hits another player.
  4. If the ball goes out the sides it is randomly positioned on a line parallel to the halfway line.
  5. If the ball goes out the ends it returns to the centre (a weak simulation of a goalkick).

For interested parties, here is the entire spreadsheet of my project. For the football program go to the "Football" sheet, then CTRL+E to bring up the menu and type in "football".

Technical details:
The way I have written this program is such that it processes the entire game before putting up the display. The benefits of this are a smoother visualisation and the ability to remove the 'trail' of the ant making the visual output much clearer. This means I am now not storing the locations of my ants in excel cells, but in 2 three dimensional arrays (three because I reference by player number, by team number and by turn). This is much more memory heavy, but gives the benefits listed above.

2101,

mathmo

Easter Egg 1: A chimpanzee riding a Segway.

Easter Egg 2: In the spreadsheet go to the sheet called "Game", CTRL+E to bring up the menu and type in "game2". Be amazed. WARNING: This uses my old style of processing so will take a while.

About Me

I am a mathmo (mathematician for anyone not familiar with Cambridge slang) studying at the University of Cambridge, and this is the blog of my summer project on Langton's Ant. This project was dreamt up one evening in the college bar when I was showing some of the compscis (computer scientists) my old visual basic excel macros and stumbled across a very basic Langton's Ant. What I showed them was just one boring black ant. By the time I left the bar that morning I had progressed to two coloured ants colliding with each other, the demo macro that most of this project is built from. Through this project I hope to expand my knowledge of visual basic, encourage others to mess around with maths on their computers, and to make a lot of pretty pictures. I will aim to keep my language fairly non-technical, but feel free to comment if you have a question or even a suggestion on how to improve my code. Here it goes...