pylinkage.optimization package
Submodules
pylinkage.optimization.grid_search module
Implementation of a grid search optimization.
It should be used for reference only as the search space will almost certainly be too big.
- pylinkage.optimization.grid_search.fast_variator(divisions, bounds)
- Return an iterable of elements’ all possibles variations. - Number of variations: ((max_dim - 1 / min_dim) / delta_dim) ** len(ite). - Here the order in the variations is not important. - Parameters:
- divisions (int) – Number of subdivisions between bounds. 
- bounds (tuple[tuple[float], tuple[float]]) – 2-uple of minimal then maximal bounds. 
 
- Returns:
- An iterable of all the dimension combinations. 
- Return type:
- Generator[float] 
 
- pylinkage.optimization.grid_search.sequential_variator(center, divisions, bounds)
- Return an iterable of each possible variation for the elements. - Number of variations: ((max_dim - 1 / min_dim) / delta_dim) ** len(ite). - Because linkage is not tolerant to violent changes, the order of output for the coefficients is very important. - The coefficient is in order: middle → min (step 2), min → middle (step 2), middle → max (step 1), so that there is no huge variation. - Parameters:
- center (Iterable[float]) – Elements that should vary. 
- divisions (int) – Number of subdivisions between bounds. 
- bounds (tuple[tuple[float], tuple[float]]) – 2-uple of minimal then maximal bounds. 
 
- Returns:
- An iterable of all the dimension combinations. 
- Return type:
- Generator[float] 
 
- pylinkage.optimization.grid_search.tqdm_verbosity(iterable, verbose=True, *args, **kwargs)
- Wrapper for tqdm, that let you specify if you want verbosity. - Deprecated since version 0.6.0: tqdm_verbosity will be removed in pylinkage 0.7.0, as tqdm can be disabled with the argument disable=True. - Parameters:
- iterable 
- verbose – (Default value = True) 
- args – Ordered args to pass to tqdm 
- kwargs – Keyword args for tqdm 
 
 
- pylinkage.optimization.grid_search.trials_and_errors_optimization(eval_func, linkage, parameters=None, n_results=10, divisions=5, **kwargs)
- Return the list of dimensions optimizing eval_func. - Each dimension set has a score, which is added in an array of n_results results, contains the linkages with the best scores in a maximization problem by default. - Parameters:
- eval_func (Callable) – Evaluation function. Input: (linkage, num_constraints, initial_coordinates). Output: score (float) 
- linkage (pylinkage.linkage.Linkage) – Linkage to evaluate. 
- parameters (list) – Parameters that will be modified. Geometric constraints. If not, it will be assigned tuple(linkage.get_num_constraints()). The default is None. 
- n_results (int) – Number of the best candidates to return. The default is 10. 
- divisions (int) – Number of subdivisions between bounds. The default is 5. 
- kwargs (dict) – - Extra arguments for the optimization. 
- boundsA 2-uple (tuple of two elements), containing the minimal and maximal bounds.
- If None, we will use parameters as a center. (Default value = None). 
 
- order_relationA function of two arguments, should return the best score of two
- scores. Common examples are min, max, abs. (Default value = - max()).
 
- verboseThe number of combinations will be printed in console if True.
- (Default value = True). 
 
- sequential : If True, two consecutive linkages will have a small variation. 
 
 
- Returns:
- 3-uplet of score, dimensions and initial position for each Linkage to return. Its size is {n_results}. 
- Return type:
- list[MutableAgent] 
 
pylinkage.optimization.particle_swarm module
Implementation of a particle swarm optimization.
Created on Fri Mar 8, 13:51:45 2019.
@author: HugoFara
- pylinkage.optimization.particle_swarm.particle_swarm_optimization(eval_func, linkage, center=None, dimensions=None, n_particles=100, leader=3.0, follower=0.1, inertia=0.6, neighbors=17, iters=200, bounds=None, order_relation=<built-in function max>, verbose=True, **kwargs)
- Particle Swarm Optimization wrapper for pyswarms. - This function is a simple wrapper to optimize a linkage using PSO. It will mainly call the LocalBestPSO function from pyswarms.single. - Parameters:
- eval_func (Callable -> float) – The evaluation function. Input: (linkage, num_constraints, initial_coordinates). Output: score (float). The swarm will look for the HIGHEST score. 
- linkage (pylinkage.linkage.Linkage) – Linkage to be optimized. Make sure to give an optimized linkage for better results 
- center (list) – A list of initial dimensions. If None, dimensions will be generated randomly between bounds. The default is None. 
- dimensions (int) – Number of dimensions of the swarm space, number of parameters. If None, it takes the value len(tuple(linkage.get_num_constraints())). The default is None. 
- n_particles (float) – Number of particles in the swarm. The default is 100. 
- inertia (float) – Inertia of each particle, w in pyswarms. The default is .3. 
- leader (float) – Learning coefficient of each particle, c1 in pyswarms. The default is .2. 
- follower (float) – Social coefficient, c2 in pyswarms. The default is .5. 
- neighbors (int) – Number of neighbors to consider. The default is 17. 
- iters (int) – Number of iterations to describe. The default is 200. 
- bounds (sequence of two list of float) – Bounds to the space, in format (lower_bound, upper_bound). (Default value = None) 
- order_relation (Callable(float, float) -> float) – How to compare scores. There should not be anything else than the built-in max and min functions. The default is max. 
- verbose (bool) – The optimization state will be printed in the console if True. (Default value = True). 
- kwargs (dict) – keyword arguments to pass to pyswarm.local.single.LocalBestPSO. 
 
- Returns:
- List of Agents: best score, best dimensions and initial positions. 
- Return type:
- List[Agent] 
 
pylinkage.optimization.utils module
This utility module provides various useful functions for optimization.
Created on Mon Jul 12 00:00:01 2021.
@author: HugoFara
- pylinkage.optimization.utils.generate_bounds(center, min_ratio=5, max_factor=5)
- Simple function to generate bounds from a linkage. - Parameters:
- center (Iterable) – 1-D sequence, often in the form of - linkage.get_num_constraints().
- min_ratio (float) – Minimal compression ratio for the bounds. Minimal bounds will be of the shape center[x] / min_ratio. (Default value = 5) 
- max_factor (float) – Dilation factor for the upper bounds. Maximal bounds will be of the shape center[x] * max_factor. (Default value = 5) 
 
 
- pylinkage.optimization.utils.kinematic_maximization(func)
- Standard run for any linkage before a complete fitness evaluation. - This decorator makes a kinematic simulation, before passing the loci to the decorated function. In case of error, the penalty value is -float(‘inf’) - Parameters:
- func (Callable) – Fitness function to be decorated. 
 
- pylinkage.optimization.utils.kinematic_minimization(func)
- Standard run for any linkage before a complete fitness evaluation. - This decorator makes a kinematic simulation, before passing the loci to the decorated function. In case of error, the penalty value is float(‘inf’) - Parameters:
- func (Callable) – Fitness function to be decorated. 
 
Module contents
Optimization package.