Artificial Intelligence Applications in Engineering (MUH-920), week 6 of 14

Swarm Intelligence: Particles, Ants and Bees

Prof. Dr. Utku Kose, Süleyman Demirel University

Collective behaviour as computation

A flock of birds turns as one body although no bird leads it. Reynolds reproduced this with three local rules for each simulated bird: avoid crowding neighbours, align with their heading, and move towards their centre [1]. Ant colonies find short paths between nest and food without a map, because ants deposit pheromone and prefer trails with more of it; shorter trails are completed faster and accumulate pheromone sooner. Honeybee colonies allocate foragers to flower patches in proportion to their quality through the waggle dance. These systems share decentralised control, simple agents, local interaction and emergent global behaviour, which Bonabeau, Dorigo and Theraulaz summarised as swarm intelligence [2].

Animation: Three rules make a flock

Each arrow follows only its neighbours. Set separation, alignment and cohesion and watch order emerge or dissolve. With alignment at zero, the flock never forms a common heading.

Formulas: The three rules of a flock

Each simulated bird ii has a position xix_i and a velocity viv_i, both vectors in the plane. Its neighbours NiN_i are the birds within a radius RR, and ∣Ni∣|N_i| is their number. Reynolds' three rules become three steering vectors [1]. Cohesion points from the bird to the centre of its neighbours, alignment is the difference between their mean velocity and its own, and separation points away from every neighbour closer than a smaller radius RsR_s:

ci=1∣Ni∣∑j∈Nixj−xi,    ai=1∣Ni∣∑j∈Nivj−vi,    si=−∑j∈Ni, dij<Rsxj−xidijc_i = \frac{1}{|N_i|} \sum_{j \in N_i} x_j - x_i, \;\; a_i = \frac{1}{|N_i|} \sum_{j \in N_i} v_j - v_i, \;\; s_i = -\sum_{j \in N_i,\, d_{ij} < R_s} \frac{x_j - x_i}{d_{ij}}

Here dij=∣xj−xi∣d_{ij} = |x_j - x_i| is the distance between the two birds, so each separation term is a vector of length one. The velocity changes by a weighted sum of the three vectors, and the bird then moves by its velocity:

vi←vi+kswssi+kawaai+kcwcci,    xi←xi+viv_i \leftarrow v_i + k_s w_s s_i + k_a w_a a_i + k_c w_c c_i, \;\; x_i \leftarrow x_i + v_i

The weights wsw_s, waw_a and wcw_c are the sliders of the animation, and the constants ks=0.0006k_s = 0.0006, ka=0.05k_a = 0.05 and kc=0.004k_c = 0.004 scale them to the units of the animation, in which the width of the field is 1. The animation also keeps every speed between 0.002 and 0.008 per step.

Worked example: One step of one bird

A bird at x=(0.5,0.3)x = (0.5, 0.3) flies with v=(0.004,0)v = (0.004, 0). The neighbourhood radius is R=0.09R = 0.09 and the separation radius Rs=0.03R_s = 0.03. Two other birds are near: bird A at (0.52, 0.31) with velocity (0.003, 0.002), and bird B at (0.56, 0.34) with velocity (0.006, 0.001). The weights are ws=1.5w_s = 1.5, wa=1w_a = 1 and wc=0.8w_c = 0.8.

Step 1. Distances.

dA=(0.52−0.5)2+(0.31−0.3)2=0.0004+0.0001=0.02236d_A = \sqrt{(0.52 - 0.5)^2 + (0.31 - 0.3)^2} = \sqrt{0.0004 + 0.0001} = 0.02236
dB=(0.56−0.5)2+(0.34−0.3)2=0.0036+0.0016=0.0052=0.07211d_B = \sqrt{(0.56 - 0.5)^2 + (0.34 - 0.3)^2} = \sqrt{0.0036 + 0.0016} = \sqrt{0.0052} = 0.07211

Both birds are neighbours, because both distances are below 0.09, but only A is closer than 0.03.

Step 2. Cohesion. The centre of the neighbours is ((0.52+0.56)/2,(0.31+0.34)/2)=(0.54,0.325)((0.52 + 0.56)/2, (0.31 + 0.34)/2) = (0.54, 0.325), so c=(0.54,0.325)−(0.5,0.3)=(0.04,0.025)c = (0.54, 0.325) - (0.5, 0.3) = (0.04, 0.025).

Step 3. Alignment. The mean velocity is ((0.003+0.006)/2,(0.002+0.001)/2)=(0.0045,0.0015)((0.003 + 0.006)/2, (0.002 + 0.001)/2) = (0.0045, 0.0015), so a=(0.0045,0.0015)−(0.004,0)=(0.0005,0.0015)a = (0.0045, 0.0015) - (0.004, 0) = (0.0005, 0.0015).

Step 4. Separation. Only A counts: s=−(0.02,0.01)0.02236=(−0.8945,−0.4472)s = -\frac{(0.02, 0.01)}{0.02236} = (-0.8945, -0.4472).

Step 5. The new velocity and position. The three factors are ksws=0.0006×1.5=0.0009k_s w_s = 0.0006 \times 1.5 = 0.0009, kawa=0.05×1=0.05k_a w_a = 0.05 \times 1 = 0.05 and kcwc=0.004×0.8=0.0032k_c w_c = 0.004 \times 0.8 = 0.0032:

Δv=0.0009×(−0.8945,−0.4472)+0.05×(0.0005,0.0015)+0.0032×(0.04,0.025)\Delta v = 0.0009 \times (-0.8945, -0.4472) + 0.05 \times (0.0005, 0.0015) + 0.0032 \times (0.04, 0.025)
Δv=(−0.000805,−0.000402)+(0.000025,0.000075)+(0.000128,0.00008)\Delta v = (-0.000805, -0.000402) + (0.000025, 0.000075) + (0.000128, 0.00008)
Δv1=−0.000805+0.000025+0.000128=−0.000652\Delta v_1 = -0.000805 + 0.000025 + 0.000128 = -0.000652
Δv2=−0.000402+0.000075+0.00008=−0.000247\Delta v_2 = -0.000402 + 0.000075 + 0.00008 = -0.000247
v=(0.004,0)+(−0.000652,−0.000247)=(0.003348,−0.000247)v = (0.004, 0) + (-0.000652, -0.000247) = (0.003348, -0.000247)
x=(0.5,0.3)+(0.003348,−0.000247)=(0.503348,0.299753)x = (0.5, 0.3) + (0.003348, -0.000247) = (0.503348, 0.299753)

The speed 0.0033482+(−0.000247)2=0.003357\sqrt{0.003348^2 + (-0.000247)^2} = 0.003357 lies inside the allowed range. The bird turns away from A, which is too close, and slightly towards the group, which is the behaviour that the animation shows for the whole flock.

Particle swarm optimisation

Kennedy and Eberhart introduced particle swarm optimisation in 1995, inspired by bird flocking and social behaviour [3]. Each particle i has a position x, which is a candidate solution, and a velocity v. It remembers the best position p it has visited, and the swarm shares the best position g found by any particle. In every iteration, the velocity is updated as v = w v + c1 r1 (p - x) + c2 r2 (g - x), and the position as x = x + v, where r1 and r2 are random numbers between 0 and 1 drawn for each dimension.

The three terms have clear meanings. The inertia weight w keeps part of the previous motion; Shi and Eberhart introduced it to balance global and local search, with values around 0.4 to 0.9 [4]. The cognitive term, weighted by c1, pulls a particle back towards its own best experience. The social term, weighted by c2, pulls it towards the best experience of the swarm. Clerc and Kennedy analysed the dynamics and derived a constriction factor that guarantees convergence; the widely used setting w = 0.7298 with c1 = c2 = 1.49618 follows from it [5]. Particle swarms are easy to implement, need no gradients, and work well on continuous problems of moderate dimension, but like every metaheuristic they can stagnate in a local optimum.

Check your understanding. In the velocity update, what does the social term c2 r2 (g - x) do?

Formulas: The particle swarm update

Particle ii has a position xix_i and a velocity viv_i. It remembers its personal best position pip_i, and the swarm shares the global best position gg. In every iteration and for every dimension dd, two new random numbers r1r_1 and r2r_2 between 0 and 1 are drawn, and the particle moves by [3, 4]

vi,d←w vi,d+c1r1(pi,d−xi,d)+c2r2(gd−xi,d)v_{i,d} \leftarrow w\, v_{i,d} + c_1 r_1 (p_{i,d} - x_{i,d}) + c_2 r_2 (g_d - x_{i,d})
xi,d←xi,d+vi,dx_{i,d} \leftarrow x_{i,d} + v_{i,d}

The first term is the inertia, the second the cognitive pull towards the particle's own best position, and the third the social pull towards the best position of the swarm. After the move, the particle evaluates its new position and replaces pip_i, and possibly gg, if the new position is better. Clerc and Kennedy showed that the swarm converges when the velocity is multiplied by the constriction factor [5]

χ=2∣2−φ−φ2−4φ∣,    φ=φ1+φ2>4\chi = \frac{2}{|2 - \varphi - \sqrt{\varphi^2 - 4\varphi}|}, \;\; \varphi = \varphi_1 + \varphi_2 > 4

which gives the update v←χ (v+φ1r1(p−x)+φ2r2(g−x))v \leftarrow \chi\,(v + \varphi_1 r_1 (p - x) + \varphi_2 r_2 (g - x)). This is the inertia form above with w=χw = \chi and c1=χφ1c_1 = \chi \varphi_1, c2=χφ2c_2 = \chi \varphi_2.

Worked example: The constriction factor

With φ1=φ2=2.05\varphi_1 = \varphi_2 = 2.05, so that φ=4.1\varphi = 4.1:

φ2−4φ=16.81−16.4=0.41,    0.41=0.640312\varphi^2 - 4\varphi = 16.81 - 16.4 = 0.41, \;\; \sqrt{0.41} = 0.640312
χ=2∣2−4.1−0.640312∣=22.740312=0.729844,    c1=c2=0.729844×2.05=1.49618\chi = \frac{2}{|2 - 4.1 - 0.640312|} = \frac{2}{2.740312} = 0.729844, \;\; c_1 = c_2 = 0.729844 \times 2.05 = 1.49618

These are the values w=0.7298w = 0.7298 and c1=c2=1.49618c_1 = c_2 = 1.49618 quoted in the lecture and used as the defaults of Part A of the interactive lab.

Worked example: One move of one particle

A particle searches the minimum of f(x)=x12+x22f(x) = x_1^2 + x_2^2. It is at x=(2,−1)x = (2, -1) with f=4+1=5f = 4 + 1 = 5 and velocity v=(0.5,0.3)v = (0.5, 0.3). Its best position is p=(1.5,−0.5)p = (1.5, -0.5) with f(p)=2.25+0.25=2.5f(p) = 2.25 + 0.25 = 2.5, and the swarm's best is g=(0.2,0.1)g = (0.2, 0.1) with f(g)=0.04+0.01=0.05f(g) = 0.04 + 0.01 = 0.05. The random numbers are r1=(0.6,0.2)r_1 = (0.6, 0.2) and r2=(0.3,0.9)r_2 = (0.3, 0.9) for the two dimensions.

Step 1. First dimension.

v1=0.7298×0.5+1.49618×0.6×(1.5−2)+1.49618×0.3×(0.2−2)v_1 = 0.7298 \times 0.5 + 1.49618 \times 0.6 \times (1.5 - 2) + 1.49618 \times 0.3 \times (0.2 - 2)
v1=0.3649+(−0.448854)+(−0.807937)=−0.891891,    x1=2+(−0.891891)=1.108109v_1 = 0.3649 + (-0.448854) + (-0.807937) = -0.891891, \;\; x_1 = 2 + (-0.891891) = 1.108109

Step 2. Second dimension.

v2=0.7298×0.3+1.49618×0.2×((−0.5)−(−1))+1.49618×0.9×(0.1−(−1))v_2 = 0.7298 \times 0.3 + 1.49618 \times 0.2 \times ((-0.5) - (-1)) + 1.49618 \times 0.9 \times (0.1 - (-1))
v2=0.21894+0.149618+1.481218=1.849776,    x2=−1+1.849776=0.849776v_2 = 0.21894 + 0.149618 + 1.481218 = 1.849776, \;\; x_2 = -1 + 1.849776 = 0.849776

Step 3. Evaluation and memory.

f(x)=1.1081092+0.8497762=1.227906+0.722119=1.950025f(x) = 1.108109^2 + 0.849776^2 = 1.227906 + 0.722119 = 1.950025

The new value is better than the personal best 2.5, so pp becomes (1.108109, 0.849776). It is worse than the global best 0.05, so gg stays. In the first dimension the social term dominates and pulls the particle towards gg; in the second dimension both pulls point upwards, and the particle overshoots gg, which is the oscillating search that the inertia and constriction keep under control. Part B of the interactive lab decomposes every update of a small swarm in the same way.

Tuning a PID controller with a swarm

The proportional-integral-derivative controller is the workhorse of industrial control. Its three gains shape the response: The proportional gain speeds up the reaction, the integral gain removes steady-state error, and the derivative gain damps oscillation [6]. Tuning them by hand is tedious, and classical rules assume simple process models. Controller tuning can instead be formulated as optimisation: Choose the gains that minimise a cost computed from a simulated step response, such as the integral of the time-weighted absolute error (ITAE), with penalties for overshoot and limits on the actuator. Particle swarms have been used in this way for automatic voltage regulators and many other loops [7].

The notebook applies the method to the speed control of a DC motor, using the standard model and parameters of the Control Tutorials for MATLAB and Simulink [8]. The armature voltage is limited to 24 volts, which makes the problem nonlinear, and the swarm evaluates all particles in one vectorised simulation. The cost function is part of the design: A cost that rewards speed alone drives the gains to their bounds, and adding a penalty on overshoot or control effort changes the optimum. The optimiser serves the engineer's definition of good behaviour; it cannot supply that definition.

Check your understanding. A swarm tunes a PID controller by minimising only the settling time, and the result shows violent oscillation of the actuator. What is the most likely cause?

Formulas: PID control, the DC motor and the cost of a response

A PID controller computes its output uu from the error e=r−ye = r - y between the set point rr and the measured output yy [6]:

u(t)=Kp e(t)+Ki∫0te(τ) dτ+Kddedtu(t) = K_p\, e(t) + K_i \int_0^t e(\tau)\, d\tau + K_d \frac{de}{dt}

The integral adds up past errors and removes a steady offset; the derivative reacts to the rate of change and damps the motion. A digital controller with the sampling time Δt\Delta t replaces the integral by a running sum and the derivative by a difference:

Ik=Ik−1+ekΔt,    Dk=ek−ek−1Δt,    uk=Kpek+KiIk+KdDkI_k = I_{k-1} + e_k \Delta t, \;\; D_k = \frac{e_k - e_{k-1}}{\Delta t}, \;\; u_k = K_p e_k + K_i I_k + K_d D_k

The lab filters the derivative with a time constant τ=0.01\tau = 0.01 s, Dkf=Dk−1f+Δtτ(Dk−Dk−1f)D^{f}_k = D^{f}_{k-1} + \frac{\Delta t}{\tau}(D_k - D^{f}_{k-1}), and limits the armature voltage to ±24\pm 24 V. The DC motor of the Control Tutorials couples the speed ω\omega and the armature current ii [8]:

Jdωdt=Ki−b ω,    Ldidt=V−Ri−KωJ \frac{d\omega}{dt} = K i - b\, \omega, \;\; L \frac{di}{dt} = V - R i - K \omega

with inertia J=0.01J = 0.01 kg m², friction b=0.1b = 0.1 N m s, motor constant K=0.01K = 0.01, resistance R=1R = 1 Ω and inductance L=0.5L = 0.5 H. At a constant voltage both derivatives vanish, so i=bω/Ki = b\omega/K and V=Rbω/K+KωV = R b \omega/K + K\omega, which gives the steady speed

ωss=KVRb+K2\omega_{\text{ss}} = \frac{K V}{R b + K^2}

The swarm judges a set of gains by the integral of the time-weighted absolute error over the simulated step response, approximated by a sum over the samples:

ITAE=∫0Tt ∣e(t)∣ dt≈∑ktk∣ek∣Δt\text{ITAE} = \int_0^{T} t\, |e(t)|\, dt \approx \sum_{k} t_k |e_k| \Delta t

Late errors weigh more than early ones, so slow settling and lasting oscillation are expensive [7].

Worked example: The motor without control and the first two samples under PID control

Step 1. Open loop. One volt gives

ωss=0.01×11×0.1+0.012=0.010.1001=0.0999 rad/s\omega_{\text{ss}} = \frac{0.01 \times 1}{1 \times 0.1 + 0.01^2} = \frac{0.01}{0.1001} = 0.0999 \text{ rad/s}

so a constant voltage of 1/0.0999=10.011/0.0999 = 10.01 V would be needed for 1 rad/s, and any change of load or friction would shift the speed. Feedback removes this dependence.

Step 2. The first sample, k=0k = 0. The controller of the lab uses Kp=100K_p = 100, Ki=200K_i = 200 and Kd=10K_d = 10 with Δt=0.002\Delta t = 0.002 s and a set point of 1 rad/s. The motor is at rest, so e0=1−0=1e_0 = 1 - 0 = 1. The integral is I0=0+1×0.002=0.002I_0 = 0 + 1 \times 0.002 = 0.002; the previous error is initialised to 1, so D0=(1−1)/0.002=0D_0 = (1 - 1)/0.002 = 0 and the filtered derivative stays 0. The controller asks for

u0=100×1+200×0.002+10×0=100+0.4+0=100.4 Vu_0 = 100 \times 1 + 200 \times 0.002 + 10 \times 0 = 100 + 0.4 + 0 = 100.4 \text{ V}

which the limit cuts to V=24V = 24 V. The motor equations give, with ω=0\omega = 0 and i=0i = 0,

dωdt=0.01×0−0.1×00.01=0,    didt=24−1×0−0.01×00.5=48\frac{d\omega}{dt} = \frac{0.01 \times 0 - 0.1 \times 0}{0.01} = 0, \;\; \frac{di}{dt} = \frac{24 - 1 \times 0 - 0.01 \times 0}{0.5} = 48

and one Euler step of 0.002 s gives ω=0\omega = 0 and i=0+48×0.002=0.096i = 0 + 48 \times 0.002 = 0.096 A.

Step 3. The second sample, k=1k = 1. The speed is still 0, so e1=1e_1 = 1, I1=0.002+0.002=0.004I_1 = 0.002 + 0.002 = 0.004 and D1=0D_1 = 0. The demand u1=100+200×0.004=100.8u_1 = 100 + 200 \times 0.004 = 100.8 V is again cut to 24 V. Now the current drives the rotor:

dωdt=0.01×0.096−0.1×00.01=0.096,    didt=24−1×0.096−0.01×00.5=47.808\frac{d\omega}{dt} = \frac{0.01 \times 0.096 - 0.1 \times 0}{0.01} = 0.096, \;\; \frac{di}{dt} = \frac{24 - 1 \times 0.096 - 0.01 \times 0}{0.5} = 47.808
ω=0+0.096×0.002=0.000192 rad/s,    i=0.096+47.808×0.002=0.191616 A\omega = 0 + 0.096 \times 0.002 = 0.000192 \text{ rad/s}, \;\; i = 0.096 + 47.808 \times 0.002 = 0.191616 \text{ A}

Step 4. The cost so far. The ITAE sum receives 0×1×0.002=00 \times 1 \times 0.002 = 0 from the first sample and 0.002×1×0.002=0.0000040.002 \times 1 \times 0.002 = 0.000004 from the second. The voltage stays at its limit for many samples: The controller is saturated, which is the nonlinearity mentioned in the lecture. Part C of the interactive lab continues this table sample by sample for any gains, and Part D lets the swarm choose the gains.

Ant colony optimisation

Dorigo, Maniezzo and Colorni turned the pheromone mechanism into the Ant System for the travelling salesman problem [9]. Each artificial ant builds a complete tour city by city. From city i, it chooses the next unvisited city j with a probability proportional to the pheromone on edge (i, j) raised to a power alpha, times a heuristic desirability, usually the inverse distance, raised to a power beta. After all ants have finished, pheromone evaporates on every edge by a factor rho, and each ant deposits pheromone on the edges of its tour in inverse proportion to the tour length. Short tours therefore reinforce their edges, while evaporation forgets poor early choices.

The Ant Colony System added a stronger exploitation rule and local pheromone updates and was competitive on benchmark instances [10]; the monograph by Dorigo and Stützle presents the family and its applications to routing, scheduling and assignment [11]. Drilling holes in a printed circuit board is a classical travelling salesman application: The drill head must visit every hole once, and the travel time between holes is wasted production time. Benchmark libraries such as TSPLIB include instances that come from drilling problems [12].

Check your understanding. What is the purpose of pheromone evaporation in ant colony optimisation?

Formulas: Ant System and Ant Colony System

An ant at city ii chooses the next city jj among the cities NiN_i it has not visited yet with the probability [9]

pij=τijα ηijβ∑l∈Niτilα ηilβp_{ij} = \frac{\tau_{ij}^{\alpha}\, \eta_{ij}^{\beta}}{\sum_{l \in N_i} \tau_{il}^{\alpha}\, \eta_{il}^{\beta}}

where τij\tau_{ij} is the pheromone on the edge from ii to jj, ηij=1/dij\eta_{ij} = 1/d_{ij} is the heuristic desirability, the inverse of the distance, and the exponents α\alpha and β\beta set the weight of the two. When all mm ants have built their tours of lengths LkL_k, the pheromone evaporates with the rate ρ\rho (rho) and every ant deposits pheromone on the edges of its tour:

τij←(1−ρ) τij+∑k=1mΔτijk,    Δτijk=QLk if ant k used edge (i,j), otherwise 0\tau_{ij} \leftarrow (1 - \rho)\, \tau_{ij} + \sum_{k=1}^{m} \Delta\tau_{ij}^{k}, \;\; \Delta\tau_{ij}^{k} = \frac{Q}{L_k} \text{ if ant } k \text{ used edge } (i, j), \text{ otherwise } 0

The Ant Colony System changes three things [10]. With a probability q0q_0 an ant takes the best edge, j=arg⁡max⁡lτilηilβj = \arg\max_{l} \tau_{il} \eta_{il}^{\beta}, and otherwise it uses the probabilities above. Every ant that crosses an edge reduces its pheromone at once, τij←(1−ξ)τij+ξτ0\tau_{ij} \leftarrow (1 - \xi)\tau_{ij} + \xi \tau_0, so that later ants try other edges. Only the best tour found so far deposits pheromone, τij←(1−ρ)τij+ρ/Lbest\tau_{ij} \leftarrow (1 - \rho)\tau_{ij} + \rho / L_{\text{best}}.

Worked example: Choosing holes and updating pheromone

A drill must visit four holes at the corners of a rectangle: A (0,0)(0, 0), B (3,0)(3, 0), C (3,4)(3, 4) and D (0,4)(0, 4), in centimetres. The distances are dAB=dCD=3d_{AB} = d_{CD} = 3, dAD=dBC=4d_{AD} = d_{BC} = 4 and dAC=dBD=32+42=5d_{AC} = d_{BD} = \sqrt{3^2 + 4^2} = 5. The parameters are α=1\alpha = 1, β=2\beta = 2, ρ=0.3\rho = 0.3 and Q=1Q = 1, and all pheromone values start at 1.

Step 1. The first decision of an ant at A. With τ=1\tau = 1, only the heuristic matters: ηAB2=(1/3)2=0.1111\eta_{AB}^2 = (1/3)^2 = 0.1111, ηAC2=(1/5)2=0.04\eta_{AC}^2 = (1/5)^2 = 0.04 and ηAD2=(1/4)2=0.0625\eta_{AD}^2 = (1/4)^2 = 0.0625, which add up to 0.2136. Hence

pAB=0.11110.2136=0.5201,    pAC=0.040.2136=0.1873,    pAD=0.06250.2136=0.2926p_{AB} = \frac{0.1111}{0.2136} = 0.5201, \;\; p_{AC} = \frac{0.04}{0.2136} = 0.1873, \;\; p_{AD} = \frac{0.0625}{0.2136} = 0.2926

Step 2. Two tours. Ant 1 builds A, B, C, D, A with length 3+4+3+4=143 + 4 + 3 + 4 = 14, and ant 2 builds A, C, B, D, A with length 5+4+5+4=185 + 4 + 5 + 4 = 18. Their deposits are 1/14=0.0714291/14 = 0.071429 and 1/18=0.0555561/18 = 0.055556.

Step 3. Evaporation and deposit. Every edge keeps (1−0.3)×1=0.7(1 - 0.3) \times 1 = 0.7 and receives the deposits of the ants that used it:

Edge Length New pheromone τ\tau
AB 3 0.7 + 0.071429 = 0.771429
AC 5 0.7 + 0.055556 = 0.755556
AD 4 0.7 + 0.071429 + 0.055556 = 0.826985
BC 4 0.7 + 0.071429 + 0.055556 = 0.826985
BD 5 0.7 + 0.055556 = 0.755556
CD 3 0.7 + 0.071429 = 0.771429

Step 4. The next decision at A. The products τη2\tau \eta^2 are 0.771429×0.1111=0.0857060.771429 \times 0.1111 = 0.085706 for B, 0.755556×0.04=0.0302220.755556 \times 0.04 = 0.030222 for C and 0.826985×0.0625=0.0516870.826985 \times 0.0625 = 0.051687 for D, with the sum 0.167615, so

pAB=0.5113,    pAC=0.1803,    pAD=0.3084p_{AB} = 0.5113, \;\; p_{AC} = 0.1803, \;\; p_{AD} = 0.3084

Edge AD, which belongs to both tours, gained weight, and the diagonal AC, which belongs only to the longer tour, lost weight. Over many iterations this feedback concentrates the pheromone on the short tour around the rectangle, of length 14. Part E of the interactive lab repeats these calculations for any parameters and tours, and Part F runs a colony on a drilling sequence and shows the pheromone trails forming.

Artificial bee colony and the metaphor question

Karaboga and Basturk's artificial bee colony algorithm assigns roles to solutions [13]. Employed bees search around their food sources, onlooker bees choose sources in proportion to their quality and search around them, and scout bees replace sources that have not improved for a number of trials with random new ones. The scout phase gives the algorithm a built-in restart mechanism. Many further algorithms followed, inspired by fireflies, bats, wolves, whales and others, including the ant lion optimiser used to train neural networks for chaotic signal prediction in earlier work [14].

Sörensen argued that the flood of metaphor-based algorithms often hides old ideas behind new vocabulary and weak experiments [15]. The no free lunch theorems add that no algorithm can be best on all problems [16]. Engineers should therefore judge a metaheuristic by its search mechanism rather than its metaphor, compare it with established methods on the problem at hand, report results over many independent runs with statistics, and count function evaluations, not iterations, because a single evaluation of an engineering simulation may take minutes.

Check your understanding. A paper claims that a new animal-inspired algorithm beats PSO, based on one run per method with different numbers of function evaluations. What is the main weakness?

Formulas: The artificial bee colony

The colony keeps SNSN food sources x1,…,xSNx_1, \ldots, x_{SN}, each a candidate solution [13]. An employed bee changes one randomly chosen component jj of its source ii, using a randomly chosen other source kk and a random number ϕ\phi between −1-1 and 11:

vij=xij+ϕ (xij−xkj)v_{ij} = x_{ij} + \phi\, (x_{ij} - x_{kj})

and keeps the new source only if it is better. For a minimisation problem with objective values fif_i, the fitness of a source is

fiti=11+fi if fi≥0,    fiti=1+∣fi∣ if fi<0\text{fit}_i = \frac{1}{1 + f_i} \text{ if } f_i \ge 0, \;\; \text{fit}_i = 1 + |f_i| \text{ if } f_i < 0

Onlooker bees choose sources with the probabilities

pi=fiti∑n=1SNfitnp_i = \frac{\text{fit}_i}{\sum_{n=1}^{SN} \text{fit}_n}

and search around them in the same way. Every source counts the trials that failed to improve it; a source whose counter exceeds a limit is abandoned, and a scout bee replaces it with a random source xij=lj+rand(0,1) (uj−lj)x_{ij} = l_j + \text{rand}(0, 1)\,(u_j - l_j) [13].

Worked example: Employed bees, onlookers and the scout

The objective is f(x)=x12+x22f(x) = x_1^2 + x_2^2 with three sources: x1=(1,2)x_1 = (1, 2) with f=1+4=5f = 1 + 4 = 5, x2=(−1,0.5)x_2 = (-1, 0.5) with f=1+0.25=1.25f = 1 + 0.25 = 1.25, and x3=(0.5,−1.5)x_3 = (0.5, -1.5) with f=0.25+2.25=2.5f = 0.25 + 2.25 = 2.5.

Step 1. An employed bee at source 1. It changes the second component, j=2j = 2, with the partner k=2k = 2 and ϕ=−0.4\phi = -0.4:

v12=2+(−0.4)×(2−0.5)=2−0.6=1.4v_{12} = 2 + (-0.4) \times (2 - 0.5) = 2 - 0.6 = 1.4

The new source (1,1.4)(1, 1.4) has f=1+1.42=2.96f = 1 + 1.4^2 = 2.96, better than 5, so it replaces source 1 and its trial counter returns to 0. Sources 2 and 3 are assumed not to improve in this phase, so their counters rise by one.

Step 2. Fitness and probabilities.

fit1=11+2.96=0.252525,    fit2=11+1.25=0.444444,    fit3=11+2.5=0.285714\text{fit}_1 = \frac{1}{1 + 2.96} = 0.252525, \;\; \text{fit}_2 = \frac{1}{1 + 1.25} = 0.444444, \;\; \text{fit}_3 = \frac{1}{1 + 2.5} = 0.285714
p1=0.2525250.982683=0.257,    p2=0.4444440.982683=0.4523,    p3=0.2857140.982683=0.2907p_1 = \frac{0.252525}{0.982683} = 0.257, \;\; p_2 = \frac{0.444444}{0.982683} = 0.4523, \;\; p_3 = \frac{0.285714}{0.982683} = 0.2907

where 0.982683=0.252525+0.444444+0.2857140.982683 = 0.252525 + 0.444444 + 0.285714.

Step 3. An onlooker. The cumulative probabilities are 0.257, 0.7093 and 1. A random number of 0.6 falls between 0.257 and 0.7093, so the onlooker searches around source 2, the best one, exactly as an employed bee would.

Step 4. The scout. If source 3 failed to improve in, say, 20 consecutive trials while the limit is 20, a scout replaces it with a random point of the search region. This restart keeps the colony from stagnating, which is the mechanism behind the metaphor. Part G of the interactive lab runs all three phases with every number shown.

Python step 6: Classes, objects and probabilistic choice

The Python step of this week is part of the Colab notebook, where every explanation stands next to a cell that runs it and the step closes with a quick check and exercises with immediate feedback. The printable lecture notes contain the same step together with the outputs of its code. The Colab button below opens the notebook at the step. Run the setup cell just above the step first, since the exercises use its check function.

Open Python step 6 in Colab View the notebook on GitHub

Review cards

Select a card to turn it over.

PSO velocity update
v = w v + c1 r1 (p - x) + c2 r2 (g - x); then x = x + v [3, 4].
Constriction setting
w = 0.7298, c1 = c2 = 1.49618, derived from a convergence analysis [5].
Pheromone update
Evaporate on all edges by rho, then deposit on each ant's tour in proportion to 1 / tour length [9].
ITAE
Integral of time multiplied by absolute error; a common time-domain cost for controller tuning.
Scout bee
Abandons a stagnant solution and samples a new random one [13].
Metaphor critique
Judge a metaheuristic by its mechanism and fair experiments, not by its inspiration [15].

Continue the week

The week continues with the simulation and the self-assessment of the interactive lab and with the Python step and the hands-on work of the Colab notebook. The week overview lists the discipline challenges, the weekly task and the research assignment.

Interactive lab Colab notebook Self-assessment Week overview and tasks

References

[1] Reynolds, C. W. (1987). Flocks, herds and schools: A distributed behavioral model. ACM SIGGRAPH Computer Graphics, 21(4), 25-34. https://doi.org/10.1145/37402.37406

[2] Bonabeau, E., Dorigo, M., & Theraulaz, G. (1999). Swarm Intelligence: From Natural to Artificial Systems. Oxford University Press.

[3] Kennedy, J., & Eberhart, R. (1995). Particle swarm optimization. In Proceedings of ICNN'95, International Conference on Neural Networks, Vol. 4 (pp. 1942-1948). IEEE. https://doi.org/10.1109/ICNN.1995.488968

[4] Shi, Y., & Eberhart, R. (1998). A modified particle swarm optimizer. In 1998 IEEE International Conference on Evolutionary Computation Proceedings (pp. 69-73). IEEE. https://doi.org/10.1109/ICEC.1998.699146

[5] Clerc, M., & Kennedy, J. (2002). The particle swarm: Explosion, stability, and convergence in a multidimensional complex space. IEEE Transactions on Evolutionary Computation, 6(1), 58-73. https://doi.org/10.1109/4235.985692

[6] Åström, K. J., & Hägglund, T. (1995). PID Controllers: Theory, Design, and Tuning (2nd ed.). Instrument Society of America.

[7] Gaing, Z.-L. (2004). A particle swarm optimization approach for optimum design of PID controller in AVR system. IEEE Transactions on Energy Conversion, 19(2), 384-391. https://doi.org/10.1109/TEC.2003.821821

[8] University of Michigan, Carnegie Mellon University, & University of Detroit Mercy (2026). Control Tutorials for MATLAB and Simulink: DC motor speed, system modeling. https://ctms.engin.umich.edu

[9] Dorigo, M., Maniezzo, V., & Colorni, A. (1996). Ant system: Optimization by a colony of cooperating agents. IEEE Transactions on Systems, Man, and Cybernetics, Part B, 26(1), 29-41. https://doi.org/10.1109/3477.484436

[10] Dorigo, M., & Gambardella, L. M. (1997). Ant colony system: A cooperative learning approach to the traveling salesman problem. IEEE Transactions on Evolutionary Computation, 1(1), 53-66. https://doi.org/10.1109/4235.585892

[11] Dorigo, M., & Stützle, T. (2004). Ant Colony Optimization. MIT Press.

[12] Reinelt, G. (1991). TSPLIB: A traveling salesman problem library. ORSA Journal on Computing, 3(4), 376-384. https://doi.org/10.1287/ijoc.3.4.376

[13] Karaboga, D., & Basturk, B. (2007). A powerful and efficient algorithm for numerical function optimization: Artificial bee colony (ABC) algorithm. Journal of Global Optimization, 39(3), 459-471. https://doi.org/10.1007/s10898-007-9149-x

[14] Kose, U. (2018). An ant-lion optimizer-trained artificial neural network system for chaotic electroencephalogram (EEG) prediction. Applied Sciences, 8(9), 1613. https://doi.org/10.3390/app8091613

[15] Sörensen, K. (2015). Metaheuristics: The metaphor exposed. International Transactions in Operational Research, 22(1), 3-18. https://doi.org/10.1111/itor.12001

[16] Wolpert, D. H., & Macready, W. G. (1997). No free lunch theorems for optimization. IEEE Transactions on Evolutionary Computation, 1(1), 67-82. https://doi.org/10.1109/4235.585893