Machine learning JavaScript:: Introduction to genetic algorithms

Source: Internet
Author: User
Tags first string

Burak Kanber

Translation: Wang Weiqiang

Original: http://burakkanber.com/blog/machine-learning-in-other-languages-introduction/

The genetic algorithm should be the last of the machine learning algorithms I came into contact with, but I like to use it as a starting point for this series of articles, because this algorithm is very suitable for introducing "value function" or "error function", as well as local and global optimal concepts, both of which are important concepts in machine learning.

The invention of genetic algorithm is inspired by nature and evolution, which is very cool for me. It is not surprising that even the artificial neural network (NN) has evolved from biology, and that evolution is the best universal learning algorithm we have ever known, and we all know that the human brain is the best tool for solving common problems. Two of the fastest growing fields in artificial intelligence and machine learning, as well as the two most important parts of our biology, are the genetic algorithms and neural networks I'm interested in, and now I'm concentrating them together.

The term "generic" I used earlier is extremely important, and for most special computing problems you might find a more efficient solution than a genetic algorithm, but the key point is not in the implementation, nor in the genetic algorithm. The use of genetic algorithms is not when you encounter complex problems, but the complexity of the problem has become a problem, or you have some completely irrelevant parameters to face.

A typical application is the biped robot walking problem. It is very difficult for a robot to walk on two feet, and a hard-coded walk program is almost impossible to succeed, even if you can really make the robot walk, the balance of the next robot may be slightly different, and your program will not work. You can choose to use genetic algorithms to teach robots how to learn to walk, rather than just teach robots to walk.

We're going to use JavaScript to build a genetic algorithm.

Problem

Write an algorithm with JavaScript to breed a text "Hello, world!."

For programmers, "Hello, world!." Almost the beginning of everything, we use genetic algorithms to reproduce this text is also considered entirely justified. Note that this problem has a high level of human participation, of course, we can directly in the source code to print out "Hello, world!." But this looks silly, since already knew the result, still want this algorithm to do? The answer is simple, it's just a learning exercise, and the next genetic algorithm (using PHP) will reduce manual traces, but we always have to start.

Fundamentals of Genetic algorithms

The basic purpose of the algorithm is to generate a string of "alternative answers" and use a series of feedback to know how far these alternatives are from the optimal solution. The furthest away from the optimal scheme is eliminated, leaving the optimal scheme close to the other alternatives and making a slight mutation, modifying the alternatives and constantly checking the distance from the optimal solution.

These "alternative answers" are called chromosomes.

Chromosomes bind to produce offspring and mutate, the fittest, the fittest, and the offspring they produce may have more characteristics to adapt to natural selection.

For the solution "Hello, world!" Such a question, is it not so weird? Don't worry, genetic algorithms are never just good at solving such problems.

Chromosome

The chromosome is an alternative expression, in our case the chromosome itself is a character, and we set all the chromosomes to be strings of length 13 (Hello, world! The length is 13). Some of the chromosomes that match the alternatives are listed below:

    • gekmo+ xosmd!
    • GEKLN, Worle "
    • Fello, wosld!
    • Gello, wprld!
    • Hello, world!.

Obviously, the last one is the "right" (or globally optimal) chromosome, but how do we measure the good chromosomes?

Value function

Value function (or Error function) is a method of measuring the degree of chromosome excellence, if we call them "fitness function", then the higher the score the better, if we use "value function", of course, the lower the better the score.

In this case, we need to define the value function as a rule:

for each byte of the string, indicate the difference between the alternatives and the target on the ASCII code, and then take the squared of the difference to ensure that the value is positive.

Example: If we have a character "a" (ASCII 65), but the expected character should be "C" (ASCII 67), then the value calculation will result in 4 (67-65 = 2, 2^2 = 4).

The square is to ensure that the value is positive, you can of course take absolute values. To deepen your learning, use different methods in your actual operation.

Using this rule, we can calculate the value of 5 chromosomes:

    • gekmo+ xosmd! (7)
    • GEKLN, Worle "(5)
    • Fello, wosld! (5)
    • Gello, wprld! (2)
    • Hello, world!. (0)

In this case, the method is simple and the manual traces are obvious, and obviously our goal is to make the cost zero, and once it is zero, the program can stop. Sometimes this is not the case, for example, when you are looking for the minimum cost, you need to end the calculation in different ways, and conversely, if you are looking for the highest adaptive score, you may need to use other conditions to stop the calculation.

The value function is a very important part of the genetic algorithm, because if you are smart enough you can use it to reconcile completely irrelevant parameters. In this case, we are only interested in characters. But if you are building a driving navigation application, you need to weigh tolls, distance, speed, traffic lights, bad neighborhood cars and bridges, and so on, encapsulate these completely irrelevant parameters into a uniform, graceful, neat value function, and finally get path information based on the weights of the different parameters.

Mating and Death

Mating is a norm in life and we use it extensively in genetic algorithms. Mating is definitely a magical moment, and the two chromosomes fall in love to share information about each other. The technical aspect of mating is "crossover", but I would like to call it "mating" because it makes the picture more intuitive.

So far we haven't talked about the concept of "population" in genetic algorithms, but I'm sure that as long as you run a genetic algorithm, you can see more than just one chromosome at a time. You may have 20,100 or 5,000 chromosomes at the same time, just like evolution, you might prefer to have the strongest chromosomes mate with each other, hoping that the offspring will be better than their parents.

In fact, it is very simple to mate the characters, like our example "hello,world!", choose two optional strings (chromosomes), each from the middle truncated into two fragments, here you can use any method, if you want to even choose random points to intercept. Let's pick the middle position and then synthesize a new chromosome (string) with the first half of the string and the second half of the string. Continue to combine the first half of the second string with the latter part of the first string into another new chromosome (string) in the same way.

Take the following two strings as an example:

    • Hello, wprld!. (1)
    • Iello, world! (1)
disconnect from the middle by merging to get two new strings, which is two new children:
    • Iello, wprld! (2)
    • Hello, world!. (0)

As seen above, among the two offspring, one contains the best traits of the parents, perfect and the other is very bad.

Mating is the process of passing a gene from the parent to the offspring.

Mutation

Mating alone can produce a problem: inbreeding. If you just get the candidates to mate for a generation, you'll get to a "local optimal" position and stuck out there, and the answer looks good, but it's not the "global best" you want.

Think of the world of genetic life as a physical setting, with undulating peaks and valleys, a valley that is the lowest in the world, and many other smaller valleys, where the genes are surrounded by these smaller valleys and, as a whole, above sea level. To find a solution, like rolling some balls from a random location at the top of the mountain, it is clear that the balls will be stuck at some low, and many of them will be stuck by tiny depressions (partial optimality) on the hill. Your job is to ensure that at least one ball reaches the lowest point in the world: global Best. Since the ball is rolled from a random position, it is difficult to control the process from the beginning, and it is almost impossible to predict which ball will be stuck. But what you can do is pick some balls at random and give them a kick, maybe this one will help them roll to the lower, and the idea is to shake the system a little bit so that the balls don't stay in the local optimal place for too long.

This is the mutation, which is a random change in the number of characters that are randomly selected by a mysterious unknown gene that you have chosen to produce a certain percentage.

As the following example shows, you stop at these two chromosomes.

    • HFLLP, worlb!
    • HFLLP, worlb!

Yes, it's a man-made case, but it's really going to happen. Your two chromosomes are exactly the same, which means that their offspring and their parents are identical, and nothing has been made. But if one of the 100 chromosomes has a mutation on a certain byte, as shown above, the second chromosome only has a mutation, from "HFLLP, worlb!" to "IFLLP, worlb!". Then evolution will continue, because the offspring and the parents are again the difference between the mutation to promote the evolution of the forward.

When and how to mutate is entirely up to you.

Again, we started the experiment, and the code I provided later would have a mutation probability of up to 50%, but it was just for demonstration purposes. You can make it less likely to mutate, like 1%. My code is to let the characters move 1 on the ASCII code, you can have your own more aggressive settings. Experiment, test, learn, that's the only way.

Chromosome: summary

Chromosomes represent your alternative to solving problems, they consist of the expression itself (in our case, a string of length 13), a value or adaptive score, and its function, mating and mutation ability.

I like to think of these things in the concept of OOP, where the class of chromosomes can be defined as follows:

Property:

    • Genetic code
    • Cost/fitness Score

Method:

    • Mate
    • Mutate
    • Calculate Fitness Score

We are now considering how to interact with genes in the last mystery of the genetic algorithm-the population.

Population

A population is a group of chromosomes, which usually maintain the same size, but will evolve over time to a more cost-balanced state.

You need to choose the size of the population, I choose 20. You can make any choice, 10,100 or 1000, as you wish. Of course there are advantages and disadvantages, as I have mentioned several times, experiments and explore on their own!

The population cannot be separated from the "generation", a typical generation may contain:

    • Calculate the cost/adaptability score for each chromosome
    • Sorting chromosomes by cost/adaptability score
    • Eliminate a certain number of weak chromosomes
    • To mate a certain number of the strongest chromosomes.
    • Random mutation Some Members
    • Some kind of integrity test, such as: How do you know that the problem has ended?
Start and end

It is very simple to create a population so that randomly generated chromosomes fill the entire population. In our case, the cost score for a completely random string would be scary, so I started with an average score of 30000 in my code. The sheer number is not a problem, that is the purpose of evolution, and that is why we are here.

Knowing how to stop population multiplication requires a little bit of skill, and the current example is simple and stops when the value score is 0 o'clock. But it doesn't always work, and sometimes you don't even know what the minimum value is, and if you replace it with adaptability, you probably don't know what the maximum value might be.

In these cases, you should specify a complete standard that can be anything you want, but it is recommended to use the following logical hop-off algorithm

If after 1000 generations, the best value has not changed, it can be said that the value is the answer, it is time to stop the calculation.

This criterion may mean that you will never get the global optimal solution, but in many cases you do not need the global optimal solution at all, close enough.

Code

I still like the OOP approach, and of course I like the rough and simple code. I try to use simple, straightforward strategies whenever possible, even if they are rough in some places.

(Note: Even if I change the gene to a chromosome in the above, the code uses the gene as a term, but there are some semantic differences.) )

var Gene = function (code) {          if (code)                This.code = code;        This.cost = 9999;}; Gene.prototype.code = ";  Gene.prototype.random = function (length) {while          (length--) {                This.code + = String.fromCharCode (Math.floor ( Math.random () *255));}        ;

Very simply, the constructor of the class takes a string as a parameter, sets a value, and an auxiliary function is used to generate a new random chromosome.

Gene.prototype.calcCost = function (compareTo) {          var total = 0;        for (i = 0; i < this.code.length; i++) {Total                + = (this.code.charCodeAt (i)-compareto.charcodeat (i)) * (This.code. charCodeAt (i)-compareto.charcodeat (i));        This.cost = total;};

The value function takes the "model"--the string as a parameter, and its own string in ASCII encoding, and then takes its square value.

Gene.prototype.mate = function (gene) {          var pivot = math.round (THIS.CODE.LENGTH/2)-1;        var child1 = this.code.substr (0, Pivot) + GENE.CODE.SUBSTR (pivot);        var child2 = gene.code.substr (0, Pivot) + THIS.CODE.SUBSTR (pivot);        return [new Gene (CHILD1), new Gene (CHILD2)];};

The mating function takes a chromosome as the parameter, finds the middle point, and returns two new fragments as an array.

Gene.prototype.mutate = function (chance) {          if (Math.random () > Chance)                return;        var index = Math.floor (Math.random () *this.code.length);        var upordown = Math.random ()

The mutation function takes a floating-point value as a parameter, which represents the mutation probability of the chromosome.

var Population = function (goal, size) {  this.members = []; This.goal = goal;
This.generationnumber = 0; while (size--) { var gene = new gene (); Gene.random (this.goal.length); This.members.push (gene);} };

The constructors in the population class use the target string and population size as parameters, and then build the population with randomly generated chromosomes.

Population.prototype.sort = function () {  this.members.sort (function (A, b) {return a.cost-b.cost;    });

Define a Population.prototype.sort method as an auxiliary function to sort the population according to their cost score.

population.prototype.generation = function () {for (var i = 0; i < This.members.length;            i++) {this.members[i].calccost (this.goal);        } this.sort ();        This.display ();        var children = this.members[0].mate (this.members[1]);        This.members.splice (This.members.length-2, 2, children[0], children[1]);                for (var i = 0; i < this.members.length; i++) {this.members[i].mutate (0.5);                This.members[i].calccost (This.goal);                        if (This.members[i].code = = this.goal) {this.sort ();                        This.display ();                return true;        }} this.generationnumber++;        var scope = this; SetTimeout (function () {scope.generation ();});}; 

The production method of the population is the heaviest part, in fact there is no magic. The display () method simply renders the result on the page, and I set the intergenerational interval so that it doesn't explode.

Note that in this case I just let the top two chromosomes mate, and as for how to deal with it in your own practice, you can do a lot of different things.

Window.onload = function () {          var population  = new population ("Hello, world!", +);        Population.generation ();};

Let's look at the example:

Http://jsfiddle.net/bkanber/BBxc6/?utm_source=website&utm_medium=embed&utm_campaign=BBxc6

Machine learning JavaScript:: Introduction to genetic algorithms

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.