Skip to content

Commit

Permalink
Johnson's rule throws exception if the problem cannot be reduced into…
Browse files Browse the repository at this point in the history
… 2-machines problem
  • Loading branch information
jbytecode committed May 17, 2024
1 parent 54150a0 commit 0f13347
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/OperationsResearchModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import .CPM: CpmActivity, earliestfinishtime, longestactivity, CpmProblem, CpmRe
import .CPM: PertActivity, PertProblem, PertResult
import .Knapsack: KnapsackResult, KnapsackProblem
import .Latex: latex
import .Johnsons: JohnsonResult, johnsons
import .Johnsons: JohnsonResult, johnsons, JohnsonException

export TransportationProblem, TransportationResult, balance, isbalanced, northwestcorner
export Connection, ShortestPathResult, MaximumFlowResult, nodes
Expand All @@ -65,7 +65,7 @@ export KnapsackResult, KnapsackProblem
export Simplex
export Utility
export latex
export JohnsonResult, johnsons
export JohnsonResult, johnsons, JohnsonException

export JuMP, HiGHS

Expand Down
15 changes: 13 additions & 2 deletions src/johnsons.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
module Johnsons

export JohnsonResult, johnsons
export JohnsonResult, JohnsonException, johnsons

# TODO: Add makespan calculation

struct JohnsonException <: Exception
message::String
end


struct JohnsonResult
permutation::Vector{Int}
# makespan::Float64
end



"""
johnsons(times::Matrix)
Expand All @@ -26,6 +33,8 @@ at least one of the following conditions must be satisfied:
- min(A) >= max(B, C)
- min(D) >= max(B, C)
The function throws a JohnsonException if the problem cannot be reduced to a 2-machine problem.
# Arguments
- `times::Matrix`: a matrix of times
Expand Down Expand Up @@ -102,7 +111,9 @@ function johnsons_nmachines(times::Matrix)::JohnsonResult
minlast = minimum(times[:, end])
maxothers = maximum(times[:, 2:(end-1)])

@assert (minfirst >= maxothers) || (minlast >= maxothers)
if !((minfirst >= maxothers) || (minlast >= maxothers))
throw(JohnsonException("The problem cannot be reduced to a 2-machine problem: minfirst >= maxothers and/or minlast >= maxothers"))
end

Gcollection = times[:, 1:(end-1)]
Hcollection = times[:, 2:end]
Expand Down
15 changes: 15 additions & 0 deletions test/testjohnsons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@
end


@testset "Cannot reduce to 2-machines" begin

times = Float64[
3 3 5 2;
8 4 8 3;
7 2 10 4;
5 1 7 5;
2 5 6 6
]

@test_throws JohnsonException johnsons(times)

end


end

0 comments on commit 0f13347

Please sign in to comment.