• Guest
HabraHabr
  • Main
  • Users

  • Development
    • Programming
    • Information Security
    • Website development
    • JavaScript
    • Game development
    • Open source
    • Developed for Android
    • Machine learning
    • Abnormal programming
    • Java
    • Python
    • Development of mobile applications
    • Analysis and design of systems
    • .NET
    • Mathematics
    • Algorithms
    • C#
    • System Programming
    • C++
    • C
    • Go
    • PHP
    • Reverse engineering
    • Assembler
    • Development under Linux
    • Big Data
    • Rust
    • Cryptography
    • Entertaining problems
    • Testing of IT systems
    • Testing Web Services
    • HTML
    • Programming microcontrollers
    • API
    • High performance
    • Developed for iOS
    • CSS
    • Industrial Programming
    • Development under Windows
    • Image processing
    • Compilers
    • FPGA
    • Professional literature
    • OpenStreetMap
    • Google Chrome
    • Data Mining
    • PostgreSQL
    • Development of robotics
    • Visualization of data
    • Angular
    • ReactJS
    • Search technologies
    • Debugging
    • Test mobile applications
    • Browsers
    • Designing and refactoring
    • IT Standards
    • Solidity
    • Node.JS
    • Git
    • LaTeX
    • SQL
    • Haskell
    • Unreal Engine
    • Unity3D
    • Development for the Internet of things
    • Functional Programming
    • Amazon Web Services
    • Google Cloud Platform
    • Development under AR and VR
    • Assembly systems
    • Version control systems
    • Kotlin
    • R
    • CAD/CAM
    • Customer Optimization
    • Development of communication systems
    • Microsoft Azure
    • Perfect code
    • Atlassian
    • Visual Studio
    • NoSQL
    • Yii
    • Mono и Moonlight
    • Parallel Programming
    • Asterisk
    • Yandex API
    • WordPress
    • Sports programming
    • Lua
    • Microsoft SQL Server
    • Payment systems
    • TypeScript
    • Scala
    • Google API
    • Development of data transmission systems
    • XML
    • Regular expressions
    • Development under Tizen
    • Swift
    • MySQL
    • Geoinformation services
    • Global Positioning Systems
    • Qt
    • Dart
    • Django
    • Development for Office 365
    • Erlang/OTP
    • GPGPU
    • Eclipse
    • Maps API
    • Testing games
    • Browser Extensions
    • 1C-Bitrix
    • Development under e-commerce
    • Xamarin
    • Xcode
    • Development under Windows Phone
    • Semantics
    • CMS
    • VueJS
    • GitHub
    • Open data
    • Sphinx
    • Ruby on Rails
    • Ruby
    • Symfony
    • Drupal
    • Messaging Systems
    • CTF
    • SaaS / S+S
    • SharePoint
    • jQuery
    • Puppet
    • Firefox
    • Elm
    • MODX
    • Billing systems
    • Graphical shells
    • Kodobred
    • MongoDB
    • SCADA
    • Hadoop
    • Gradle
    • Clojure
    • F#
    • CoffeeScript
    • Matlab
    • Phalcon
    • Development under Sailfish OS
    • Magento
    • Elixir/Phoenix
    • Microsoft Edge
    • Layout of letters
    • Development for OS X
    • Forth
    • Smalltalk
    • Julia
    • Laravel
    • WebGL
    • Meteor.JS
    • Firebird/Interbase
    • SQLite
    • D
    • Mesh-networks
    • I2P
    • Derby.js
    • Emacs
    • Development under Bada
    • Mercurial
    • UML Design
    • Objective C
    • Fortran
    • Cocoa
    • Cobol
    • Apache Flex
    • Action Script
    • Joomla
    • IIS
    • Twitter API
    • Vkontakte API
    • Facebook API
    • Microsoft Access
    • PDF
    • Prolog
    • GTK+
    • LabVIEW
    • Brainfuck
    • Cubrid
    • Canvas
    • Doctrine ORM
    • Google App Engine
    • Twisted
    • XSLT
    • TDD
    • Small Basic
    • Kohana
    • Development for Java ME
    • LiveStreet
    • MooTools
    • Adobe Flash
    • GreaseMonkey
    • INFOLUST
    • Groovy & Grails
    • Lisp
    • Delphi
    • Zend Framework
    • ExtJS / Sencha Library
    • Internet Explorer
    • CodeIgniter
    • Silverlight
    • Google Web Toolkit
    • CakePHP
    • Safari
    • Opera
    • Microformats
    • Ajax
    • VIM
  • Administration
    • System administration
    • IT Infrastructure
    • *nix
    • Network technologies
    • DevOps
    • Server Administration
    • Cloud computing
    • Configuring Linux
    • Wireless technologies
    • Virtualization
    • Hosting
    • Data storage
    • Decentralized networks
    • Database Administration
    • Data Warehousing
    • Communication standards
    • PowerShell
    • Backup
    • Cisco
    • Nginx
    • Antivirus protection
    • DNS
    • Server Optimization
    • Data recovery
    • Apache
    • Spam and antispam
    • Data Compression
    • SAN
    • IPv6
    • Fidonet
    • IPTV
    • Shells
    • Administering domain names
  • Design
    • Interfaces
    • Web design
    • Working with sound
    • Usability
    • Graphic design
    • Design Games
    • Mobile App Design
    • Working with 3D-graphics
    • Typography
    • Working with video
    • Work with vector graphics
    • Accessibility
    • Prototyping
    • CGI (graphics)
    • Computer Animation
    • Working with icons
  • Control
    • Careers in the IT industry
    • Project management
    • Development Management
    • Personnel Management
    • Product Management
    • Start-up development
    • Managing the community
    • Service Desk
    • GTD
    • IT Terminology
    • Agile
    • Business Models
    • Legislation and IT-business
    • Sales management
    • CRM-systems
    • Product localization
    • ECM / EDS
    • Freelance
    • Venture investments
    • ERP-systems
    • Help Desk Software
    • Media management
    • Patenting
    • E-commerce management
    • Creative Commons
  • Marketing
    • Conferences
    • Promotion of games
    • Internet Marketing
    • Search Engine Optimization
    • Web Analytics
    • Monetize Web services
    • Content marketing
    • Monetization of IT systems
    • Monetize mobile apps
    • Mobile App Analytics
    • Growth Hacking
    • Branding
    • Monetize Games
    • Display ads
    • Contextual advertising
    • Increase Conversion Rate
  • Sundry
    • Reading room
    • Educational process in IT
    • Research and forecasts in IT
    • Finance in IT
    • Hakatonas
    • IT emigration
    • Education abroad
    • Lumber room
    • I'm on my way

We cultivate AI - Genetic algorithms: introduction /Habr

We cultivate AI - Genetic algorithms: introduction /Habr  
(generated image)
 
There are many ways to create an artificial neural network or even “artificial intelligence”. But all these methods are discouraging, from the part of the complexity which I do not fully understand, partly from the fact that everything comes down to mathematical formulas.
 
There is nothing wrong with such approaches; they help to solve the tasks assigned to them. But it seems like I really want to write a bike.
genetic algorithms and write a simple little program.
 
The term "genetic algorithm" refers to a specific algorithm, implemented in a special way to solve certain problems. Although the formal genetic algorithm alone will serve as the basis for the examples that we will create in this article, we do not need to worry about implementing the algorithm with perfect accuracy, given that we are looking for a creative use of evolutionary theories.
 
Let's start with the traditional computer genetic algorithm. This algorithm was designed to solve problems in which the solution space is so large that the brute force algorithm would simply take too much time. For example: I think of a number. The number is from one to one billion. How long will it take you to guess this? Solving a problem using brute force refers to the process of checking every possible solution. It's one? Are these two? Is it three? Is it four? And so on and so forth. Although luck plays an important role here, we will look forward with brute force to the years when you count to one billion. However, what if I could tell you if your answer was good or bad? Warm or cold? Hot? Super, super cold? If you could evaluate how “suitable” the assumption is, you could choose other numbers closer to this assumption and find the answer faster. Your answer may evolve.
 
Why genetic algorithms?
 
Although computer modeling of evolutionary processes dates back to the 1950s, much of what we now call genetic algorithms (also known as “GA”) was developed by John Holland, a professor at the University of Michigan, whose book Adaptation in Natural and Artificial Systems was the first in GA studies. Today, more and more genetic algorithms are part of a wider field of research, often called Evolutionary Computing.
 
To illustrate the traditional genetic algorithm, we start with monkeys. No, not our evolutionary ancestors. We're going to start with some fictional monkeys that knock on the keys to print the complete works of Shakespeare. Just like my colleagues with their Java, shaking their legs all day and adjusting the air conditioning with the window.
 

 
The “Infinite Monkey Theorem" is formulated as follows: A monkey accidentally pressing keys on a typewriter will ultimately print all of Shakespeare's works (given an infinite amount of time). The problem with this theory is that the likelihood that the monkey actually printed Shakespeare is so small that even if this monkey started with the Big Bang, it is unlikely that we will have Hamlet by now.
 
Let's look at a monkey named Vova. Vova types on a small typewriter containing only twenty-seven characters: twenty-six letters and one space. Thus, the probability of Vova getting into any given key is one in twenty-seven.
 
Let's look at the phrase “to be or not to be that is the question” (we simplify it from the original “to be or not to be: that is the question”). The phrase is 39 characters long. If Vova starts typing, the probability that he will print the first character correctly is 1 to 27. The first two characters are 1 to 27 * 27. Therefore, the probability that Vova will write the full phrase is

 
Which is 1 to 6?55?93?03?86?82?60?89?54?24?09?48?95?01?61?83?73?22?163.
 
Even if Vova could print a million random phrases per second, he would have to print within ??? ??? ??? (??? 017 years). (The age of the universe is only ??? billion years.) 3-3-331329.
 
The point of all these incomprehensibly large numbers is not to confuse you, but to demonstrate that the brute force algorithm (enumerating each random phrase) is not a reasonable strategy for the "to be or not to be that is the question" set. Genetic algorithms will help us.
 
Of course, this is a stupid task, because we know the answer, and genetic algorithms are usually used to search for an unknown solution. But our task is to study genetic algorithms, Skynet will create tomorrow.
 
Natural selection of Darwin
 
Let's look at the three basic principles of Darwin evolution that will be required when implementing our algorithm. For natural selection to occur as it happens in nature, all three of these elements must be present:
 
Heredity 3-3-31139. . There must be a process by which children obtain the properties of their parents. If the creatures live long enough to breed, then their traits are passed on to their children in the next generation of creatures.
 
Variability (diversity) 3-3-31139. .
 

 
There must be some diversity in the population, or something that brings it. For example, let's say there is a population of beetles in which all beetles are exactly the same: the same color, the same size, the same wingspan, the same thing. Without any diversity in the population, children will always be identical to their parents and to each other. New combinations of traits will never arise, and nothing develops.
 
Selection (selection)
. There must be a mechanism by which some members of the population are able to be parents and transmit their genetic information. This is usually called “ the survival of the fittest ". For example, a gazelle population, lions drive every day. Faster gazelles are more likely to avoid lions and, therefore, are more likely to live longer, having the ability to breed and pass on their genes to children. However, the term “most appropriate” may be misleading. As a rule, we associate this with
greater than
,
faster than
or
stronger than
. In some cases, this is true, but natural selection is based on the principle that some traits are better suited to the environment and therefore the probability of survival and reproduction depends on them. This has nothing to do with the fact that the creature is "better" (after all, this is a subjective term) or more "physically developed." In the case of typing monkeys, the steeper monkey is the one that typed the phrase closer to “to be or not to be”.
 
Genetic algorithms - creating a population of
 

 
As part of the solution to the problem of writing monkeys, we will not create more monkeys named Vova, one is enough. Instead, we will form a population of phrases (a collection of strings). At this stage, we implement the principle of diversity. Let's say our task is to get the string
cat
using evolution and we have a population of the following species:
 
hug
 
rid
 
won
 
Of course, there is a variety in these three phrases, but if you don’t mix, you won’t be able to get a cat. There is not enough variety to find the optimal solution. However, if we had a population of thousands of phrases, all obtained at random, that is, it is likely that at least one variant in the population will have the character “c” as the first character, one will have “a” as the second, and one "a" as the third. Most of the population is likely to give us enough diversity to generate the desired phrase (a little later we will introduce another mechanism to regulate diversity).
 
So the first step is
creation of a random population of
.
 
Genotype and Phenotype
 
The data that we operate, transmit from generation to generation, is a genotype. The phenotype is the representation of these data.
 
 
(color code (genotype) and color itself (phenotype)) 3-3-31292.
 
 
(length (genotype) and line (phenotype)) 3-3-31292.
 
How to record data and present it depends on the specific task. In our case, with the text from the monkeys, there is not much difference between the genotype and phenotype. Unless we can write a character in the form of its ANSI code in an arbitrary array called genes.
 
Our initial population will consist of randomly generated genotypes - genes - DNA. Here I am not a biologist, + - within the framework of the fact that we are doing this all one and the same.
 
Genetic algorithms - selection
 
Breeding is the science of creating new and improving existing breeds of domestic animals, cultivars and strains of microorganisms 3-3-33397.
In our case, we can break the selection into two parts.
 
1. Assessment of suitability
 
Suitability, fitness, suitability, compliance
. I draw information from English sources and there it is called
fitness
, from
fit
.
 
For the correct functioning of our genetic algorithm, we need the so-called Fitness function. The function evaluates numerically to determine the suitability of a particular individual. In the real world, creatures simply survive or not. But in the case of the traditional genetic algorithm, where we are trying to work out the optimal solution to the problem, we should be able to give a numerical estimate.
 
In the case of our task, we must evaluate the correctness of words. For example, for cat, the most suitable option would be car:
 

 
2. Tinder (mating pool)
 

 
After we rated our population, it's time to start playing swagood luck.
 
Here you can use several different approaches. For example, we could use the so-called elitist method: "Which two members of the population scored the most points? You two will make all the children for the next generation."
 
This is one of the easiest approaches, but in this case we will lose diversity. If two members of the population (out of perhaps thousands) are the only ones available for reproduction, the next generation will have little variety, and this may slow down the evolutionary process. We could also take half the population, for example, the first 500 out of 1000. It is also very simple to do, but this approach will not give optimal results. In this case, elements with high indicators will have the same chance of being selected as a parent, as well as medium ones. And why does the monkey number 500 have the ability to play, and the number 501 is gone?
 
The best solution for our tinder is to use the probabilistic method, which we will call the “wheel of fortune” (also known as “roulette”). To illustrate this method, let's look at a simple example where we have a collection of five elements, each of which has an estimate:
 

 
Our roulette will look like this:
 

 
As we can see, B has the greatest chance. But even the smallest ones, such as C, are not without a chance at all. Often elements with a low usability rating have those necessary and missing necessary parts. For example, the phrase "to be or not to be":
 
A: to be or not to go
 
B: to be or not to pi
 
C: xxxxxxxxxxxxxxxxbe
 
A and B fit best, but C contains the necessary ending. Therefore, do not exclude such elements from the selection.
 
Absolutely any part of the genetic algorithm in which there is an estimate and probability can be implemented by different approaches. It should proceed from a specific task. In our case, you can do without the methods of Monte Carlo and other things more difficult to implement and understand.
 
Genetic algorithms - a reproduction of
 
The final part of the genetic algorithm that is responsible for obtaining the next generation of Vovanov. This generation will stop touching the air conditioner every 5 minutes. It also consists of two stages that should be distinguished:
 
1. Crossover
 
Crossbreeding involves the creation of a child from the genetic code of two parents. In the case of monkeys, let's assume that we selected two phrases from the mating pool.
 
Parent A: FORK
 
Parent B: PLAY
 
 
Now we have to make a child phrase out of these two. Perhaps the most obvious way (let's call it the 50/50 method) is to take the first two characters from A and the second two from B, leaving us with:
 

 
For a greater variety, we can entrust the choice of the middle of chance. In this case, we can get different child phrases from the same parents:
 

 
We can go further and randomly select the parent of each individual character:
 

 
But since this is not a mutation, the results are similar to the approach above. For us there is not much difference. As I said above, any actions requiring randomness can be implemented in different ways. In our case, this approach is enough.
 
2. Mutations 3-3-3458.
 
The last process before adding a new child to a new population is mutation. Mutation is an optional step, as in some cases it is not needed. But she is present in the real world and in Darwin's evolution. We need a mutation to introduce diversity along the evolutionary process. We created the initial population at random, making sure that we start with diverse members of the population. But along the evolutionary process, we will be limited by the diversity of the population, which may decrease with each new generation. At the end of the article, we will conduct an experiment with a different percentage of mutation and see if it is necessary.
 
Mutation is estimated as a percentage. Suppose we got FORY as a result of crossing, if the mutation percentage is 1%, then this means that each symbol in FORY has a 1% chance of being changed by another random symbol. This is a fairly small percentage and in 96% of cases the mutation will not occur. But if suddenly, then it will look like this:
 

 
A large percentage of mutation can lead to degradation of the following populations with respect to our understanding of 3-3-31291. suitability
, we will not be able to guarantee the "similarity" of children to their parents.
 
Let's summarize our algorithm and write it as a pseudo-code:
 
SETUP
:
 
Step 1
: Initialization, creating a random population of N elements. Each element has random DNA.
 
LOOP
:
 
Step 2
: Selection, evaluate each element of the population and form a list of potential parents.
 
Step 3
: Reproduction. Repeat N times:
 
 
Choose two parents
 
Cross
 
We mutate
 
Add to the new population
 
 
Step 4
. The new population replaces the current and repeat Step 2.
 
 
Implementation of the genetic algorithm
 
It's time to write an implementation of the genetic algorithm. Since we have a clear algorithm, its implementation will go in the same order.
 
Often, things like this use Python. We do not need specific libraries, so I'm using my usual jаvascript.
 
If for you JS is not the main language, I think it will not be difficult by analogy to write in any other. To start, we will use Node.js. There will be a note in the code about the adaptation mechanism, I added it while I was writing the article and we will discuss it below. Everything works fine without him :)
 
First, create the main DNA class:
 
//DNA.js
const random = require ('./random');
class DNA {
constructor (length, dna = null) {
//Main
this.length = length;
this.genes = dna? dna.genes: this.getRandomGenes (this.length);
//Additional
this.fitness = 0;
}
getRandomGenes (length) {
return new Array (length)
.fill (0)
.map (() => random.getRandomInt (3? 128));
}
//An adaptation mechanism that is beyond the scope of the above, we will discuss it below
updateLength (length) {
if (length <= this.genes) {
throw 'Error';
}
this.genes =[this.genes, this.getRandomGenes(length - this.genes.length)];
}
}
3r3r???r3r3r3r3r3r3r3r3r3r3r3r  
The DNA class accepts the length of the phrase that we want to get and the DNA set, in case it is a child.
 
getRandomInt
returns a random number between 32 and 128 inclusive. In fact, this is the English alphabet in ANSI code.
 
//random.js
function getRandomInt (min, max) {
min = Math.ceil (min);
max = Math.floor (max);
return Math.floor (Math.random () * (max - min)) + min; //Including
}
module.exports = {
random: Math.random,
getRandomInt
}
 
DNA.js stores characters in the genes field. This is a kind of genotype of our phrase.
 
The class of the population will be engaged in the main work. Creating a population, breeding, crossbreeding and mutation.
 
initPopulation () {
this.population = new Array (this.n)
.fill (0)
.map (() => new this.dna (this.target.length));
this.population.forEach ((dna) => dna.fitness = this.evaluateFitness (dna));
}
 
Initialization of the population begins by creating an empty array of size n, which we pass through the constructor. Then, for each element of the array, a population element with its random DNA is created. A little lower, immediately we are assessing the entire population for suitability.
 
evaluateFitness (dna) {
return (dna.genes.reduce ((acc, current, index) => {
if (current === this.target.codePointAt (index)) {
return acc + 1;
}
return acc;
}, 0) /this.target.length);
}
 
We run through the phrase, and knock out the arithmetic mean of the suitability of the phrase. this.target stored as a phenotype string. Of course, it is wiser to immediately store in the form of a genotype, but all for the sake of clarity of the genetic algorithm.
 

    getMatingPool () {
let matingPool =[];
this.population.forEach ((dna) => {
const n = parseInt (dna.fitness * 100);
matingPool =[matingPool, (new Array(n).fill(dna))];
});
return matingPool;
}

 

An excellent collective farm method of creating roulette based on the approach that we examined above. We take the percentage of suitability of a particular element, create the same number of its clones and put it in an array. Then, when we randomly select an element from the array, we will with the same probability choose this element.


 
    findRandomParent (matingPool) {
return matingPool[random.getRandomInt(0, matingPool.length)];
}

 

Cross two parents


 
    crossover (dna? dna2) {
const child = new this.dna (this.target.length);
const midpoint = random.getRandomInt (? dna1.genes.length);
for (let i = 0; i < dna1.genes.length; i++) {
if (i> midpoint) {
child.genes[i]= dna1.genes[i];
}
else {
child.genes[i].;
}
}
Return child;
}

 

Randomly choose the middle, we take part from the first parent, part from the second and the child is ready.


 


 

It remains only to carry out the mutation:


 
    mutation (dna) {
const mutatedDNA = new this.dna (this.target.length, dna);
//We go over all the characters of the phrase
for (let i = 0; i < dna.genes.length; i++) {
if (random.random () < this.mutationRate) {
//Write a new random character
mutatedDNA.genes[i]= random.getRandomInt (3? 128);
}
} 3r3rr
Return mutatedDNA;
}

 

Everything is very simple, we go over all the characters of the phrase and, in accordance with the percentage of mutation, replace or do not replace the symbol with a new random symbol.


 

Full code population class:


 
    //Population.js
const random = require ('./random');
class Population {
constructor (target, dna, n = 10? mutationRate = 0.0? adaptive = false, adaptiveThreshold = 0.? cutLength = 5) {
this.endTarget = target; //String
this.adaptive = adaptive;
this.adaptiveThreshold = adaptiveThreshold;
this.cutLength = cutLength;
this.target = this.adaptive? target.substr (? this.cutLength): target; //String
this.dna = dna;
this.n = n;
this.mutationRate = mutationRate;
this.population =[]
this.populationInfo = {
generationCount: 0
};
this.initPopulation ();
}
initPopulation () {
this.population = new Array (this.n)
.fill (0)
.map (() => new this.dna (this.target.length));
this.population.forEach ((dna) => dna.fitness = this.evaluateFitness (dna));
}
//An adaptation mechanism that is beyond the scope of the above, we will discuss it below
updateDNA (target) {
this.target = target;
this.population.forEach ((dna) => {
dna.updateLength (target.length);
});
}
getPopulationInfo () {
let statsFitness1 = 0;
return {
generationCount: this.populationInfo.generationCount,
averageFitness: this.population
.reduce ((acc, current) => {
if (this.target === String.fromCharCode.apply (this, current.genes)) {
statsFitness1 + = 1;
}
return acc + current.fitness
}, 0) /this.population.length,
statsFitness1
}
}
getMatingPool () {
let matingPool =[];
this.population.forEach ((dna) => {
const n = parseInt (dna.fitness * 100);
matingPool =[matingPool, (new Array(n).fill(dna))];
});
return matingPool;
}
evaluateFitness (dna) {
return (dna.genes.reduce ((acc, current, index) => {
if (current === this.target.codePointAt (index)) {
return acc + 1;
}
return acc;
}, 0) /this.target.length);
}
findRandomParent (matingPool) {
return matingPool[random.getRandomInt(0, matingPool.length)];
}
nextGeneration () {
let matingPool = this.getMatingPool ();
this.population.forEach ((_, index) => {
const parentA = this.findRandomParent (matingPool);
let parentB = null;
while (! parentB || (parentB === parentA) ) {
ParentB = this.findRandomParent (matingPool);
}
Let child = this.crossover (parentA, parentB);
Child = this.mutation (child); 3r332.fr = this.evaluateFitness (child)
this.population[index]= child;
});
this.populationInfo.generationCount + = 1;
//An adaptation mechanism that is beyond the scope of the above, we will discuss it below
if (this.adaptive && this.target! == this.endTarget) {
if (this.getPopulationInfo (). averageFitness> = this.adaptiveThreshold) {
this.updateDNA (this.endTarget.substr (? this.target.length + this.cutLength));
}
}
}
crossover (dna? dna2) {
const child = new this.dna (this.target.length);
//Picking a random “midpoint” in the genes array
const midpoint = random.getRandomInt (? dna1.genes.length);
for (let i = 0; i < dna1.genes.length; i++) {
//Before midpoint copy genes from one parent, after midpoint copy genes from the other parent
if (i> midpoint) {
child.genes[i]= dna1.genes[i];
}
Else {
Child.genes[i]= Dna2.genes[i];
}
}
mutatedDNA = new this.dna (this.target.length, dna);
//Looking at each gene in the array
for (let i = 0; i < dna.genes.length; i++) {
if (random.random () < this.mutationRate) {
//Mutation, a new random character
MutatedDNA.genes[i]= Random.getRandomInt (3? 128);
}
}
Return mutate dDNA;
}
}
module.exports = Population;

 

Before you start experimenting with our genetic algorithm, you need to acquire visualization. This is usually not the easiest task in machine learning, but in our case, everything is quite simple. Since this is Node.js and there is no particular desire to bother with WebGL, I had to look for something that could work with standard output - the console.


 

Fortunately, there is such a tool blessed-contrib which allows you to create dynamic dashboards.


 


 

We are interested in graphs, there will be two. One showing the general suitability of the colony, and the second the number of correct results.


 

A small wrapper for convenience, which we will use to display the [/b] graphs.
    //Log.js
const blessed = require ('blessed');
const contrib = require ('blessed-contrib');
class Log {
constructor (rows, cols) {
this.lines =[];
this.screen = blessed.screen ();
this.grid = new contrib.grid ({
rows,
cols,
screen: this.screen
});
this.screen.on ('resize', () => {
this.lines.forEach ((line) => {
line.emit ('attach');
})
});
this.screen.key (['escape', 'q', 'C-c'], function (ch, key) {
return process.exit (0);
});
}
addLine (x, label, row, col) {
const line = this.grid.set (row, col, ? ? contrib.line, {
label,
style: {
line: "yellow",
text: "green",
baseline : "black"
},
xLabelPadding: ?
xPadding: 5
});
this.screen.append (line);
line.setData ([{
x: x,
y: x.map(v => v * 100)
}]);
return this.lines.push (line) - 1;
}
updateLineData (index, x) {
this.lines[index]
.setData ([{
x,
y: x
}]
);
}
render () {
this.screen.render ();
}
}
module.exports = Log;


 

To get this data, I added a method to the population class that returns the generation number, average fitness level and the number of 100% matches.


 
    getPopulationInfo () {
let statsFitness1 = 0;
return {
generationCount: this.populationInfo.generationCount,
averageFitness: this.population
.reduce ((acc, current) => {
if (this.target === String.fromCharCode.apply (this, current.genes)) {
statsFitness1 + = 1;
}
return acc + current.fitness
}, 0) /this.population.length,
statsFitness1
}
}

 

Testing


 

And so, let's try our craft.


 
    //index.js
const DNA = require ('./DNA');
const Population = require ('./Population');
const Log = require ('./Log');
const contrib = require ('blessed-contrib');
//The code for the dashboard in the console is
const log = new Log (? 2);
const statsFitnessAverageIndex = log.addLine ([], 'Fitness', ? 0);
const statsFitness1Index = log.addLine ([], 'Fitness 1', ? 1);
const screenLog = log.grid.set (? ? ? ? contrib.log, {
label: 'logs'
});
const TARGET = 'Hello Habr!';
const GENERATIONS = 1000;
const POPULATION_NUMBER = 150;
const MUTATION_RATE = ???;
const population = new Population (TARGET, DNA, POPULATION_NUMBER, MUTATION_RATE);
//To collect statistics
const statsFitnessAverage =[];
const statsFitness1 =[];

 

The most interesting thing is the creation of a population, we pass the necessary parameters in advance by declaring them as constants.


 
    //index.js
function main () {
population.nextGeneration ();
const stats = population.getPopulationInfo ();
statsFitnessAverage.push (stats.averageFitness);
statsFitness1.push (stats.statsFitness1);
}

 

This function creates a new population and writes statistics to the array. Based on this array, we will build graphics.


 
    function update () {
log.updateLineData (statsFitnessAverageIndex, statsFitnessAverage);
log.updateLineData (statsFitness1Index, statsFitness1);
}
function sync () {
for (let i = 0; i < GENERATIONS; i++) {
main ();
}
update ();
log.render ();
}
screenLog.log (`$ {population.endTarget}: $ {population.target}`)

 

sync makes the required number of generations and displays a graph. The asynchronous option can come in handy when it takes too much time and you want to follow the process:


 
    function async () {
setInterval (() => {
main ();
update ();
log.render ();
}, 10)
}

 

The result of executing the code above:


 

 
Not the most successful graphics for the article, you somehow increase them in a separate tab.


 

As we can see, almost immediately we get a 90% increase in fitness, which persists for many generations and does not approach 100% due to the mutation process.
 
The Fintess score of 1 is the number of 100% of the matching elements in a population. There are some leaps from 20 to 75 per generation.


 


 

Let's try to increase the phrase. Hello Habr! the phrase is too small and it is very easy for the algorithm. To be or not to be :


 


 

A good result, but the truth the first run gave only one correct answer for 1000 generations. The longer the phrase, the more generations it may take to reach the right decision.


 

An increase in the number of generations does not particularly affect the result:


 


 



 

What if we try to increase the chance of mutation, from 0.1% to 1%?


 


 

With the same other parameters, the performance dropped from 80% to 31-33%. Generations mutate too much and this prevents the population from developing in the right direction.


 

Adaptation


 

One way to improve performance for longer text is to adapt. Honestly, I did not study it in terms of how it works in nature, but I went the logical way.


 

If shorter phrases give faster and better results, maybe it’s worth hitting longer phrases into smaller ones? As the percentage of suitability of a population grows, we will stress it with new data.


 

And by the way, it's called Adaptive Genetic Algorithms. Anyone stole the idea from me.


 


 

In the code that I wrote above, there is already the possibility of an adaptive approach. You just need to pass an additional 3 new parameters: adaptive - use the adaptive approach, adaptiveThreshold - the percentage of suitability for lengthening the phrase, cutLength - how many characters to add.


 

Double the phrase:


 
    const TARGET = 'To be or not to be To be or not to be';
const GENERATIONS = 1000;
const POPULATION_NUMBER = 150;
const MUTATION_RATE = ???;
const population = new Population (TARGET, DNA, POPULATION_NUMBER, MUTATION_RATE, true, 0.? 7);

 

 
The teeth are clearly visible on the graph, this is the moment when a new goal was set and the suitability of the population fell. In this case, for 1000 generations, we have not received the right solution. It remains only to play with the parameters, can set the genetic algorithm on the genetic algorithm to select the necessary parameters?
 
Conclusion 3-3-31248.
 
We examined the very basis of genetic algorithms using a very simple example. But even here there is much to improve. All those places where we do random number generation can be replaced with Monte Carlo methods that are also used to simulate various complex processes. Change the algorithm for choosing parents and the mechanism of crossing two different DNA. Make a floating percentage of
mortgage 3-3-31253. mutation, because sometimes it is useful, and sometimes it prevents to get a 100% result.
 
Genetic algorithms are able to solve more practical problems, optimization, search.
 
Generative Art:
 
 
(The example is better in the header, but here the point is in the evolutionary approach with the choice of the user. Alya is the logo of the dream)
 
Smart missiles example:
 

 
Ecosystem Evolution Simulation:
 
 
(squares - food)
 
And of course neural network training due to genetic algorithms.
 
We will consider such solutions in the next article. It seems that I got into my field of view as a language https://processing.org/ and the HYPE framework which creates a lot of interesting things. I will try to play with them.
 
Github turnip with source codes from article
 
https://towardsdatascience.com/artificial-neural-networks-optimization-using-genetic-algorithm-with-python-1fe8ed17733e
 
https://natureofcode.com/book/
 
https://en.wikipedia.org/wiki/Genetic_algorithm
 
https://chriscummins.cc/s/genetics/

It may be interesting

  • Comments
  • About article
  • Similar news
raymond weber 19 November 2020 12:38
Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a  pet sitter, which is very enjoyable, but I need to additional  expand. Regards.먹튀검증

raymond weber 21 November 2020 16:31
Whether it is popular games like Baccarat, Slots, Blackjack, Roulette and Dragon Tiger etc.สล็อตออนไลน์



An fascinating discussion is value comment. I think that it is best to write extra on this matter, it won’t be a taboo topic however generally people are not enough to talk on such topics. To the next. Cheerssagame66



I truly welcome this superb post that you have accommodated us. I guarantee this would be valuable for the vast majority of the general population.James River Capital blog

raymond weber 23 November 2020 15:50
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!okdermo.com

raymond weber 28 November 2020 14:52
I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...place value

raymond weber 14 December 2020 14:56
Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work.pretty gaming
raymond weber 16 December 2020 11:23
Your article is extremely helpful exceptionally fascinating subject i am looking that sort of post thank for imparting to us keep it up.CBD vapes

raymond weber 17 December 2020 15:24
I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up.buffet catering company Oxford
raymond weber 18 December 2020 11:36
PusatQQ has been operating since 2014 to provide services that are considered the best by card gambling lovers in Indonesia which finally received an award as the best online gambling site 2020.Situs Pkv Bandarqq Online PusatQQ
raymond weber 19 December 2020 18:44
My Sip Of Coffee is a coffee bean subscription that serves specialty coffee and donates to charity through Oak LifeCoffee bean subscription


Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it.widowed and young

raymond weber 20 December 2020 10:50
This Japanese technology has been sold worldwide since 1974. Pioneered in Okinawa, Enagic has received awards including The Water Quality Association (WQA) Gold Seal Certificate – a not-for-profit international trade association representing the residential, commercial and industrial water treatment industry.smart water alkaline

quinussu6uzueomcom

Author

5-05-2020, 00:01

Publication Date

Development / Machine learning

Category
  • Comments: 10
  • Views: 2 494
Genetic algorithm in Python for finding
MatLab /Octave machine learning:
Revolution in AI will not produce
Children to order in the near future?
Optimizing the placement of virtual
Machine learning algorithms
Write a comment
Name:*
E-Mail:


Comments
I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading. I had to go show it to my friend and he ejoyed it as well!seo toronto



Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. [Url = https: //mtsoul.net] 먹튀 검증 [/ url]

Today, 16:41

raymond weber

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.먹튀검증

Today, 15:58

raymond weber

You understand your projects stand out of the crowd. There is something unique about them. It seems to me all of them are brilliant.https://mtsoul.net

Today, 13:58

raymond weber

I wish more authors of this type of content would take the time you did to research and write so well. I am very impressed with your vision and insight.메이저사이트

Today, 13:47

raymond weber

Nice to be visiting  your blog once more, it has been months for me. Well this article that ive  been waited for therefore long. i want this article to finish my assignment  within the faculty, and it has same topic together with your article. Thanks,  nice share.  gogo  anime
Today, 12:00

Legend SEO

Adv
Website for web developers. New scripts, best ideas, programming tips. How to write a script for you here, we have a lot of information about various programming languages. You are a webmaster or a beginner programmer, it does not matter, useful articles will help to make your favorite business faster.

Login

Registration Forgot password