Selections
hgp_lib.selections.tournament_selection.TournamentSelection
Bases: BaseSelection
Selection strategy that holds tournaments among randomly sampled subsets of the population.
The population is effectively sorted by fitness. For each selection event,
tournament_size candidates are randomly sampled from the population.
The candidate with the best rank in the sample is selected with probability p.
If not selected, the second best is considered with probability p, and so on.
The selection probability for the i-th ranked individual in a tournament is:
P(i) = selection_p * (1 - selection_p)^i
This creates selection pressure that favors fitter individuals while maintaining
diversity. Higher tournament_size or selection_p increases selection pressure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tournament_size
|
int
|
The number of candidates to include in each tournament. Must be greater
than |
10
|
selection_p
|
float
|
The probability of selecting the best candidate in the tournament.
Must be between |
0.4
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> from hgp_lib.selections import TournamentSelection
>>> from hgp_lib.rules import Literal
>>> np.random.seed(42)
>>> selection = TournamentSelection(tournament_size=3, selection_p=0.5)
>>> rules = [Literal(value=i) for i in range(5)]
>>> scores = [0.1, 0.9, 0.5, 0.3, 0.7]
>>> selected_rules, selected_scores = selection.select(rules, scores, 3)
>>> len(selected_rules)
3
Source code in hgp_lib\selections\tournament_selection.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
select(rules, scores, n_select)
Selects n_select rules using tournament selection.
The method performs n_select independent tournaments. In each tournament:
1. tournament_size indices are sampled randomly from the population (without replacement).
2. These indices are sorted by the fitness of the corresponding rules (best to worst).
3. A winner is chosen based on the pre-calculated geometric probabilities.
The caller must ensure that len(rules) ==len(scores)andn_select >= self.tournament_size`.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
Sequence[Rule]
|
The collection of candidate rules to select from. Length must be
greater than or equal to |
required |
scores
|
ndarray | Sequence[float]
|
Fitness scores corresponding to each rule. Higher scores indicate better
fitness. Must have the same length as |
required |
n_select
|
int
|
Number of rules to select. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[Rule], ndarray]
|
Tuple[List[Rule], ndarray]: A tuple containing: - List[Rule]: Copies of the selected rules. The same rule may appear multiple times. - ndarray: The fitness scores of the selected rules. |
Examples:
>>> import random
>>> import numpy as np
>>> from hgp_lib.selections import TournamentSelection
>>> from hgp_lib.rules import Literal
>>> random.seed(42); np.random.seed(42)
>>> selection = TournamentSelection(tournament_size=3, selection_p=0.5)
>>> rules = [
... Literal(value=0),
... Literal(value=1),
... Literal(value=2),
... ]
>>> scores = [0.2, 0.8, 0.5]
>>> selected_rules, selected_scores = selection.select(rules, scores, 2)
>>> len(selected_rules)
2
>>> all(isinstance(rule, Rule) for rule in selected_rules)
True
Source code in hgp_lib\selections\tournament_selection.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
hgp_lib.selections.roulette_selection.RouletteSelection
Bases: BaseSelection
Fitness-proportionate selection using roulette wheel sampling.
Each rule's probability of being selected is proportional to its fitness score. Higher scores receive proportionally higher selection probabilities. Negative scores are handled by shifting all scores to be non-negative before computing probabilities. Selection is performed with replacement, so the same rule can appear multiple times in the result.
Examples:
>>> import random
>>> import numpy as np
>>> from hgp_lib.selections import RouletteSelection
>>> from hgp_lib.rules import Literal
>>> random.seed(42); np.random.seed(42)
>>> selection = RouletteSelection()
>>> rules = [
... Literal(value=0),
... Literal(value=1),
... Literal(value=2),
... ]
>>> scores = [0.1, 0.5, 0.4]
>>> selected_rules, selected_scores = selection.select(rules, scores, 2)
>>> len(selected_rules)
2
Source code in hgp_lib\selections\roulette_selection.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
select(rules, scores, n_select)
Selects n_select rules using roulette wheel (fitness-proportionate) selection.
The probability of selecting each rule is proportional to its fitness score. If any scores are negative, all scores are shifted to be non-negative before computing probabilities. When all scores are equal, selection is uniform. Selection is performed with replacement, so the same rule may appear multiple times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
Sequence[Rule]
|
The collection of candidate rules to select from. |
required |
scores
|
ndarray | Sequence[float]
|
Fitness scores corresponding to each rule. Higher scores indicate better
fitness. Must have the same length as |
required |
n_select
|
int
|
Number of rules to select. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[Rule], ndarray]
|
Tuple[List[Rule], ndarray]: A tuple containing: - List[Rule]: Copies of the selected rules. The same rule may appear multiple times. - ndarray: The fitness scores of the selected rules. |
Examples:
>>> import random
>>> import numpy as np
>>> from hgp_lib.selections import RouletteSelection
>>> from hgp_lib.rules import Literal
>>> random.seed(42); np.random.seed(42)
>>> selection = RouletteSelection()
>>> rules = [
... Literal(value=0),
... Literal(value=1),
... Literal(value=2),
... ]
>>> scores = [0.1, 0.5, 0.4]
>>> selected_rules, selected_scores = selection.select(rules, scores, 2)
>>> len(selected_rules)
2
>>> all(isinstance(rule, Rule) for rule in selected_rules)
True
Source code in hgp_lib\selections\roulette_selection.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |