
Greetings to you again, dear habrazhiteli!
This article is laid out in the midst of a session, so I can disappear from the comments.2. Simulation and the simplest bot
Today we will deal with the settings of the simulation and look at the simplest bot (or a robot, at your convenience). No, I will not thoroughly consider the interface of the program - this will be your homework :-) And yes, the very concept of “genetic algorithm” is clearly described in Wikipedia, so we omit this explanation.
2.1. Simulation settings
Let's start with the simulation settings. In order to keep in you the desire to learn something more than just a bunch of tabs with incomprehensible settings, I will hide this unformatted part of the article under the spoiler.
Simulation settingsTo start the simulation process you can:
- Select in the “File” menu item “New simulation”;
- Click the corresponding button on the toolbar;
- Press the F1 key.
Consider the contents of the tabs in the window that appears Simulation Settings. Immediately, I’ll make a reservation that many settings seem dubious - either they don’t work or they don’t have any effect on the simulation (is
n’t it one and the same? ). Some of these features I omit, some will consider.
')
Species tab (Fig. 1):
Picture 1Native Species - list with loaded DNA codes (bots). To manage the list, you can use the buttons:
Add - allows you to consider new varieties to be added to the list (simply, add a DNA code file).
Duplicate - allows you to give the properties of the selected bot to another bot.
Delete species - removal of DNA from modeling.
Comments - shows the comments from the header of the DNA code.
Varieties of initial properties (section Species starting properties):
Skin - drawings on the surface of individuals. These drawings have no meaning, but when the robot is modified, the image is also modified, so you can have an idea about the genetic difference between the two bots.
Change - a random change in the initial pattern.
Color - sets the initial color of the bot, but it can also change with mutations.
Starting position - defines the starting position for the genus on the modeling field.
Individuals - the number of bots of one species at the beginning of the simulation.
Starting energy - the amount of energy of each individual at the start.
Vegetable (autotroph) - with this mark all individuals of the species accumulate energy. This should contribute to the reproduction of new energy in the simulation. Autotrophs (organisms whose growth and reproduction does not depend on external sources of organic compounds) work like plants, forming the basis of the food chain. Such individuals will always be present in the simulation. Mutation can not change such an individual.
Fixed in place - bots of this species do not move on the simulation field. It is a plant simulation. Mutation cannot change individuals of this species.
Disable Vision - disables the eyes of bots.
Disable Voluntary Movement - disables random movements of the bot, which allow them to avoid looping on the simulation field (this process is also called Brownian motion).
Disable Reproduction - disables reproduction.
Virus Immune - this species will never be infected with viruses.
Disable Mutation - disables DNA mutations.
Mutation rates (Fig. 2) - opens the window for determining the initial mutation coefficients for this species. There are many different mutation coefficients, many types of DNA mutations. Many incomprehensible letters, maybe someone can make out - this is intended to fine tune the mutation and in most cases remains unchanged.
Figure 2On the General tab (Fig. 3), the following parameters are configured:
Figure 3Field Controls - the size of the simulation field
Waste - sets the garbage threshold for all bots on the simulation field. If the value is “-1”, garbage is not dumped.
Graphing - time to update graphs (in cycles)
Population control - a section in which you can regulate a population in the simulation field: set the limit on the number of individuals, including autotrophs, the amount of energy in autotrophs, its replenishment period, the energy / body ratio, the mode of day and night when it reaches a certain amount of total energy in the simulation field and much more.
The “Physics and Costs” tab (Fig. 4) controls the physics of the bot world and the cost of each bot operation.
Figure 4The “Physics” section has the following settings:
The Big Blue Screen Acts Like A - select the type of simulation field. It can be represented by a liquid or a flat surface; it also has more advanced settings when you click the Custom Physics button (Fig. 5): fluid properties, its density and flow rate, world gravity, static and kinetic coefficients, Brownian motion coefficient;
Figure 5Movement Efficiency - sets the model of movement of bots: biological, mechanical and ideal;
Brownian Movement - Brownian type of movement: bacterium or animal;
Vertical Gravity - a type of vertical gravity: the Earth or the Moon;
Max Velocity - the maximum speed of bots;
Collision Elasticity - adjust the elasticity of collision bots.
The “Costs and Retuned Shots” section (Fig. 6) contains the following settings:
Costs - the cost of each bot operation. Either it is disabled, or it is set in accordance with the standard cost in the F1 league (I will talk about the leagues a little later), or it is manually configured when you click the “Custom Simulation Costs” button. In the “Costs” window that appears, you can adjust the cost of DNA commands, its changes, limitations, and cost, depending on the number of cycles lived.
Figure 6Shot Energy Exchange Method - determines the amount of energy that a bot can spend to shoot. It may be limited to a certain number or be proportional to the total amount of bot energy. By default, a bot can use all its energy to fire.
The “Mutations” tab (Fig. 7) contains the mutation settings:
Figure 7Disable All Mutations - disable all mutations
Current Multiplier - sets the mutation ratio from 1 / 32x to 32x.
Oscillation Frequency - the frequency of random changes in the coefficient of mutation (fixed 1 / 16x and 16x within the selected coefficient). Random change is applied through the selected number of cycles.
Scripts - a set of scripts acting on bots. Compiled independently on the principle of "condition - DNA cell - action." Thus, the script “If Robot gains Sysvar - pain - then Pause and highlight robot” means “If the bot received Pain, then stop the simulation and select this bot.” The condition is defined as “bot received”, “bot lost”, “DNA contains” and “DNA does not contain”. A DNA cell is a variable used in calculations in DNA. The action is given as “Suspend simulation and select bot”, “Kill bot” and “Take a snapshot”.
The Restart and League tab (Fig. 8) contains settings for restarting simulation and participation in leagues. Leagues are competitions between bot developers. There are 4 major leagues that differ in rules, starting conditions and world settings. Thus, the F1 league has no rules for the behavior of bots in the world, and the F2 league prohibits the united bots to feed and attack, and also disables the mutation and eliminates the world from viruses. The restart of the simulation is reduced to one condition - to start again, if all the bots are dead.
Figure 8The “Internet” tab (Fig. 9) contains the settings of the Internet mode, in which bot developers can connect to each other and use one simulation field. It is an experimental implementation of the Player versus Player mode, which exists in many online games. Plans to check the operation of this mode. As they say, stay tuned!
Figure 9The “Recording” tab (Fig. 10) has the settings for saving the current simulation or the DNA of the best bots after a specified time, as well as creating a database in which every bot has ever existed in the simulation.
Figure 10To load or save simulation settings, you need to click on the “Load Settings” or “Save Settings” buttons, respectively. The “Paused / Unpaused” button switches the start mode of the simulation: start the simulation immediately or not start it. The “Start new” button will start the simulation, and the “Change” button will change the properties of the already running simulation.
2.2. The simplest bot
So, we come close to the story about writing a bot. In this article I will not consider the structure of DNA entirely (in store for the future). You can see other bots in the “Robots” folder in the program directory.
So what is the usual bot in the Darwinbots system. Here's what:
'How does it work?
cond
start
100 * .nrg 1000 sub sgn 0 floor 500 * .body sub sgn 0 floor mult .strbody mult store 100 500 * .nrg sub sgn 0 floor .fdbody mult store 200 * .shell sub 0 floor 200 ceil 200 * .shell sub sgn abs .mkshell mult store 10 * .venom sub 0 floor 10 ceil * .venom 10 sub sgn abs .strvenom mult store * .tiepres * 51 * .tiepres sub sgn abs * .tiepres sgn mult .deltie mult store * 51 * .refeye sgn -1 mult 1 add * .refeye * .myeye sub sgn abs * .eye5 35 sub 0 floor sgn mult mult .tie mult store * 51 * .tie sgn * .tiepres * 51 sub sgn abs -1 mult 1 add add sgn .tienum mult store -1 * .tie sgn * .tiepres * 51 sub sgn abs -1 mult 1 add add sgn .tieloc mult store -1000 * .tie sgn * .tiepres * 51 sub sgn abs -1 mult 1 add add sgn .tieval mult store 1 * .robage sgn 1 sub abs .tie mult store .mkshell * .vloc sgn -1 mult 1 add .vloc mult store -100 * .venval sgn 1 add .venval mult store 999 rnd 1 add * 51 sgn -1 mult 1 add 51 mult store 200 * .aim add * .refeye *. Myeye sub sgn abs -1 mult 1 add. Setaim mult store *. Maxvel * .vel sub 0 floo r * .robage sgn * .refeye * .myeye sub sgn abs mult .up mult store * .refxpos * .refypos angle * .rod sgn abs. * .refeye * .myeye sub sgn abs * .eye5 sgn mult mult .setaim mult store * .refveldx * .refeye * .myeye sub sgn abs * .eye5 sgn mult. dx mult store * .refeye *. myeye sub sgn abs -1 mult 1 add 50 mult inc 0 50 50 sub 0 floor sgn. refeye mult store 0 * 50 30 sub 0 floor sgn 50 mult store * .refshell 1 sub 0 floor sgn 3 mult 5 sub * .refeye sgn mult 1 sub * .refeye * .myeye sub sgn abs * .eye5 35 sub 0 floor sgn mult .shoot mult store * .shoot 3 add sgn 10 mult 0 floor * .refeye sgn 30 mult add * .refshell 1 sub 0 floor sgn 20 mult sub * .shoot sgn abs .shootval mult store 50 * .body 250 sub sgn 0 floor * .nrg 2000 sub sgn 0 floor * .eye5 30 sub 0 floor sgn -1 mult 1 add mult mult. Repro mult store * .thisgene -1 mult 3 add abs * .genes 1 sub sgn. Delgene mult store
stop
end
Surprised by the complexity of the code? In fact, this bot is written by the user under the nickname Shen as an experiment. What - you will soon understand. In fact, this is a good example of in-depth study of the possibilities of a DNA code.
So, let's start!
The DNA bot code is composed of one or more genes that are executed all together in one simulation cycle. Genes are represented by conditions that are defined by the following constructs:
Cond
<one or more conditions>
Start
<operations inside the gene>
Stop
or
Cond
<one or more conditions>
Else
<operations inside the gene>
Stop
The “Start” command performs operations if all conditions are true, and the “Else“ command - if all conditions are false. Example condition:
ab> (if 'a' is greater than 'b', then the condition is true). The following comparison operations can be used: = (equal),! = (Not equal),% = (approximately equal - returns true if the first number differs from the second by no more than 10%),!% = (Gives the opposite result of the previous comparisons),> (more), <(less),> = (greater than or equal), <= (less than or equal), ~ = (uses three numbers, returns true if the second number is greater than the first by the third number of percent), ! ~ = (gives the opposite result of the previous comparison). The principle of operation of all operations is described in the
Operators section of the official site. The site has a cleverly
cut navigation system, so the necessary section will have to be searched for and persistently.
As you have probably noticed, the reverse Polish notation is used to record transactions. A distinctive feature of the reverse Polish notation is that all arguments (or operands) are located before the sign of the operation. In general, the entry is as follows:
A record of a set of operations consists of a sequence of operands and characters of operations. Operands in terms of writing are separated by spaces. The expression is read from left to right. When an operation sign is encountered in an expression, the corresponding operation is performed on the last two operands encountered before it in the order in which they are written. The result of the operation replaces the sequence of its operands and its sign in the expression, after which the expression is calculated further by the same rule. The result of evaluating the expression becomes the result of the last calculated operation.
Newbies . Example: you need to calculate the expression
(a + b) * (c + d) . Polish reverse record and, accordingly, the code will be as follows:
ab add
cd add mult
Read more
in the article on Wikipedia .
Genes operate on pointers or numbers. For example, 110 * or * 110 means "returning a value from cell 110". Half of the genetic memory is predetermined to function as bot properties. This area of ​​memory is called Sysvars (or System Variables). For example, cells from 501 to 509 contain the distances of objects in the field of view of each bot eye and can be used by operations like * 501 ... * 509 and * .eye1 ... * .eye9. Cells 1 (.up), 2 (.dn), 3 (.sx), 4. (Dx), 5 (.aimdx), 6 (.aimsx) and 11 (.maxvel) store movement speed in different directions around axes and maximum speed, depending on the weight of the bot. Each system variable has a name and number, so they can be accessed differently. Free-for-use cells are listed in the
Free Variables section, and a list of system variables in the
Sysvar section.
Gene operations can change memory cell values, for example: 10 .up store or 10 1 store means “set the speed of movement forward equal to 10”. The same result will be if you perform the operation 5 5 add 1 store. The operation .up inc is equivalent to the operation * .up 1 add .up store (extract the value from the cell .up (10), add one, write to .up). Accordingly, the operation .up dec means "reduce the forward speed by 1". An example of a ready-made DNA code is presented below.
A comment is indicated by an apostrophe at the beginning of a line.
'Gene 1 Movement
cond
* .nrg 100> 'condition - as long as the energy is more than 100 ...
start
10 .dn store '... the bot moves backward at a speed of 10
stop
end 'end of the DNA code
Creating a bot. The choice of behavior patterns and the development of genes.
For this article, we consider a bot that will have the most operations needed in the world of bots. DNA will consist of four genes: "Food Search," "Eating Food," "Avoiding Combining," and "Reproduction."
The full DNA code is presented under the spoiler below,
taken from the official site of the project. The text below will be a translation of a learning article that includes some additions from yourself.
Full code'Gene 1 Food Finder
cond
* .eye5 0>
* .refeye * .myeye! =
start
* .refveldx .dx store
* .refvelup 30 add .up store
stop
'Gene 2 Eat Food
cond
* .eye5 50>
* .refeye * .myeye! =
start
-1 .shoot store
* .refvelup .up store
stop
'Gene 3 Avoiding Family
cond
* .eye5 0 =
* .refeye * .myeye = or
start
314 rnd .aimdx store
stop
'Gene 4 Reproduce
cond
* .nrg 20000>
start
10 .repro store
stop
end
So let's break down by genes.
Gene "Food Search"'Gene 1 Food Finder
cond
* .eye5 0>
* .refeye * .myeye! =
start
* .refveldx .dx store
* .refvelup 30 add .up store
stop
For the convenience of recording the bot, which we will “isolate” and whose DNA we will consider, we will call it “ours”. This does not negate the fact that there may be several bots with the same code.
Consider the conditions in this gene. The value of eye5 will be greater than zero if another bot is directly in front of ours (in the field of view of the fifth, central eye). Thus, this code will later force our bot to attack only when the enemy is right "under the nose." The string * .refeye * .myeye! = Is the classic recognition system used by bots since the beginning of program development. If other bots do not have the same number of eyes as ours, then it is not a relative, but food. Almost the same as in the real world :-).
Go to the operations. Refveldx and Refvelup return the speed of any bot in our field of vision. Values ​​are relative. Operation * .refveldx .dx store will allow our bot to move sideways with the speed of movement of the bot that came into view. This will allow our experimental not to become a victim of a bot spinning around and shooting on all sides. Operation * .refvelup 30 add .up store will force our bot to move forward 30 units of speed faster than the enemy to catch the prey. Naturally, the speed will not be greater than the value of the cell .maxvel, that is, the maximum speed depending on the weight. The operations of this gene can also be written as follows:
*.refvelup .up store
*.up 30 add .up store
Gene "Eating Food"'Gene 2 Eat Food
cond
* .eye5 50>
* .refeye * .myeye! =
start
-1 .shoot store
* .refvelup .up store
stop
We analyze the conditions. As in the previous gene, our bot will only respond to a bot that came into view of the fifth eye. However, in this gene, the search will take place as much as 50 units of distance ahead. If the bot approaches a distance less than 50, this will be a signal to attack. The second rule remains unchanged and is taken from the first gene. The reason for specifying the same recognition operation is a feature of the DNA code execution: all genes are executed sequentially in one cycle, each next gene can use only the values ​​of memory cells and cannot refer to the results of the execution of the previous gene if they have not been written to memory.
Consider the reaction of the bot to the fulfillment of all conditions. In this code, as has already become apparent, there is no movement operation. This is also a consequence of the sequential execution of the DNA code - the speed of the bot was recorded earlier, so our bot will move with it, unless, of course, in the next gene the speed is changed. Line -1 .shoot will force the bot to shoot at its chosen target as many times as it needs. The string * .refvelup .up store will force the bot to slow down and start moving at the speed of its target. This is an important operation, since bot collisions can lead to wasted time and energy.
Gene "Avoid association"'Gene 3 Avoiding Family
cond
* .eye5 0 =
* .refeye * .myeye = or
start
314 rnd .aimdx store
stop
This condition is a bit more complicated than the previous one. It uses the logical operator or (or). The .eye5 cell is zero if no one is visible. Recognition will return a boolean “false” if an individual similar to ours suddenly appears before our eyes. Here the or operator takes effect. By means of it, the general condition for fulfillment will be true if one of the conditions included in it is true - either if no one is near, or if the “own” is near. Well here, I think, there is nowhere more clear.
If the condition has worked, the bot will start spinning to the right at random angles from 0 to 314 degrees. With this operation, our bot will be able to more rationally inspect the environs of the world in search of food. In principle, you can put any value of the angle (not even random), but too small an angle will lead to a deterioration in the ability to search for food.
Also, this gene is a classic method of avoiding a bot uniting with others, unless, of course, a standard. About the association will tell in the following articles.
Gene "Reproduction"'Gene 4 Reproduce
cond
* .nrg 20000>
start
10 .repro store
stop
The last gene sets the rules for bot reproduction.
Briefly, this gene can be explained as follows: if the energy of a bot is more than 20,000, then a new generation of bot is created with a starting amount of energy equal to 10% of the energy of the parent. Of course, a descendant can be given 50%, but there is a risk of losing both generations. The same risk can be attributed to the reduction of the required amount of energy for reproduction. The faster the bot multiplies, the less energy it can give to the descendant.
Conclusion
This bot is an excellent tool for learning the basics of the genetic algorithm of the Darwinbots program, as it contains the basic functions of an individual. However, this bot is not perfect - under certain circumstances it is doomed to death, like any other bot. This gives the developer the opportunity to improve his bots, try to run them in leagues or online, identify strengths and weaknesses and further improve. Bots written specifically for participation in leagues may contain more than 20 genes with several dozens of operations, may contain only two genes with a huge number of conditions and operations, and may even have one gene in which a bunch of operations are unconditionally performed (of course, not absolutely unconditionally - logical operators are used. Such a bot was in the first example.) - it all depends on the developer's strategy. In DNA, you can use a huge number of system variables that are responsible for almost every aspect of a bot’s life, ranging from visual latitude and garbage dumping to properties inside a combined individual and a counter, the total number of individuals on the simulation field. If you have a desire, I will try to make out a complex bot in one of the following articles.
The program Darwinbots does not put restrictions on the complexity of the code, but the more complex the bot, the more computer resources will be needed to calculate the following cycles. The developers recommend running the simulation on a free computer and leave it for one or two weeks, after having set up the simulation to save the best individuals. Thus, it is possible to study several generations of bots and wait for global natural selection.
At the official forum of the project, you can ask any question of interest, and it will be happy to answer. Unfortunately, some, for this you need to have at least a basic knowledge of English.
I hope this extensive article was more exciting than the introductory one. I am sure that many people after this article will try to write their own bots. Maybe even arrange a small parade of bots in the comments. But it is in the future, if the program will interest readers.
As always, in the comments I accept your wishes regarding the content of the following articles. We will write down!
Sources:
1.
The only laboratory work on the program throughout the Runet, with which I began work on the course ;
2.
The official site of the project Darwinbots ;
3.
Wikipedia .