## 2 Materials and Methods
### NMPC with PSO Solver
The control goal is to reach the desired set point within a specified tolerance, and therefore the cost function coefficients \(\lambda_{1}\) and \(\lambda_{2}\) are set to \(\lambda_{1}=0\) and \(\lambda_{2}=0\) when the expected crane positioning accuracy \(e_{a}\) is satisfied and the payload sway oscillation is within the acceptable tolerance \(\alpha_{a}\), respectively; otherwise, nonzero weights are applied (\(\lambda_{1}=1\), \(\lambda_{2}=\lambda_{20}\)). The tolerance \(\alpha_{a}\) of the pendulum oscillation corresponds to the range of the payload linear deviation \(d_{p}\) from the stable equilibrium point, where the linear deviation of a payload depends on the rope length \(d_{p}=l\sin\alpha\), and for small values of the sway angle, it can be approximated as \(d_{p}\approx l\alpha\). In the experiments carried out in this work, the crane and payload positioning accuracy was assumed to be \(e_{a}=d_{p}=0.02\) m. Thus, during the experiments, the desired range of magnitude of the residual sway was set as \(\alpha_{a}=e_{a}/l=0.02/l\). The PSO algorithm is adapted to determine the sequence of control signal values \(u(k+i-1)\) over the control horizon \(N_{u}\) to minimize the cost function (15). Consider a population of candidate solutions (a swarm of particles) in the constrained \(N_{u}\)-dimensional search space, where each particle's position is represented by the coordinates corresponding to the sequence of control signals over the control horizon \(N_{u}\), as follows:  
\[u_{j}=[u(k+i-1),\ u(k+1),\ldots,u(k+N_{u}-1)] \tag{16}\]  
satisfying the box constraints  
\[u_{\min}\leq u_{j}\leq u_{\max} \tag{17}\]  
where \(j=1,\ 2,\ \ldots,\ n_{s}\); and \(n_{s}\) is the size of the swarm. At each time step, \(t\), called an iteration, the \(j^{\text{th}}\) particle's position and velocity are updated in the hyperspace according to  
\[\widetilde{u}_{j}^{t}=\widetilde{w}\widetilde{u}_{j}^{t-1}+c_{1}r_{1}\Big{(}u _{j,p}-u_{j}^{t-1}\Big{)}+c_{2}r_{2}\Big{(}u_{g}-u_{j}^{t-1}\Big{)} \tag{18}\]  
\[u_{j}^{t}=u_{j}^{t-1}+\widetilde{u}_{j}^{t} \tag{19}\]  
where \(u_{j}^{t-1}\) and \(\widetilde{u}_{j}^{t-1}\) are the particle's position and velocity in the previous iteration, respectively; \(u_{j,p}\) and \(u_{g}\) are the personal and global best solutions, respectively; \(w\) is called the inertia weight; positive constants \(c_{1}\) and \(c_{2}\) are called the acceleration coefficients and are responsible for the influence of the global and local attractors on the particle's velocity; and constants \(r_{1}\) and \(r_{2}\) are drawn uniformly from the interval \([0,\ 1]\). In this work, following [55], the inertia weight varies linearly between 0.9 and 0.4, together with the acceleration coefficients of 1.5, to ensure an efficient balance between exploration (theability of the particles to search wide regions of the solution space) and exploitation (the ability of the particles to refine the solution in the neighborhood of an attractor). Since the velocity of a particle refers to the change in a control signal, which is constrained by the lower and upper limits (\(u_{min},\ u_{max}\)), the velocity in formula (18) is updated by taking into account the control signal limitation:  
\[\widetilde{u}_{j}^{t}=\max\left(\min\left(\widetilde{u}_{j}^{t},\ \widetilde{u}_{j,\max}^{t}\right), \widetilde{u}_{j,\min}^{t}\right) \tag{20}\]  
where \(\widetilde{u}_{j,\min}^{t}=u_{\min}-u_{j}^{t-1}\) and \(\widetilde{u}_{j,\max}^{t}=u_{\max}-u_{j}^{t-1}\). The constraint described in (20) is the absorption constraint, i.e., if the particle's velocity would cause the particle to leave the feasible region, then the boundary \"absorbs\" the excess velocity, causing the particle to stay on the boundary. The personal best position \(u_{j,p}\) of the \(j\)-th particle and the global best position of swarm \(u_{g}\) are updated at each iteration. The iterative process is repeated at each time step \(k\), starting with a randomly initialized swarm of particles and their velocities satisfying condition (20).