Original: http://fatelei.github.io/2015/09/08/random number according to assigned weights
Description: Usually take random numbers, take the probability of each number is the same, such as taking n times, the probability of taking a number is 1/n. Now that the situation has changed, to randomly take the number, each number is set a weight (weight), such as:
The meaning of the above chart is: Random fetch, take 1 probability is 1/5, take 2 probability is 2/5, the probability of taking 3 is 2/5. It is now required that you use the code to complete the process of taking values according to different weights.
The first way to think about this is:
Using an array, the subscript of the array corresponds to the range of the weight, the value of the subscript value
is the required value, such as the above example, for example, in the range of 1 < x <= 3, and then a random value for the entire interval length, rand (1, 5) will get a subscript value, Then, according to this subscript value to get the desired value. The problem with this solution is that if a value has a large weight, then a large array is needed, which is very, very memory-intensive, and obviously not a good workaround. But there was no better way of thinking about it.
The evening came back to think about that there is no way to optimize the direction
1 2 3 |
p (x = 1) = 1/5 (x <= Span class= "number" >1) p (x = 2) = 2/5 (1 < x <= 3) Span class= "line" >p (x = 3) = 2/5 (3 < x <= 5) |
Convert to cumulative probability:
1 2 3 |
cp (x = 1) = 1/5 (x <= Span class= "number" >1) cp (x = 2) = 3/5 (1 < x <= 3) cp (x = 3) = 1 (3 < x <= 5) |
With this mapping key above, at 0-1 randomly taking a number x, if x <= cp[i], I is the desired result:
Python implementation
1 2 3 4 5 6 7 |
Import random def Span class= "title" >random_by_weight For V, w in zip (values, CP): Rand = Random.randint (0 , 1) if rand <= w: return v |
Selected from: http://blog.csdn.net/v_july_v/article/details/6132775
3.1. Select operator
The genetic algorithm uses the choice operation to perform the fittest operation to the individual.
Highly adaptable individuals are more likely to be genetically inherited into next-generation groups, and individuals with low fitness are less likely to be genetically inherited into next-generation groups.
The task of selecting an operation is to select individuals from the parent group and to inherit from the next Generation group.
The selection operator in the basic genetic Algorithm (SGA) adopts the roulette choice method.
Ok, here's a look at this example of roulette, this example is easy to understand, it is very helpful for understanding the selection operator.
Roulette Selection method
Roulette choice is also called proportional selection operator, the basic idea is:
The probability of each individual being selected is proportional to the size of its fitness function value.
If the population size is n and the adaptive degree of individual XI is F (xi), the choice probability of individual XI is:
The roulette selection method can be implemented using the following process simulations:
(1) in [0, 1] produces a uniformly distributed random number R.
(2) If r≤q1, the chromosome X1 is selected.
(3) if Qk-1<r≤qk (2≤k≤n), the chromosome xk is selected.
The qi is called the accumulation probability of chromosome XI (i=1, 2, ..., n), which is calculated as:
Accumulation Probability Example:
How to implement the roulette selection method:
(1) Calculating the fitness value of all the individuals in the population;
(2) Calculating the choice probability of each individual;
(3) calculating accumulation probability;
(4) The use of simulated betting operations (i.e. the generation of random numbers between 0 and 1 and the probability of each individual being inherited to the next generation group)
To determine whether individual individuals are genetically inherited into the next generation group.
For example, there are chromosomes
s1= 13 (01101)
S2= 24 (11000)
s3= 8 (01000)
s4= 19 (10011)
Assuming that the degree of fitness is f (s) =s^2, the
F (S1) = f (13) = 13^2 = 169
F (s2) = f (24) = 24^2 = 576
F (S3) = f (8) = 8^2 = 64
F (S4) = f (19) = 19^2 = 361
The probability of chromosome selection is:
The cumulative probability of a chromosome is:
According to the above formula, you can get:
For example, a set of 4 random numbers from the interval [0, 1] is generated:
R1 = 0.450126, r2 = 0.110347
R3 = 0.572496, R4 = 0.98503
To calculate random numbers according to the specified weights