YTread Logo
YTread Logo

Coding Challenge 179: Elementary Cellular Automata

Apr 22, 2024
autom autom autom autom autom okay everyone, I'm here for a

coding

challenge

. I don't know what number

coding

challenge

this is for 2023, but I'm certainly not above a hand here, I haven't done more than five, it's been a slow process. year I was working on the nature of the codebook, the original version was published in 2012. I've been working on a new version of the book, you know, basically, since the first version came out, but I finally finished writing it. In fact, I can reserve it. There will be a physical version of the book. Everything is available online.
coding challenge 179 elementary cellular automata
I'm still working on the website design. I'll show you some examples here, but maybe even if you go to the URL. Natureof Code.com right now you might see something new, a newer design, maybe let me know in the comments when you're watching this. I hope that in 2024 I can get back to doing more regular coding challenges and I'm going to start today trying to code without much practice of course, and I haven't seen this example in over a year, so we'll see how it goes on Wolfram Elementary mobile, Oh dear, I have to say this word. now

automata

automata

I think that's right automata now I have completed a coding challenge About

cellular

automata before coding Challenge number 85 The game of life about the game of life was famously written in the Scientific American article 19 19 70 of Martin Gardner describing it as recreational Mathematics is advancing rapidly more than 30 years later and Steph Wolfram published the 1280-page Tome, a new type of scientific book by Wolfram that discusses how

cellular

atoms are not simply clever tricks, but are relevant for the study of biology, chemistry, physics and all branches of science. first define what is an automaton seller adada adada adada the song The central fundamental element of a CA that will make my life much easier if I just say ca from now on it is a cell this is the cell in cellular the cell has a state typically some a finite number of options and the most basic basic version of that would be just a zero or a one, the cell exists within a neighborhood, in the case of a one-dimensional CA, that cell will simply have a neighbor to its right and a neighbor to is to the left in the case of a two-dimensional CA, as in the game of life, that particular cell could have neighbors surrounding it in all directions.
coding challenge 179 elementary cellular automata

More Interesting Facts About,

coding challenge 179 elementary cellular automata...

You could imagine how you could get an idea that a cube is a particular cell within a three-dimensional cube. dimensional CA, but the simplest version of this, Cellar's elemental automata, are in one dimension with a cell that has neighbors to its left and to its right, so these are the elements of the cell with a state and the fact What you have a neighborhood here is a CA is a grid of cells in one dimension, each with a state. Now it might be interesting to think of this as infinitely long as it would be in retical space, but the fact is that I'm going to try to represent this visually in a p5.js sketch and then I have a limited amount of real space on the board.
coding challenge 179 elementary cellular automata
I'm going to do it right now. I'm choosing my AC has 1 2 3 four 5 six seven uh cells what's missing what I haven't drawn here yet On the board, a big missing piece of what really makes an AC exciting is the rules, what happens with cells and their states over time, but before we get into that, let's see how I'm going to encode the elements of a CA and represent them. Visually, a single generation, let's represent that as an array now just for cleanliness. Here my canvas measures 400 by 400. What if it was 10 cells wide so that each one was 40 pixels wide?
coding challenge 179 elementary cellular automata
Let's create a variable for the width of each cell and I. We'll code that now to 40 and within the drawing let's visually represent those cells. What if a one will be black and a zero will be white? Well, that's 10 cells wide if I said fill with cell index multiplied by 255, so now when there's a one. the cell is white when there is a zero the cell is black You could reverse that perfectly, so the next question is how do we determine the states of the cells in generation one. Remember that a cell neighborhood is three cells together, how many ways are there to configure? three cells if each cell can only have a state of zero or one if I did this correctly I should have eight possibilities this is like a three bit number 2 to the power 3 is eight wolfram rules for an

elementary

CA specify a result a new state for every possible configuration of a neighborhood of three cells, this is what is known as a rule set, let's take these first three cells, their configuration is one0 0.
I can see that up here one0 has a result of one. This particular neighborhood produces this particular new state. value okay let's look at 0 01 one and I don't know why I'm drawing some like this and some On like this it's confusing 0 one 0 one 0 1 0 one 0o so for any given cell look at its state along with its neighbor left and its right neighbor state produce the result defined by the rule set, so a couple of questions arise. I skipped these cells. What do I do with the edges? One approach is this cell has a state of one.
You might consider your right neighbor to have a state of zero and your left neighbor to be the one on the other end of the envelope, so to speak. You could also ignore those cells and just copy their values ​​or perhaps define a different set of rules. Let's not worry about that for now and just copy their values. The other thing you might be wondering is why you chose these numbers. 0 1 0 0 1 1 01 Why are these new states based on these configurations? Well, I hold that thought. I just arbitrarily picked those numbers out of thin air, let's see if with some particular defined set of rules we can make the code work to go from generation zero to generation one to generation two to generation three etc. and then I'll come back and talk about why.
You may want to choose certain values ​​over other values, let's create another array. I'm going to call it next cells or next generation, then I'm going to iterate over all the cells and I need the cell's new state to be a function of its current state along with its left and right neighbors, so I made up variable names to the neighborhood, the left cell, the right cell and then I'm just using State for the current cell, the middle one, so to speak. I'm imagining that there is a function called compute state, it takes those three states and then gives me a new state that I put into the next array, so I need to write that fun function and it takes three arguments.
I'll just call them ab and c and then I need to return a particular value based on that setting if I have a is zero B is zero C is zero then it should return a zero and for reasons I'll explain in a minute I think it will do more Does it make sense if I actually start dial 111 first now for all of you who are screaming on whatever device you're watching this on? I know, I know I'm going to do this a different way in a minute. I just want to write this. in the longest, most ridiculous handcrafted way possible to understand the mechanics of what's happening and I should also put something in here to handle the edges, so let's make the loop ignore the edges and hardcode those values, so now I'm creating a new array that I'm copying to the edges so I don't have to worry about them and then starting from index one I'm calculating all the new states based on the ruleset, once that's done the old cells are now the new ones cells are fine, so while this works, I don't get any kind of visual results.
Let's make the cells much smaller. If the cells are only 10 pixels wide, I need to have more. I'll start by having them alone. it's going to be an empty array and I'm going to fill it in and we're going to fill it in randomly this is going to give me a zero or a one okay great now this is a time where you can choose to be very creative how are you going to visualize the generations of the ca system, the classic way of doing it, the way Wolf does it in a new type of science to visualize them is by stacking them and this is what I have drawn here generation zero and then go down a row to show generation one and so on , let's add that to the code, so I need a variable called y, we'll call it, we'll draw all the cells at comma X and every once in a while, after we're done rendering a generation of cells.
I'll just increase the size of the square. Oh, that looks fun, but I'm not going to need to erase the background and there we go, fascinating. This is what I got with this arbitrary set of rules I chose out of thin air. air with the first generation, zero generation if you will, of all the cells having a random state, the typical way wolm would prove 1dca is to have the first generation all with zero and only the middle cell with a state of one, so let's set them all to zero and then let's set the middle state oh, I have an even number of cells there, 40 of them, let's make the canvas 410, that will be fun, okay, look what we have there, I have it now in as long as this lasts.
The video has taken a Wolfram CA programmed element looking at the Wolfram's Math World website, the page about the elemental vendor automaton, you'll see there are 256 rules and if I scroll over here I think I should be able to find the one I just created. 186 which actually looks like the one I just coded. Rule 242 also looks suspiciously similar, as does Rule 250. I'm going to take this number and write it horizontally like this. This is an 8-bit binary number, two to the eighth power. is 256, that's why there are 256 possible rule sets, remember it's a bit confusing because there are zeros and ones everywhere, the state values ​​are zeros and ones, the

elementary

CA could be 100 cells long and th000 cells long infinitely long, but a neighborhood has only three cells, which means there are only eight possible neighborhoods, each possible neighborhood needs a new state value, which means there are eight new state values, there are only 256 ways to set eight new state values, this is one of them, what is the decimal equivalent of this number?
Well, do I have to? Do these calculations now. I can use a calculator. Let me. I'm going to do it. I can do this. 178. It's like that. I got 178. Converting this from binary to decimal. Hey, look at that rule. 178. Let's see what happens if I try it. rule 182 looks pretty good, how do I write 182 as a binary number one? 0 1 1 0 1 1 0 I think that's right 1 0 1 1 0 1 1 Z looks pretty good, let's make it a little bit wider and maybe the cell will be a little bit smaller, we don't need to have the stroke and look at that, Do you recognize that that is the fractal of the Serinsky triangle, this system, which is a system of cells that only have states zeros and ones, there are only small neighborhoods of three zeros and there are only 256 ways to configure a set of rules in some way With this simple system, the Serinsky fractal triangle emerges, it is not that surprising.
I want to examine this further, but first let's make some improvements to the code. You could represent the rule set as This is a string of zeros and ones. I'm going to make a matrix because that will make my life easier. Now all I need to do is take the three values ​​and convert them to the index of the array if this is the array. 0 1 2 3 4 5 6 7 so this is a little complicated here this place zero is the value 111 so this is 0 1 2 3 4 5 6 7 but I will worry about investing and getting later everything I need What's there What to do is simply take three bits and convert them to a decimal number and, in fact, in JavaScript that is quite easy to do.
I actually don't remember how to do this. I can make a string that joins them together and then I could say, "Okay," so this is the neighborhood and then I'm going to say the value is equal. I think you could use parse int parse int the neighborhood in base two is a binary number and then return the rule. Set the value, well I got a different display but that's because once again m inverted so when the states are 0 0 0 I want the last element of the array when the states are one one one I want the first element of the array so you should be able to say uh 7 minus and here we go, let's do this rule sets a global variable and look at some other rules just to make sure everything works.
Let's make rule one. Oh, rule one will be fun. I love it in Wolfram's, a new kind of science. He classifies each rule into one of four possibilities. The first is uniformity where all cells tend over time towards a particular state. Rule 222 is an example of that. You know, I'd like to be able to put the rule in the code as a decimal number. Now how do I convert it? The other. way if I parse the value of the rule oh no can I do it? What if I do it this way as a string rule value for string base two?
Does it work in JavaScript? I'm completely making up code. Am I supposed to do this? ah yes, that worked fine, I need the number, I have the number, convert it to a string. Let's make the ruleset a string and then change this function to return uh parse int. Okay, cool, awesome ruleset 222, uniformity, next category is repetition a. set ofrules by which cells oscillate repeat some pattern over time rule 246 let's try that, here we go, that's what you would expect from such a simple system the next two categories are where things get a little more interesting the Third categorization is random and one of the most famous rules for AC wolf is Rule 30.
Remember there are no random numbers here. This is a fixed discrete deterministic system, but let's put rule 30. What did I do wrong? Oh, because I don't have the leading zeros, okay? so this is a problem. I guess I can add the leading strings, uh, while the leading zeros sorry while the rules are set. the length is less than eight rule sets. This is a terrible way to do it. It is equal to a zero plus rule set. Will someone suggest me a better way to do this in the comments. I'm sure that's fine, but cool, here we go. rule 30 now I'm only seeing a fraction of this, we're going to give myself a lot more room and the fact that I'm holding the side is a little bit of a problem if we add the wrap, let's add the wrapper, so on the left I'm -1 plus let's say the length is equal to the cells. the length is the total number of cells, so if I add the total number of cells and then apply modulo with the length, this will give me a summary, oh but I have to look at all the cells now and I don't need this. more try to find the pattern here this could be a pseudo-random number generator what a question but there is more let's see possibly the most famous rule of all rule 110 look this is random difficult to predict yes there is structure yes this is something like a mix between repetition and randomness and this I would say is the essence of the beauty of the cellular automata system look at this textile cone look at this Wolfram's rule 110 I would love for you to make your own version of this, what can you? what to do if you think of the ac system in terms of color, what if you invent your own system with more than two states?
What if the states are not discrete values ​​but continuous floating point numbers? What other things can you do to visualize the ac system? Could you make it an infinite scroll? Could you make the rules change over time? There are too many possibilities for me to even think about. I hope you'll consider making your own creative version of the Southern Automata Wolf. Send it to the passenger of the coding train. Show up and stay tuned because this is just the beginning. What do you want to see next? From the nature of the codebook? Don't I have a video about that?
Would like to see one in the coding train. See you next time.

If you have any copyright issue, please Contact