optimizer

Trials-and-errors and PSO optomizers

The optimizer module proposing different optimization algorithms.

The output of this functions is generally dimensions of linkages.

Created on Fri Mar 8 13:51:45 2019.

@author: HugoFara

pylinkage.optimizer.generate_bounds(center, min_ratio=5, max_factor=5)

Simple function to generate bounds from a linkage.

Parameters
  • center (sequence) – 1-D sequence, often in the form of linkage.get_num_constraints().

  • min_ratio (float, optional) – Minimal compression ratio for the bounds. Minimal bounds will be of the shpae center[x] / min_ratio. The default is 5.

  • max_factor (float, optional) – Dilation factor for the upper bounds. Maximal bounds will be of the shpae center[x] * max_factor. The default is 5.

pylinkage.optimizer.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, optional) – A list of initial dimension. If None, dimensions will be generated randomly between bounds. The default is None.

  • dimensions (int, optional) – 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, optional) – Number of particles in the swarm. The default is 100.

  • iner (float, optional) – Inertia of each particle, w in pyswarms. The default is .3.

  • leader (float, optional) – Learning coefficient of each particle, c1 in pyswarms. The default is .2.

  • follower (float, optional) – Social coefficient, c2 in pyswarms. The default is .5.

  • neighbors (int, optional) – Number of neighbors to consider. The default is 17.

  • iters (int, optional) – Number of iterations to describe. The default is 200.

  • order_relation (callable(float, float) -> float, optional) – How to compare scores. Should not be anything else than the built-in max and min functions. The default is max.

  • verbose (bool, optional) – The optimization state will be printed in console if True. The default is True.

  • **kwargs (dict) – keyword arguments to pass to pyswarm.local.single.LocalBestPSO.

Returns

List of 3 elements: best score, best dimensions and initial positions.

Return type

list[float, list[float], list[list[float]]]

pylinkage.optimizer.tqdm_verbosity(iterable, verbose=True, *args, **kwargs)

Wrapper for tqdm, that let you specify if you want verbosity.

pylinkage.optimizer.trials_and_errors_optimization(eval_func, linkage, parameters=None, n_results=10, divisions=5, bounds=None, order_relation=<built-in function max>, verbose=True)

Return the list of dimensions optimizing eval_func.

Each dimensions set has a score, which is added in an array of n_results results, containg the linkages with 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, optional) – Parameters that will be modified. Geometric constraints. If not, it will be assignated tuple(linkage.get_num_constraints()). The default is None.

  • n_results (int, optional) – Number of best cancidates to return. The default is 10.

  • divisions (int, optional) – Number of subdivisions between bounds. The default is 5.

  • bounds (tuple[tuple], optional) – A 2-uple (tuple of two elements), containing the minimal and maximal bounds. If None, we will use parameters as center. The default is None.

  • order_relation (callable, optional) – A function of two arguments, should return the best score of two scores. Common examples are min, max, abs. The default is max.

  • verbose (bool, optional) – The number of cominations will be printed in console if True. The default is True.

Returns

results – 3-uple of score, dimensions and initial position for each Linkage to return. Its size is {n_results}.

Return type

tuple[tuple[float, tuple[float], tuple[tuple[float]]]]

pylinkage.optimizer.variator(center, divisions, bounds)

Return an iterable of all possibles variations of elements.

Number of variations: ((max_dim - 1 / min_dim) / delta_dim) ** len(ite).

Because linkage are 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 (sequence of floats) – Elements that should vary.

  • divisions (int) – Number of subdivisions between bounds.

  • bounds (tuple[tuple[float]]) – 2-uple of minimal then maximal bounds.

Returns

Each element is the list of floats with little variations.

Return type

generator