# Applying Particle Swarm Optimization Variations to Solve the Transportation Problem Effectively
## 4 The Basic PSO for Solving the TP
\(\min\) (\(i\),\(1\)) = \(\min\) (\(i\), \(j\)) + \(ww\)
19. \(d(j)=d(j)-s(i)\)
20. else if \(d(j)\) smaller than \(s(i)\)
21. \(ww=\min\) (\(s(i)\), \(d(j)\))
22. \(\min(i,j)=\min\) (\(i\), \(j\)) + \(ww\)
23. \(s(i)=s(i)-d(j)\)
24. end
25. end
26. end
27. Return \(L=Solution\)
```  
**Algorithm 1**Initialization algorithm  
The exported values of the particle's position, although satisfying demand and supply constraints, were observed to be taking occasionally negative and/or fractional values. These values cannot support the aspect of the solution since the values are quoted in quantities (only positive values are allowed); therefore, appropriate modifications have been made for the final form of the particle position. Two sub-algorithms were designed to repair the algorithm, replacing negative and fractional volumes with natural numbers without breaking the supply and demand conditions. Algorithm 3 takes as input a matrix--_particle(i)_. _Position_--that has negative values in its cells. The aim is for the negative elements to be eliminated as in [17]. Through an iterative process, which is illustrated in line 3, the algorithm checks each line of the cell of the table and sets as _neg_ the value of the cell with the negative value. Subsequently, it searches the maximum element of the column where its negative element was found, as shown in lines 5 and 6. The cardinal value of the negative value is subtracted from the cell with the largest negative value, while the cell with the negative value is set to zero. In line 9, a cell is randomly selected from the row that corresponds to the negative element. If the value is positive, the cardinal of the negative element is subtracted from it. Simultaneously, a cell of this row is counterbalanced by adding to it the cardinal of the negative cell as shown in line 13. Algorithm 3 exports the _particle(i)_._Position_ with non-negative values, while sustaining the supply and demand conditions. Applying the above transformation, the result is a matrix with positive but also fractional elements. Algorithm 4 takes as its input the matrix of particles' positions after removing the negative elements. In line 3, a new matrix named _pos_ is defined as containing the integer elements of matrix _particle(i)_._Position_. In line 4, a vector named _sumrow_ is created which contains the sum of each row of the _pos_ matrix; while in line 5, a vector named _sumcol_ is created, containing the sum of each column. In lines 6 and 7, the differences between the quantities of the _Supply_ and _sumrow_ and _Demand_ and _sumcol_ matrices are noted, respectively, in order to record the quantities missing from the _pos_ matrix. Then, through an iterative loop, the \(u\) cell of \(s(u)\) is compared with the \(v\) cell of \(d(v)\). The minimum quantity of these two is selected and entered into the _pos_ matrix, reallocating the integer amounts in an appropriate manner to satisfy the available supply and demand items. The algorithm terminates when vectors \(s\) and \(v\) are zeroed and the integer quantities are inserted into _pos_ matrix, which is the output of Algorithm 4. The final solution is a non-negative integer solution matrix satisfying the requested constraints. ```
1. for\(k=1:m\)
2. for\(l=1:n\)
3. if\(\mathit{particle(i).Position(k,l)<0}\)
4. Set \(\mathit{neg}=\mathit{particle(i).Position(k,l)}\)
5. Find the maximum element of the \(i\)-th column, as \(\mathit{max}=\mathit{particle(i).Position(\cdot,l)}\)
6. Find the exact position of the maximum element
7. Change the value of maximum as \(\mathit{particle(i).Position(a,b)=max\_element-1 neg}\)
8. Set the negative element \(\mathit{particle(i).Position(k,l)=0}\)
9. Select a random number of the \(k\)-th row
10. \(l\_count=1\)
11. while (\(\mathit{(j\_count\Leftarrow n)}\)
12. if\(\mathit{particle(i).Position(k,l\_count)>0}\)
13. Set \(\mathit{particle(i).Position(k,l\_count)=particle(i).Position(k,l\_count)-1 neg}\)
14.