API Reference¶
Core¶
Population¶
pikaia.data.population.PikaiaPopulation
¶
Represents a population matrix for genetic algorithms.
The matrix shape is (N, M) where N is the number of organisms and
M is the number of genes (features). All values must lie in [0, 1].
Source code in pikaia/data/population.py
6 7 8 9 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 107 108 109 110 111 112 113 114 115 116 | |
Attributes¶
M: int
property
¶
Returns the number of genes (columns) in the population matrix.
N: int
property
¶
Returns the number of organisms (rows) in the population matrix.
matrix: np.ndarray
property
¶
Returns the underlying population matrix.
Methods:¶
__getitem__(idx)
¶
Allows direct indexing into the population.
Example
population[i, j] or population[i].
__init__(matrix: np.ndarray, skip_correlation_validation: bool = True)
¶
Initialize the Population with a matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
A 2D numpy array of shape (N, M) representing the population. All values must be between 0 and 1. Higher values are treated as more desirable features, whereas lower values are less desirable. |
required |
skip_correlation_validation
|
bool
|
If True, skips the correlation validation between features. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the matrix does not meet the validation criteria. |
Source code in pikaia/data/population.py
Model¶
pikaia.models.pikaia_model.PikaiaModel
¶
Bases: GeneticModel
Central organizing class for the Genetic AI model.
This class orchestrates the evolutionary simulation. It takes a population, a set of gene and organism strategies, and runs a simulation over a specified number of iterations. It tracks the history of gene and organism fitness, as well as the mixing coefficients for the strategies.
The model is fitted using the fit method, which iteratively updates the
fitness values based on the provided strategies.
Source code in pikaia/models/pikaia_model.py
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 149 150 151 152 153 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 222 223 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 287 288 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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
Methods:¶
__init__(*args, use_d_matrix: bool = False, **kwargs)
¶
Initialises the PikaiaModel.
Accepts all arguments of :class:GeneticModel plus:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_d_matrix
|
bool
|
When |
False
|
Source code in pikaia/models/pikaia_model.py
fit() -> None
¶
Fits the genetic model to the population data by running the simulation.
This method iteratively updates the gene and organism fitness values based on the
provided strategies. The simulation runs for a maximum number of iterations as
defined by max_iter. If an epsilon value is provided, the simulation will
stop early if the change in gene fitness between iterations falls below this
threshold, indicating convergence.
Source code in pikaia/models/pikaia_model.py
predict(population: PikaiaPopulation) -> np.ndarray
¶
Predicts the organism fitness for a new population using the fitted model.
This method computes the organism fitness values for a given population based on the final gene fitness distribution obtained from the last iteration of the fitted model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
The new population for which to predict organism fitness. |
required |
Returns: np.ndarray: A vector of predicted organism fitness values of shape (N,), where N is the number of organisms in the provided population.
Source code in pikaia/models/pikaia_model.py
Preprocessor¶
pikaia.preprocessing.pikaia_preprocessor.PikaiaPreprocessor
¶
Preprocessor for Pikaia genetic algorithm data.
This class preprocesses feature data for use with the Pikaia genetic algorithm. It applies specified transformation functions to each feature and checks whether the transformed data is suitable for the genetic algorithm by checking that all values fall within the [0, 1] range.
The class follows the scikit-learn transformer interface, providing fit(), transform(), and fit_transform() methods for compatibility with ML pipelines.
Source code in pikaia/preprocessing/pikaia_preprocessor.py
9 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 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
Methods:¶
__init__(num_features: int, feature_types: Sequence[FeatureType], feature_transforms: Sequence[Callable[[np.ndarray], np.ndarray] | None])
¶
Initialize the PikaiaPreprocessor.
Sets up the preprocessor with the specified number of features, their types, and the transformation functions to apply to each feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
The number of features in the dataset. This must match the number of columns in the input data arrays passed to fit() and transform(). |
required |
feature_types
|
list[FeatureType]
|
A list of FeatureType enums, one for each feature. Each FeatureType indicates whether the feature represents a cost (lower values better) or gain (higher values better), though this info is stored for potential future use. |
required |
feature_transforms
|
list[Callable[[NDArray], NDArray] | None]
|
A list of the same length as num_features. Each element is either a callable function that takes a 1D numpy array (a feature column) and returns a transformed 1D array, or None if no transformation should be applied to that feature. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the lengths of feature_types or feature_transforms do not match num_features. |
Source code in pikaia/preprocessing/pikaia_preprocessor.py
fit(X: np.ndarray) -> PikaiaPreprocessor
¶
Fit the preprocessor to the input data.
This method validates that the input data X has the correct number of features as specified during initialization. No actual fitting (e.g., parameter estimation) is performed since transformations are predefined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The input data array with shape (n_samples, n_features). Must have exactly num_features columns. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PikaiaPreprocessor |
PikaiaPreprocessor
|
Returns self to allow method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of features in X does not match num_features. |
Source code in pikaia/preprocessing/pikaia_preprocessor.py
fit_transform(X: np.ndarray) -> np.ndarray
¶
Fit the preprocessor and transform the data in one step.
Equivalent to calling fit(X, y).transform(X). This is a convenience method for scikit-learn compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The input data array with shape (n_samples, n_features). Must have exactly num_features columns. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The transformed data array with the same shape as X. |
Source code in pikaia/preprocessing/pikaia_preprocessor.py
transform(X: np.ndarray) -> np.ndarray
¶
Transform the input data using the specified transformation functions.
Applies the transformation function to each feature column if provided. For features marked as COST type, the values are inverted using the formula max_val + min_val - value to ensure higher values are more desirable for the genetic algorithm, regardless of the original data range. After all transformations, checks that all values in the transformed data are within the [0, 1] range. If not, logs a warning as this may indicate that the data is not suitable for the genetic algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The input data array with shape (n_samples, n_features). Must have exactly num_features columns. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The transformed data array with the same shape as X, where each feature column has been processed according to the specified transformation and COST features have been inverted. |
Source code in pikaia/preprocessing/pikaia_preprocessor.py
Plotter¶
pikaia.plotting.pikaia_plotter.PikaiaPlotter
¶
A class for plotting results from a PikaiaModel.
This class provides a set of methods to visualize the outputs of an evolutionary simulation, including fitness histories, mixing coefficients, and similarity matrices.
Source code in pikaia/plotting/pikaia_plotter.py
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 149 150 151 152 153 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
Methods:¶
__init__(model: PikaiaModel)
¶
Initializes the PikaiaPlotter with a PikaiaModel instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PikaiaModel
|
The fitted PikaiaModel to be plotted. |
required |
Source code in pikaia/plotting/pikaia_plotter.py
plot(plot_type: PlotType, show: bool = False, save_path: Path | None = None, gene_labels: list[str] | None = None, org_labels: list[str] | None = None, title: str | None = None) -> tuple[Figure, Axes]
¶
Plots the specified data from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plot_type
|
PlotType
|
The type of plot to generate. |
required |
show
|
bool
|
If True, the plot is displayed. Defaults to False. |
False
|
save_path
|
Path | None
|
If provided, the plot is saved to this path. Defaults to None. |
None
|
gene_labels
|
list[str] | None
|
Custom labels for genes. |
None
|
org_labels
|
list[str] | None
|
Custom labels for organisms. |
None
|
title
|
str | None Custom title for the plot. If None, a default title is used. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[Figure, Axes]
|
A tuple containing the matplotlib Figure and Axes objects. |
Source code in pikaia/plotting/pikaia_plotter.py
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 | |
pikaia.plotting.pikaia_plotter.PlotType
¶
Bases: StrEnum
Enum for the different types of plots.
Source code in pikaia/plotting/pikaia_plotter.py
Schemas¶
pikaia.schemas.strategies.GeneStrategyEnum
¶
Bases: str, Enum
Enum representing gene-level evolutionary strategies.
Source code in pikaia/schemas/strategies.py
Attributes¶
ALTRUISTIC = 'ALTRUISTIC'
class-attribute
instance-attribute
¶
Gene acts altruistically toward others.
DOMINANT = 'DOMINANT'
class-attribute
instance-attribute
¶
Gene expresses dominance over others.
ENTROPY_MAX = 'ENTROPY_MAX'
class-attribute
instance-attribute
¶
Information-theoretic supervised strategy.
Rewards features with high mutual information with the target weighted by differential entropy. Converges within 5 iterations.
KIN_ALTRUISTIC = 'KIN_ALTRUISTIC'
class-attribute
instance-attribute
¶
Gene favors kin altruism.
NONE = 'NONE'
class-attribute
instance-attribute
¶
No specific strategy — zero contribution.
ORTHO_GENE = 'ORTHO_GENE'
class-attribute
instance-attribute
¶
Orthogonality-based strategy.
Promotes features that are minimally correlated with all other features.
PARTIAL_CORR = 'PARTIAL_CORR'
class-attribute
instance-attribute
¶
Partial-correlation supervised strategy.
Rewards features whose relationship with the target survives controlling for all other features.
REDUNDANCY_PENALTY = 'REDUNDANCY_PENALTY'
class-attribute
instance-attribute
¶
Redundancy-penalty strategy.
Suppresses features that are highly correlated with their peers.
SELFISH = 'SELFISH'
class-attribute
instance-attribute
¶
Gene acts in its own interest.
SELL_EASY = 'SELL_EASY'
class-attribute
instance-attribute
¶
Trading sell signal weighted by gene ease — the inverse of SELL_HARD.
Easy genes (high mean expression) lose more value.
Pair with OrgStrategyEnum.BUY_EASY.
SELL_HARD = 'SELL_HARD'
class-attribute
instance-attribute
¶
Trading sell signal weighted by gene difficulty.
Hard genes (low mean expression) lose more value per unit of performance.
Pair with OrgStrategyEnum.BUY_HARD.
SELL_UNIFORM = 'SELL_UNIFORM'
class-attribute
instance-attribute
¶
Trading sell signal applied uniformly to all genes.
All genes lose value at the same rate, independent of difficulty.
Pair with OrgStrategyEnum.BUY_UNIFORM.
VARIANCE = 'VARIANCE'
class-attribute
instance-attribute
¶
Rewards genes with high cross-organism dispersion (column std).
pikaia.schemas.strategies.OrgStrategyEnum
¶
Bases: str, Enum
Enum representing organism-level evolutionary strategies.
Source code in pikaia/schemas/strategies.py
Attributes¶
ALTRUISTIC = 'ALTRUISTIC'
class-attribute
instance-attribute
¶
Organism acts altruistically toward similar organisms.
BALANCED = 'BALANCED'
class-attribute
instance-attribute
¶
Organism balances gene contributions to promote uniform fitness.
BUY_EASY = 'BUY_EASY'
class-attribute
instance-attribute
¶
Trading buy-phase paired with SELL_EASY — mirror of BUY_HARD.
Redistributes capital with inverted sign relative to BUY_HARD.
Pair with GeneStrategyEnum.SELL_EASY.
BUY_HARD = 'BUY_HARD'
class-attribute
instance-attribute
¶
Trading buy-phase paired with SELL_HARD.
Redistributes hard-gene sell capital to easy genes the organism failed.
Pair with GeneStrategyEnum.SELL_HARD.
BUY_UNIFORM = 'BUY_UNIFORM'
class-attribute
instance-attribute
¶
Trading buy-phase paired with SELL_UNIFORM.
Redistributes uniform sell capital to hard genes the organism failed.
Pair with GeneStrategyEnum.SELL_UNIFORM.
KIN_SELFISH = 'KIN_SELFISH'
class-attribute
instance-attribute
¶
Organism is selfish toward non-kin, altruistic toward kin.
NONE = 'NONE'
class-attribute
instance-attribute
¶
No specific strategy — zero contribution.
SELFISH = 'SELFISH'
class-attribute
instance-attribute
¶
Organism acts selfishly, promoting its own gene expression.
pikaia.schemas.strategies.MixStrategyEnum
¶
Bases: str, Enum
Enum representing strategy mixing modes.
Source code in pikaia/schemas/strategies.py
Attributes¶
FIXED = 'FIXED'
class-attribute
instance-attribute
¶
Fixed mixing coefficients — proportions do not adapt over iterations.
NONE = 'NONE'
class-attribute
instance-attribute
¶
No mixed strategy applied.
SELF_CONSISTENT = 'SELF_CONSISTENT'
class-attribute
instance-attribute
¶
Self-consistent mixing — coefficients adapt each iteration based on delta magnitude.
Strategies¶
Base Classes¶
pikaia.strategies.base_strategies.GeneStrategy
¶
Bases: ABC
Abstract base class for gene strategies.
Defines the interface for all gene-level evolutionary strategies. Subclasses
must implement the __call__ method, which calculates the fitness delta
for a specific gene based on the strategy's logic.
Source code in pikaia/strategies/base_strategies.py
Attributes¶
name: str
abstractmethod
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
abstractmethod
¶
Computes the delta for a gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta value |
Source code in pikaia/strategies/base_strategies.py
__init__(**kwargs)
¶
Initializes the strategy with optional parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arbitrary keyword arguments that can be used to configure
the strategy. These are stored in the |
{}
|
Source code in pikaia/strategies/base_strategies.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: Optional[np.ndarray] = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Return the D-matrix kernel contribution (D, d) for this strategy.
D is an (M, M) array for the bilinear term
gamma * (D @ gamma); d is an (M,) array for the linear
term. Either may be None when the strategy has no contribution
of that type.
The default returns (None, None) (zero contribution). Subclasses
that support the fast D-matrix path override this method.
Source code in pikaia/strategies/base_strategies.py
pikaia.strategies.base_strategies.OrgStrategy
¶
Bases: ABC
Abstract base class for organism strategies.
Defines the interface for all organism-level evolutionary strategies.
Subclasses must implement the __call__ method, which calculates the
fitness deltas for all genes based on the organism's interactions.
Source code in pikaia/strategies/base_strategies.py
Attributes¶
name: str
abstractmethod
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> np.ndarray
abstractmethod
¶
Computes deltas for an organism strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray:
A vector of shape |
Source code in pikaia/strategies/base_strategies.py
__init__(**kwargs)
¶
Initializes the strategy with optional parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arbitrary keyword arguments that can be used to configure
the strategy. These are stored in the |
{}
|
Source code in pikaia/strategies/base_strategies.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: Optional[np.ndarray] = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Return the D-matrix kernel contribution (D, d) for this strategy.
The default returns (None, None) (zero contribution). Subclasses
that support the fast D-matrix path override this method.
Source code in pikaia/strategies/base_strategies.py
pikaia.strategies.base_strategies.MixStrategy
¶
Bases: ABC
Abstract base class for mixing strategies.
Defines the interface for strategies that determine how to mix or weigh
the contributions of different evolutionary strategies (gene or organism).
Subclasses must implement the __call__ method.
Source code in pikaia/strategies/base_strategies.py
Attributes¶
name: str
abstractmethod
property
¶
The name of the strategy.
Methods:¶
__call__(delta: np.ndarray, mix_coeffs: np.ndarray) -> tuple[np.ndarray, np.ndarray]
abstractmethod
¶
Mixes organism deltas and dynamically updates mixing coefficients.
The method first calculates the combined delta using the current mixing coefficients. It then updates these coefficients for the next iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta
|
ndarray
|
A 3D array of shape |
required |
mix_coeffs
|
ndarray
|
A 1D array of shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[ndarray, ndarray]
|
np.ndarray: The mixed delta matrix of shape |
Source code in pikaia/strategies/base_strategies.py
__init__(**kwargs)
¶
Initializes the strategy with optional parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arbitrary keyword arguments that can be used to configure
the strategy. These are stored in the |
{}
|
Source code in pikaia/strategies/base_strategies.py
pikaia.strategies.base_strategies.StrategyContext
dataclass
¶
Holds the context for a strategy calculation.
Source code in pikaia/strategies/base_strategies.py
Factories¶
pikaia.strategies.strategy_factories.GeneStrategyFactory
¶
Factory class for creating gene strategy instances.
This factory provides a centralized way to instantiate gene strategy
objects based on the GeneStrategyEnum. It maps the enum members to their
corresponding strategy classes.
Source code in pikaia/strategies/strategy_factories.py
Methods:¶
get_strategy(name: GeneStrategyEnum, *args, **kwargs) -> GeneStrategy
classmethod
¶
Retrieves an instance of the requested gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
GeneStrategyEnum
|
The enum member representing the desired strategy. |
required |
``*args``
|
Positional arguments to pass to the strategy's constructor. |
required | |
``**kwargs``
|
Keyword arguments to pass to the strategy's constructor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GeneStrategy |
GeneStrategy
|
An instance of the corresponding gene strategy class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested strategy name is not found in the factory's registry. |
Source code in pikaia/strategies/strategy_factories.py
pikaia.strategies.strategy_factories.OrgStrategyFactory
¶
Factory class for creating organism strategy instances.
This factory provides a centralized way to instantiate organism strategy
objects based on the OrgStrategyEnum. It maps the enum members to their
corresponding strategy classes.
Source code in pikaia/strategies/strategy_factories.py
Methods:¶
get_strategy(name: OrgStrategyEnum, *args, **kwargs) -> OrgStrategy
classmethod
¶
Retrieves an instance of the requested organism strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
OrgStrategyEnum
|
The enum member representing the desired strategy. |
required |
``*args``
|
Positional arguments to pass to the strategy's constructor. |
required | |
``**kwargs``
|
Keyword arguments to pass to the strategy's constructor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OrgStrategy |
OrgStrategy
|
An instance of the corresponding organism strategy class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested strategy name is not found in the factory's registry. |
Source code in pikaia/strategies/strategy_factories.py
pikaia.strategies.strategy_factories.MixStrategyFactory
¶
Factory class for creating mixing strategy instances.
This factory provides a centralized way to instantiate mixing strategy
objects based on the MixinGeneStrategyEnum.
Source code in pikaia/strategies/strategy_factories.py
Methods:¶
get_strategy(name: MixStrategyEnum, *args, **kwargs) -> MixStrategy
classmethod
¶
Retrieves a singleton instance of the requested mixing strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
MixinGeneStrategyEnum
|
The enum member representing the desired strategy. |
required |
``*args``
|
Positional arguments to pass to the strategy's constructor. |
required | |
``**kwargs``
|
Keyword arguments to pass to the strategy's constructor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MixStrategy |
MixStrategy
|
An instance of the corresponding mixing strategy class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested strategy name is not found. |
Source code in pikaia/strategies/strategy_factories.py
Gene Strategies¶
pikaia.strategies.gs_strategies.dominant_strategy.DominantGeneStrategy
¶
Bases: GeneStrategy
A gene strategy that promotes dominant genes.
This strategy increases the fitness of genes that are highly expressed
(dominant), reinforcing their prevalence in the population. The delta is
proportional to the square of the gene's fitness and its expression level.
This implementation follows the logic from the original alg.py.
Source code in pikaia/strategies/gs_strategies/dominant_strategy.py
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Computes the delta for a dominant gene.
The formula reinforces the fitness of the gene based on its current fitness and expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta value |
Source code in pikaia/strategies/gs_strategies/dominant_strategy.py
__init__(**kwargs)
¶
Initialise the Dominant gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Diagonal D matrix from population mean expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Unused. |
required |
org_similarity
|
ndarray
|
Unused. |
required |
initial_org_fitness_range
|
float
|
Unused. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
with |
Source code in pikaia/strategies/gs_strategies/dominant_strategy.py
pikaia.strategies.gs_strategies.sell_hard_strategy.SellHardGeneStrategy
¶
Bases: GeneStrategy
Trading sell signal weighted by gene difficulty.
Hard genes (low mean expression, high exclusiveness) lose more value per unit of performance — organisms that solved them "sell" at a premium.
For organism i, gene j:
Summed over all organisms this equals
-mean_j · excl_j / (1 - excl_j), the proportional sell loss
weighted by gene difficulty.
Pair with BuyHardOrgStrategy for the full hard-gene trading round.
Source code in pikaia/strategies/gs_strategies/sell_hard_strategy.py
Methods:¶
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Linear d-vector: d[j] = -mean_j · excl_j / (1 - excl_j + eps).
Source code in pikaia/strategies/gs_strategies/sell_hard_strategy.py
pikaia.strategies.gs_strategies.sell_uniform_strategy.SellUniformGeneStrategy
¶
Bases: GeneStrategy
Trading sell signal applied uniformly to all genes.
All genes lose value at the same rate regardless of difficulty — organisms "sell" their solved genes uniformly.
For organism i, gene j:
Summed over all organisms this equals -mean_j, a uniform sell loss
independent of gene difficulty.
Pair with BuyUniformOrgStrategy for the full uniform trading round.
Source code in pikaia/strategies/gs_strategies/sell_uniform_strategy.py
Methods:¶
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Linear d-vector: d[j] = -mean_j for non-trivial genes only.
Source code in pikaia/strategies/gs_strategies/sell_uniform_strategy.py
pikaia.strategies.gs_strategies.sell_easy_strategy.SellEasyGeneStrategy
¶
Bases: GeneStrategy
Trading sell signal weighted by gene ease — the inverse of SellHardGeneStrategy.
Easy genes (high mean expression, low exclusiveness) lose more value.
For organism i, gene j:
Summed over all organisms this equals
+mean_j · excl_j / (1 - excl_j), the exact negation of the
SellHardGeneStrategy signal.
Pair with BuyEasyOrgStrategy for the full easy-gene trading round.
Source code in pikaia/strategies/gs_strategies/sell_easy_strategy.py
Methods:¶
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Linear d-vector: d[j] = +mean_j · excl_j / (1 - excl_j + eps).
Source code in pikaia/strategies/gs_strategies/sell_easy_strategy.py
pikaia.strategies.gs_strategies.altruistic_strategy.AltruisticGeneStrategy
¶
Bases: GeneStrategy
A gene strategy that promotes altruistic behavior.
This strategy models altruism where a gene's fitness is influenced by its
interaction with other genes. The delta for a gene's fitness is calculated
based on its similarity to other genes and their respective fitness values.
This implementation follows the logic from the original alg.py.
Source code in pikaia/strategies/gs_strategies/altruistic_strategy.py
7 8 9 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 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Computes the delta for an altruistic gene.
The formula is derived from the replicator equation, considering the interactions between the current gene and all other genes in the organism.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta value |
Source code in pikaia/strategies/gs_strategies/altruistic_strategy.py
__init__(**kwargs)
¶
Initialise the Altruistic gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Full (M, M) D matrix encoding cross-gene altruistic interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Gene similarity matrix of shape |
required |
org_similarity
|
ndarray
|
Unused. |
required |
initial_org_fitness_range
|
float
|
Unused. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
|
tuple[ndarray | None, ndarray | None]
|
|
tuple[ndarray | None, ndarray | None]
|
and the diagonal set to zero. |
Source code in pikaia/strategies/gs_strategies/altruistic_strategy.py
pikaia.strategies.gs_strategies.selfish_strategy.SelfishGeneStrategy
¶
Bases: GeneStrategy
A gene strategy that promotes selfish behavior.
Warning
This strategy is experimental and its behavior may change in future versions.
This strategy models selfish behavior where a gene's fitness is increased
at the expense of other, dissimilar genes within the same organism. The
effect is proportional to the similarity, meaning it acts more selfishly
against more similar genes. This implementation follows the logic from the
original alg.py.
Source code in pikaia/strategies/gs_strategies/selfish_strategy.py
7 8 9 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 107 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Computes the delta for a selfish gene.
The formula calculates a negative delta contribution, effectively penalizing other genes to benefit the current one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta value |
Source code in pikaia/strategies/gs_strategies/selfish_strategy.py
__init__(**kwargs)
¶
Initialise the Selfish gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Full (M, M) D matrix as the negation of the altruistic kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Gene similarity matrix of shape |
required |
org_similarity
|
ndarray
|
Unused. |
required |
initial_org_fitness_range
|
float
|
Unused. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
matrix with the diagonal set to zero. |
Source code in pikaia/strategies/gs_strategies/selfish_strategy.py
pikaia.strategies.gs_strategies.kin_altruistic_strategy.KinAltruisticGeneStrategy
¶
Bases: GeneStrategy
A gene strategy that promotes altruism towards kin (similar genes).
Warning
This strategy is experimental and its behavior may change in future versions.
This strategy increases a gene's fitness by helping other, similar genes,
even at a potential cost to itself. The altruistic effect is inversely
proportional to the similarity, meaning it helps less similar genes more.
This implementation follows the logic from the original alg.py.
Source code in pikaia/strategies/gs_strategies/kin_altruistic_strategy.py
7 8 9 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 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 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Computes the delta for a kin-altruistic gene.
The formula considers the interaction with other genes, weighted by a
factor of (0.5 - similarity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta value |
Source code in pikaia/strategies/gs_strategies/kin_altruistic_strategy.py
__init__(**kwargs)
¶
Initialise the KinAltruistic gene strategy.
Other Parameters:
| Name | Type | Description |
|---|---|---|
kin_range |
int
|
Number of most-similar genes to consider as kin
when computing the interaction term. Defaults to |
**kwargs |
Additional options forwarded to |
Source code in pikaia/strategies/gs_strategies/kin_altruistic_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Full (M, M) D matrix with kin-weighted similarity.
Respects the kin_range option: per gene j, only the top
kin_range most similar genes k contribute (self excluded).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Gene similarity matrix of shape |
required |
org_similarity
|
ndarray
|
Unused. |
required |
initial_org_fitness_range
|
float
|
Unused. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
|
tuple[ndarray | None, ndarray | None]
|
|
tuple[ndarray | None, ndarray | None]
|
and the diagonal set to zero. |
Source code in pikaia/strategies/gs_strategies/kin_altruistic_strategy.py
pikaia.strategies.gs_strategies.variance_strategy.VarianceGeneStrategy
¶
Bases: GeneStrategy
A gene strategy that rewards features with high cross-organism dispersion.
Scales a Dominant-style expression signal by the column's normalised standard deviation so that genes which separate organisms more strongly receive larger fitness deltas. Near-constant columns contribute ~0.
Source code in pikaia/strategies/gs_strategies/variance_strategy.py
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Computes the delta for a variance-weighted gene.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta value |
Source code in pikaia/strategies/gs_strategies/variance_strategy.py
__init__(**kwargs)
¶
Initialise the Variance gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Diagonal D matrix scaled by normalised column std.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Unused. |
required |
org_similarity
|
ndarray
|
Unused. |
required |
initial_org_fitness_range
|
float
|
Unused. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
with |
Source code in pikaia/strategies/gs_strategies/variance_strategy.py
pikaia.strategies.gs_strategies.entropy_max_strategy.EntropyMaxGeneStrategy
¶
Bases: GeneStrategy
A supervised gene strategy driven by information-theoretic relevance.
Warning
This strategy is experimental and its behavior may change in future versions.
Scores each feature as the product of its normalised mutual information
with the target y and its normalised differential entropy (a proxy for
feature variance). Both components are independently normalised to
[0, 1] before multiplication, so the combined score rewards features
that are simultaneously informative and spread out.
The replicator delta is::
delta[j] = gf[j] * (4 / N) * (info_score[j] - 0.5)
where info_score[j] ∈ [0, 1] is the product described above.
When y is not supplied the strategy falls back to entropy-only scores
(mutual information term is zero), making all info_score[j] = 0 and
the delta uniformly negative — equivalent to a mild anti-high-variance
penalty. For the strategy to be useful, always pass y.
The per-feature scores are computed once on the first call and cached. To
supply labels when using the kernel path, pass y to kernel().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bins
|
int
|
Number of bins used when discretising continuous features for
mutual information estimation. Default |
10
|
precomputed_info
|
ndarray | None
|
Pre-computed info scores of shape |
None
|
**kwargs
|
Forwarded to |
{}
|
Source code in pikaia/strategies/gs_strategies/entropy_max_strategy.py
7 8 9 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 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Compute delta for the EntropyMax gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Strategy context. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta |
Source code in pikaia/strategies/gs_strategies/entropy_max_strategy.py
compute_info_scores(X: np.ndarray, y: np.ndarray | None = None, n_bins: int = 10) -> np.ndarray
staticmethod
¶
Compute per-feature information scores as MI × entropy (both normalised).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data matrix of shape |
required |
y
|
ndarray | None
|
Target array of shape |
None
|
n_bins
|
int
|
Number of histogram bins for MI estimation. |
10
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
Source code in pikaia/strategies/gs_strategies/entropy_max_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Diagonal D: D[j,j] = 4 * (info_score[j] - 0.5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Current population. |
required |
gene_similarity
|
ndarray
|
Gene-similarity matrix (unused). |
required |
org_similarity
|
ndarray
|
Organism-similarity matrix (unused). |
required |
initial_org_fitness_range
|
float
|
Initial fitness range (unused). |
required |
y
|
ndarray | None
|
Target labels. Pass these to enable MI computation. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray | None, ndarray | None]
|
|
Source code in pikaia/strategies/gs_strategies/entropy_max_strategy.py
pikaia.strategies.gs_strategies.orthogonality_strategy.OrthoGeneStrategy
¶
Bases: GeneStrategy
A gene strategy driven by feature orthogonality (low pairwise correlation).
Warning
This strategy is experimental and its behavior may change in future versions.
Rewards features that are minimally correlated with all other features. The orthogonality score for feature j is::
orthogonality[j] = 1 - mean(|corr(j, k)|) for k ≠ j
where correlations are computed on the MinMax-normalised data matrix. The
score is in [0, 1]; a perfectly uncorrelated feature scores 1.
When the target y is provided, it is appended as an extra column before
computing correlations (supervised mode). Because orthogonality is now
measured against the augmented matrix, features that correlate strongly
with y receive lower scores and are suppressed — the opposite of
conventional supervised feature selection. This makes the strategy a
novelty/diversity pressure: it promotes features that add information
beyond what the target and the other features already capture. It is most
useful when mixed with a target-aware strategy (e.g. DOMINANT or
ENTROPY_MAX) that handles target relevance, leaving OrthoGene to
enforce diversity.
The replicator delta is::
delta[j] = gf[j] * (4 / N) * (orthogonality[j] - 0.5)
Scores are computed once on the first call and cached; a mode change (supervised ↔ unsupervised) triggers a recomputation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Forwarded to |
{}
|
Source code in pikaia/strategies/gs_strategies/orthogonality_strategy.py
7 8 9 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 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 149 150 151 152 153 154 155 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Compute delta for the OrthoGene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Strategy context. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta |
Source code in pikaia/strategies/gs_strategies/orthogonality_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Diagonal D: D[j,j] = 4 * (orthogonality[j] - 0.5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Current population. |
required |
gene_similarity
|
ndarray
|
Gene-similarity matrix (unused). |
required |
org_similarity
|
ndarray
|
Organism-similarity matrix (unused). |
required |
initial_org_fitness_range
|
float
|
Initial fitness range (unused). |
required |
y
|
ndarray | None
|
Optional target labels for supervised mode. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray | None, ndarray | None]
|
|
Source code in pikaia/strategies/gs_strategies/orthogonality_strategy.py
pikaia.strategies.gs_strategies.partial_corr_strategy.PartialCorrGeneStrategy
¶
Bases: GeneStrategy
A gene strategy driven by partial correlation with the target.
Warning
This strategy is experimental and its behavior may change in future versions.
Rewards features whose relationship with the target survives controlling for all other features. The partial correlation of feature j with the target is estimated via shrinkage precision matrices to handle multicollinearity::
pc[j] = |P[j, target]| / sqrt(|P[j,j]| * |P[target,target]|)
where P is the shrinkage precision matrix of [X | y]. Scores are
clipped to [0, 1].
The replicator delta is::
delta[j] = gf[j] * (4 / N) * (pc[j] - 0.5)
Without a target (unsupervised mode), all partial correlations are set to
zero, making every delta negative. For meaningful results, always pass
y.
Pre-computed partial correlations can be supplied via precomputed_pc
to skip the expensive matrix inversion. The scores are cached on first
use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precomputed_pc
|
ndarray | None
|
Pre-computed partial correlations of shape
|
None
|
n_bins
|
int
|
Unused; kept for API compatibility. |
10
|
**kwargs
|
Forwarded to |
{}
|
Source code in pikaia/strategies/gs_strategies/partial_corr_strategy.py
7 8 9 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 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Compute delta for the PartialCorr gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Strategy context. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta |
Source code in pikaia/strategies/gs_strategies/partial_corr_strategy.py
compute_partial_correlations(X: np.ndarray, y: np.ndarray | None = None, reg: float = 0.5) -> np.ndarray
staticmethod
¶
Compute partial correlations between each feature and the target.
Uses Ledoit-Wolf-style shrinkage towards a scaled identity to regularise the precision matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data matrix |
required |
y
|
ndarray | None
|
Target array |
None
|
reg
|
float
|
Shrinkage coefficient in |
0.5
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
Source code in pikaia/strategies/gs_strategies/partial_corr_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Diagonal D: D[j,j] = 4 * (pc[j] - 0.5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Current population. |
required |
gene_similarity
|
ndarray
|
Gene-similarity matrix (unused). |
required |
org_similarity
|
ndarray
|
Organism-similarity matrix (unused). |
required |
initial_org_fitness_range
|
float
|
Initial fitness range (unused). |
required |
y
|
ndarray | None
|
Target labels. Pass these to enable partial correlation computation. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray | None, ndarray | None]
|
|
Source code in pikaia/strategies/gs_strategies/partial_corr_strategy.py
pikaia.strategies.gs_strategies.redundancy_penalty_strategy.RedundancyPenaltyGeneStrategy
¶
Bases: GeneStrategy
A gene strategy that penalises redundant (highly correlated) features.
Warning
This strategy is experimental and its behavior may change in future versions.
Computes a redundancy score for each feature as the mean absolute pairwise correlation with all other features::
redundancy[j] = mean(|corr(j, k)|) for k ≠ j
and promotes features with low redundancy::
delta[j] = gf[j] * (4 / N) * (0.5 - redundancy[j])
Features with redundancy below 0.5 receive a positive delta (promoted); highly correlated features receive a negative delta (suppressed).
In supervised mode (when y is provided), the target is appended as an
extra column before computing the correlation matrix, biasing the strategy
towards features that are both non-redundant among themselves and
non-redundant relative to the target. The target column is excluded from
the returned scores.
Scores are computed once on first use and cached; a mode change triggers a recomputation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precomputed_redundancy
|
ndarray | None
|
Pre-computed redundancy scores of shape
|
None
|
**kwargs
|
Forwarded to |
{}
|
Source code in pikaia/strategies/gs_strategies/redundancy_penalty_strategy.py
7 8 9 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 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 149 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> float
¶
Compute delta for the RedundancyPenalty gene strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Strategy context. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The computed delta |
Source code in pikaia/strategies/gs_strategies/redundancy_penalty_strategy.py
compute_redundancy(X: np.ndarray) -> np.ndarray
staticmethod
¶
Compute mean absolute pairwise correlation for each column of X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Matrix of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
Source code in pikaia/strategies/gs_strategies/redundancy_penalty_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Diagonal D: D[j,j] = 4 * (0.5 - redundancy[j]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Current population. |
required |
gene_similarity
|
ndarray
|
Gene-similarity matrix (unused). |
required |
org_similarity
|
ndarray
|
Organism-similarity matrix (unused). |
required |
initial_org_fitness_range
|
float
|
Initial fitness range (unused). |
required |
y
|
ndarray | None
|
Optional target labels for supervised mode. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray | None, ndarray | None]
|
|
Source code in pikaia/strategies/gs_strategies/redundancy_penalty_strategy.py
Organism Strategies¶
pikaia.strategies.os_strategies.balanced_strategy.BalancedOrgStrategy
¶
Bases: OrgStrategy
An organism strategy that promotes balanced gene contributions.
This strategy adjusts gene fitness to favor organisms where the
contribution of each gene to the organism's total fitness is balanced.
It penalizes genes that contribute disproportionately (more or less) than
the average. This implementation follows the logic from the original
alg.py.
Source code in pikaia/strategies/os_strategies/balanced_strategy.py
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> np.ndarray
¶
Computes deltas for a balanced organism strategy.
The formula calculates the deviation of each gene's contribution from
the ideal balanced state (1/m) and adjusts its fitness accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A vector of computed delta values |
Source code in pikaia/strategies/os_strategies/balanced_strategy.py
__init__(**kwargs)
¶
Initialise the Balanced organism strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Rank-1 D matrix exploiting gamma normalisation.
Because sum_j gamma_j = 1, a row-constant matrix
D[j, k] = -2 * x_bar_j satisfies (D @ gamma)_j = -2 * x_bar_j
for any normalised gamma, exactly reproducing the balanced-org
contribution delta_j ≈ -2 * x_bar_j * gamma_j.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Unused. |
required |
org_similarity
|
ndarray
|
Unused. |
required |
initial_org_fitness_range
|
float
|
Unused. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
with |
Source code in pikaia/strategies/os_strategies/balanced_strategy.py
pikaia.strategies.os_strategies.altruistic_strategy.AltruisticOrgStrategy
¶
Bases: OrgStrategy
An organism strategy that promotes altruistic behavior towards relatives.
Warning
This strategy is experimental and its behavior may change in future versions.
This strategy models altruism where an organism's fitness contribution is
adjusted based on its interaction with related organisms (kin). The delta
is calculated based on the fitness difference between the organism and its
relatives, weighted by their similarity. This implementation follows the
logic from the original alg.py.
Source code in pikaia/strategies/os_strategies/altruistic_strategy.py
8 9 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 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 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> np.ndarray
¶
Computes deltas for an altruistic organism strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A vector of computed delta values |
Source code in pikaia/strategies/os_strategies/altruistic_strategy.py
__init__(**kwargs)
¶
Initialise the Altruistic organism strategy.
Other Parameters:
| Name | Type | Description |
|---|---|---|
kin_range |
int
|
Maximum number of organisms to consider as kin
when computing the interaction term. Defaults to |
**kwargs |
Additional options forwarded to |
Source code in pikaia/strategies/os_strategies/altruistic_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Full (M, M) D matrix for kin-altruistic org interactions.
Identical computation to SelfishOrgStrategy's kernel
(D_alt = D_sel) because the formula is symmetric under the sign
convention used in the replicator equation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Unused. |
required |
org_similarity
|
ndarray
|
Organism similarity matrix of shape |
required |
initial_org_fitness_range
|
float
|
Used to normalise the D matrix. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
outer products of gene-expression vectors weighted by kin |
tuple[ndarray | None, ndarray | None]
|
similarity differences, scaled by |
Source code in pikaia/strategies/os_strategies/altruistic_strategy.py
pikaia.strategies.os_strategies.selfish_strategy.SelfishOrgStrategy
¶
Bases: OrgStrategy
An organism strategy that promotes selfish behavior.
This strategy models selfishness where an organism aims to increase its
own fitness, potentially at the expense of others. The delta is calculated
based on the fitness difference between the organism and its relatives,
weighted by their similarity. This is identical to the AltruisticOrgStrategy
but is kept for semantic clarity and future independent development.
This implementation follows the logic from the original alg.py.
Source code in pikaia/strategies/os_strategies/selfish_strategy.py
7 8 9 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 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 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> np.ndarray
¶
Computes deltas for a selfish organism strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A vector of computed delta values |
Source code in pikaia/strategies/os_strategies/selfish_strategy.py
__init__(**kwargs)
¶
Initialise the Selfish organism strategy.
Other Parameters:
| Name | Type | Description |
|---|---|---|
kin_range |
int
|
Maximum number of organisms to consider when
computing the interaction term. Defaults to |
**kwargs |
Additional options forwarded to |
Source code in pikaia/strategies/os_strategies/selfish_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Full (M, M) D matrix for kin-selfish org interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Unused. |
required |
org_similarity
|
ndarray
|
Organism similarity matrix of shape |
required |
initial_org_fitness_range
|
float
|
Used to normalise the D matrix. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
|
tuple[ndarray | None, ndarray | None]
|
summed over kin neighbours of each organism. |
Source code in pikaia/strategies/os_strategies/selfish_strategy.py
pikaia.strategies.os_strategies.kin_selfish_strategy.KinSelfishOrgStrategy
¶
Bases: OrgStrategy
An organism strategy that promotes selfish behavior towards non-kin.
Warning
This strategy is experimental and its behavior may change in future versions.
This strategy models selfish behavior where an organism's fitness is
increased at the expense of less related organisms. The selfish effect is
inversely proportional to similarity, meaning it acts more selfishly
towards organisms that are less similar. This implementation follows the
logic from the original alg.py.
Source code in pikaia/strategies/os_strategies/kin_selfish_strategy.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 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 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(ctx: StrategyContext) -> np.ndarray
¶
Computes deltas for a kin-selfish organism strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
StrategyContext
|
Context object containing all required and optional fields. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A vector of computed delta values |
Source code in pikaia/strategies/os_strategies/kin_selfish_strategy.py
__init__(**kwargs)
¶
Initialise the KinSelfish organism strategy.
Other Parameters:
| Name | Type | Description |
|---|---|---|
kin_range |
int
|
Maximum number of organisms to consider as kin
when computing the interaction term. Defaults to |
**kwargs |
Additional options forwarded to |
Source code in pikaia/strategies/os_strategies/kin_selfish_strategy.py
kernel(population: PikaiaPopulation, gene_similarity: np.ndarray, org_similarity: np.ndarray, initial_org_fitness_range: float, y: np.ndarray | None = None) -> tuple[np.ndarray | None, np.ndarray | None]
¶
Full (M, M) D matrix with kin-inverted similarity weights.
Like SelfishOrgStrategy's kernel but uses (0.5 - s^o_il)
as the similarity weight, flipping the sign for close kin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
PikaiaPopulation
|
Population providing the |
required |
gene_similarity
|
ndarray
|
Unused. |
required |
org_similarity
|
ndarray
|
Organism similarity matrix of shape |
required |
initial_org_fitness_range
|
float
|
Used to normalise the D matrix. |
required |
y
|
ndarray | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Tuple |
ndarray | None
|
|
tuple[ndarray | None, ndarray | None]
|
summed over kin neighbours of each organism. |
Source code in pikaia/strategies/os_strategies/kin_selfish_strategy.py
pikaia.strategies.os_strategies.buy_hard_strategy.BuyHardOrgStrategy
¶
Bases: OrgStrategy
Trading buy-phase paired with SellHardGeneStrategy.
Each organism spends its sell capital (earned from hard genes) on genes it
failed, weighted by how easy those genes are (mean_j). Organisms that
solved many hard genes accumulate more capital and redistribute it to the
easy genes they missed.
For organism i, the capital earned from selling is:
normalised by the easy-weighted sum of failed genes:
The buy contribution from organism i to gene j is:
Pair with SellHardGeneStrategy for the full hard-gene trading round.
Source code in pikaia/strategies/os_strategies/buy_hard_strategy.py
pikaia.strategies.os_strategies.buy_uniform_strategy.BuyUniformOrgStrategy
¶
Bases: OrgStrategy
Trading buy-phase paired with SellUniformGeneStrategy.
Each organism spends its uniform sell capital (proportional to average
performance) on genes it failed, weighted by how hard those genes are
(excl_j). Unlike BuyHardOrgStrategy, easy organisms and hard genes
receive more attention here.
For organism i, the capital from uniform selling is:
normalised by the hard-weighted sum of failed genes:
The buy contribution from organism i to gene j is:
Pair with SellUniformGeneStrategy for the full uniform trading round.
Source code in pikaia/strategies/os_strategies/buy_uniform_strategy.py
pikaia.strategies.os_strategies.buy_easy_strategy.BuyEasyOrgStrategy
¶
Bases: OrgStrategy
Trading buy-phase paired with SellEasyGeneStrategy — mirror of BuyHardOrgStrategy.
Capital is earned with a negative sign (from the easy sell signal),
so the redistribution flows in the opposite direction to BuyHardOrgStrategy:
organisms that solved easy genes accumulate capital and redistribute it
to genes they failed, weighted by how easy those genes are.
For organism i, the (negative) capital from easy selling is:
normalised by the easy-weighted sum of failed genes (same as BuyHard):
The buy contribution from organism i to gene j is:
Pair with SellEasyGeneStrategy for the full easy-gene trading round.
Source code in pikaia/strategies/os_strategies/buy_easy_strategy.py
Mix Strategies¶
pikaia.strategies.mix_strategies.fixed_strategy.FixedMixStrategy
¶
Bases: MixStrategy
Applies a fixed set of mixing coefficients to a delta tensor.
This strategy multiplies the input delta tensor by the provided mixing coefficients using Einstein summation, without updating or adapting the coefficients.
Example
strategy = FixedMixStrategy() mixed_delta, coeffs = strategy(delta, mix_coeffs)
Source code in pikaia/strategies/mix_strategies/fixed_strategy.py
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(delta: np.ndarray, mix_coeffs: np.ndarray) -> tuple[np.ndarray, np.ndarray]
¶
Apply fixed mixing coefficients to the input delta tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta
|
ndarray
|
A 3D tensor of shape (n, m, k) representing the deltas to be mixed. |
required |
mix_coeffs
|
ndarray
|
A 1D array of length k containing the mixing coefficients to apply. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
tuple[np.ndarray, np.ndarray]: - The mixed delta array of shape (n, m) after applying the coefficients. - The unchanged mixing coefficients array. |
Source code in pikaia/strategies/mix_strategies/fixed_strategy.py
__init__(**kwargs)
¶
Initialise the Fixed mix strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
pikaia.strategies.mix_strategies.self_consistent_strategy.SelfConsistentMixStrategy
¶
Bases: MixStrategy
Adaptively updates mixing coefficients based on the mean absolute delta.
This strategy computes a weighted sum of the input delta tensor using the current mixing coefficients, then updates the coefficients in a self-consistent manner based on the mean absolute value of the mixed deltas.
Example
strategy = SelfConsistentMixStrategy() mixed_delta, updated_coeffs = strategy(delta, mix_coeffs)
Source code in pikaia/strategies/mix_strategies/self_consistent_strategy.py
8 9 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 107 108 109 110 | |
Attributes¶
name: str
property
¶
The name of the strategy.
Methods:¶
__call__(delta: np.ndarray, mix_coeffs: np.ndarray) -> tuple[np.ndarray, np.ndarray]
¶
Apply self-consistent mixing to the input delta tensor and update mixing coefficients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta
|
ndarray
|
A 3D tensor of shape (n, m, k) representing the deltas to be mixed. |
required |
mix_coeffs
|
ndarray
|
A 1D array of length k containing the current mixing coefficients. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
tuple[np.ndarray, np.ndarray]: - The mixed delta array of shape (n, m) after applying the coefficients. - The updated mixing coefficients array, adjusted based on the mean absolute delta. |
Notes
The mixing coefficients are updated by applying a function to the mean absolute value of the mixed deltas, scaled by the number of columns in the delta array.
Source code in pikaia/strategies/mix_strategies/self_consistent_strategy.py
__init__(**kwargs)
¶
Initialise the SelfConsistent mix strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword options forwarded to |
{}
|
update_coeffs_d_matrix(D_list: list[np.ndarray | None], d_list: list[np.ndarray | None], gamma: np.ndarray, mix_coeffs: np.ndarray) -> np.ndarray
staticmethod
¶
Update mixing coefficients for the D-matrix iteration path.
Replaces the (N, M, K) tensor magnitude used in __call__ with
per-strategy D-matrix magnitudes:
- Bilinear strategy
s:mean_j(|gamma_j * (D_s @ gamma)_j|) - Linear (balanced org) strategy:
mean_j(|d_j|)— constant
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D_list
|
list[ndarray | None]
|
Per-strategy |
required |
d_list
|
list[ndarray | None]
|
Per-strategy |
required |
gamma
|
ndarray
|
Current gene fitness vector, shape |
required |
mix_coeffs
|
ndarray
|
Current mixing coefficients, shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated and renormalized mixing coefficients, shape |