Class NormalOptimizer<T, TInput, TOutput>
- Namespace
- AiDotNet.Optimizers
- Assembly
- AiDotNet.dll
Implements a normal optimization algorithm with adaptive parameters.
public class NormalOptimizer<T, TInput, TOutput> : OptimizerBase<T, TInput, TOutput>, IOptimizer<T, TInput, TOutput>, IModelSerializer
Type Parameters
TThe numeric type used for calculations, typically float or double.
TInputTOutput
- Inheritance
-
OptimizerBase<T, TInput, TOutput>NormalOptimizer<T, TInput, TOutput>
- Implements
-
IOptimizer<T, TInput, TOutput>
- Inherited Members
- Extension Methods
Remarks
The NormalOptimizer uses a combination of random search and adaptive parameter tuning to find optimal solutions. It incorporates elements from genetic algorithms but operates on a single solution at a time.
For Beginners: Imagine you're trying to find the highest peak in a mountain range, but you can't see very far. This optimizer is like a hiker who starts at random spots, climbs to the nearest peak, and then jumps to another random spot. The hiker learns from each climb and adjusts their strategy (like how far to jump or how carefully to look around) based on whether they're finding higher peaks or not.
Constructors
NormalOptimizer(IFullModel<T, TInput, TOutput>, GeneticAlgorithmOptimizerOptions<T, TInput, TOutput>?)
Initializes a new instance of the NormalOptimizer class.
public NormalOptimizer(IFullModel<T, TInput, TOutput> model, GeneticAlgorithmOptimizerOptions<T, TInput, TOutput>? options = null)
Parameters
modelIFullModel<T, TInput, TOutput>The model to optimize.
optionsGeneticAlgorithmOptimizerOptions<T, TInput, TOutput>The optimization options.
Remarks
This constructor sets up the NormalOptimizer with the provided options and dependencies. If no options are provided, it uses default settings.
For Beginners: This is like preparing for your hike. You're deciding what equipment to bring, how long you'll hike for, and setting up rules for how you'll explore the mountain range.
Methods
Deserialize(byte[])
Deserializes a byte array to restore the optimizer's state.
public override void Deserialize(byte[] data)
Parameters
databyte[]The byte array containing the serialized optimizer state.
Remarks
This method takes a byte array (previously created by the Serialize method) and uses it to restore the optimizer's state, including its options.
For Beginners: This is like using a saved snapshot of a hiking strategy to set up your approach exactly as it was before. You're recreating all the details of your previous setup from the saved information.
Exceptions
- InvalidOperationException
Thrown when deserialization of optimizer options fails.
GetOptions()
Gets the current optimization algorithm options.
public override OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions()
Returns
- OptimizationAlgorithmOptions<T, TInput, TOutput>
The current optimization algorithm options.
Remarks
This method returns the current set of options used by the NormalOptimizer.
For Beginners: This is like checking your current hiking plan and equipment list.
Optimize(OptimizationInputData<T, TInput, TOutput>)
Performs the optimization process.
public override OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData)
Parameters
inputDataOptimizationInputData<T, TInput, TOutput>The input data for the optimization process.
Returns
- OptimizationResult<T, TInput, TOutput>
The result of the optimization process.
Remarks
This method implements the main optimization loop. It generates random solutions, evaluates them, and keeps track of the best solution found. It also adapts its parameters based on the performance of each solution.
For Beginners: This is your actual hike through the mountain range. You're repeatedly: 1. Picking a random spot to start from. 2. Climbing to the nearest peak and measuring its height. 3. Remembering the highest peak you've found so far. 4. Adjusting your strategy based on whether you're finding higher peaks or not. 5. Deciding whether to keep going or stop if you think you've found the highest peak.
Serialize()
Serializes the current state of the optimizer into a byte array.
public override byte[] Serialize()
Returns
- byte[]
A byte array representing the serialized state of the optimizer.
Remarks
This method converts the current state of the optimizer, including its options, into a byte array that can be stored or transmitted.
For Beginners: This is like taking a snapshot of your current hiking strategy and equipment setup, so you can recreate it exactly later or share it with others.
UpdateAdaptiveParameters(OptimizationStepData<T, TInput, TOutput>, OptimizationStepData<T, TInput, TOutput>)
Updates the adaptive parameters of the optimizer based on the current and previous optimization steps.
protected override void UpdateAdaptiveParameters(OptimizationStepData<T, TInput, TOutput> currentStepData, OptimizationStepData<T, TInput, TOutput> previousStepData)
Parameters
currentStepDataOptimizationStepData<T, TInput, TOutput>Data from the current optimization step.
previousStepDataOptimizationStepData<T, TInput, TOutput>Data from the previous optimization step.
Remarks
This method adjusts various parameters of the optimization process based on the performance of the current solution compared to the previous one. It updates feature selection, mutation rate, exploration/exploitation balance, population size, and crossover rate.
For Beginners: This is like adjusting your hiking strategy based on your recent experiences. If you're finding higher peaks, you might decide to look more closely in the areas you're in. If not, you might decide to take bigger jumps to new areas or look at different aspects of the landscape.
UpdateOptions(OptimizationAlgorithmOptions<T, TInput, TOutput>)
Updates the optimization algorithm options.
protected override void UpdateOptions(OptimizationAlgorithmOptions<T, TInput, TOutput> options)
Parameters
optionsOptimizationAlgorithmOptions<T, TInput, TOutput>The new optimization algorithm options to apply.
Remarks
This method updates the optimizer's options with a new set of options. It checks if the provided options are of the correct type before applying them.
For Beginners: This is like updating your hiking plan with new information or equipment. You're making sure the new plan is compatible with your current approach before adopting it.
Exceptions
- ArgumentException
Thrown when the provided options are not of the expected type.