This is part of the course “Optimization for Programmers”.
Transportation simplex method can be described in four steps.
- Balance the problem.
- Find an initial basic feasible solution with one of the methods, for example with northwest corner rule.
- For all basic variables use u₁ = 0 and uᵢ + vⱼ = cᵢⱼ to calculate uᵢ and vⱼ. For all non-basic variables calculate wᵢⱼ = uᵢ + vⱼ -ciⱼ. If wᵢⱼ ≤ 0, the current basic feasible solution is optimal. Otherwise, choose the variable with the most positive wᵢⱼ as the entering variable.
- Obtain a new basic feasible solution using loop pivoting, and go to step 3.
Select Entering Variable
After we had received an initial basic feasible solution, we can calculate each uᵢ and vⱼ by going through each cell containing a basic variable.
Next, we calculate wᵢⱼ for all non-basic variables. Since there are some wᵢⱼ that are more than zero, it means we have not reached an optimal solution. Therefore we select a variable that will enter the next basic feasible solution.
With an understanding of how to select a variable that will enter the basic feasible solution, we can write a code for this. First, let’s create the function that will calculate uᵢ and vⱼ for each cell with a basic variable. It receives two parameters — basic feasible solution and costs, go over each basic variable and fill lists containing uᵢ and vⱼ.
Then we write a function that receives basic variables, costs, us, vs and returns list with wᵢⱼ. It calculates wᵢⱼ for each non-basic variable using a simple formula(wᵢⱼ = uᵢ + vⱼ -ciⱼ), wᵢⱼ represented as a tuple containing its position and value.
If this is some wᵢⱼ that more than zero, it means the solution can be improved.
If a solution can be improved, we select a variable to enter by finding wᵢⱼ with the largest value and return its position.
Loop is an ordered sequence of at least four different cells that satisfy all three conditions:
- Any two consecutive cells lie in either the same row or same column.
- No three or more consecutive cells lie in the same row or column.
- The last cell is in the same row or column as the first cell.
We are using loop pivoting to improve the basic feasible solution, and its process can be described in four steps.
- Find the only loop involving the entering variable and some of the basic feasible variables.
- Count the cells in the loop (starting from 0), label them as odd cells or even cells.
- Find the odd cell with the smallest value. Call this value θ. This cell corresponds to the leaving variable.
- Decrease each odd cell in the loop by θ and increase each even cell in the loop by θ.
We already knew the position of the entering variable and can find the only possible loop. At the start, we move up from the entering variable(here we choose direction randomly) then we go right to the last basic variable in the row since we can’t have three consecutive cells in the row and then one cell down.
Then we mark loop cells as even and odd.
After that, we find an odd cell with the smallest value.
Then we go over each cell in the loop and add θ to the value if the cell is even and subtract if odd.
At the end of the pivoting operation, we have a new basic feasible solution.
Now, let’s code the pivoting part. First, we write a function that returns possible next nodes for a given loop. It is pretty straightforward.
Then we need a function that returns loop for a given list with basic variables positions and position of entering variable. We are using recursion in this function. If a loop can be closed we pass to the get_possible_next_nodes position of the entering variable only. If the loop can’t be closed, we recursively go over each possible next node.
The function that makes pivoting operation receives a previous basic feasible solution and loop. First, we gather even and odd cells, then take the leaving variable and return new basic variables.
Now we can put everything together.
To calculate the total sum we need to go over each cost, take the number of units from the solution, multiply the cost of transportation and number of units that will be shipped and add it to the total cost.
Reach the next level of focus and productivity with increaser.org.