psopt.Permutation

class psopt.Permutation(obj_func, candidates, constraints=None, **kwargs)

Solver to find an optimal permutation of candidates

Implementation based on:
Pan, Q.-K., Fatih Tasgetiren, M., and Liang, Y.-C. (2008)
A discrete particle swarm optimization algorithm for the no-wait flowshop scheduling problem.
Parameters:
  • obj_func – objective function (or method) to be optimized. Must only accept the candidates as input. If the inherited structure does not allow it, use functools.partial to comply
  • candidates – list of available candidates to the objective function
  • constraints – function or list of functions to limit the feasible solution space
Returns:

Permutation optimizer object

Example

>>> candidates = [2,4,5,6,3,1,7]
>>> # e.g. obj_func([a, b, c, d, e]) ==> a + b/2 + c/3 + d/4 + e/5
>>> def obj_func(x): return sum([a / (i+1) for i, a in enumerate(x)])
>>> # constraint: sum of values cannot be greater than 16
>>> constraint = {"fn":sum, "type":">", "value":16}
>>> # minimize the obj function
>>> opt = Permutation(obj_func, candidates, constraints=constraint)
>>> sol = opt.minimize(selection_size=5)
maximize(selection_size=None, verbose=0, **kwargs)

Seeks the solution that yields the maximum objective function value

Parameters:
  • selection_size (int) – number of candidates that compose a solution.
  • verbose (int) –

    controls the verbosity while optimizing

    1. Display nothing (default);
    2. Display statuses on console;
    3. Display statuses on console and store them in .logs.
  • w (float or sequence) – The inertia factor controls the contribution of the previous movement. If a single value is provided, w is fixed, otherwise it linearly alters from min to max within the sequence provided.
  • c1 (float) – The self-confidence factor controls the contribution derived by the difference between a particle’s current position and it’s best position found so far.
  • c2 (float) – The swarm-confidence factor controls the contribution derived by the difference between a particle’s current position and the swarm’s best position found so far.
  • population (float or int) – Factor to cover the search space (e.g. 0.5 would generate a number of particles of half the search space). If population is greater than one, the population size will have its value assigned.
  • max_iter (int) – Maximum possible number of iterations (default 300).
  • early_stop (int) – Maximum number of consecutive iterations with no improvement that the algorithm accepts without stopping (default max_iter).
Returns:

a Result object containing the solution, metadata

and stored metrics history

minimize(selection_size=None, verbose=0, **kwargs)

Seeks the solution that yields the minimum objective function value

Parameters:
  • selection_size (int) – number of candidates that compose a solution.
  • verbose (int) –

    controls the verbosity while optimizing

    1. Display nothing (default);
    2. Display statuses on console;
    3. Display statuses on console and store them in .logs.
  • w (float or sequence) – The inertia factor controls the contribution of the previous movement. If a single value is provided, w is fixed, otherwise it linearly alters from min to max within the sequence provided.
  • c1 (float) – The self-confidence factor controls the contribution derived by the difference between a particle’s current position and it’s best position found so far.
  • c2 (float) – The swarm-confidence factor controls the contribution derived by the difference between a particle’s current position and the swarm’s best position found so far.
  • population (float or int) – Factor to cover the search space (e.g. 0.5 would generate a number of particles of half the search space). If population is greater than one, the population size will have its value assigned.
  • max_iter (int) – Maximum possible number of iterations (default 300).
  • early_stop (int) – Maximum number of consecutive iterations with no improvement that the algorithm accepts without stopping (default max_iter).
Returns:

a Result object containing the solution, metadata

and stored metrics history