Populations
hgp_lib.populations.generator.PopulationGenerator
Generates a population of rules using one or more strategies with weighted probability.
Attributes:
| Name | Type | Description |
|---|---|---|
strategies |
Sequence[PopulationStrategy]
|
The list of strategies to use. |
population_size |
int
|
The total number of rules to generate. Default: |
weights |
Sequence[float] | ndarray | None
|
Weights for random selection of strategies.
If |
Examples:
>>> from hgp_lib.populations import PopulationGenerator, RandomStrategy
>>> strategy = RandomStrategy(num_literals=5)
>>> generator = PopulationGenerator(strategies=[strategy], population_size=10)
>>> population = generator.generate()
>>> len(population)
10
Source code in hgp_lib\populations\generator.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 | |
__init__(strategies, population_size=100, weights=None)
Initialize the PopulationGenerator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategies
|
Sequence[PopulationStrategy]
|
A non-empty sequence of PopulationStrategy instances. |
required |
population_size
|
int
|
The number of rules to generate. Must be greater than |
100
|
weights
|
Sequence[float] | ndarray | None
|
Optional weights for each strategy.
Must sum to > |
None
|
Source code in hgp_lib\populations\generator.py
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 | |
generate()
Generates the full population of rules.
Returns:
| Type | Description |
|---|---|
List[Rule]
|
List[Rule]: A list containing |
Source code in hgp_lib\populations\generator.py
86 87 88 89 90 91 92 93 94 95 96 97 | |
hgp_lib.populations.populations_factory.PopulationGeneratorFactory
Factory for creating PopulationGenerator instances.
Stores configuration-time parameters (population_size) and defers
data-dependent construction to create. Override create_strategies
to customise which strategies are instantiated.
Attributes:
| Name | Type | Description |
|---|---|---|
population_size |
int
|
Number of rules the generator will produce.
Default: |
Examples:
>>> from hgp_lib.populations import PopulationGeneratorFactory
>>> factory = PopulationGeneratorFactory(population_size=50)
>>> factory.population_size
50
Subclass to use custom strategies:
>>> import numpy as np
>>> from hgp_lib.populations import PopulationGeneratorFactory, BestLiteralStrategy
>>> class MyFactory(PopulationGeneratorFactory):
... def create_strategies(self, num_literals, score_fn, train_data, train_labels):
... return [BestLiteralStrategy(
... num_literals=num_literals, score_fn=score_fn,
... train_data=train_data, train_labels=train_labels,
... )]
>>> factory = MyFactory(population_size=20)
>>> data = np.array([[True, False], [False, True]])
>>> labels = np.array([1, 0])
>>> def acc(p, l): return float((p == l).mean())
>>> gen = factory.create(2, acc, data, labels)
>>> len(gen.generate())
20
Source code in hgp_lib\populations\populations_factory.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 | |
create_strategies(num_literals, score_fn, train_data, train_labels)
Create the list of strategies for the generator.
Override this method to use custom strategies. The default creates
a single RandomStrategy(num_literals=num_literals).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_literals
|
int
|
Number of boolean features (columns in train_data). |
required |
score_fn
|
Callable
|
Fitness function |
required |
train_data
|
ndarray
|
Training data (2-D boolean array). |
required |
train_labels
|
ndarray
|
Training labels (1-D array). |
required |
Returns:
| Type | Description |
|---|---|
List[PopulationStrategy]
|
List[PopulationStrategy]: Strategies to pass to |
Source code in hgp_lib\populations\populations_factory.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
create(num_literals, score_fn, train_data, train_labels)
Create a PopulationGenerator with data-dependent strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_literals
|
int
|
Number of boolean features (columns in train_data). |
required |
score_fn
|
Callable
|
Fitness function |
required |
train_data
|
ndarray
|
Training data (2-D boolean array). |
required |
train_labels
|
ndarray
|
Training labels (1-D array). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PopulationGenerator |
PopulationGenerator
|
A generator ready to produce the initial population. |
Source code in hgp_lib\populations\populations_factory.py
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 | |
Sampling Strategies
hgp_lib.populations.sampling.FeatureSamplingStrategy
Bases: SamplingStrategy
Samples a subset of features from the training data.
Each child population receives a subset of the parent's feature columns.
The number of features per child is ceil(num_features * feature_fraction).
Overlap behavior (controlled by replace parameter):
- replace=False: No overlap between children (partitioning) — each feature
appears in at most one child population.
- replace=True: Overlap allowed — features can appear in multiple children.
When feature_fraction=1.0, all children receive all features regardless of
replace.
Within each child, features are always unique (no duplicates within a single child).
Attributes:
| Name | Type | Description |
|---|---|---|
feature_fraction |
float
|
Fraction of features per child. Default: |
replace |
bool
|
Allow feature overlap between children. Default: |
Examples:
>>> import numpy as np
>>> np.random.seed(42)
>>> strategy = FeatureSamplingStrategy(feature_fraction=0.5)
>>> data = np.random.rand(100, 10) > 0.5
>>> labels = np.random.randint(0, 2, 100)
>>> results = strategy.sample(data, labels, num_children=3)
>>> len(results)
3
>>> len(results[0].feature_indices)
5
Source code in hgp_lib\populations\sampling.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
sample(data, labels, num_children)
Sample features for child populations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Training data as 2D boolean array (instances x features). |
required |
labels
|
ndarray
|
Training labels as 1D integer array. |
required |
num_children
|
int
|
Number of child populations to create. |
required |
Returns:
| Type | Description |
|---|---|
List[SamplingResult]
|
List of SamplingResult, one per child, with sampled feature columns, |
List[SamplingResult]
|
all instances preserved, and instance_indices set to None. |
Source code in hgp_lib\populations\sampling.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
hgp_lib.populations.sampling.InstanceSamplingStrategy
Bases: SamplingStrategy
Samples a subset of instances from the training data.
Each child population receives a subset of the parent's rows. All features
are preserved. The number of instances per child is
ceil(num_instances * sample_fraction).
Overlap behavior (controlled by replace parameter):
- replace=False: No overlap between children (partitioning).
- replace=True: Overlap allowed.
When sample_fraction=1.0, all children receive all instances regardless of
replace.
Examples:
>>> import numpy as np
>>> np.random.seed(42)
>>> strategy = InstanceSamplingStrategy(sample_fraction=0.8)
>>> data = np.random.rand(100, 10) > 0.5
>>> labels = np.random.randint(0, 2, 100)
>>> results = strategy.sample(data, labels, num_children=3)
>>> len(results)
3
>>> len(results[0].instance_indices)
80
Source code in hgp_lib\populations\sampling.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
sample(data, labels, num_children)
Sample instances for child populations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Training data as 2D boolean array (instances x features). |
required |
labels
|
ndarray
|
Training labels as 1D integer array. |
required |
num_children
|
int
|
Number of child populations to create. |
required |
Returns:
| Type | Description |
|---|---|
List[SamplingResult]
|
List of SamplingResult, one per child, with sampled instance rows, |
List[SamplingResult]
|
all features preserved, and feature_mapping set to None. |
Source code in hgp_lib\populations\sampling.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
hgp_lib.populations.sampling.CombinedSamplingStrategy
Bases: SamplingStrategy
Combines feature and instance sampling.
Applies both feature sampling and instance sampling to create child populations with reduced feature and instance sets.
Attributes:
| Name | Type | Description |
|---|---|---|
feature_fraction |
float
|
Fraction of features per child. Default: |
sample_fraction |
float
|
Fraction of instances per child. Default: |
replace |
bool
|
Whether to allow overlap between children. Default: |
Examples:
>>> import numpy as np
>>> np.random.seed(42)
>>> strategy = CombinedSamplingStrategy(
... feature_fraction=0.5,
... sample_fraction=0.5,
... replace=False
... )
>>> data = np.random.rand(100, 10) > 0.5
>>> labels = np.random.randint(0, 2, 100)
>>> results = strategy.sample(data, labels, num_children=3)
>>> len(results)
3
>>> results[0].data.shape
(50, 5)
Source code in hgp_lib\populations\sampling.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
sample(data, labels, num_children)
Sample both features and instances for all children at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Training data as 2D boolean array (instances x features). |
required |
labels
|
ndarray
|
Training labels as 1D integer array. |
required |
num_children
|
int
|
Number of child populations to create. |
required |
Returns:
| Type | Description |
|---|---|
List[SamplingResult]
|
List of SamplingResult, one per child, with both feature and instance |
List[SamplingResult]
|
subsets applied, containing both feature_indices and instance_indices. |
Source code in hgp_lib\populations\sampling.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |