Search

Geek Girl Joy

Ancestor Simulations Conway’s Mysterious Rules For Life

When we left off in my last post Conway’s Mysterious Game of Life, the small group of mathematicians lead by John Conway had just sat down to test his new rules.

Soon after beginning the new game came the cry “It’s walking!”!

Mathematician Richard Kenneth Guy was the first to discover the Glider while exploring Conway’s new world.

Like Dr. Frankenstein coming face to face with the monster of his own making, Conway was finally staring his creation in the face!

How did he do it? What makes it work?

Conway’s Rules:

  1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

 

Conway’s Creature

Game of life animated glider

As a result of Conway’s Rules the “Glider” was the first simulated self-organizing construct sometimes called a “Spaceship“. Conway later dubbed the game “Life” after observing a version that had been programmed into a mainframe computer.

The Moore Neighborhood

Conway’s Game of Life uses the Moore Neighborhood, which consists of all 8 squares directly around the cell you are computing for.

The basic strategy is to count how many “alive” cells are around the current cell to figure out what the next generation should be.

Then based on the number of neighbors the cell has, change it to alive or dead if a change is required.

According to the rules, if there are exactly 3 “alive” neighbors it should become “alive” too.

If it is already alive then stay alive if it has 2 or 3 “alive” neighbors, otherwise in all other cases the next value will be “dead”.

It sounds more difficult than it really is, let me demonstrate using the glider.

Generation 0

 

Generation 1

 

Generation 2

 

Generation 3

 

Generation 4

 

At this point you can see that we are now back to the initial pattern, but the glider has moved to the right by 1 column and down by 1 row.  I have animated the stills below so you can see it walk with the calculation showing.

Animated

 

So now that we are ready to start looking at code, I am going to end this post here and pick up tomorrow where we left off.

Have a great evening everyone!

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

Advertisements

Why We Marry Programming Languages

For right or wrong, better or worse, in sickness and in health… it happens to most of us at one point or another, we fall in love and I’m not talking that “puppy love” either…

I mean the “in it for the long term, I hear wedding bells” kind of love! 😛

When you have been programming for a goodly number of years and “gone steady” with at least a few languages, you find that generally there is a lot of overlap and similarity between them! Of course, there are differences… some specialize in their selected niche either by design or by homogenization of a developer community.

Essentially though, all programming languages are more or less capable of doing the same things, though they go about it in different ways.

I know you embedded developers are probably disagreeing with me at this point, let me just say I totally agree that when it comes to low level embedded situations, C Language is what you really want, so don’t bite my head off in the comments! 😛

My accedence (it’s a real word) about embedded applications notwithstanding, I think you can GENERALLY agree with this quote (though I’m not sure who to attribute the quote to) and I’m only just paraphrasing anyway, it goes like this:

“You can do just about the same things in all programming languages. What sets them apart is what they make easy and what they make hard.”

~Unknown

And that’s really the crux of it isn’t it!

After you have become familiar with your “lenguaje del amor” it’s suddenly like… traversing that billion-element 3D tuple isn’t so bad! 😛

Of course, it’s not quite that simple in reality but the fact that any given language can make something subjectively “easier” to an individual compared to another language due to a methodology or strategy employed by that language is very much at the heart of why programmers seem to “merry” their languages!

As I mentioned, there are various styles, methodologies and strategies implemented by the designers of programming languages that change how you think about problems they set out to tackle.

Further, despite comparing “multi-paradigm” languages with one another you get certain languages that just seem to fit better at solving certain types of problems.

I’ll give you an example of the subjectivity I’m talking about with 3 different “multi-paradigm” capable languages namely Python, JavaScript and my personal favorite PHP (some of you just gagged, please compose yourself… I’ll wait 😛 )…

So, lets use a string in each language as the subject of our examination.

In Python and JavaScript strings are said to be “immutable” whereas in PHP strings are “mutable”, now before I begin I am NOT stating that one is objectively better than the other, but I am going to try to demonstrate that there are differences between languages, which in and of itself should not be controversial.

Each has its pros and cons (I.E. reasons why an individual programmer may prefer one over the other), but they are all capable languages and great to know!

I simply aim to show that there ARE differences and those stylistic differences are a large factor, though of course not the only factor.

Python

Let’s start with “immutable” strings in Python. Let’s say we have a string variable with the label ‘name’ and either mistakenly assign or “point it to” the value of ‘coy’ or as part of our project we simply need the ability to modify the string.

Let’s then give ourselves the challenge of correcting the string during program execution to ‘Joy’ but without simply reassigning the name variable to ‘Joy’, in other words let change the ‘c’ to a ‘J’.

Now since Python strings are “immutable” they cannot be directly accessed and changed however that doesn’t mean that you CAN’T change them.

>>> name = 'coy'
>>> name[0] = 'J'
>>> print(name)

If you ran this code you would get this lovely little error:


Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'str' object does not support item assignment
name[0]

HONESTLY… I don’t think I have ever seen as CLEAN and pleasantly descriptive error in my entire tech career! Extreme kudos to the Python community! 🙂 That level of clarity is rare in programming errors!

In order to accomplish what we want we can do something like the following:

>>> name = 'coy'
>>> name = name[:0] + 'J' + name[1:]
>>> print(name)

The traditional “pythonic” response to this problem (I think) is to create a list and store the individual characters so that you can then get index access like this:

>>> name = list("Joy")
>>> name[0] = 'c'
>>> print(''.join(name))

Not better or worse than the other languages necessarily though not my personal favorite.

JavaScript

If you run this code you will NOT get an error…

var name = 'coy';
name[0] = 'J';
console.log(name);

Though the name variable remains ‘coy’! In order to accomplish what we want we can do something like the following:

var name = 'coy'; 
name = 'J' + name.substr(1);
console.log(name);

I don’t think there is a “prescribed” solution in the JS community though it is possible to do the Python solution and “split” the string into an array:

var name = 'coy'.split('');
name[0] = 'J';
console.log(name.join(''));

The thing is if you try this code on JSBin (not a sponsor) you get this rather cryptic error:

"error"
"@null.js:16:38
l</b.render/<@https://static.jsbin.com/js/prod/runner-4.1.0.min.js:1:13848
k</a.use/<@https://static.jsbin.com/js/prod/runner-4.1.0.min.js:1:10792
"

But it works fine on JSFiddle (not a sponsor) so I am not sure if it’s a versioning issue but coder beware! If there are Any JS coders in the audience feel free to contact me with the reason why this might be and I’ll credit you for the info! 🙂

PHP

PHP is a little different in that all strings are “byte” arrays already which just means that the indexes are already open to direct modification and that works like this:

$name = 'coy';
$name[0] = 'J';
echo $name;

The real difference being that with “immutable” strings you cannot change the value directly and instead have to reassign the value to another variable.

In both the JavaScript and Python examples we used the same label (name) for the variables but the internal ID’s for those variables are different. Using this methodology basically means having to “split” the string by hand, cull the unwanted value, insert the new value and recombine the string.

Again, neither approach is is objectively “correct” but if you work with a lot of string data you might soon come to appreciate the ease of PHP in that respect, I certainly do! 😛

The funny thing is though that although you can “marry” your language, it might not excel in all ways and you are then forced to admit that only one language cannot be everything you need it to be all the time… In fact, in my article The Impending Death of the Programmer I briefly talked about the need for “transpiler” software which does level the “playing field” somewhat but not entirely.

Several interesting tranpilers in the PHP community are wxPHP (not a sponsor) that allows for native WIN, MAC, & LINUX application development using PHP.

There is also KikAppTools (not a sponsor) which allows native Android & iOS mobile application development using PHP.

These tools extend the reach of a language significantly!

I am still waiting for Unity3D to implement a PHP version. 😛

Still though, it’s hard for just one language to “be all things to all people”, so to continue the metaphor, you just might find that “polyamory”… er “polyglotism” is the way to go! 😛

In any case, even if we are comfortable working in multiple languages… there will always our favorites!

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

Ancestor Simulations Conway’s Mysterious Game Of Life

In the 1935 operatic film Naughty Marietta there is a rather lovely song titled “Ah! Sweet Mystery of Life” that opens with these lyrics:

MariettaVH2“Ah! Sweet mystery of life
At last I’ve found thee
Ah! I know at last the secret of it all;
All the longing, seeking, striving, waiting, yearning
The burning hopes, the joy and idle tears that fall!”

You can find the 1935 version of the song on YouTube here. The “Sweet Mystery” it describes is the feeling of being passionately in love.

In the 1974 comedy horror classic Young Frankenstein Mel Brooks used the song as a bit of a “running gag” and the “Sweet Mystery” sometimes alluded to the secret that Dr. Frankenstein had discovered which enabled the “reanimation” of the monster, while other times Mel Brooks use the song to allude love and or sex.

Four years prior to the theatrical release of Young Frankenstein there was a young mathematician contemplating his own “sweet mystery” that lead to mysterious “creatures” not of once dead flesh and bone but rather were mathematical in nature and they seemed to have minds of their own!

In 1970 John Horton Conway or more frequently referred to as just “Conway”, was inspired by mathematician John von Neumann (are most mathematician’s named John? 😛 ) to explore the concepts of universal self-replication, evolution and simulations when he came across the Von Neumann Universal Constructor.

The Universal Constructor is a machine constructed out of 2D cellular automata that is capable of reproducing itself and also which exhibits evolutionary growth and variability in its operation.

When Von Neumann created the Universal Constructor he designed it using 29 cell states and an orthogonal neighborhood restricted to the four cardinal points.

The Von Neumann Neighborhood

 

The technical brilliance of the Universal Constructor cannot be overstated! However, Conway wasn’t satisfied with the engineered complexity that was required for the Universal Constructor to work and became convinced that fewer and simpler rules could be used that would allow complexity to emerge as a byproduct of the system.

Conway believed:

  1. There should be no explosive growth.
  2. There should exist small initial patterns with chaotic, unpredictable outcomes.
  3. There should be potential for Von Neumann universal constructors.
  4. The rules should be as simple as possible, whilst adhering to the above constraints.

Conway experimented with various ways to simplify Von Neumann’s 29 state rule set and eventually became convinced that you could use only two states, zero and one and these are colloquially refereed to as “dead” and “alive” states.

A Mad Scientist Experiments

It was 1970 and the personal computer revolution wouldn’t even start for the better part of a decade (The Apple I was released in 1976) and what most people thought of as a computer at the time was called a “Mainframe” and a single Mainframe could easily take up an entire large room!

Like Dr. Victor Von Frankenstein who had to invent new tools and methodologies in order to bring his creation to life, Conway had to get creative to experiment with his rules!

So, Conway started his experiments using only paper and pencils but quickly graduated to using Go boards laid out on coffee tables and floors to create the 2D grid or plane on which Conway’s Game of Life is “played”.

One day, after making a few changes to his rule set he sat down with some friends and colleagues to run a new experiment and not long after they had started came an excited exclamation from across the grid “It’s walking!”…  almost seeming to mirror the “It’s Alive” scene in the 1931 adaptation of Frankenstein:

While not abiogenesis  (the origin of life or “life from lifelessness”) as Dr. Frankenstein had accomplished, certainly it was none the less a thrilling moment for everyone present!

Everyone crowded around the corner of the Go board to observe the discovery.

What they had witnessed was the the first “creature” brought to life in a game that would come to be called “Conway’s Game of Life“.

The creature they observed “walking” is now called a “Glider” and it looks like this:

A Glider

Game of life animated glider

I hope you enjoyed this post! If you liked this topic please come back tomorrow and we will dig into Conway’s rules and look into how we might use Conway’s Game of Life in our Ancestor Simulation, thank you!

Please Fund, Subscribe, Like, Share & Comment:

I got two new sponsors over the weekend, Gabriel Kunkel, Shanon Garcia. Thank you for your support! It means everything to me that you guys appreciate my work!

Of course this post was made possible by the generous contributions of all of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

256 PreTrained Elementary Cellular Automata Neural Networks

Welcome back and happy Friday everyone!

I regularly release new content (including advanced code) that usually goes way beyond any simple proof of concept you can find elsewhere!

If you review my Topics & Posts page you will almost certainly find a project to pique your interest!

Of the topics I have written about already, overwhelmingly the posts on Neural Networks are the most popular! I believe this is the case because unlike other technical writers, I don’t stop at merely describing how things work, my job would be so much easier if I did that! No, I go above and beyond for you guys and release ACTUAL code and REAL neural networks for you guys to learn and use!

I even went so far as to submit them to the official FANN for PHP GitHub Repo so now when you obtain your copy of FANN, the examples are already available to you, for free!

I’m not just talking about all the basic logic gates either! You can get a free copy of a Pathfinding neural network as well as an OCR (Optical Character Recognition) neural network as well! These are REAL examples for you to experiment with!

You can find all my Neural Networking tutorials over on my Topics & Posts page.

Well, today I want to talk to you about 256 Neural Networks that I trained for you to perform all the 1D Wolfram Elementary Cellular Automata rules.

Being that I have released so much free neural network code and tutorials publicly and considering their amazing popularity, I feel like I need to put these behind my Patreon “pay wall”.

Frankly, I much prefer publishing my content for everyone to enjoy but I’m not kidding when I say that It takes a lot of work to make these projects, so… if you like my work then consider becoming my Supporter over on Patreon for as little as $1 getting access to these Neural Networks!

Seriously, at $1 a month… That’s less than 4 cents a day! Your daily coffee costs more than that!

By supporting me you will make it possible for me to focus solely on posting bigger and better projects!

What do you get exactly?

Well, I am publishing everything listed below to the $1 supporter level so for just $12 a year you are going to get the following:

  • 256 training sets (.data files) to review, modify and train neural networks to perform all 256 (0 – 255) rules conveniently organized into separate files and named after the rule that it trains the network on.
  • 256 PRE-TRAINED neural networks (.net files) capable of performing the computation as outlined in my Elementary Cellular Automata series but except via neural networks.
  • 5 separate programs (.php files):
    1. generate_rule_set.php: This program will recreate the entire set of 256 .data training files.
    2. train_all.php:  This program will train all 256 FANN .net neural networks.
    3. train_specific.php: This program lets you enter the rule numbers into an array prior to running to train the specific rules as FANN .net file neural networks.
    4. ann_output_all_rule_set_images.php: This program will render all the rules as images using the neural networks.
    5. ann_output_specific_rule_images.php This program will render the specified rules as images using the specified neural networks.
  • The entire set of rules (0 – 255) per-rendered as 1025x1025px PNG images.

 

The 256 Neural Networks waiting for you over on Patreon right now!

You will need PHP and FANN installed for this to work. I have a public tutorial on how to setup your test environment for free here:

 

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

Ancestor Simulations Elementary Cellular Automata Part 4

Happy Thursday evening everyone! I know it’s late, sorry :/

Today we’re going to conclude our examination of 1 dimensional Wolfram Elementary Cellular Automata.

For your convenience, here are the other posts in this series in case you missed them as they will fill in all the details I don’t cover here and I encourage you to go read them as well:

 

When we left off in my last post we were considering how we might use the Cellular Automata function I provided to create images of the rules, which is just one of the many awesome uses for Wolfram Cellular Automata!

Rather than tease you with with the prospect of getting the code and making you wait till tomorrow like I did on Monday and Tuesday I think I’m feeling generous and just going to let you have the code now, give it a quick read through and we’ll discuss it below. 😛

 

// Image Size Setting //////////////////////
$full_size = pow(2, 10) + 1; // 1025
////////////////////////////////////////////

////////////////////////////////////////////
// Run through all rules
for($i = 0; $i <= 255; $i++){
    $cellular_automata = imagecreatetruecolor($full_size, $full_size); /* New Image */
    $black = imagecolorallocate($cellular_automata, 0, 0, 0);  /* Allocate Black */
    $white = imagecolorallocate($cellular_automata, 255, 255, 255); /* Allocate White */
 
    /* Create seed of zeros with a one in the exact center */
    $seed = str_repeat('0', round($full_size / 2) - 1) . '1' . str_repeat('0', round($full_size / 2) - 1);
    
    $current_seed = str_split($seed); /* Split the $seed string into an array */
    
    /* For each value in the current_seed array */
    foreach($current_seed as $col=>$value){
        if($value == 1){ 
            imagesetpixel($cellular_automata, $col, 0, $white); /* Set Pixel */
        }
    }

    /* For each row and column pixel in the image */
    $rule = $i;
    for($row = 1; $row <= $full_size; $row++){
        $seed = ElementaryCellularAutomata($rule, $seed); /* Compute the new $seed */
        $current_seed = str_split($seed); /* Split the $seed string into an array */
        
        /* For each value in the current_seed array */
        foreach($current_seed as $col=>$value){
            if($value == 1){ 
                imagesetpixel($cellular_automata, $col, $row, $white); /* Set Pixel */
            }
        }
    }
    imagepng($cellular_automata, "$rule.png"); /* Output Image */
    imagedestroy($cellular_automata);/* Free memory */
}
echo PHP_EOL . "Program Complete!" . PHP_EOL;
///////////////////////////////////////////////////////

How does it work?

Excellent question!

First I declare a variable called $full_size and  assign it the value of:

$full_size = pow(2, 10) + 1; // 1025

This is because  (1024 % 2)  == 0 and I need it to equal 1 which (1025 % 2) gives me,  this basically just allows a center column of cells, so I need a remainder of 1 when dividing the image into 2 vertical halves.

After that I setup a for loop that will step from 0 to (and include) 255 (all the binary rules 00000000 – 11111111).

I then create an image resource in memory and assign it the name of $cellular_automata followed by allocating the colors $white (255, 255, 255)  and $black. (0, 0, 0) for the image resource to paint with.

I follow that up with this little gem:

$seed = str_repeat('0', round($full_size / 2) - 1) . '1' . str_repeat('0', round($full_size / 2) - 1);

Basically this has to do with dividing the image in half as I mentioned above. What this line says is create a string of zero’s on the left then add a one in the middle cell followed by zeros to the right for the rest of the image.

Once I have my “initial $seed” value I use str_split() to convert the string into an array so that we can step through the array as values and apply color the top row of pixels in the image using the initial seed values. 0 = black & 1 = white.

After that I take the seed and pass it to the ElementaryCellularAutomata($rule, $seed) function I released in my previous post on the subject( Ancestor Simulations Elementary Cellular Automata Part 3 ) so that a new seed can be computed, and the explanation of how that occurs is mainly covered in my last post.

Now I just repeat the step of computing a new $seed for each row of the image based on the previously computed seed and then paint the row’s pixels.

Once the image is ready I save it and destroy the image resource so I can create a new one for the next image, Easy Peasy Lemon Squeezy! 😛

Now I imagine that you would like to get your hands on the code for this project am I right? If you head over to this link on my GitHub you will find EVERYTHING you need to get you started!

ca.php – Contains the code I showcased in these posts and will create images of all the Wolfram rules for you to enjoy at 1025 x 1025 px.

random.php – Will create a random seed and select a random rule and then proceed to generate an image 1025 x 1025 px.

It’s interesting to note that several of the rules generate the Sierpinski Triangle fractal using a vary different method than I used in my post A Chaos Game so if you are looking for a great read and want to learn more about fractals consider giving that post a read. 😉

I hope you have as much fun exploring the Wolfram Code as I had preparing this project for you! 🙂

Are these the “Ancestors” we seek?

I hate to say it, but hardly!

As unquestionably cool as Dr. Stephen Wolframs’s Elementary Cellular Automata are, and despite the fact that they DO exhibit a form emergent behavior, they are incapable (seemingly) of exhibiting anything close to real consciousness and so cannot be the basis of our Ancestors.

I can imagine several ways to make use of CA’s that could lead to more intelligent behaviors but… I am not prepared to explore them at this time.

What Wolfram has suggested is that he believes that Elementary Cellular Automata may be the best way to explain how our universe seemingly self organizes (from a computational perspective) through chaotic means and creates the complex behavior of everything we see around us. He has suggested that perhaps with enough time, processing power and research, a form of Elementary Cellular Automata may be the key to unifying “Classical Physics” the so called “Standard Model” with “Quantum Mechanics” both of which offer the ability to correctly make predictions about the universe while remaining incomplete.

If indeed we are living in a simulation then perhaps Rule 30 which IS found nature, in conjunction with Rule 110 represent the greatest hidden fingerprint (signature?) left behind by the programmer who created our universe…

Then again, it may just be a weird little quirk of how patterns of strings of binary data behave when you apply intelligent binary rule sets to manipulate them.

Is it a mysterious code hidden in the numbers that reveals that we are living in the Matrix or simply a glimpse at one of the many forms of complex beauty that math reveals? You are free to draw your own conclusions.

Is that all there is? Can’t we do anything else with the Elementary Cellular Automata?

Well… there is one thing… maybe a neural network, or more like 256 Neural Networks! 😉

Come back tomorrow and I’ll tell you all about it.

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

Ancestor Simulations Elementary Cellular Automata Part 3

Welcome back everyone for my third post all about Elementary Cellular Automata!

For reference here are the other posts in this series in case you missed them as they explain quite a bit about how this code works!

Ancestor Simulations Elementary Cellular Automata

Ancestor Simulations Elementary Cellular Automata Part 2

So, I know I have been making you wait for the code but rest assured that you are going to get code today I promise! 😉

But, before we get to the code I would like to take this opportunity to say thanks for reading my posts!

I truly appreciate all of my readers so I put a lot of time into selecting great projects for you to read about and tons of effort into implementing interesting code, creating or editing “featured” images as well as other graphical content that I use in these posts as well as simply curating an active public code base over on my GitHub profile, and frankly it’s a lot of work so if you would like to help support my work or just say thanks for the edutainment , please consider supporting me over on Patreon for as low as $1 dollar a month $12 a year.

If you want to support me but are not too keen on subscriptions, you can use my Bitcoin Tip Jar address to anonymously send me any amount of satoshi (0.00000001 bitcoin) that you feel comfortable with.

Interesting tidbit, 0.00000239 BTC (or 239 satoshi) is currently worth about 1 US penny.

Donations will go toward making improvements in my technology stack so that I can make even more cool stuff for you guys!

So without further ado, lets get to the code, shall we? This is the function you need in order to compute the Wolfram code using a 1D cellular automaton!

 

ElementaryCellularAutomata()

This function has two parameters, $rule and $seed.

$rule – Accepts integer values between 0 & 255.

$seed – Accepts string values containing only 1 or 0.

There is no error checking to enforce these conditions as I wanted this to be a clean example and I assume that you will take heed of these requirements in your own implementations.

function ElementaryCellularAutomata($rule, $seed){
    $seed = str_split($seed); /* Split $seed string into array */
    $seed_length = count($seed); /* How long is the seed array */
    $new_seed = "";
    
    if(strlen($rule) < 8){ /* If rule is dec */
            $rule = sprintf('%08b', $rule); /* Convert rule to bin */
    }$rule = str_split($rule); /* Split the binary value of $rule as a string into an array */

    /* For each value in the seed */
    for($i = 0; $i < $seed_length; $i++){
        $left = $center = $right = null; /* set all positions to null */

        /* $left */
        /* If this is the first element in the array */
        /* Wrap around and set $left to last element in array */
        /* Otherwise $left = the element to the left of $center */
        if($i == 0) {$left = $seed[$seed_length - 1];}
        else{$left = $seed[$i - 1];}
        
        /* $center */
        /* Set $center to the current position */
        $center = $seed[$i];
        
        /* $right */
        /* If this is the last element in the array */
        /* Wrap around and set $right to first element in array */
        /* Otherwise $right = the element to the right of $center */
        if($i + 1 >= $seed_length) {$right = $seed[0];}
        else{$right = $seed[$i + 1];}
        
        // Apply Rule ///////////////////////////////////////////////////////////
        if($left == 1 && $center == 1 && $right == 1){$new_seed .= $rule[0];}
        elseif($left == 1 && $center == 1 && $right == 0){$new_seed .= $rule[1];}
        elseif($left == 1 && $center == 0 && $right == 1){$new_seed .= $rule[2];}
        elseif($left == 1 && $center == 0 && $right == 0){$new_seed .= $rule[3];}
        elseif($left == 0 && $center == 1 && $right == 1){$new_seed .= $rule[4];}
        elseif($left == 0 && $center == 1 && $right == 0){$new_seed .= $rule[5];}
        elseif($left == 0 && $center == 0 && $right == 1){$new_seed .= $rule[6];}
        elseif($left == 0 && $center == 0 && $right == 0){$new_seed .= $rule[7];}
        /////////////////////////////////////////////////////////////////////////
    }

    /* Return the new seed which was created by applying the rule set */
    return $new_seed; 
}

The function above will use the rule to compute the new seed and return it as a string. This allows the output of this function to be fed back into it during a loop however you might wish to implement a recursive function that will call itself until a condition has been met and that would operate just as well though depending on implementation some coders may find it harder to read and debug than a purely functional approach which I demonstrate here.

Now you might be wondering how you would use this to generate images of all the rules using  the “initial seed”  we used yesterday, for reference code here is the seed again:

Initial Seed

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

 

Tell you what… come back tomorrow and I will give you that code too, sound like a plan? Great! I will see you tomorrow! 😉

 

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

Ancestor Simulations Elementary Cellular Automata Part 2

Today we’re going to continue our examination of 1 dimensional Wolfram Elementary Cellular Automata by examining Rule 1 (00000001) and manually step through a few cycles or generations.

We will create a 1D grid with a length of 24 + 1 (17) so that we have a center value equal distance from both ends, we will assign all cells the off state except for the center cell which we will turn on.

Like this

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

 

We will call this our “initial seed” value. Wolfram CA rules are usually demonstrated with some version of this seed, though it is important to remember that the length can be as long as you want, both shorter or longer and the cell states can be set however you need them to begin.

Before proceeding review the neighborhood because it is now important to understand this:

The Wolfram Neighborhood

 

Lets look at Rule 1(00000001) and get started computing new cell states.

Rule 1

R1 R2 R3 R4 R5 R6 R7 R8
111 110 101 100 011 010 001 000
0 0 0 0 0 0 0 1

 

If you imagine the rule 1 table above as an “array” then the first row represents the “keys” and below I will refer to R8 or R6 or other keys by these names as shortcuts that are referring to the  neighborhood patterns on the second row, example R3 refers to a neighborhood pattern of a left cell of 1, a center value of 0 and a right value of 1… got it?

Just to be sure that you are able to follow along  consult the center values in the initial seed, its a 1 and to its left is a 0 and to it’s right is also a 0, therefore on the center position in the “new seed” we will apply R6 which means the new center value would be a 0 Still with me? Good! 😉

Only one more thing to talk about before we get to computing and that is how to handle the edge cells which are the first cell $seed[0] and the last cell of the seed $seed[17] . Because there is no $seed[-1] and $seed[18] there are two options for handling this scenario. A lot of people simply say those positions are 0’s (or 1’s depending on the case) but I like the second option which is to wrap around and grab the value at the other end of the seed. This methodology allows the patterns to wrap rather than stop when an edge is encountered however depending on your use needs you may want to wrap or use a hard value, neither way is technically correct.

So now that we are on the same page lets start figuring out what rules to use to compute the new seed.

 

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
R8 R8 R8 R8 R8 R8 R8 R7 R6 R4 R8 R8 R8 R8 R8 R8 R8

 

 

We can replace the rules with their values and then figure out what rules to use next:

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1
R1 R1 R1 R1 R1 R1 R2 R4 R6 R7 R5 R1 R1 R1 R1 R1 R1

 

And if we replace the rules with the rule values you can see we are back where we started:

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

 

This pattern will repeat over and over again until we decide to stop, here’s 14 more generations to demonstrate:

The Finished Rule 1 Image

 

This isn’t always the case however, lets look at a different rule that will give us many more interesting neighborhood patterns, Rule 26 (00011010)!

Rule 26

R1 R2 R3 R4 R5 R6 R7 R8
111 110 101 100 011 010 001 000
0 0 0 1 1 0 1 0

 

Notice that we reset the seed to what we started with so that we can illustrate the differences in the rules. Nothing prevents us from using the output of Rule 1 (or any rule) as the input seed for another rule, in fact you can get some very interesting results by doing that!

Notice also that because the seed is the same as before, if we fill in the cell rules they are same neighborhood patterns that we used before, what’s different this time is the values the patterns are mapped to.

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
R8 R8 R8 R8 R8 R8 R8 R7 R6 R4 R8 R8 R8 R8 R8 R8 R8

 

If we replace the neighborhood pattern with the rule value we get a completely different result than we got with Rule 1 before! While we’re at it lets also fill out the next set of rules too:

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
R8 R8 R8 R8 R8 R8 R7 R6 R3 R6 R4 R8 R8 R8 R8 R8 R8

 

Once again we replace the cell rule with the rule value and we have a completely different grid, we also figure out the next row’s rule patterns:

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
R8 R8 R8 R8 R8 R7 R6 R4 R8 R7 R6 R4 R8 R8 R8 R8 R8

 

OK lets now convert those rules to values to see what we get:

 

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0

 

Here’s 12 more generations, there is a pattern but its not quite like the pattern that Rule 1 gave us:

The Finished Rule 26 Image

 

I was going to start looking at code with you today but it’s getting late and this post is already fairly long so come back tomorrow and we will FINALLY dig into the code 😛  have a great evening everyone!

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

Ancestor Simulations Elementary Cellular Automata

Today we open another chapter in Ancestor Simulations, namely “LIFE” or rather our exploration of simulating our “Ancestors” and therefore it behooves us to start with Cellular Automata.

Originally conceived of in the 1940’s by Stanislaw Ulam and John von Neumann (who should be among your personal heroes because of their scientific brilliance by the way 😛 ) consist of a discreet grid of “cells” which can be represented in one or more dimensions.

5x5-grid

Example of a 5×5 two dimensional grid

The cells can have properties assigned to them and cells can exhibit varying types of behaviors called “states” which the cells are said to be “in”.

Various “Rules” are applied to determine when cells can change states and if so, what states can they change to, the most common of which (and simplest) are “Alive” and “Dead“.

Often the grid is encoded as digital data using 0 to mean the cell is switched “Off” or “Dead” and 1 to mean the cell is switched “On” or “Alive”.

Frequently it is more convenient for people to view the grid encoded as pixels rather than ones and zeros and the simplest way to go about visualizing the data is to assign the on and off states separate color values, such as white and black, although black and red or white and blue are also fairly popular.

Often the rules used to determine the cell states involve checking to see what states the “neighborhood” (the cells around the current cell ) are in.

The Von Neumann Neighborhood

If the blue cell is the current cell being examined then the “Von Neumann Neighborhood” examines the four cells surrounding it in the four  cardinal directions directly around the current cell (shown in red) and the “extended neighborhood” would then include the cells in pink.

 

The Moore Neighborhood

If the blue cell is the current cell being examined then the “Moore Neighborhood” examines the eight cells surrounding it directly around the current cell (shown in red) and the “extended neighborhood” would then include the ring of cells around the red cells.

 

The Wolfram Code

We’ll cover 2D Cellular Automata  in future posts because today we are going to look at one of the simplest cases of a CA, namely a 1 dimensional Wolfram CA called the Wolfram Code.

Originally discovered by Stephen  Wolfram in the early 1980’s when he was studying computer simulations and the nature of the universe… kinda like we are doing with our Ancestor Simulation! 😉

I called the Wolfram code “simple” only because it “has fewer moving parts” so to speak, it is still capable of complex behavior, including modeling Chaos Theory so as such the Wolfram code embodies emergent behavior very elegantly!

Emergence is the idea that simple rules can be implemented and followed and their results can lead to highly complex behaviors and structures that somehow “emerge” without having to be specifically implemented or designed into the system.

Unlike the 2D grid “neighborhoods” (Von Neumann & Moore) outlined above the 1D Wolfram CA is just a single row of boxes. Technically It need not be 1D but the Wolfram CA is usually introduced and discussed as a 1D CA and as such, when we think of the resulting output we think of it more as a series of time steps away from the original pattern. That is to say that while we display the output of the Wolfram CA in 2D image just like other CA’s, the 2D grids are computed row by row whereas any given image of a 2D CA is computed entirely and then rendered.

This means that that the image generated by the Wolfram CA is the sum of all the states or calculations that occurred and each row, being the resulting sum of states or calculations occurring at n-generation and is n rows (steps) away from the originating pattern.

The Wolfram CA allows for an arbitrary length (meaning the length can be as long as you want) with a minimum size of 3 because the Wolfram CA has a neighborhood that consists of 3 cells. the cells to the direct left and the direct right (shown in red) of the cell being considered (shown in blue).

The Wolfram Neighborhood

 

An interesting result of this is that with a 3 cell neighborhood and 2 states (on & off) there are 23 (8) possible state combinations for a given neighborhood and 28 (256) possible rules.


$number_of_rules = pow(2, pow(2,3)); // 256

 

Because there are only 2 possible cell states and only 8 values are needed to encode each possible rule, we can generate all of the Wolfram CA rules using this code:

 

for($i = 0; $i < $number_of_rules; $i++ ){
    printf("$i: " . '%08b' . PHP_EOL, $i);
}
/*
0: 00000000
1: 00000001
2: 00000010
3: 00000011
4: 00000100
5: 00000101
6: 00000110
7: 00000111
8: 00001000
9: 00001001
10: 00001010
...
250: 11111010
251: 11111011
252: 11111100
253: 11111101
254: 11111110
255: 11111111
*/

 

The cell states are checked against these rule patterns and in this order:

R1 R2 R3 R4 R5 R6 R7 R8
111 110 101 100 011 010 001 000

Therefore if the neighborhood pattern was 111, the first value of the rule is applied. If it is 110 the second it used and so forth.

Some of the rules like Rule 30 (00011110) in combination with Rules 86 (01010110) , 135 (10000111) and 149 (10010101) have been shown to encode chaotic behavior.

Other rules are either suspected of being “Turing Complete” (without a “formal proof” having been created or discovered yet) or ARE known to be Turing complete such as Rule 110 (note this refers to the octet binary representation for the number 110 (01101110)  not the 3 cell neighborhood pattern 110 (R2) as shown in the table above).

For example, here is the results of Rule 73 (01001001) when initialized with a single “living” cell in the center of the image on row: 0 col: 513:

Click for full Size

And with that, have a great evening everyone!

Come back tomorrow for the next part of this post which will include my implementation of a 1D Wolfram Automata which I used to generate the image above.

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon & anonymous contributions to my Bitcoin Tip Jar.

 

 

I will see you all in my next post!

Much Love,

~Joy

 

 

Ancestor Simulations Wednesday Winnowing

The term “winnowing” literally means to remove and separate grain (something desirable and good) from it’s husk or chaff (something undesirable) so that you are left with a filtered grain (endosperm, bran and germ) ready to be “milled” or otherwise prepared for human consumption or use.

That’s what this post is about, winnowing the undesirable parts of our Solar System Generator so that we are left with something better than when we started.

Predominately this means “facing the music”  and accepting that browsers are impatient creatures and simply don’t like sitting around and waiting for 19+ hours while the server “chunks” out orbital dynamics calculations and then turns the resulting data set into a series of images that when strung together create an animation of what was computed to have occurred during the simulation.

Speaking of “facing the music”, I’m currently listening to the song “Vampires” by the New Retro Wave (Synthwave) group “The Midnight” (not a sponsor) I just love the amazing saxophone in this song and I’ve got it on repeat! 😛 I highly recommend checking them out over on YouTube though I have embedded the video below.

 

So now lets talk about what I “winnowed” from the code.

First, I removed the $_POST statements in favor of variables that can be adjusted as needed since we are removing everything browser-centric (mainly HTML) since I am now almost exclusively interacting with the “Solar Systems Generator” from the command line.

I also made the image output optional which it the main causal  source of most of the speed improvement since writing out the orbital data to the images is what takes the most time. My tests show that by disabling the image output enables us to run about 50 thousand simulations in the course of about 5 minutes on my machine.

That’s with 1 Million objects in the Solar Nebula and equates to ~10 thousand years at a “$delta_time” of 0.1, which means that in order to simulate the entire 4.6 Billion year life cycle of our solar system we would need ~58.298611 days! Interesting little tidbit, If we started the simulation today and actually ran that 4.6 billion year sim it wouldn’t finish until ~7:10 am PST | Friday, November 24, 2017.

If we included the image & animation generation we would have to reduce the number of objects in the Solar Nebula to ~100K and it would then then take ~1538.612874531963 years (slightly over 1.5 millennia) and the simulation wouldn’t finish until 7:29:23 am PDT | Tuesday, May 8, 3556 (a REALLY LONG time from now). 😛

Therefore our speed performance improvements amount to roughly an increase of 963205.4537168415% in our favor! 😎 🤓

I still need to finish the clean merging of the elements when there are collisions but that’s coming and I will highlight that is my next Ancestor Simulations post.

For now here is a small sampling of the elements.txt report generated:


[Planet 2]
Americium 18.181818181818
Fermium 18.181818181818
Nihonium 18.181818181818
Neodymium 18.181818181818
Astatine 18.181818181818
Hafnium 18.181818181818
Promethium 18.181818181818
Tungsten 18.181818181818
Fluorine 18.181818181818
Strontium 18.181818181818
Polonium 18.181818181818
Oganesson 18.181818181818
Cerium 18.181818181818
Oxygen 18.181818181818
[Planet 3]
Sulfur 150
Titanium 150
Thallium 150
Americium 150
Gadolinium 150
Fluorine 150
Oganesson 150
Bromine 150
Mercury 150
Krypton 150
Bromine 150
Oganesson 150
Krypton 150
Beryllium 150
Cerium 150
[Planet 4]
Oganesson 15.66265060241
Dubnium 15.66265060241
Bromine 15.66265060241
Osmium 15.66265060241
Krypton 15.66265060241
Fermium 15.66265060241
Scandium 15.66265060241
Mendelevium 15.66265060241
Lanthanum 15.66265060241
Manganese 15.66265060241
Osmium 15.66265060241
Neodymium 15.66265060241
Seaborgium 15.66265060241
[Planet 5]
Nihonium 46.666666666667
Oganesson 46.666666666667
Dubnium 46.666666666667
Europium 46.666666666667
Cerium 46.666666666667
Mendelevium 46.666666666667
Osmium 46.666666666667
Cerium 46.666666666667
Protactinium 46.666666666667
Tungsten 46.666666666667
Seaborgium 46.666666666667
Cadmium 46.666666666667
Lead 46.666666666667
Hassium 46.666666666667
...

You will notice that all the elements have the same values and are sometimes duplicated rather than merged into a single quantity which will just not do! I mean, sure we could say they are separate quantities and are part of the whole and treat them accordingly. This would give us the option if we wanted to have impacts knock off sections of planets like in the Giant Impact Hypothesis  and do less math when we do that but I really don’t see us running that high of fidelity simulation so I will work to merge them.

And that’s it for today!

You can find the updated code for this project over on my GitHub profile.

Please Like, Comment below & Share this post with your friends and followers on social media.

Please Fund & Subscribe:

This post was made possible by the generous contributions of my sponsors on Patreon.


 
 

If you would prefer to support me anonymously using bitcoins please use the address below or scan the QR code using your bitcoin wallet app, thank you!

Please Send Here: 1GgUHjVAYqFLBWufFTpkoDEti16DSSNNG7

As always, have a great week and I will see you all in my next post!

Much Love,

~Joy

Create a free website or blog at WordPress.com.

Up ↑