all 7 comments

[–]magnora7[S] 3 insightful - 1 fun3 insightful - 0 fun4 insightful - 1 fun -  (0 children)

This is a simulation game, I made it mostly for myself to explore artificial intelligence and neural networks so it's not incredibly polished or user-friendly. But it is very interesting from a technological perspective. It's entirely html5 and javascript, and open source. You can right-click and 'inspect' to see all the code right in the file.

HOW TO PLAY: Each fish has a neural network of 12 neurons. Those neurons are connected to each other in random ways with random strengths. So all the fish have random brains at the start, and thus random behaviors. Each fish is unique.

Then a generation happens over a few seconds, the fish run their neural network programs. Each fish has inputs of knowing the angle of the nearest food and that food's color. It has outputs of what speed it swims and how much to turn. Each fish has a unique neural network, and every generation has 100 fish.

The 10 fish that are the best at eating food by the end of the round go on to create the "children" for the next generation. This mimics natural selection. The children are mixtures of the genes of the adults, plus some random noise. This mimics evolution.

The green food is worth is +1 point to the fish health, yellow is +3, and red is -1. The healthiest 10 fish at the end of the generation have the children for the next generation, as ranked by these scores.

This process then repeats again and again, generation after generation, and the neural nets get better at the task given their inputs and outputs, through the process of natural selection and evolution. The connection strengths between the virtual neurons find an arrangement that produces the selected-for behavior.

If you want to see this in fast-time, toggle with the letter 'F' and it will do the generations very quickly and graph them for you. The green line is the median health score of a fish for each generation. The yellow line is the top 25%, and the red is the top 10% (the ones that will make the children for the next generation). The zig zag lines show every generation and the straight lines are the average across the last 10. This shows you the total improvement, regardless of season. The zig-zag lines go away after generation 200 to just leave you with the averages.

The black line that goes down is the genetic diversity. When it starts, all the brains are random, so the genetic diversity is very high. As the system hones in on a good design, the genetic diversity of the population shrinks, which in turn shrinks the possible genetic diversity of the children. The genes determine the brain design, more specifically how strongly each neuron in the neural network is connected to every other neuron.

If the diversity gets too narrow, it can get "stuck" on a certain gene arrangement. Sometimes this is good to lock in on a really nice behavior, but sometimes it's not. If it gets stuck on a bad behavior you can increase the gene mutation rate (like radiation) by hitting 'P' or reduce it by hitting 'O'. This value is called "User added std" (standard deviation). This means you're adding more noise to the children's genes. If you add too much noise all the brains eventually go back to completely random after a few generations, but if you have too little then it might "lock in" too early and not have the opportunity to discover new gene patterns that might be even more efficient at eating food.

You can let it run as long as you want, and it'll go as fast as your processor can handle. If you watch the fish after evolving them for 15 minutes, you can see they're quite good at the task! After a few hours (if you have a good run), I don't think I could actually program them to do it more efficiently than they do it. It's kind of amazing and showcases how powerful neural network evolution can be.

When you restart the game by refreshing the tab, it'll start with an entirely new pool of random brains, so each evolution is completely different and the fish discover different strategies.

Also there is a "seasonal" cycle that repeats every 10 generations. During "summer" there is a lot of yellow and green food and very little red food. During "winter" it's mostly red food to be avoided with very little green. This diversity of food situations creates more robust fish behavior, that is good both at finding the yellow/green and avoiding the red.

Anyway, if you have questions let me know. I might develop this more if people are interested, but I thought you all might enjoy a chance to play with your own neural network and to try evolving things from scratch using neural network technology right in javascript and html5.

tl;dr: "O" creates less genetic diversity in future generations, "P" makes more, "F" goes fast with graphs that hopefully go up. The fish get really good at eating the food and they learn how all by themselves without being programmed. You can open multiple tabs and have them compete and compare how they evolve differently.

[–]d3rr 3 insightful - 1 fun3 insightful - 0 fun4 insightful - 1 fun -  (5 children)

Very nice.

[–]magnora7[S] 2 insightful - 1 fun2 insightful - 0 fun3 insightful - 1 fun -  (4 children)

Thanks, it's based off of a phd project I found online. I changed the javascript and html quite a bit, but it's really a short program when you consider all that it does. Maybe 2k lines total for everything.

[–]d3rr 2 insightful - 2 fun2 insightful - 1 fun3 insightful - 2 fun -  (3 children)

Yeah this emergent behavior stuff is fascinating. I like watching the dumb bastard fish swim in circles before they get smart.

[–]magnora7[S] 2 insightful - 1 fun2 insightful - 0 fun3 insightful - 1 fun -  (2 children)

Haha yeah there are some really dumb ones in the beginning. Sometimes they lean toward the "avoid the red" strategy and sometimes they lean more toward the "get the green/yellow" strategy. Sometimes you'll get a genius population that's good at both, but you have to get lucky to discover the right gene settings, since it's basically a smart random-walk function. But I guess real evolution is too. It's super fascinating.

[–]d3rr 2 insightful - 1 fun2 insightful - 0 fun3 insightful - 1 fun -  (1 child)

This is really impressive considering the lines of code. I like the 'F' interface too. And my fish swim way faster on desktop. Also my kid says "that's amazing" and "how cute".

So is the skipping/redrawing I'm seeing happen for each new generation? It's distracting but maybe it needs to be there.

[–]magnora7[S] 2 insightful - 1 fun2 insightful - 0 fun3 insightful - 1 fun -  (0 children)

I'm glad you and your kid like it! haha

So is the skipping/redrawing I'm seeing happen for each new generation?

Yup that's exactly right. It's resetting the food to new random positions, creating all the new fish for the next generation, then placing them.

I can make the generations longer, which is more fun to watch, but it takes longer for the intelligence to evolve because each generation is longer. Maybe I could make that a variable you could change on the intro screen if I made such a thing. There's like 20 variables to mess with at the start