particle_swarm
particle_swarm(fun, lb, ub, n_particles=100, max_iter=100, momentum=0.9, phi=0.1, verbose=False, topology='full', use_cpp=True)
Particle swarm optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
callable
|
Function to minimize |
required |
lb
|
(n,) numpy double array
|
Vector of lower bounds |
required |
ub
|
(n,) numpy double array
|
Vector of upper bounds |
required |
n_particles
|
int, optional (default: 100)
|
Number of particles |
100
|
max_iter
|
int, optional (default: 1000)
|
Maximum number of iterations |
100
|
topology
|
str, optional (default: 'full')
|
Connectivity of the swarm. Either 'full' or 'ring'. |
'full'
|
verbose
|
bool, optional (default: False)
|
Print progress to stdout |
False
|
use_cpp
|
bool, optional (default: True)
|
Whether to use the C++ binding when available. Falls back to the pure-Python implementation if the binding cannot be imported. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
(n,) numpy double array
|
Best solution |
f |
double
|
Best objective value |
Examples:
import numpy as np
from gpytoolbox import particle_swarm
# Minimize a function with a known minimum at x = val
val = 3.5
def fun(x):
return (x - val)**2
lb = np.array([-10])
ub = np.array([10])
x, f = particle_swarm(fun, lb, ub, max_iter=1000)
# x is now close to val
Source code in src/gpytoolbox/particle_swarm.py
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |