Optimization API Reference¶
Algorithm¶
Genetic Algorithm¶
seapopym_optimization.algorithm.genetic_algorithm.genetic_algorithm
¶
This module contains the main genetic algorithm functions that can be used to optimize the model.
GeneticAlgorithmParameters
dataclass
¶
The structure used to store the genetic algorithm parameters.
Can generate the toolbox with default parameters. parameters.
Parameters¶
MUTPB: float Represents the probability of mutating an individual. It is recommended to use a value between 0.001 and 0.1. ETA: float Crowding degree of the mutation. A high eta will produce a mutant resembling its parent, while a small eta will produce a solution much more different. It is recommended to use a value between 1 and 20. INDPB: float Represents the individual probability of mutation for each attribute of the individual. It is recommended to use a value between 0.0 and 0.1. If you have a lot of parameters, you can use a 1/len(parameters) value. CXPB: float Represents the probability of mating two individuals. It is recommended to use a value between 0.5 and 1.0. NGEN: int Represents the number of generations. POP_SIZE: int Represents the size of the population. cost_function_weight: tuple | float = (-1.0,) The weight of the cost function. The default value is (-1.0,) to minimize the cost function.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
__post_init__()
¶
Check parameters and set default functions for selection, mating, mutation and variation.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
generate_toolbox(parameters)
¶
Generate a DEAP toolbox with the necessary functions for the genetic algorithm.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
GeneticAlgorithm
dataclass
¶
Genetic algorithm for optimizing SeapoPym models.
By default, the process order is SCM: Select, Cross, Mutate.
Uses the Strategy pattern for individual evaluation, allowing easy switching between sequential and hybrid modes as needed.
Examples¶
from seapopym_optimization.algorithm.genetic_algorithm import GeneticAlgorithmFactory ga = GeneticAlgorithmFactory.create_sequential(meta_params, cost_function) results = ga.optimize()
Attributes¶
meta_parameter: GeneticAlgorithmParameters The parameters of the genetic algorithm. cost_function: CostFunctionProtocol The cost function to optimize. evaluation_strategy: AbstractEvaluationStrategy Strategy pattern for evaluating individuals. constraint: Sequence[ConstraintProtocol] | None The constraints to apply to the individuals. If None, no constraints are applied. save: PathLike | None The path to save the logbook (in Parquet format). If None, the logbook is not saved.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
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 | |
__post_init__()
¶
Check parameters and initialize the evaluation strategy.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
update_logbook(logbook)
¶
Update the logbook with the new data and save to disk if a path is provided.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
optimize()
¶
This is the main function. Use it to optimize your model.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
individual_creator(cost_function_weight)
¶
Create a custom individual class for DEAP genetic algorithms.
This individual class inherits from list and includes a fitness attribute. It is redefined to work with the
Dask framework, which does not support the default DEAP individual structure created with deap.creator.create.
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/genetic_algorithm.py
Factory¶
seapopym_optimization.algorithm.genetic_algorithm.factory
¶
Factory for creating configured GeneticAlgorithm instances.
This module provides factory methods to simplify the creation of GeneticAlgorithm instances with different evaluation strategies, hiding configuration complexity for business users.
GeneticAlgorithmFactory
¶
Factory for creating GeneticAlgorithm instances with different configurations.
This factory simplifies genetic algorithm creation by encapsulating the configuration logic for evaluation strategies and distribution.
See Also¶
seapopym_optimization.algorithm.genetic_algorithm.genetic_algorithm.GeneticAlgorithm : Main GA class
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/factory.py
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 | |
create_sequential(meta_parameter, cost_function, **kwargs)
staticmethod
¶
Create a GA in sequential mode.
Simplest evaluation mode, suitable for small populations or situations where parallelization is not necessary.
Parameters¶
meta_parameter : GeneticAlgorithmParameters Genetic algorithm parameters cost_function : CostFunctionProtocol Cost function to optimize **kwargs Additional arguments for GeneticAlgorithm
Returns¶
GeneticAlgorithm Instance configured in sequential mode
Examples¶
ga = GeneticAlgorithmFactory.create_sequential(meta_params, cost_function) results = ga.optimize()
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/factory.py
create_parallel(meta_parameter, cost_function, n_jobs=-1, **kwargs)
staticmethod
¶
Create a GA in parallel mode using multiprocessing.
Uses ProcessPoolExecutor to evaluate individuals across multiple CPU cores for improved performance.
Parameters¶
meta_parameter : GeneticAlgorithmParameters Genetic algorithm parameters cost_function : CostFunctionProtocol Cost function to optimize n_jobs : int, default=-1 Number of parallel jobs. If -1, use all available CPUs **kwargs Additional arguments for GeneticAlgorithm
Returns¶
GeneticAlgorithm Instance configured in parallel mode
Examples¶
ga = GeneticAlgorithmFactory.create_parallel(meta_params, cost_function, n_jobs=4) results = ga.optimize()
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/factory.py
create_distributed(meta_parameter, cost_function, client, **kwargs)
staticmethod
¶
Create a GA in distributed mode with Dask.
Automatically detects if data is already distributed (Futures) and distributes if necessary. Uses Dask client.map() with distributed data to evaluate individuals across multiple workers efficiently.
WARNING: This method modifies the cost_function in-place by replacing forcing and observations data with Dask Futures.
Parameters¶
meta_parameter : GeneticAlgorithmParameters Genetic algorithm parameters cost_function : CostFunctionProtocol Cost function to optimize (will be modified in-place) client : Client Dask client for distributed computing **kwargs Additional arguments for GeneticAlgorithm
Returns¶
GeneticAlgorithm GA instance configured for distributed execution
Raises¶
TypeError If client is not a Dask Client instance
Examples¶
from dask.distributed import Client client = Client() ga = GeneticAlgorithmFactory.create_distributed( ... meta_params, cost_function, client ... ) results = ga.optimize() client.close()
Source code in packages/seapopym-optimization/src/seapopym_optimization/algorithm/genetic_algorithm/factory.py
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 | |
Functional Groups¶
Parameters¶
seapopym_optimization.functional_group.base_functional_group
¶
A module that contains the base class for functional groups declaration and parameter management in optimization process.
Parameter
dataclass
¶
The definition of a parameter to optimize.
Parameters¶
name: str The name of the parameter. lower_bound: float The lower bound of the parameter. upper_bound: float The upper bound of the parameter. init_method: Callable[[float, float], float], optional The method used to get the initial value of a parameter. Default is a random uniform distribution that exclude the bounds values.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
__post_init__()
¶
Check that the parameter is correctly defined.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
AbstractFunctionalGroup
dataclass
¶
Bases: ABC
The Generic structure used to store the parameters of a functional group as used in SeapoPym.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
parameters
property
¶
Return the parameters representing the functional group. Order of declaration is preserved.
as_dict()
¶
Return the functional group as a dictionary with parameter names as keys (without functional group name).
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
get_parameters_to_optimize()
¶
Return the parameters to optimize as a sequence of Parameter.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
FunctionalGroupSet
dataclass
¶
The structure used to generate the matrix of all parameters for all functional groups.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
__post_init__()
¶
Check that the functional groups are correctly typed.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
functional_groups_name()
¶
Return the ordered list of the functional groups name.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
unique_functional_groups_parameters_ordered()
¶
Return the unique optimized parameters of all functional groups in the order of declaration.
Used to setup toolbox for optimization algorithms.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
generate(x)
¶
Generate a list of dictionaries representing the functional groups with their parameters values.
The order of the parameters is defined by the unique_functional_groups_parameters_ordered method.
The input x should match the order of the parameters returned by that method.
It is used by the configuration_generator to generate the model.
Parameters¶
x: Sequence[float] A sequence of float values representing the parameters to set for each functional group.
Returns¶
list[AbstractFunctionalGroup] A list of functional groups with their parameters and their corresponding values.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/base_functional_group.py
NoTransport Implementation¶
seapopym_optimization.functional_group.no_transport_functional_groups
¶
This module contains the cost function used to optimize the parameters of the SeapoPym model.
NoTransportFunctionalGroup
dataclass
¶
Bases: AbstractFunctionalGroup
The parameters of a functional group as they are defined in the SeapoPym NoTransport model.
Source code in packages/seapopym-optimization/src/seapopym_optimization/functional_group/no_transport_functional_groups.py
Cost Function¶
Core¶
seapopym_optimization.cost_function.cost_function
¶
This module contains the cost function used to optimize the parameters of the SeapoPym model.
CostFunction
dataclass
¶
The cost function generator for SeapoPym models.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/cost_function.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 | |
__post_init__()
¶
Convert observations to dict if needed and validate observation names match dictionary keys.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/cost_function.py
get_evaluator()
¶
Return the evaluation function to be called on workers.
This method is used by distributed evaluation strategies to obtain the core evaluation function without captured parameters.
Returns¶
Callable[..., tuple[Number, ...]] Function that takes (args, forcing, observations) and returns a tuple of fitness values
Examples¶
evaluator = cost_function.get_evaluator() fitness = evaluator(args, forcing_data, observations_data)
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/cost_function.py
get_distributed_parameters()
¶
Return parameters that should be distributed to workers as a dictionary.
Dask will automatically resolve any Futures contained in this dictionary when it's passed as an argument to client.map().
Returns¶
dict[str, Any] Dictionary with keys: - 'forcing': ForcingParameter or Future - 'observations': Dict of observations {name: observation} or {name: Future}
Notes¶
If you subclass CostFunction and add new distributed parameters, override this method to include them in the returned dictionary.
Examples¶
params = cost_function.get_distributed_parameters() params['forcing']
params['observations']
See Also¶
get_evaluator : Get the evaluation function to use with these parameters
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/cost_function.py
Processors¶
seapopym_optimization.cost_function.processor
¶
Observation processing components for cost function evaluation.
AbstractScoreProcessor
¶
Bases: ABC
Abstract class for processing model state and observations to return a score.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
TimeSeriesScoreProcessor
¶
Bases: AbstractScoreProcessor
Processes observations in time series format by applying preprocessing and comparison metrics.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
__init__(comparator, preprocess=None)
¶
Initialize with a comparator metric.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
process(state, observation)
¶
Compare prediction with observation by applying the comparator. Can pre-process data if needed.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
LogTimeSeriesScoreProcessor
¶
Bases: TimeSeriesScoreProcessor
Processes observations in time series format by applying log preprocessing and comparison metrics.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
process(state, observation)
¶
Compare log prediction with log observation by applying the comparator. Can pre-process data if needed.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
SpatialScoreProcessor
¶
Bases: AbstractScoreProcessor
Processes observations in spatial format by applying comparison metrics.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
process(state, observation)
¶
Compare prediction with observation by applying the comparator.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
aggregate_biomass_by_layer(data, position, name, layer_coordinates, layer_coordinates_name='layer')
¶
Aggregate biomass data by layer coordinates.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/processor.py
Metrics¶
seapopym_optimization.cost_function.metric
¶
Protocols and implementations for metrics to compare model outputs with observations.
MetricProtocol
¶
Bases: Protocol
Protocol for comparing prediction data with observations.
All future metric functions should follow this protocol.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/metric.py
__call__(prediction, observation)
¶
rmse_comparator(prediction, observation)
¶
Calculate Root Mean Square Error (RMSE) between prediction and observation.
Parameters¶
prediction : ArrayLike Predicted values. observation : ArrayLike Observed values.
Returns¶
Number RMSE value.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/metric.py
nrmse_std_comparator(prediction, observation)
¶
Calculate Normalized RMSE (by standard deviation) between prediction and observation.
The RMSE is divided by the standard deviation of the observation to provide a scale-invariant error metric.
Parameters¶
prediction : ArrayLike Predicted values. observation : ArrayLike Observed values.
Returns¶
Number Normalized RMSE value.
Source code in packages/seapopym-optimization/src/seapopym_optimization/cost_function/metric.py
Observations¶
Base¶
seapopym_optimization.observations.protocol
¶
Protocol for observations used in cost functions.
ObservationProtocol
¶
Bases: Protocol
Protocol for observations used in cost function evaluation.
Any observation object used in the optimization framework must implement this protocol. Observations represent empirical or reference data against which model predictions are compared.
Attributes¶
name : str Unique identifier or name of the observation. observation : object The observation data. The type and structure depend on the cost function processor (e.g., xarray.Dataset for time series or spatial data, numpy.ndarray for arrays).
Source code in packages/seapopym-optimization/src/seapopym_optimization/observations/protocol.py
Time Series¶
seapopym_optimization.observations.time_serie
¶
This module contains the cost function used to optimize the parameters of the SeapoPym model.
TimeSeriesObservation
dataclass
¶
Bases: Observation
The structure used to store the observations as a time series.
Meaning that the observation is a time series of biomass values at a given location and layer.
Source code in packages/seapopym-optimization/src/seapopym_optimization/observations/time_serie.py
__post_init__()
¶
Check that the observation data is compliant with the format of the predicted biomass.
Source code in packages/seapopym-optimization/src/seapopym_optimization/observations/time_serie.py
Spatial¶
seapopym_optimization.observations.spatial
¶
This module contains the cost function used to optimize the parameters of the SeapoPym model.
SpatialObservation
dataclass
¶
Bases: Observation
The structure used to store the observations as a spatial dataset.
Meaning that the observation is a set of biomass values at given locations and times. The observation data must be an xarray.DataArray with a single dimension (e.g. "index" or "obs_id") and coordinates for time, latitude, and longitude.
Source code in packages/seapopym-optimization/src/seapopym_optimization/observations/spatial.py
__post_init__()
¶
Check that the observation data is compliant with the format of the predicted biomass.
Source code in packages/seapopym-optimization/src/seapopym_optimization/observations/spatial.py
Constraints¶
seapopym_optimization.constraint.energy_transfert_constraint
¶
All the constraints (as penalty functions) used by the DEAP library to contraint parameters initialization.
EnergyCoefficientConstraint
dataclass
¶
Constraint to ensure that the sum of all energy transfert coefficients is within a specified range.
This constraint is used to apply a penalty if the sum of the coefficients is greater than max_energy_coef_value
or less than min_energy_coef_value.
Attributes.¶
parameters_name: Sequence[str]
The names of the parameters that are involved in the constraint, typically the energy transfert
coefficients.
min_energy_coef_value: float
The minimum allowed value for the sum of the energy transfert coefficients.
max_energy_coef_value: float
The maximum allowed value for the sum of the energy transfert coefficients.
Source code in packages/seapopym-optimization/src/seapopym_optimization/constraint/energy_transfert_constraint.py
generate(parameter_names)
¶
Generate the DeltaPenalty object used by the DEAP library.
Apply the penalty on individuals that do not satisfy the constraint.