psopt.Combination¶
-
class
psopt.
Combination
(obj_func, candidates, constraints=None, **kwargs)¶ Solver to find the optimal combination of candidates
- Implementation based on:
- Unler, A. and Murat, A. (2010).
- A discrete particle swarm optimization method for feature selection in binary classification problems.
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: Combination optimizer object
Example
>>> candidates = [2,4,5,6,3,1,7] >>> # e.g. obj_func([a, b, c, d, e]) ==> a + b + c + d + e >>> def obj_func(x): return sum(x) >>> # constraint: sum of values cannot be even >>> def mod(x): return sum(x) % 2 >>> constraint = {"fn":mod, "type":"==", "value":1} >>> # define a threshold of acceptance for early convergence >>> limit=15 >>> # maximize the obj function >>> opt = Combination(obj_func, candidates, constraints=constraint) >>> sol = opt.maximize(selection_size=5, verbose=True, threshold=limit)
-
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
- Display nothing (default);
- Display statuses on console;
- 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
- Display nothing (default);
- Display statuses on console;
- 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