Skip to content

Commit dd75fff

Browse files
committed
(#2) Implement solve() interface for Johnson (Flow-Shop Scheduling)
1 parent 6033c11 commit dd75fff

File tree

4 files changed

+125
-60
lines changed

4 files changed

+125
-60
lines changed

docs/src/scheduling.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# Scheduling Problems
22

3+
## Flow-Shop Problem
4+
5+
```@docs
6+
OperationsResearchModels.JohnsonProblem
7+
```
8+
9+
310
## Johnson's Rule for Flow-shop Scheduling
411

512
```@docs
6-
OperationsResearchModels.johnsons
13+
OperationsResearchModels.solve(problem::JohnsonProblem)
14+
```
15+
16+
## Flow-Shop Result
17+
18+
```@docs
19+
OperationsResearchModels.JohnsonResult
720
```
821

922
## Genetic Algorithm for the problems that cannot be solved with using Johnson's Rule

src/OperationsResearchModels.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using JuMP, HiGHS
1313
# solve(g::Game)::GameResult
1414
# solve(m::MstProblem)::MstResult
1515
# solve(t::TravelingSalesmanProblem)::TravelingSalesmanResult
16+
# solve(p::JohnsonProblem)::JohnsonResult
1617
solve() = nothing
1718

1819
# solve!(s::SimplexProblem)::SimplexProblem
@@ -90,7 +91,7 @@ import .CPM: CpmActivity, earliestfinishtime, longestactivity, CpmProblem, CpmRe
9091
import .CPM: PertActivity, PertProblem, PertResult
9192
import .Knapsack: KnapsackResult, KnapsackProblem
9293
import .Latex: latex
93-
import .Johnsons: JohnsonResult, johnsons, JohnsonException, makespan, johnsons_ga
94+
import .Johnsons: JohnsonProblem, JohnsonResult, JohnsonException, makespan, johnsons_ga
9495
import .RandomKeyGA: Chromosome, run_ga
9596
import .TravelingSalesman: TravelingSalesmanResult, TravelingSalesmanProblem
9697
import .Simplex: SimplexProblem, simplexiterations, createsimplexproblem, gaussjordan, OptimizationType
@@ -109,7 +110,7 @@ export Simplex
109110
export Utility
110111
export latex
111112
export Chromosome, run_ga
112-
export JohnsonResult, johnsons, JohnsonException, makespan, johnsons_ga
113+
export JohnsonProblem, JohnsonResult, JohnsonException, makespan, johnsons_ga
113114
export TravelingSalesmanResult, TravelingSalesmanProblem
114115
export simplexiterations, SimplexProblem, createsimplexproblem, gaussjordan, OptimizationType
115116

src/johnsons.jl

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module Johnsons
22

33

44
import ..RandomKeyGA: run_ga, Chromosome, Population, generation, run_ga
5+
import ..OperationsResearchModels: solve
56

67
export JohnsonResult
78
export JohnsonException
8-
export johnsons
99
export makespan
1010
export johnsons_ga
1111

@@ -14,10 +14,35 @@ struct JohnsonException <: Exception
1414
message::String
1515
end
1616

17+
"""
18+
JohnsonProblem
19+
20+
# Description
21+
22+
Represents a Johnson problem instance, containing the job processing times.
1723
24+
# Fields
25+
26+
- `times::Matrix{<:Real}`: A matrix of job processing times.
27+
"""
28+
struct JohnsonProblem
29+
times::Matrix{<:Real}
30+
end
31+
32+
33+
"""
34+
JohnsonResult
35+
36+
# Description
37+
38+
Represents the result of the Johnson's algorithm, containing the optimal job permutation.
39+
40+
# Fields
41+
42+
- `permutation::Vector{Int}`: The optimal job permutation found by the algorithm.
43+
"""
1844
struct JohnsonResult
1945
permutation::Vector{Int}
20-
# makespan::Float64
2146
end
2247

2348
struct Process
@@ -28,15 +53,15 @@ end
2853

2954

3055
"""
31-
johnsons_ga(times::Matrix; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::JohnsonResult
56+
johnsons_ga(problem::JohnsonProblem; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::JohnsonResult
3257
33-
Given a matrix of times, returns a JohnsonResult with the permutation of the jobs.
34-
The function uses a genetic algorithm to find the best permutation of the jobs.
58+
Given a problem containing a matrix of times, returns a JohnsonResult with the permutation of the jobs.
59+
The function uses a genetic algorithm to find the best permutation of the jobs.
3560
The genetic algorithm is implemented in the RandomKeyGA module.
3661
3762
# Arguments
3863
39-
- `times::Matrix`: a matrix of times
64+
- `problem::JohnsonProblem`: a problem containing a matrix of times
4065
- `popsize::Int`: the population size. Default is 100
4166
- `ngen::Int`: the number of generations. Default is 1000
4267
- `pcross::Float64`: the crossover probability. Default is 0.8
@@ -61,17 +86,18 @@ times = Float64[
6186
7.0 4.0
6287
]
6388
64-
result = johnsons(times)
89+
result = johnsons_ga(times)
6590
6691
println(result.permutation)
6792
```
6893
"""
69-
function johnsons_ga(times::Matrix; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::JohnsonResult
94+
function johnsons_ga(problem::JohnsonProblem; popsize = 100, ngen = 1000, pcross = 0.8, pmutate = 0.01, nelites = 1)::JohnsonResult
7095

71-
n, m = size(times)
96+
times = problem.times
97+
n, _ = size(times)
7298

7399
function costfn(perm::Vector{Int})
74-
return makespan(times, perm)
100+
return makespan(problem, perm)
75101
end
76102

77103
finalpop = run_ga(popsize, n, costfn, ngen, pcross, pmutate, nelites)
@@ -82,9 +108,9 @@ end
82108

83109

84110
"""
85-
johnsons(times::Matrix)
111+
johnsons(problem::JohnsonProblem)::JohnsonResult
86112
87-
Given a matrix of times, returns a JohnsonResult with the permutation of the jobs.
113+
Given a problem containing a matrix of times, returns a JohnsonResult with the permutation of the jobs.
88114
If number of machines is 2, it uses the Johnson's algorithm for 2 machines.
89115
If number of machines is greater than 2, it uses the Johnson's algorithm by transforming the
90116
problem into a 2-machine problem.
@@ -121,12 +147,13 @@ times = Float64[
121147
7.0 4.0
122148
]
123149
124-
result = johnsons(times)
150+
result = solve(JohnsonProblem(times))
125151
126152
println(result.permutation)
127153
```
128154
"""
129-
function johnsons(times::Matrix)
155+
function solve(problem::JohnsonProblem)::JohnsonResult
156+
times = problem.times
130157
_, m = size(times)
131158
if m == 2
132159
return johnsons_2machines(times)
@@ -196,13 +223,13 @@ end
196223

197224

198225
"""
199-
makespan(times::Matrix, permutation::Vector{Int})
226+
makespan(problem::JohnsonProblem, permutation::Vector{Int})
200227
201-
Given a matrix of times and a permutation of the jobs, returns the makespan of the jobs.
228+
Given a problem containing a matrix of times and a permutation of the jobs, returns the makespan of the jobs.
202229
203230
# Arguments
204231
205-
- `times::Matrix`: a matrix of times
232+
- `problem::JohnsonProblem`: a problem containing a matrix of times
206233
- `permutation::Vector{Int}`: a permutation of the jobs
207234
208235
# Returns
@@ -220,11 +247,12 @@ julia> times = Float64[
220247
2 5 6
221248
]
222249
223-
julia> result = makespan(times, [1, 4, 5, 3, 2])
250+
julia> result = makespan(JohnsonProblem(times), [1, 4, 5, 3, 2])
224251
```
225252
"""
226-
function makespan(times::Matrix, permutation::Vector{Int})::Float64
253+
function makespan(problem::JohnsonProblem, permutation::Vector{Int})::Float64
227254

255+
times = problem.times
228256
n, m = size(times)
229257

230258
timetable = Matrix{Process}(undef, m, n)

0 commit comments

Comments
 (0)