Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

     RWW: So it took two months to do this?

     PD: It’s not a trivial problem. It’s tricky. 
         I’m quite proud of the way I did it. 
         I can’t talk about the way we actually implemented
         it.   (laughs)
I wonder how they implemented it so that it renders so efficiently.


I figured caching the results of common patterns appearing in blocks of 2x2, 3x3, 4x4, etc. pixels which are surrounded by all 'off' pixels, you can easily skip many repetitive calculations.

  ----    ----
  -oo- =\ -oo-
  -oo- =/ -oo-
  ----    ----

  -----    -----    -----
  --o--    -----    --o--
  --o-- =\ -ooo- =\ --o--
  --o-- =/ ----- =/ --o--
  -----    -----    -----

  ------    ------    ------
  -oo---    -oo---    -oo---
  -oo--- =\ -o---- =\ -oo---
  ---oo- =/ ----o- =/ ---oo-
  ---oo-    ---oo-    ---oo-
  ------    ------    ------
Since repeating patterns tend to turn up a lot on their own in CGoL they would end up cached, and large blocks of n cells could be computed in 1 move rather than requiring o(8n) time. That seems to be how Hashlife works: http://en.wikipedia.org/wiki/Hashlife


Here is a CoffeeScript implementation of Hashlife

https://github.com/raganwald/cafeaulife



Where doesn't it give appropriate credit?

1. The description line for the Github package reads "Gosper's HashLife in CoffeeScript"

2. The readme says: "Cafe au Life implements Bill Gosper's HashLife algorithm."

3. The docs say "Cafe au Life is based on Bill Gosper's brilliant <a href="http://en.wikipedia.org/wiki/Hashlife>HashLife</a...; algorithm."


Well, I could be wrong but I swear did an 'F3' on the page earlier to search for Gosper and it seemed like it wasn't there then.

Apologies for not being able to use a browser and falsely accusing you!


Credit for ideas is important. I'd rather 100 people falsely accuse me than have one omission slip through. Thanks for caring.


FYI: It has its own home page: http://recursiveuniver.se


I think listlife is a simple and efficient algorithm: http://dotat.at/prog/life/life.html

Some years ago I implementend something similiar in javascript: http://pmav.eu/stuff/javascript-game-of-life-v3.1.1/

The benchmark can be really slow in some steps.


I've seen a detailed explanation of a very sophisticated implementation of GOL in java (using caching, cycle detection and similar things), but I can't find it right now. Damn.

Edit: found it http://www.ibiblio.org/lifepatterns/lifeapplet.html

I tend to think of cellular automata optimization as being related to data compression. This is also a simple concept with no simple solution, and what solutions are best depends on the type of data being processed. In Conway's Life, patterns tend to be blobby.

For blobby universes, one should probably consider dividing the universe up into blocks approximately the size of the blobs. For Life, 4x4 to 8x8 seem reasonable. I chose the upper bound, 8x8, for reasons of convenience: There happen to be 8 bits in a byte. I strongly considered 4x4, but it didn't work out as nice....

You should put the blocks in some kind of list, so that you waste zero time in the empty parts of the universe.

Already, note a complication: New elements in the list must be introduced if the pattern grows over a block's boundaries, but we have to know if the block's neighbor already exists. You can either do a simple linear search of the list, or binary search, or keep some kind of map. I chose to make a hash table. This is solely used for finding the neighbors of a new block; each existing block already keeps a pointer to its neighbors, as they will be referenced often.

There must also be an efficient algorithm within the blocks. I chose to primarily blaze straight thru each block. There are no inner loops until all cells in a block are processed. Also, fast-lookup tables are employed. I look up 4x4 blocks to determine the inner 2x2.

Note: CA programs typically consist of 2 main loops (plus a display loop), because CA rules operate on the cells in parallel, while the microprocessor is conceptually serial. This means that there must be two copies of the universe, effectively, so that no important info is destroyed in the process of creating the next generation. Often these 2 copies are not symmetrical. It was a great struggle for me, since almost every time I took something out of one loop to make it faster, I had to add something else to the other loop! Almost every time, that is; the exceptions to that rule lead to the best optimizations. In particular, there are good tradeoffs to be considered in bit-manipulations: shifting, masking, recombining to form an address in the lookup table....

It can also be considered that sometimes the contents of a block may stabilize, requiring no further processing. You could take the block out of the list, putting it in a "hibernation" state, only to be re-activated if a neighboring block has some activity spilling into it. These blocks would take zero processing time, just like a blank region of the universe.

Period-2 oscillators might also not be very difficult to detect, and remove from the processing time. This might be worthwhile in Life, because the blinker is the most common kind of random debris. Higher period oscillators are much more rare. It is also possible that gliders could be detected and simulated. You will get diminishing returns from this kind of optimization, unless you take it to an extreme (cf. HashLife).

Also, a block of cells that's completely empty might not be worth deallocating and removing from the hash table for a while. That takes some processing time, which could be significant in the case of an oscillator moving in and out of its space repeatedly. Only when memory gets low should the oldest blocks from the "morgue" be recycled.

When the program is fast enough, it should be considered that it isn't worth displaying generations any faster than the eye can see, or at least not much faster than the refresh rate of the monitor. Especially in windowed environments, display time can be a real bottleneck.


Meeeeee, too. Me, too. Believe me, I tried.


Can you talk about what is difficult about it? I know that Windows game of life programs can use hashes of positions to compute new steps even for massive worlds very efficiently. Is the JS difficulty with the graphics on the canvas? Or something else?


He wouldn't give me any specifics at all, but I can tell you that the challenge was related to the extremely low tolerance for lag on a search result page that's treated as law company-wide. He had to do it without slowing down the search page AT ALL, for anybody. Check it out; it even works on mobile.


Oh, I thought you meant you tried to implement something like this and had trouble. LOL. Well, thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: