| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.dishevelled.evolve.select; |
| 25 | |
|
| 26 | |
import java.util.Map; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.Comparator; |
| 30 | |
import java.util.Collection; |
| 31 | |
import java.util.Collections; |
| 32 | |
|
| 33 | |
import org.dishevelled.weighted.WeightedMap; |
| 34 | |
import org.dishevelled.weighted.HashWeightedMap; |
| 35 | |
|
| 36 | |
import org.dishevelled.evolve.Selection; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public final class ElitistSelection<I> |
| 45 | |
implements Selection<I> |
| 46 | |
{ |
| 47 | |
|
| 48 | |
private final int individuals; |
| 49 | |
|
| 50 | |
|
| 51 | |
private static final float DEFAULT_LOAD_FACTOR = 0.75f; |
| 52 | |
|
| 53 | |
|
| 54 | 14 | private final WeightedMapEntryComparator weightedMapEntryComparator = new WeightedMapEntryComparator(); |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public ElitistSelection(final int individuals) |
| 65 | 14 | { |
| 66 | 14 | if (individuals < 0) |
| 67 | |
{ |
| 68 | 2 | throw new IllegalArgumentException("individuals must be greater than or equal to zero"); |
| 69 | |
} |
| 70 | 12 | this.individuals = individuals; |
| 71 | 12 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public int getIndividuals() |
| 80 | |
{ |
| 81 | 1 | return individuals; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
public Collection<I> select(final Collection<I> population, |
| 86 | |
final WeightedMap<I> scores) |
| 87 | |
{ |
| 88 | 6 | if (scores.totalWeight() == 0.0d) |
| 89 | |
{ |
| 90 | 1 | throw new IllegalStateException("scores total weight must be greater than zero"); |
| 91 | |
} |
| 92 | 5 | int size = population.size(); |
| 93 | 5 | List<I> selected = new ArrayList<I>(size); |
| 94 | |
|
| 95 | |
|
| 96 | 5 | List<Map.Entry<I, Double>> sortedKeys = new ArrayList<Map.Entry<I, Double>>(scores.entrySet()); |
| 97 | 5 | Collections.sort(sortedKeys, weightedMapEntryComparator); |
| 98 | |
|
| 99 | |
|
| 100 | 5 | List<Map.Entry<I, Double>> eliteKeys = sortedKeys.subList(0, Math.min(individuals, size)); |
| 101 | |
|
| 102 | |
|
| 103 | 5 | WeightedMap<I> intermediate = new HashWeightedMap<I>(individuals, DEFAULT_LOAD_FACTOR); |
| 104 | 5 | for (Map.Entry<I, Double> e : eliteKeys) |
| 105 | |
{ |
| 106 | 105 | intermediate.put(e.getKey(), Double.valueOf(0.0d)); |
| 107 | 105 | } |
| 108 | |
|
| 109 | |
|
| 110 | 5 | for (I i : intermediate.keySet()) |
| 111 | |
{ |
| 112 | 105 | intermediate.put(i, Double.valueOf(1.0d / individuals)); |
| 113 | 105 | } |
| 114 | |
|
| 115 | |
|
| 116 | 308 | for (int i = 0; i < size; i++) |
| 117 | |
{ |
| 118 | 303 | selected.add(intermediate.sample()); |
| 119 | |
} |
| 120 | |
|
| 121 | 5 | sortedKeys = null; |
| 122 | 5 | eliteKeys = null; |
| 123 | 5 | intermediate = null; |
| 124 | 5 | return selected; |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | 1340 | private class WeightedMapEntryComparator |
| 132 | |
implements Comparator<Map.Entry<I, Double>> |
| 133 | |
{ |
| 134 | |
|
| 135 | |
|
| 136 | |
public int compare(final Map.Entry<I, Double> e1, final Map.Entry<I, Double> e2) |
| 137 | |
{ |
| 138 | |
|
| 139 | 1312 | return e2.getValue().compareTo(e1.getValue()); |
| 140 | |
} |
| 141 | |
} |
| 142 | |
} |