1. Problem definition:
Ask a array. The values in these arrays have their own weights. How can design to efficiently take priority to pick up the weight? Intersection
For example:
Copy code code as follows:
Weight: 8 2 11 79
The value returned by weight: 0 1 2 3
2. Analysis of the problem:
Idea 1: Create an array of the size and weight of the size and weight. If the weight of the value 0 is 8, put 8 0 values, and the weight of the value 1 is 2, then 2 1 value is placed, and the category is pushed in order.
Then use a random number of weight and size to generate random numbers. Disadvantages should occupy too much memory.
Idea 2:
Weight and array w [i] store the weight and time complexity of all elements of [0, i].
Random [0, w [399]]] See which time the random number is falling in which time complexity O (longn)
So the total time complexity complexity o (n) spatial complexity o (n)
pseudocode:
Rotating gambling is not a particularly good option, but it is easy to achieve.
First of all, it is necessary to understand that because of cross -and -mutation and other operators, the direction of evolution cannot be controlled, so the heavy responsibility of evolution is on the selection operator.
If you understand this, it is easy to do.
Rotating gambling is to accumulate probability to achieve it. Usually, the probability of being selected is generally high.
If: FIT is the adaptation of the array, a total M copy code code is as follows:
for i = 1 to m '
SUM = SUM+Fit (i)
Next I
For i = 1 to n 'n- How many individuals do you want to generate?
TEMP = TEMP + FIT (i)
If RND <= Temp / S
Output i is the result
Exit function
End if
Next I
Third, solve the problem:
Copy code code as follows:
package datastruct;
Import java.util.hashmap;
Import java.util.map;
/**
Weight random number:
If weight: 8 2 11 79
The value returned by weight: 0 1 2 3
@author ajian005 [email protected]
2014-2-16 21:12
Output results: {2.0 = 184128, 11.0 = 348551, 79.0 = 1308100, 8.0 = 159221}}
*/
public class weightrandomtest {
Private Static Double [] WeightArrays = {8.0, 2.0, 11.0,79.0}; // The bidding of the array is the value to be returned, the array value is the weight of the undercut the target under the array.
Public Static Void Main (String [] args) {{
Weightrandom weightrandom = new weightrandom ();
MAP <Double, Integer> Stat = New HashMap <Double, Integer> ();
for (int i = 0; i <2000000; i ++) {
int weightValue = Weightrandom.getweightrandom (weightarrays);
if (weightvalue <0) {{
continue;
}
System.out.println ("Random number returned by weight:" + weightValue);
if (Stat.get (WeightArrays [WeightValue]) == NULL) {
Stat.put (weightArrays [weightValue], 1);
} Else {
stat.put (weightArrays [weightValue], stat.get (weightArrays [weightValue])+1);
}
}
System.out.println (STAT);
}
}
class weightrandom {
java.util.random r = new java.util.random ();
Private Double WeightArraySum (Double [] WeightArrays) {{
double weightsum = 0;
For (double weightValue: weightArrays) {
Weightsum += WeightValue;
}
Return weightsum;
}
Public int Getweightrandom (double [] weightarrays) {{
double weightsum = weightArraySum (weightarrays);
double stepweightsum = 0;
for (int i = 0; I <weightarrays.length; i ++) {
Stepweightsum += WeightArrays [i];
if (math.random () <= Stepweightsum/Weightsum) {
//System.out.println (i);
Return i;
}
}
System.out.println ("Error");
Return -1;
}
}
Fourth, summary summary:
Russian round gambling is to accumulate probability to achieve
Dependent load scheduling, etc.