optimizer

Trials-and-errors and PSO optomizers

The optimizer module proposing different optimization algorithms.

The output of these functions is generally dimensions of linkages.

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

@author: HugoFara

pylinkage.optimizer.fast_variator(divisions, bounds)

Return an iterable of all possibles variations of elements.

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]]) – 2-uple of minimal then maximal bounds.

Returns:

An iterable of all the dimension combinations.

Return type:

generator

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 shape center[x] / min_ratio. The default is 5.

  • max_factor (float, optional) – Dilation factor for the upper bounds. Maximal bounds will be of the shape 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 dimensions. 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.

  • inertia (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.

  • bounds (sequence of two list of float) – Bounds to the space, in format (lower_bound, upper_bound).

  • order_relation (callable(float, float) -> float, optional) – How to compare scores. There 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 the 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.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 (sequence of float) – 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

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, **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, optional) – 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, optional) – Number of the best candidates to return. The default is 10.

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

  • **kwargs (dict, optional) –

    Extra arguments for the optimization.

    boundstuple[tuple], optional

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

    order_relationcallable, optional

    A function of two arguments, should return the best score of two scores. Common examples are min, max, abs. The default is max.

    verbosebool, optional

    The number of combinations will be printed in console if True. The default is True.

    sequentialbool

    If True, two consecutive linkages will have a small variation.

Returns:

results – 3-uplet 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]]]]