My Games

Showing posts with label STEM. Show all posts
Showing posts with label STEM. Show all posts

Sunday, May 14, 2023

How to Count to 31 on One Hand

I was reading The Unbeatable Squirrel Girl, a really funny and clever comic from a while back, and it reminded me of a cute, but also really cool trick, for counting on one hand.

Imagine from pinky to thumb, that the value of each finger doubles:
Pinky = 1
Ring = 2
Middle = 4
Index = 8
Thumb = 16
And each finger can be either Up/On (1), or Down/Off (0).

So let's start counting. I show it as multiplication and addition here, as vectors, but you can just hold up your hand, or lay it on a surface like a keyboard or piano, whatever makes sense for you, and just feel it on your fingers.
1: 1(1) + 2(0) + 4(0) + 8(0) + 16(0) = 1 + 0 + 0 + 0 + 0 = 1
2: 1(0) + 2(1) + 4(0) + 8(0) + 16(0) = 0 + 2 + 0 + 0 + 0 = 2 
Simple enough so far. You can see from the ones and zeroes, it's just the position of your fingers. But what about 3, a number that you don't have a finger for? Just set your pinky and ring finger both to the ON position.
3: 1(1) + 2(1) + 4(0) + 8(0) + 16(0) = 1 + 2 + 0 + 0 + 0 = 3
And numbers larger than what we have fingers for, like 17? Pinky and thumb are ON, the rest are OFF.
17: 1(1) + 2(0) + 4(0) + 8(0) + 16(1) = 1 + 0 + 0 + 16 = 17
Normally when we think of numbers, we think of Arabic Numerals in Base 10, meaning 0 through 9, with 10, 20, 30, ..., 100, etc., as like the next iteration.

When we use this approach, we're counting in Base 2, also known as Binary. If we think of our fingers each as representing binary states, we can infer the value just from 0's and 1's. But, understanding the relationship between Base 10 and Base 2 visually and kinesthetically through our fingers makes it easier to understand.

In other words, starting from 0, we can think of each finger as representing 2** (to the power of) the incrementing values of our fingers:
1: 2**0 == Pinky 2: 2**1 == Ring 4: 2**2 == Middle 8: 2**3 == Index 16: 2**4 == Thumb
So instead of:
17: 1(1) + 2(0) + 4(0) + 8(0) + 16(1) = 1 + 0 + 0 + 16 = 17
We can just represent it as:
17: 1 0 0 0 1
As I was reading the comic, I couldn't help but start counting from 0 to 31, it was just fun to do, even meditative. It took a bit of effort at first, but now I can usually on two hands get to 128 fairly trivially. Going from 128 to 256 I usually end up losing focus or making a mistake somewhere along the way, but it's still really fun!




Here is our full table:
n:   1 2 4 8 16
0:   0 0 0 0 0
1:   1 0 0 0 0
2:   0 1 0 0 0
3:   1 1 0 0 0
4:   0 0 1 0 0
5:   1 0 1 0 0
6:   0 1 1 0 0
7:   1 1 1 0 0
8:   0 0 0 1 0
9:   1 0 0 1 0
10: 0 1 0 1 0
11: 1 1 0 1 0
12: 0 0 1 1 0
13: 1 0 1 1 0
14: 0 1 1 1 0
15: 1 1 1 1 0
16: 0 0 0 0 1
17: 1 0 0 0 1
18: 0 1 0 0 1
19: 1 1 0 0 1
20: 0 0 1 0 1
21: 1 0 1 0 1
22: 0 1 1 0 1
23: 1 1 1 0 1
24: 0 0 0 1 1
25: 1 0 0 1 1
26: 0 1 0 1 1
27: 1 1 0 1 1
28: 0 0 1 1 1
29: 1 0 1 1 1
30: 0 1 1 1 1
31: 1 1 1 1 1

Or, on our hands:
First image I found on google images. They went the opposite direction (thumb -> pinky) rather than what I said but it amounts to the same thing. You can also reverse the up and down if your pinky is capable of that, and then you get cool ninja / spiderman / punkrock handsigns. Whatever works for you, have fun with it.


During my Insight Fellowship after leaving academia, I struggled to learn a lot of the algorithmic and other computer science and software engineering fundamentals that I needed for industry. I didn't have the training and it definitely didn't come to me intuitively. Another Fellow in the group who had a much deeper intuition towards algorithms would often help me study, and one of the algorithms he worked through with me was in converting a decimal (number) to a binary representation. Rather than counting on my hands (honestly that may have been better...), I came to understand the algorithm as a vector representation, just another way of getting at the same thing, but either way, in so doing, a lot of things clicked into place for me. Not just how to write algorithms, but how to understand mathematical relationships, and in understanding aspects of natural philosophy, or how the universe works.


Imagine a World where people counted this way. What kind of culture would this be? What would be their myths, laws, philosophies, arts, and sciences?


There are other ways to reconceptualize numbers physically. This system is in Base 20, and computations can be inferred from the physical relationship between strokes:

A Number System Invented by Inuit Schoolchildren

Imagine a world where people counted this way? You don't have to imagine it.

From the article linked above:

The Alaskan Inuit language, known as Iñupiaq, uses an oral counting system built around the human body. Quantities are first described in groups of five, 10, and 15 and then in sets of 20. The system “is really the count of your hands and the count of your toes,”...

Because of the tally-inspired design, arithmetic using the Kaktovik numerals is strikingly visual. Addition, subtraction and even long division become almost geometric. The Hindu-Arabic digits are an awkward system, Bartley says, but “the students found, with their numerals, they could solve problems a better way, a faster way.”...

Thanks to efforts by linguists working with the Script Encoding Initiative at the University of California, Berkeley, the numerals were included in the September 2022 update of Unicode, an international information technology standard that enables the digitization of the world’s written languages. The new release, Unicode 15.0, provides a virtual identifier for each Kaktovik numeral so developers can incorporate them into digital displays. “It really is revolutionary for us,” Judkins says. “Right now we either have to use photos of the numerals or write them by hand.”


Sunday, May 7, 2023

The Dimensional Hierarchy is a Lie

The cube is a lie.
The square is a lie.
Even the line, is a lie.

Imagine two lines:
━  ━  ━
━  ━  ━

Or this way:
│ │

│ │

│ │

What is their relationship? Which is length? Which is width?



│━  ━  ━

If I rotated them, how would you know the difference? (Ignoring that these are different ascii characters...)

We think of dimensionality as a hierarchy; a one-dimensional line, two-dimensional square, three-dimensional cube, four-dimensional hypercube or time... but really, these are all single dimensions which may or may not have their own properties (is time just a rotation of space, or something fundamentally different?), which may very well be heterarchical, but we've chosen to think of them hierarchically.

And there is value in doing so, both because there is still clearly some kind of underlying systemic relationship, but also, having a shared understanding of what is up or down, left or right, lateral, before or after, or causal, is just a very useful assumption to make, and makes our perceptions of reality more communicable. Yet, when we fail to recognize that we are not always sharing this understanding, that our different perceptions may reflect the relativistic nature of reality itself, it only holds us back.

A zero-dimensional space is simply a dot. A line is a series of dots that can more parsimoniously be described by the equation Y=A+BX (or you may have learned it as Y=MX+B but it amounts to the same thing...). From the two-dimensional graph space, we can explain a series of dots in terms of their distance from a line cutting through them; the slope, B, being our one dimension.


If we add a second dimension to explain a three-dimensional graph by the distance of dots from a plane (or square) cutting through the space Y=A+B1X1+B2X2, we get something like: 

To the extent that we can conceptualize information, data, as a series of zero-dimensional objects, and create lines and planes and hyperplanes, cutting through a graphical space necessarily at least one dimension larger than the shape we cut through it (this is related to degrees of freedom or rank in statistics), and use that to often quite effectively explain patterns and even create learning algorithms, is it hard to imagine that this might speak to the nature of physical and metaphysical reality as well? We have plenty of other reasons already to believe that reality is better explained via probabilities than causalities, admittedly outside of my wheelhouse. 

Maybe all of these data are just being funneled through the fixed structure we think of as spacetime. But I dunno, maybe it makes more sense to think of reality as an "emergent" property of interactions of data- not that it can't be represented or interfaced with, but that, necessarily, part of the network exists in a larger (NOT "higher", necessarily) dimensional space than we can perceive all at once.

But it may not be that we can't perceive those other dimensions. Again, thinking of them as "higher" may be erroneous. It may just be that we can't perceive all of them at once; we may only be able to perceive, at most, n-1 of them at any one time.

But also, remember, these dimensions may themselves be unfixed, whether it's a matter of rotation, translation, reflection, or maybe even entire re-configuration.

They say that to effectively break the rules, you have to understand them first. I believe there is a numinousness that comes from the ability to think in terms of graphical representations, equations, and probabilities. Maybe it's all something between panentheism and animism? A series of wills superimposed on top of each other, a systemic or functional relationship between all things, each operating within their own dimensionalities; different frequency networks or phase networks, a push and pull of "spiritual forces", no one seeing all of reality, any one only seeing at most n-1 of it; none of it existing in isolation, every one making up its ever-changing totality.

Sunday, April 30, 2023

Maximum Recursion Depth... in 3D!!! (Proof of Concept)

While I did not design the algorithm, I've been playing around with the StereoscoPy open source codebase to create anaglyph 3D images. I've talked about my interest in anaglyph 3D before with my Positive and Negative Planes Concept so it was fun to do this. I actually bought red and cyan glasses to try this out (you can find them pretty cheap online).

The results are by no means perfect, but short of designing it from scratch with anaglyph 3D in mind, the fact that it works at all and I can more or less automate it is pretty cool.


How does Anaglyph 3D Work?
Anaglyph 3D takes two images offset from each other, like imagine a photo of a scene, and a subsequent photo taken from a foot to the left, or something like that. It then aligns and merges the images, and color codes them, most frequently with red and cyan. Then, when wearing red and cyan lenses, the respective colors are filtered from one eye or the other, such that your eyes receive competing information that makes it appear 3D.

Tbf since I did not code the algorithm myself, this is a bit of a weak sauce explanation, this is among the reasons why I prefer to do things from scratch like with my Genetic Algorithm that Learns the Konami Code, but for now this is fine.

Anyway, after installing that StereoscoPy module, and after experimenting with several different parameters, I used the following command to generate an anaglyph 3D version of the MRD Cover:
  StereoscoPy -A -S 20 0 -a ./MRD_Cover.jpg ./MRD_Cover.jpg
    ./MRD_Anaglyph.jpg
Rather than using two images, I just used the one cover, but offset it to the left (-S 10), and used their auto-alignment algorithm (-A).

In addition to installing this module with the auto-align optional dependencies, I did also have to install some linux packages for this to work, and only was able to figure that out by tracing some errors and googling around, so it's not as user-friendly as it could be. Perhaps I could submit a PR to make it more user friendly, in lieu of writing it from scratch...


Maximum Recursion Depth in 3D!!!
Reminder that this requires red and cyan glasses.

Anaglyph 3D conversion





JPG of original cover


Sunday, April 16, 2023

I Wrote a Genetic Algorithm to Learn the Konami Code!

It's been a pet interest of mine for years to learn how to write Genetic Algorithms, but it was never quite clicking for me. I ended up stumbling upon this video (embedded below but if you don't see it it's also the previous hyperlink), and it finally clicked. This video is entirely no-code, just a simple and intuitive explanation. They said there'd be a code walkthrough video as well but unfortunately it doesn't look like they ever posted it.


My Genetic Algorithm to Learn the Konami Code is open source, available on github here. With a little work this could probably be fitted to an emulator to actually trigger in a game, or maybe even with a real game console if the controller inputs are programmable, that's a little outside my purview. It would be cool if somebody wanted to try that, but really this is just intended as a technical demonstration.

Genetic Algorithms use the principles of genetics; fitness, reproduction, and mutation, to converge on solutions given some task.

They have a lot of applications, such as robotics, neurocomputation, and machine learning, but this is just a simple Genetic Algorithm, no ML involved here.

I also have some ideas for using Genetic Algorithms for worldbuilding, TTRPG stuff, or quasi-videogames, but still need to give all of those some thoughts.

Keeping it simple, I thought it would be fun to train a Genetic Algorithm to learn the Konami Code.



I give a simple explanation of it here, but if you follow the link above to the github repo, the README explains it in more detail and also shows an example.


Konami Code

["↑", "↑", "↓", "↓", "←", "→", "←", "→", "B", "A", "START"]

The Konami Code is a common set of user inputs in videogames to activate cheats or secrets, most famously old Konami games.

We can think of these 11 inputs as the genes making up a strand of DNA.

Players

We have some number of players. We can represent each player as a strand of DNA composed of 11 genes of seven types: ↑, , ←, →, B, A, or START.

Our initial population has entirely random genes; any 11 inputs among those seven values.

Fitness

We can imagine each player presses their controller inputs one by one (i.e. their genes), and each time their current input matches the Konami Code, their score goes up by one. If they press the wrong button, they lose a life, and they only have one life, so whatever score they had before the mistake is their final score. 

There are certainly more efficient fitness algorithms, but I like the simplicity of this one and the way it works for our metaphor, as if literally pressing the buttons one-by-one on a controller.

Selection and Crossover

We take the players with the top X score and drop the rest of them. 

Then, we generate new players, up to the number of the original playerbase size, by crossing the winners of the previous round. We take any two winners at random, and for each of our 11 genes of the DNA strand, we pick randomly from one parent or the other.

Since all of the new players were created from the selection of the highest scoring players of the previous generation, the genes they inherent are more likely to be correct, so they generally show improved performance over previous generations.

If all of the highest scoring players had a score of two or higher, this means they all inputted ↑ ↑ of the Konami Code correctly, and as a result all of the new players will also get at least a score of two, because they can only inherent ↑ for those first two genes.

On the other hand, if none of the starting players had START as their last gene, then no future generation of players will ever be able to win, because they can never inherent START in the final gene.

This is why we add Mutation.

Mutation

Mutation creates a percent likelihood that any given gene will change into any of our possible inputs, even if they did not inherit that input. With a mutation rate of 0.05, there is a 5% chance that any of the 11 genes of a player's DNA will mutate into any of the seven inputs.

Mutation can cause a good gene i.e. a correct input of the Konami Code to change, meaning it might take more generations of play before a sufficient number of players win. However, it also means that if you start with a low-scoring or non-viable starting group of players, that they can catch up more quickly, or succeed where it might otherwise have been impossible.


AND THAT'S IT!
If you want to know more about the particulars, check out the open source github project, or feel free to ask more questions here.


Sure this is cool I guess, but what's the big deal?

If you think coding and technology is black magic, nothing I say is likely to change that and while I think that's a shame, I'm not trying to change your mind.

All the same, if you're at all interested in natural philosophy or understanding the world, this is a cool demonstration. The fact that we can abstract the principles of genetics and use them to solve problems analogically demonstrates the existence of these systems and how they operate, and by extension informs our understanding of the world. The mathematical or algorithmic representation of them is just a language to make it easier to understand.

To me, conceiving of and training an algorithm to solve the Konami Code, it's like a puzzle, or a poem. It's a little piece of art. That's not to deny that these things can be used poorly, but both things can be true, and these systems exist in the world whether we use them or not, and also, people are using them, so isn't it better to find ways to find the beauty in them and use them for good? Even if you believe there is no good way to use them, if you really believe they're so powerful and so dangerous, isn't that all the more reason to understand what they are and how they work, so that you can better protect yourself from them?