Author Topic: Evolve, Order Out of Chaos  (Read 5087 times)

0 Members and 1 Guest are viewing this topic.

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Evolve, Order Out of Chaos
« on: December 25, 2019, 08:23:49 pm »
About 35 years ago I embarked on a journey of the mind quite by accident. I thought my idea would just be called Pixel Wars. Soon however, I discovered I had stumbled upon pseudo evolution and it became Evolve, instead. The idea is simple. Fill the screen with random colored pixel and apply one or more rules to those pixels. Here is the very first rule that at the time I named Pixel Wars.

Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. WHILE repeat
  16.     FOR h = 1 TO 32000
  17.         x = RND * screenw
  18.         y = RND * screenh
  19.         c = POINT(x, y)
  20.         eat = c + 1
  21.         IF eat > 15 THEN eat = 0
  22.         FOR i = x - 1 TO x + 1
  23.             FOR j = y - 1 TO y + 1
  24.                 c = POINT(i, j)
  25.                 IF c = eat THEN
  26.                     PSET (x, y), c
  27.                     EXIT FOR
  28.                 END IF
  29.             NEXT
  30.             IF c = eat THEN EXIT FOR
  31.         NEXT
  32.     NEXT
  33.     _DISPLAY
  34.     ch = INKEY$
  35.     IF ch = CHR$(27) THEN repeat = 0
  36.  

Then magic happened. Patterns developed that resembled living systems. And patterns combined to form what resemble living organisms. The universe has probably thousands if not millions of rules. And yet this was the result from just one rule!

Next I wanted to see what just one entity, as I came to call them, would look like. But I made a mistake and deleted one line of code. What happened is that the entity was unstable and the result was the Big Bang and the "formation of the Universe".

Code: QB64: [Select]
  1. DIM radian AS SINGLE
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. x = screenw / 2
  7. y = screenh / 2
  8.  
  9. FOR radius = 1 TO 40
  10.     c = 0
  11.     FOR radian = 0.0 TO 5.91 STEP 0.368
  12.         CIRCLE (x, y), radius, c, radian, (radian + 0.3925)
  13.         c = c + 1
  14.     NEXT
  15.  
  16. repeat = -1
  17.  
  18. WHILE repeat
  19.     FOR h = 1 TO 32000
  20.         x = RND * screenw
  21.         y = RND * screenh
  22.         c = POINT(x, y)
  23.         eat1 = c + 1
  24.  
  25.         FOR i = x - 1 TO x + 1
  26.             FOR j = y - 1 TO y + 1
  27.                 c = POINT(i, j)
  28.                 IF c = eat1 THEN
  29.                     PSET (x, y), c
  30.                     EXIT FOR
  31.                 END IF
  32.             NEXT
  33.             IF c = eat1 THEN EXIT FOR
  34.         NEXT
  35.     NEXT
  36.     _DISPLAY
  37.     ch = INKEY$
  38.     IF ch = CHR$(27) THEN repeat = 0
  39.  

Still wanting to see what only one correct entity would do ...

Code: QB64: [Select]
  1. DIM radian AS SINGLE
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. x = screenw / 2
  7. y = screenh / 2
  8.  
  9. FOR radius = 1 TO screenw / 2
  10.     c = 0
  11.     FOR radian = 0.0 TO 5.91 STEP 0.3694
  12.         CIRCLE (x, y), radius, c, radian, (radian + 0.3925)
  13.         c = c + 1
  14.         IF c = 16 THEN EXIT FOR
  15.     NEXT
  16.  
  17. repeat = -1
  18.  
  19. WHILE repeat
  20.     FOR h = 1 TO 32000
  21.         x = RND * screenw
  22.         y = RND * screenh
  23.         c = POINT(x, y)
  24.         eat = c + 1
  25.         IF eat > 15 THEN eat = 0
  26.         FOR i = x - 1 TO x + 1
  27.             FOR j = y - 1 TO y + 1
  28.                 c = POINT(i, j)
  29.                 IF c = eat THEN
  30.                     PSET (x, y), c
  31.                     EXIT FOR
  32.                 END IF
  33.             NEXT
  34.             IF c = eat THEN EXIT FOR
  35.         NEXT
  36.     NEXT
  37.     _DISPLAY
  38.     ch = INKEY$
  39.     IF ch = CHR$(27) THEN repeat = 0
  40.  

However, on an 80386 it was taking way to long. The next idea was to do pseudo randomness and select every pixel only once before repeating so I divided the screen into 10 x 10 squares and did each pixel in turn. And yes it is magnitudes faster. It is so fast that _LIMIT 60 was added to be able to see it evolve. And one would never guess in a million years the resulting pattern.

Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh - 1
  7.     FOR x = 0 TO screenw - 1
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. WHILE repeat
  16.  
  17.     _LIMIT 10
  18.  
  19.     FOR boxy = 0 TO 9
  20.         FOR boxx = 0 TO 9
  21.             FOR scrny = 0 TO screenh - 11 STEP 10
  22.                 FOR scrnx = 0 TO screenw - 11 STEP 10
  23.                     x = scrnx + boxx
  24.                     y = scrny + boxy
  25.                     c = POINT(x, y)
  26.                     eat = c + 1
  27.                     IF eat > 15 THEN eat = 0
  28.                     FOR x2 = x - 1 TO x + 1
  29.                         FOR y2 = y - 1 TO y + 1
  30.                             c = POINT(x2, y2)
  31.                             IF c = eat THEN
  32.                                 PSET (x, y), c
  33.                                 EXIT FOR
  34.                             END IF
  35.                         NEXT
  36.                         IF c = eat THEN EXIT FOR
  37.                     NEXT
  38.                 NEXT
  39.             NEXT
  40.             _DISPLAY
  41.         NEXT
  42.     NEXT
  43.     ch = INKEY$
  44.     IF ch = CHR$(27) THEN repeat = 0
  45.  

After scratching my head over that one for a while I decided to select randomly within each 10 x 10 block. However, it was changed to a 4 x 4 block because it did not seem to be doing much. The 4 x 4 block was similar but I did notice a few "living" systems start to form. But they remained chaotic and did not form stable entities. It is a mystery. I finally solved the mystery though. This approach to even random coverage does not produce spiral entities. Therefore there are no spiral arms to "sweep away" the chaos. Therefore the "living systems" do not evolve. I thought at the time that maybe it would then eventually die off. So I let it run for days. I'm not sure what the block size was as it was 35 years ago. Then a spiral entity did form and did sweep all the unorganized life systems away. I suggest not running this one unless you have a spare PC to run it on.

That is it for now as I have to get back to my other project. You may wonder how I can remember this after all these years. It was just that fascinating to me. After this stage I started writing my Evolve Engine wherein it remembered all the different rules and I could pick and chose them. Then due to a thunder storm I lost two hard drives on two PC's and it was gone. But by then I had stopped working on it anyway.

Code: QB64: [Select]
  1.    
  2.  
  3. screenw = _DESKTOPWIDTH
  4. screenh = _DESKTOPHEIGHT
  5. SCREEN _NEWIMAGE(screenw, screenh, 256)
  6.  
  7. FOR y = 0 TO screenh - 1
  8.     FOR x = 0 TO screenw - 1
  9.         clr = INT(RND * 16)
  10.         PSET (x, y), clr
  11.     NEXT
  12.     _DISPLAY
  13.  
  14. repeat = -1
  15.  
  16. WHILE repeat
  17.     FOR scrny = 0 TO screenh - 5 STEP 4
  18.         FOR scrnx = 0 TO screenw - 5 STEP 4
  19.             x = scrnx + INT(RND * 4)
  20.             y = scrny + INT(RND * 4)
  21.             c = POINT(x, y)
  22.             eat = c + 1
  23.             IF eat > 15 THEN eat = 0
  24.             FOR x2 = x - 1 TO x + 1
  25.                 FOR y2 = y - 1 TO y + 1
  26.                     c = POINT(x2, y2)
  27.                     IF c = eat THEN
  28.                         PSET (x, y), c
  29.                         EXIT FOR
  30.                     END IF
  31.                 NEXT
  32.                 IF c = eat THEN EXIT FOR
  33.             NEXT
  34.         NEXT
  35.     NEXT
  36.     _DISPLAY
  37.     ch = INKEY$
  38.     IF ch = CHR$(27) THEN repeat = 0
  39.  
My name is Michael, but you can call me Mike :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #1 on: December 26, 2019, 10:24:34 am »
Cool stuff romichess, thanks!

The one you posted in the Discussion board looking for faster updates brought back a flood of memories from early 90's when playing with cellular automata, chaos, Conway's Life... with QB or early VB. It looked like a version of... ? time for some research :D

Update: here it is
https://en.wikipedia.org/wiki/Cyclic_cellular_automaton

I was remembering devil spirals and it is demon cycles.

Here is my attempt:
Code: QB64: [Select]
  1. _TITLE "Demon Cycle" 'B+ 2019-12-26
  2. DEFINT A-Z
  3. CONST xmax = 800, ymax = 600, modu = 16
  4. SCREEN _NEWIMAGE(800, 600, 12)
  5.  
  6. DIM vs(xmax, ymax), ng(xmax, ymax)
  7.  
  8. FOR y = 0 TO ymax
  9.     FOR x = 0 TO xmax
  10.         ng(x, y) = RND * modu
  11.     NEXT
  12. WHILE _KEYDOWN(27) = 0
  13.     FOR y = 1 TO ymax - 1
  14.         FOR x = 1 TO xmax - 1
  15.             vs(x, y) = ng(x, y)
  16.             PSET (x, y), vs(x, y)
  17.             FOR yy = y - 1 TO y + 1
  18.                 FOR xx = x - 1 TO x + 1
  19.                     IF vs(xx, yy) = (vs(x, y) + 1) MOD modu THEN ng(x, y) = (vs(x, y) + 1) MOD modu: GOTO skip
  20.                 NEXT
  21.             NEXT
  22.             skip:
  23.         NEXT
  24.     NEXT
  25.     _DISPLAY
  26.  
  27.  
Demon Cycles.PNG
* Demon Cycles.PNG (Filesize: 223.25 KB, Dimensions: 801x629, Views: 236)
« Last Edit: December 26, 2019, 12:28:11 pm by bplus »

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #2 on: December 26, 2019, 01:08:58 pm »
Mike, I tried your first program.  Quite amazing and lovely.  After about 8 minutes on my Core i5 machine I got a structured pattern of spirals (somewhat dependent upon screenw & screenh), with the spiral patterns actually spiralling.  But I note that you do not have a RANDOMIZE(TIMER) anywhere in the code.  Elsewhere on this forum, the experts have discussed how random is the function RND.  So I inserted RANDOMIZE(TIMER) to happen periodically in the loop.  I found (twice) that without RANDOMIZE(TIMER) I got spirals, but with RANDOMIZE(TIMER) I did not get spirals (although they may have taken rather longer to form).
With Randomize(timer).png
* With Randomize(timer).png (Filesize: 944.48 KB, Dimensions: 1390x836, Views: 245)
Without Randomize(timer).png
* Without Randomize(timer).png (Filesize: 676.94 KB, Dimensions: 1394x834, Views: 247)
« Last Edit: December 27, 2019, 04:53:18 am by Qwerkey »

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #3 on: December 26, 2019, 04:07:19 pm »
Ah, so the spirals are a mathematical artifact of the continuous ( cycling ) of a random number generator function. And the reason there were no spirals in the other examples is because that randomization was totally absent or broken up. That explains a lot. Thanks.
My name is Michael, but you can call me Mike :)

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #4 on: December 26, 2019, 05:07:33 pm »
Cool stuff romichess, thanks!

The one you posted in the Discussion board looking for faster updates brought back a flood of memories from early 90's when playing with cellular automata, chaos, Conway's Life... with QB or early VB. It looked like a version of... ? time for some research :D

Update: here it is
https://en.wikipedia.org/wiki/Cyclic_cellular_automaton

I was remembering devil spirals and it is demon cycles.

Here is my attempt:
Code: QB64: [Select]
  1. _TITLE "Demon Cycle" 'B+ 2019-12-26
  2. DEFINT A-Z
  3. CONST xmax = 800, ymax = 600, modu = 16
  4. SCREEN _NEWIMAGE(800, 600, 12)
  5.  
  6. DIM vs(xmax, ymax), ng(xmax, ymax)
  7.  
  8. FOR y = 0 TO ymax
  9.     FOR x = 0 TO xmax
  10.         ng(x, y) = RND * modu
  11.     NEXT
  12. WHILE _KEYDOWN(27) = 0
  13.     FOR y = 1 TO ymax - 1
  14.         FOR x = 1 TO xmax - 1
  15.             vs(x, y) = ng(x, y)
  16.             PSET (x, y), vs(x, y)
  17.             FOR yy = y - 1 TO y + 1
  18.                 FOR xx = x - 1 TO x + 1
  19.                     IF vs(xx, yy) = (vs(x, y) + 1) MOD modu THEN ng(x, y) = (vs(x, y) + 1) MOD modu: GOTO skip
  20.                 NEXT
  21.             NEXT
  22.             skip:
  23.         NEXT
  24.     NEXT
  25.     _DISPLAY
  26.  
  27.  

Hmm, seems like the idea of one color eating another color has been thought of by others. Nice example!
My name is Michael, but you can call me Mike :)

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #5 on: December 27, 2019, 05:26:45 am »
Ah, so the spirals are a mathematical artifact of the continuous ( cycling ) of a random number generator function.

I do not have the skill or mathematical insight to say that absolutely.  But it is a maxim to always be sceptical (naturally) and to be careful with RND.  For a third time I did not get spirals with occasional RANDOMIZE(TIMER).

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #6 on: December 28, 2019, 12:32:50 am »
I do not have the skill or mathematical insight to say that absolutely.  But it is a maxim to always be sceptical (naturally) and to be careful with RND.  For a third time I did not get spirals with occasional RANDOMIZE(TIMER).

Thanks Qwerkey for double checking. I did my own investigation. Putting RANDOMIZE TIMER just outside of the iteration loop and running 32000 it just ran too slow. Then 1 to 10 million iteration test:

1,000,000 no spirals
2,000,000 no spirals
3,000,000 chaotic spirals that often broke up
4,000,000 chaotic spirals that did not break up as often
5 to 9 each a little better
10,000,000 pretty close to the original

This in my opinion is proof that the cyclic nature of the RND function is conducive to forming spirals. Good catch!
My name is Michael, but you can call me Mike :)

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #7 on: December 29, 2019, 09:34:54 am »
Hello romichess - so here is a deep question for you...if the existence of Chaos is infinite BUT you have place boundaries on your Chaotic universe by the number of pixels displayed on a computer screen, would you conclude that All Chaos which has boundaries seeks to Order itself?

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #8 on: December 29, 2019, 05:12:53 pm »
Hello romichess - so here is a deep question for you...if the existence of Chaos is infinite BUT you have place boundaries on your Chaotic universe by the number of pixels displayed on a computer screen, would you conclude that All Chaos which has boundaries seeks to Order itself?
The question is a little above my pay grade. But I would say that as long as there are rules in play that allow it, then yes.
My name is Michael, but you can call me Mike :)

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #9 on: December 30, 2019, 12:04:25 pm »
Mike - Way over my pay grade as well - but I was just thinking the RND function has boundaries like the limitation of your (or anyone's) display screen. Ultimately, as your program seems to prove, BECAUSE of the boundaries, RND will move towards and away from some kind of Order. It's those many monkeys all typing randomly on typewriters who come up with a best selling novel. They were bounded by the alphabet.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #10 on: December 30, 2019, 12:44:42 pm »
There is doubtless a whole branch of mathematics devoted to chaos and many Ph.D.s will have been awarded for such stuff - they will have looked into boundary effects, I'm sure.  For us ordinary people it's just a question of trying the delights of QB64 and seeing what delight happens.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Evolve, Order Out of Chaos
« Reply #11 on: December 30, 2019, 10:06:53 pm »
Quote
All Chaos which has boundaries seeks to Order itself?

Maybe - I point out that the top of a frozen lake is an ordered boundary, while the water underneath can be considered chaotic.

To answer your question it fully it really depends on what the system is able to do with its energy, particularly at the boundary itself. In the case of a wintry lake, it will either entirely freeze or entirely melt after infinite time. Encase some water in a glass/crystal vase, and I again have an ordered boundary with a chaotic inside, but water doesn't interact with, say silica/quartz crystal the way it does with ice.

Hm, I'm just rambling. Good question though.
« Last Edit: December 30, 2019, 10:28:00 pm by STxAxTIC »
You're not done when it works, you're done when it's right.