Skip to content

Commit 25cd46c

Browse files
committed
make initial solution optional in transportation problems
1 parent 47d66a7 commit 25cd46c

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### 0.2.6 (Upcoming Release)
22

3+
- Make initial solution optional in Transportation problems
4+
35

46
### 0.2.5
57

src/transportation.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ end
4545
- `cost::Real`: The optimal cost of the transportation problem.
4646
4747
# Description
48+
4849
The `TransportationResult` struct represents the result of solving a transportation problem.
4950
It contains the original problem, the balanced problem, the solution matrix, and the optimal cost.
5051
"""
@@ -120,17 +121,24 @@ end
120121

121122

122123
"""
123-
solve(t)
124+
solve(t, initial = NoInitial)
124125
125126
# Arguments
126-
`a::TransportationProblem`: The problem in type of TransportationProblem
127+
128+
- `a::TransportationProblem`: The problem in type of TransportationProblem
127129
128130
# Output
129-
`TransportationResult`: The custom data type that holds problem, solution, and optimum cost.
131+
- `TransportationResult`: The custom data type that holds problem, solution, and optimum cost.
132+
- `initial::TransportationResult`: The initial solution of the transportation problem (optional).
130133
131134
# Description
135+
132136
Solves a transportation problem given by an object of in type `TransportationProblem`.
133137
138+
initial is used to store the initial solution of the transportation problem. Any custom
139+
implementation should take a `TransportationProblem` and return a `TransportationResult` object.
140+
Currently, `northwestcorner` and `leastcost` are implemented as custom initial solutions.
141+
134142
# Example
135143
136144
```julia
@@ -168,7 +176,7 @@ Solution:
168176
[-0.0 -0.0 -0.0 100.0; 100.0 -0.0 -0.0 -0.0; -0.0 -0.0 100.0 -0.0; -0.0 100.0 -0.0 -0.0]
169177
```
170178
"""
171-
function solve(t::TransportationProblem)::TransportationResult
179+
function solve(t::TransportationProblem; initial::Function = NoInitial)::TransportationResult
172180
newt = balance(t)
173181

174182
model = JuMP.Model(HiGHS.Optimizer)
@@ -182,7 +190,7 @@ function solve(t::TransportationProblem)::TransportationResult
182190
@constraint(model, sum(x[1:n, j] for j = 1:p) .== newt.supply)
183191
@constraint(model, sum(x[i, 1:p] for i = 1:n) .== newt.demand)
184192

185-
initial_solution = northwestcorner(newt).solution
193+
initial_solution = initial(newt).solution
186194
JuMP.set_start_value.(x, initial_solution)
187195

188196
optimize!(model)
@@ -194,6 +202,8 @@ function solve(t::TransportationProblem)::TransportationResult
194202
return result
195203
end
196204

205+
206+
197207
"""
198208
northwestcorner(a::TransportationProblem)::TransportationResult
199209
@@ -286,4 +296,9 @@ function leastcost(t::TransportationProblem)::TransportationResult
286296
end
287297

288298

299+
function NoInitial(t::TransportationProblem)::TransportationResult
300+
TransportationResult(t, t, zeros(size(t.costs)), 0.0)
301+
end
302+
303+
289304
end # end of module

test/testtransportation.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175

176176

177177

178-
@testset "Big Example - Check if it is solved in reasonable times" verbose = true begin
178+
@testset "Big Example with Nortwest-corner initial solution - Check if it is solved in reasonable times" verbose = true begin
179179

180180
t = TransportationProblem(
181181
rand(10:1000, 350, 450) * 1.0,
@@ -184,7 +184,22 @@
184184
)
185185

186186

187-
result = solve(t)
187+
result = solve(t, initial = northwestcorner)
188+
189+
@test result.cost > 0
190+
191+
end
192+
193+
@testset "Big Example with least cost initial solution - Check if it is solved in reasonable times" verbose = true begin
194+
195+
t = TransportationProblem(
196+
rand(10:1000, 350, 450) * 1.0,
197+
rand(1:100, 450) * 1.0,
198+
rand(1:100, 350) * 1.0,
199+
)
200+
201+
202+
result = solve(t, initial = leastcost)
188203

189204
@test result.cost > 0
190205

0 commit comments

Comments
 (0)