optimizer

Trials-and-errors and PSO optomizers

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

@author: HugoFara

Module proposing different function optimization algorithms. The output is generally leg dimensions of walking linkages.

pylinkage.optimizer.particle_swarm_optimization(eval_func, linkage, center=None, dimensions=None, n_particles=100, iner=0.3, leader=0.2, follower=0.5, iters=200, bounds=None, **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.

eval_funccallable, must return float

The evaluation function. Input: a set of dimensions and intial position for the linkage Output: a score, a float The swarm will look for the HIGHEST score.

linkageLinkage

Linkage to be optimized. Make sure to give an optimized linkage for better results

centerlist

A list of initial dimension. If None, dimensions will be generated randomly between bounds. The default is None.

dimensionsint

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_particlesfloat, optional

Number of particles in the swarm. The default is 100.

inerfloat, optional

Inertia of each particle, w in pyswarms. The default is .3.

leaderfloat, optional

Learning coefficient of each particle, c1 in pyswarms. The default is .2.

followerfloat, optional

Social coefficient, c2 in pyswarms. The default is .5.

itersint, optional

Number of iterations to describe. The default is 200.

**kwargsdict

keyword arguments to pass to pyswarm.local.single.LocalBestPSO.

list

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

pylinkage.optimizer.recurs_variator(ite, copy, coef_list, num=0)

Recursive dimensions generator.

Called by variator. Only “copy” is modified.

pylinkage.optimizer.trials_and_errors_optimization(eval_func, linkage, parameters=None, n_results=10, delta_dim=0.5, min_dim=2, max_dim=2)

Return the list of dimensions optimizing eval_func.

We start wy making the dimensions vary, then we try a crank revolution on 10 points. If no error we try on 75 points (higher precision).

Each dimensions set has a score, which is added in an array of n_results results, containg the linkages with best scores.

eval_funccallable

Evaluation function. Its signature should be R^len(linkage.joints) → R.

linkagepylinkage.linkage.Linkage

Linkage to evaluate.

parameterslist

Parameters that will be modified. Geometric constraints. If not, it will be assignated tuple(linkage.get_num_constraints()). The default is None.

n_resultsint, optional

Number of best cancidates to return. The default is 10.

delta_dimfloat, optional

Dimension variation between two consecutive tries. The default is .5.

min_dimfloat, optional

Minimal scale reduction. Each parameter should not be below original size / min_dim. The default is 2.

max_dimfloat, optional

Maximal scale augmentation factor. The default is 2.

resultstuple

tuple of dimensions, score, initial position for each Linkage to return. Its size is {n_results}.

pylinkage.optimizer.variator(ite, delta_dim, min_dim=5, max_dim=5)

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.

itesequence of floats

Elements that should vary.

delta_dimfloat

Scale factor for each variation.

min_dimfloat, optional

Minimal scale reduction (dimensions not shorter that dim/min_dim). The default is 5.

max_dimfloat, optional

maximal scale augmentation (not above dim * max_dim). The default is 5.

generator

Each element is the list of floats with little variations.