easy_vic_build.tools.calibrate_func.algorithm_NSGAII
NSGA-II base implementation for calibration workflows.
This module defines NSGAII_Base, a reusable DEAP-based NSGA-II runner
with checkpoint persistence, generation history tracking, and optional Pareto
front plotting.
Classes
|
A class that implements the NSGA-II (Non-dominated Sorting Genetic Algorithm II) for multi-objective optimization. |
- class easy_vic_build.tools.calibrate_func.algorithm_NSGAII.NSGAII_Base(algParams={'cxProb': 0.7, 'maxGen': 250, 'mutateProb': 0.2, 'popSize': 40}, save_path='checkpoint.pkl')[source]
Bases:
objectA class that implements the NSGA-II (Non-dominated Sorting Genetic Algorithm II) for multi-objective optimization. This class provides methods for setting up the genetic algorithm, evaluating individuals, applying genetic operators (crossover, mutation), selecting the next generation, and saving/loading the algorithm state.
- popSize
The population size for each generation.
- Type:
int
- maxGen
The maximum number of generations to run the algorithm.
- Type:
int
- save_path
The path to save and load the algorithm state.
- Type:
str
- history
A history of the population and non-dominated fronts at each generation.
- Type:
list
- current_generation
The current generation number.
- Type:
int
- initial_population
The initial population before any genetic operators are applied.
- Type:
list
- population
The current population of individuals.
- Type:
list
- __init__(algParams, save_path):
Initializes the NSGA-II algorithm with the given parameters and attempts to load the state.
- get_obs():
Placeholder function to get the observed values (to be designed for specific use cases).
- get_sim():
Placeholder function to get the simulated values (to be designed for specific use cases).
- set_algorithm_params(popSize, maxGen, cxProb, mutateProb):
Sets the parameters for the genetic algorithm (population size, max generations, crossover probability, and mutation probability).
- createFitness():
Creates the fitness function for individuals.
- createInd():
Creates an individual representation (a list).
- samplingInd():
Samples a new individual by generating random values for its elements.
- registerInd():
Registers the individual sampling function with the toolbox.
- registerPop():
Registers the population initialization function with the toolbox.
- evaluate(ind):
A placeholder function for evaluating an individual’s fitness (to be customized for specific use cases).
- registerEvaluate():
Registers the evaluation function with the toolbox.
- evaluatePop(population):
Evaluates the fitness of the entire population.
- operatorMate(parent1, parent2):
Defines the crossover operation for mating two individuals (using a two-point crossover).
- operatorMutate(ind):
Defines the mutation operation for an individual (using a bit-flip mutation).
- operatorSelect(population):
Defines the selection operation (using tournament selection).
- registerOperators():
Registers the genetic operators (mate, mutate, and select) with the toolbox.
- apply_genetic_operators(offspring):
Applies the genetic operators (crossover and mutation) to the offspring.
- select_next_generation(combined):
Selects the next generation by sorting individuals based on Pareto dominance and applying crowding distance.
- print_results(population):
Prints the results of the best individual from the final population.
- load_state():
Loads the algorithm state from the specified save path (if a saved state exists).
- save_state():
Saves the current algorithm state (current generation, population, and history) to the specified save path.
- run():
Runs the NSGA-II algorithm for the specified number of generations, applying genetic operators and selecting the next generation.
Initializes the NSGA-II algorithm with the given parameters.
- Parameters:
algParams (dict) – Dictionary containing the algorithm parameters: - popSize: The population size for each generation. - maxGen: The maximum number of generations to run the algorithm. - cxProb: The crossover probability. - mutateProb: The mutation probability.
save_path (str, optional) – The path to save and load the algorithm state (default is “checkpoint.pkl”).
- __init__(algParams={'cxProb': 0.7, 'maxGen': 250, 'mutateProb': 0.2, 'popSize': 40}, save_path='checkpoint.pkl')[source]
Initializes the NSGA-II algorithm with the given parameters.
- Parameters:
algParams (dict) – Dictionary containing the algorithm parameters: - popSize: The population size for each generation. - maxGen: The maximum number of generations to run the algorithm. - cxProb: The crossover probability. - mutateProb: The mutation probability.
save_path (str, optional) – The path to save and load the algorithm state (default is “checkpoint.pkl”).
- set_algorithm_params(popSize=None, maxGen=None, cxProb=None, mutateProb=None, **kwargs)[source]
Sets the parameters for the genetic algorithm.
- Parameters:
popSize (int, optional) – The population size for each generation (default is 40).
maxGen (int, optional) – The maximum number of generations to run the algorithm (default is 250).
cxProb (float, optional) – The crossover probability (default is 0.7).
mutateProb (float, optional) – The mutation probability (default is 0.2).
- get_obs()[source]
Placeholder function to get observed values (to be designed for specific use cases).
- Return type:
None
- get_sim()[source]
Placeholder function to get simulated values (to be designed for specific use cases).
- Return type:
None
- samplingInd()[source]
Samples a new individual by generating random values for its elements.
- Returns:
A new individual sampled with random elements.
- Return type:
Individual
- evaluate(ind)[source]
A placeholder function for evaluating an individual’s fitness.
- Parameters:
ind (Individual) – The individual to evaluate.
- Returns:
A tuple containing the fitness values.
- Return type:
tuple
- static operatorMate(parent1, parent2)[source]
Defines the crossover operation for mating two individuals.
- Parameters:
parent1 (Individual) – The first parent individual.
parent2 (Individual) – The second parent individual.
- Returns:
A tuple containing the offspring resulting from the crossover.
- Return type:
tuple
- static operatorMutate(ind)[source]
Defines the mutation operation for an individual.
- Parameters:
ind (Individual) – The individual to mutate.
- Returns:
A tuple containing the mutated individual.
- Return type:
tuple
- static operatorSelect(population)[source]
Defines the selection operation for choosing individuals from the population.
- Parameters:
population (list of Individual) – The population from which to select individuals.
- Returns:
A list of selected individuals.
- Return type:
list
- registerOperators()[source]
Registers the genetic operators (mate, mutate, and select) with the toolbox.
- evaluatePop(population)[source]
Evaluates the fitness of the entire population.
- Parameters:
population (list of Individual) – The population to evaluate.
- apply_genetic_operators(offspring)[source]
Applies the genetic operators (crossover and mutation) to the offspring.
- Parameters:
offspring (list of Individual) – The offspring to apply the genetic operators to.
- select_next_generation(combined)[source]
Selects the next generation from the combined population and offspring.
- Parameters:
combined (list of Individual) – The combined population of parents and offspring.
- Returns:
The selected next generation.
- Return type:
list
- load_state()[source]
Loads the algorithm state from the specified save path if a saved state exists.
- save_state()[source]
Saves the current algorithm state (current generation, population, and history) to the specified save path.
- print_results(population)[source]
Prints the results of the best individual from the final population.
- Parameters:
population (list of Individual) – The final population.
- plot_front_pairwise(population, front, gen, names_plot=None, plot_dir='pareto_progress', transform_func=None)[source]
Plot pairwise objective scatter for full population and first front.
- Parameters:
population (list) – Population to visualize (typically parent + offspring).
front (list) – First non-dominated front.
gen (int) – Generation index used in figure title.
names_plot (list of str, optional) – Objective axis labels. If
None, labels are generated asobj1,obj2, …plot_dir (str, optional) – Directory used to store figure output.
transform_func (callable, optional) – Optional transformation applied to objective arrays before plotting.
- run(plot_progress=False, plot_dir='pareto_progress', names_plot=None, transform_func=None)[source]
Runs the NSGA-II algorithm for the specified number of generations.
This method applies genetic operators, evaluates individuals, selects the next generation, and stores the results.
- Parameters:
plot_progress (bool, optional) – Whether to save a pairwise Pareto plot each generation.
plot_dir (str, optional) – Output directory for progress plots.
names_plot (list of str, optional) – Objective labels passed to
plot_front_pairwise().transform_func (callable, optional) – Optional transform function passed to
plot_front_pairwise().
- Returns:
The final population after all generations.
- Return type:
list