cbx.dynamics.CBXDynamic#
- class cbx.dynamics.CBXDynamic(f, noise='isotropic', batch_args=None, dt=0.01, alpha=1.0, sigma=5.1, lamda=1.0, correction='no_correction', correction_eps=0.001, compute_consensus=None, **kwargs)[source]#
Bases:
ParticleDynamic
The base class for consensus based dynamics
This class implements functionality that is specific to consensus based dynamics. It inherits from the ParticleDynamic class.
- Parameters:
f – Callable The function to optimize.
noise – str or Callable, optional A string can be one of ‘isotropic’, ‘anisotropic’, or ‘sampling’. It is also possible to use a Callable instead of a string. This Callable needs to accept a single argument, which is the dynamic object. Default: ‘isotropic’.
batch_args – dict, optional The batch arguments. Default: None.
dt – float, optional The time step size \(dt\) of the dynamic. Default: 0.1.
alpha – float, optional The alpha parameter \(\alpha\) of the dynamic. Default: 1.0.
sigma – float, optional The sigma parameter \(\sigma\) of the dynamic, scaling the noise. Default: 5.1.
lamda – float, optional The lamda parameter \(\lambda\) of the dynamic. Default: 1.0.
max_time – float, optional The maximum time to run the dynamic. Default: None.
correction – str or Callable, optional The correction method. Default: ‘no_correction’. One of ‘no_correction’, ‘heavi_side’, ‘heavi_side_reg’ or a Callable.
correction_eps – float, optional The parameter \(\epsilon\) for the regularized correction. Default: 1e-3.
- Returns:
None
- init_alpha(alpha)[source]#
Initialize alpha per batch. If alpha is a float it is broadcasted to an array similar to x with dimensions (x.shape[0], 1). Otherwise, it is set to the given parameter.
- Parameters:
alpha – The initial value of alpha
- Returns:
None
- init_batch_idx(batch_args)[source]#
Initializes the batch index for the given batch arguments.
- Parameters:
batch_args –
dict. Dictionary containing the batch arguments.
- sizeint, optional
The size of each batch. Defaults to self.N.
- partialbool, optional
Whether to allow partial batches. Defaults to True.
- seedint, optional
The seed for random number generation. Defaults to 42.
- varstr, optional
The variable to use for resampling. Defaults to ‘resample’.
- Returns:
None
- set_batch_idx_unbatched()[source]#
Set the batch index for the particles.
This method sets the indices for unbatched dynamics.
- Parameters:
None
- Returns:
None
- set_batch_idx_batched()[source]#
Set the batch index for the particles.
This method sets the batch index batched dynamics. If the indices are exhausted, it generates new indices using np.repeat and np.arange.
If the self.batch_var is set to ‘concat’, we concatenate the new indices with the existing indices using np.concatenate, otherwise, it replaces the existing indices with the new indices.
After updating the indices, we set the batch_idx to the first batch_size columns of the indices, and remove these columns from the indices.
If batch_partial is True, we set particle_idx to consensus_idx, otherwise, to Ellipsis.
- Parameters:
None
- Returns:
None
- default_sched()[source]#
A function that returns a default scheduler.
- Parameters:
None
- Returns:
The scheduler object.
- Return type:
- set_correction(correction)[source]#
Set the correction method for the object.
- Parameters:
correction ((str or Callable): The type of correction to be set. Can be one of the following:) –
‘no_correction’: No correction, correction is the identity.
’heavi_side’: Calculate the Heaviside correction.
’heavi_side_reg’: Calculate the regularized Heaviside correction, with parameter eps.
- Returns:
None
- correction(x)[source]#
Calculate the correction for the given input.
- Parameters:
(ndarray) (x)
- Returns:
The correction value.
- Return type:
ndarray
- set_noise(noise)[source]#
Set the noise model for the object.
- Parameters:
Callable) (noise (str or) –
‘isotropic’ or None: Set the isotropic noise model.
’anisotropic’: Set the anisotropic noise model.
’sampling’: Set the sampling noise model.
’covariance’: Set the covariance noise model.
else: use the given input as a callable.
- Return type:
None
- noise()[source]#
Calculate the noise vector. Here, we use the callable
noise_callable
, which takes the dynamic as an input viaself
.- Parameters:
None
- Returns:
The noise vector.
- Return type:
ndarray
- update_covariance()[source]#
Update the covariance matrix \(\text{Cov}(x)\) of the noise model
Parameters
- Return type:
None.
- pre_step()[source]#
The pre-step function. This function is used in meth:step before the inner step is performed. This function can be overwritten by subclasses to perform any pre-steps before the inner step.
- Parameters:
None
- Returns:
None
- inner_step()[source]#
This function is used in meth:step to perform the inner step. This function implements the actual update of the dynamic and is therfore the most important function in the dynamics class. This function should be overwritten by subclasses to perform any inner steps.
- Parameters:
None
- Returns:
None
- post_step()[source]#
Execute the post-step operations after each iteration.
This function updates the difference between the current particle position and the previous particle position if the ‘x_old’ attribute exists in the object. The difference is computed using the numpy.linalg.norm function along the specified axes and is divided by the total number of particles.
The function then calls the ‘update_best_cur_particle’ method to update the best current particle position. Next, it calls the ‘update_best_particle’ method to update the best particle position. After that, it calls the ‘track’ method to track the progress of the optimization algorithm. Then, it calls the ‘process_particles’ method to perform any necessary post-processing operations on the particles.
Finally, the function increments the ‘it’ attribute by 1 to keep track of the current iteration.
- Parameters:
None
- Returns:
None