Analogue of the game "Life" - Evo

Greetings harazhiteli!

Recently I read an article about the game Life , and I remembered that in May of this year I began to write my project of this orientation. Only here the interest in him for the routine of work quickly faded, although a lot was written. And now, inspired by this article, I took this project from a dusty shelf and added a few features, which I will discuss later.
In short, my option has the following conditions:
  • life develops on a field of 256 * 256 cells;
  • objects of three types can be placed on the field: living creatures , food (let's call it grass) and stone (obstacle);
  • living creature is actually a modified Turing machine , more precisely, it is more like an automaton with store memory , i.e. living creatures are a “processor” that executes their “genetic” code;
  • living creatures have the ability to perform certain actions (move, eat, reproduce (so far only by cloning, mutations will be from day to day, crossing in the future)), giving the appropriate commands;
  • stepping on the grass, its creatures trample down;
  • to absorb food, you must give the command "Eat in this direction!", being in the next cell;
  • living creatures have a memory, which allows you to build cycles, conditions, etc., i.e. Turing complete (correct me, if not right!), the amount of memory is unlimited;
  • living creatures can add and subtract values ​​in the mind, bit capacity is limited to one byte;
  • There is the possibility of implementing genetic algorithms (not yet implemented).
To whom details are interesting, I ask under a cut!

Short description


I called the program Evo ( git ), because it was originally planned for organisms to evolve there. But nothing prevents you from describing the living behavior algorithm yourself and loading it into the program. At the moment, only by editing the code (in MainWindow :: MainWindow () ), but loading livestock will be implemented soon.
Evo is written in C ++ / Qt . Functionality is tested only under Linux , although the program should work on any platform that is supported by the Qt library.
To get the binary, just do:
    git clone git@github.com:icoz/evo.git
    cd evo
    qmake
    make


So, when you start, a window with several buttons appears. We start and observe how the field is filled with flowers.


I will explain what color has:
  • emptiness is white;
  • the stone is red;
  • the grass, of course, is green;
  • and in blue I highlighted livestock.

At the start, the living creature genetic code is generated using the DSP. So there is no magic. True, later magic appears ! Something randomly generated begins to crawl, eat each other, and pasture too.
There are a few simple rules:
  • Every 150 (default) rounds on the field, 100 to 1100 grass cells are thrown to compensate for eating and trampling, and for each blade of grass an exclusively unoccupied cell is searched.
  • Every 500 (default) rounds, 150 randomly generated animals are added, which also begin to try to survive.
  • If the population exceeds 1500 individuals, they are cleaned: all animals with fitness == 0 are killed .

Logical partitioning is as follows.
The field (Map class) implements the functions of storing and moving objects, as well as the function of scattering a given amount of food.
The world (World class) describes the interaction of living creatures with the outside world.
Animals (instances of the Animal class) are automatic machines, the program of which is a “genetic” code (and I recall that each individual has a memory and the ability to add and subtract in his mind). In the process of executing this code, the body makes some other decisions. The world processes these requests (all this is implemented through a system of signals / slots).

In the case of acceptable behavior, living creatures are rewarded with an increase in the fitness parameter . Over the absorption of grass, fitness increases by 1, for eating other animals - +10 , for reproduction - +50 . The higher the fitness value , the more successful the body. In fact, this parameter describes the vitality of the body. By the way, I am waiting for your suggestions for the introduction of new rules in order to complicate the life of these cute creatures and make them come up with something new.

All this disgrace is implemented like this:

I would like to note that work was done to optimize the program so that it does not absorb tons of resources. But in view of the specifics of her work, she loads the percent heavily. The slowest is rendering the frame, so by default, rendering is done only every 100th round.
When saving livestock, not only binary code is saved, but also a text file with the * .code extension is created , in which the decryption of this code is given.

What else do you want to do


The wish list is as follows:
  • There is still a lack of implementation of genetic algorithms so that the most tenacious individuals can be selected and crossed. The implementation of crossbreeding seems to me the most difficult in this, because in fact we need to cross 2 sets of bytecode. (If there are ideas of algorithms - please comment).
  • It might make sense to review the set of available commands.
  • I would like to realize the "vision" of animals, i.e. the ability to determine the type of neighboring cells
  • You can modify the generator so that the living creatures are not randomly generated, but taking into account the weights of the teams.
  • Consider options for how to parallelize computations to improve performance.
  • Write an editor for the "genetic" code, so that it is convenient to develop the living creature yourself.

What will be done in the near future


The list of upcoming changes is as follows:
  • When propagated, mutations will be introduced into the “genetic” code.
  • Clicking “Save Current Picture” will prompt you for a file name to save the current map.
  • It will be possible to load saved live animals.
  • Possibility to load several animals from the folder at once at startup (working name - Autorun-folder ).

In the end


While I was starting to write this note, I had this program spinning in the background (on the fourth stump there were no optimizations in speed). At that moment, I reached fitness = 3441 for living creatures . To do this, more than 210,000 individuals lived and died, and more than 6,500 rounds passed.
To get really interesting animals that could do something interesting, you need (by my estimates) at least 10 ^ 6 rounds. Maybe someone more powerful computer, twist? But, of course, it will become more interesting when genetic algorithms and a more intelligent generator of initial sets are implemented. Well, with the addition of generated animals by individuals written by living people. This will give a good start for future generations.
Having driven the program on core i5, I pretty quickly got walking herds. Vitality has already reached 10,390 units!
It’s interesting to watch these cute creatures. You begin to empathize with them.
Finally, here are some interesting "life" pictures. Visible are the migration routes of populations, the formation of herds.



The most serious result is 36480 (this is after 10 hours of work and 2 * 10 ^ 6 rounds):

Download, run, watch!
Who cares, you can try to write bots yourself and see who is stronger!

That's all. Thanks for attention!

Bug reports and suggestions of new features are welcome.

UPD: the mutation is already implemented at a basic level.
UPD2: bug tracker
UPD3: thanks dtf forbinaries for Windows
UPD4: thanks dtf for the new binaries for Windows

Also popular now: