@@ -6,6 +6,10 @@ using JuMP, HiGHS
66
77export TransportationProblem
88export TransportationResult
9+ export northwestcorner
10+ export leastcost
11+ export isbalanced
12+ export balance
913
1014import .. OperationsResearchModels: solve
1115
@@ -151,7 +155,7 @@ function solve(t::TransportationProblem)::TransportationResult
151155 @constraint (model, sum (x[1 : n, j] for j = 1 : p) .== newt. supply)
152156 @constraint (model, sum (x[i, 1 : p] for i = 1 : n) .== newt. demand)
153157
154- initial_solution = northwestcorner (newt). solution
158+ initial_solution = northwestcorner (newt). solution
155159 JuMP. set_start_value .(x, initial_solution)
156160
157161 optimize! (model)
@@ -198,6 +202,33 @@ function northwestcorner(t::TransportationProblem)::TransportationResult
198202 return result
199203end
200204
205+ function leastcost (t:: TransportationProblem ):: TransportationResult
206+ problem = t
207+ if ! isbalanced (t)
208+ problem = balance (t)
209+ end
210+ supply = Base. copy (problem. supply)
211+ demand = Base. copy (problem. demand)
212+ n, m = size (problem. costs)
213+ asgnmatrix = zeros (Float64, n, m)
214+ cost = 0.0
215+ while (sum (supply) > 0 ) && (sum (demand) > 0 )
216+ mincost = minimum (problem. costs)
217+ mincostrow, mincostcol = argmin (problem. costs). I
218+ amount = min (supply[mincostrow], demand[mincostcol])
219+ asgnmatrix[mincostrow, mincostcol] = amount
220+ supply[mincostrow] -= amount
221+ demand[mincostcol] -= amount
222+ if supply[mincostrow] == 0
223+ problem. costs[mincostrow, :] .= Inf
224+ elseif demand[mincostcol] == 0
225+ problem. costs[:, mincostcol] .= Inf
226+ end
227+ cost += amount * mincost
228+ end
229+ result = TransportationResult (t, problem, asgnmatrix, cost)
230+ return result
231+ end
201232
202233
203234end # end of module
0 commit comments