# Applying Particle Swarm Optimization Variations to Solve the Transportation Problem Effectively
## 4 The Basic PSO for Solving the TP  
The proposed PSO algorithm used to solve the TP is presented in this section. The primary goal is the initialization of the particles according to the problem's instances. This is achieved through a sub-algorithm (an initialization algorithm), as presented below. Initially, the amounts of the supply and demand were defined in tables. Subsequently, through control conditions, the amounts were randomly distributed, satisfying the constraints of the sums of supply and demand. First, Algorithm 1 creates two vectors, namely, _Supply_ and _Demand_, which are its input, as shown in lines 1 and 2. Next, variables \(m\) and \(n\) are computed. These variables are equal to the values of parameters _Supply_ and _Demand_, respectively. Then, a matrix is created consisting of random real numbers (line 7). In line 10, the elements of the candidate solution matrix are rounded to the nearest integer as the amounts of commodities should be non-negative integer values. In the following lines of the algorithm, a process of readjustment and redistribution of matrix \(L\) begins so that its values correspond to the given _Supply_ and _Demand_ amounts. In lines 11 and 12, the sum of all elements of each row of matrix \(L\) is stored in vector _Sumrow_, while the sum of all elements of each column of matrix \(L\) is stored in vector _Sumcol_. Then, two new vectors, namely, \(s\) and \(d\), are created by subtracting _Sumrow_ from _Supply_ and _Sumcol_ from _Demand_, respectively. In the following lines, for each cell of the final matrix, the shortcomings of the matrix are located and assembled appropriately to each cell by zeroing out the excess amount of vectors \(s\) and \(d\). The output of Algorithm 1 is a matrix consisting of the initial solutions (Initial Basic Feasible Solutions--IBFS), which comprises the input of Algorithm 2 (see below). All possible Initial Basic Feasible Solutions (IBFS) are non-negative integer values satisfying the supply and demand constraints. Next, we present the structure of the basic PSO algorithm, which will be applied to solve the TP (Algorithm 2). The process starts with the initialization of the population size _npop_, the maximum number of iterations \(t_{max}\), the personal and social acceleration coefficients \(c_{1}\) and \(c_{2}\), the random variables \(r_{1}\) and \(r_{2}\) and, finally, the inertia weight \(w\) (line 1). Moreover, subsequently, the _Supply_, _Demand_ and _Cost_ matrixes are defined (line 2). Line 6 calculates the total transport cost of each particle. Then, in lines 7 and 8, whether the total cost of the current particle is less than the minimum transport cost calculated up to then is checked. If the statement is true, the value of global best cost is upgraded, and this particle is now defined as the best. This process is continued for all candidate particles. In lines 9 to 14, through an iterative loop, the position and velocity of the particles are calculated using Equations (5) and (6). The algorithm exports the particle with the optimal position and its respective optimal transport cost. ```
1. Define \(Supply=[s_{1},s_{2},...,s_{m}]\)
2. Define \(Demand=[d_{1},d_{2},...,d_{n}]\)
3. Define \(m=\) length (\(Supply\))
4. Define \(n=\) length (\(Demand\))
5. Initialize a solution matrix \(I=\) zeros (\(m\), \(n\))
6. Initialize a supporting table \(B=\) zeros (\(m\times n\))
7. Generate a new random number \(x\), \(x=-\log\) (\(\text{rand}(m\times n\), 1)
8. Set \(x=x/\text{Sum}(x)\)
9. Take a matrix \(L=\) reshape (\(B\times x\), \([m\), \(n]\))
10. Set solution as a matrix to round each element of \(L\) to the nearest integer less than or equal to that element of \(L\) as Solution = floor \([L]\)
11. Set \(Sunrow\) as the sum of the elements of all rows. 12. Set \(Sumcol\) as the sum of the elements of all columns. 13. Set \(s=Supply-Sunrow\) and \(d=Demand-Sumcol\)
14. for \(i=1\) to \(m\) do
15. for \(j=1\) to \(n\) do
16. if \(s(i)\) smaller or equal to \(d(j)\)
17. \(ww=\min\) (\(d(i)\), \(d(j)\))
18.