Skip to content

Commit a246689

Browse files
committed
Temp save
1 parent 53a9a2d commit a246689

File tree

5 files changed

+131
-82
lines changed

5 files changed

+131
-82
lines changed

src/ConstraintModels.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ const LS = LocalSearchSolvers
1010

1111
import LocalSearchSolvers: Options
1212

13-
export qap
14-
export mincut
13+
export chemical_equilibrium
1514
export golomb
15+
export qap
1616
export magic_square
17+
export mincut
1718
export n_queens
19+
export scheduling
1820
export sudoku
19-
export chemical_equilibrium
2021

2122
include("assignment.jl")
2223
include("chemical_equilibrium.jl")
2324
include("cut.jl")
2425
include("golomb.jl")
2526
include("magic_square.jl")
2627
include("n_queens.jl")
28+
include("scheduling.jl")
2729
include("sudoku.jl")
2830

2931
end

src/scheduling.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function scheduling(processing_times, due_dates, ::Val{:JuMP})
2+
model = JuMP.Model(CBLS.Optimizer)
3+
n = length(processing_times) # number of jobs
4+
max_time = sum(processing_times)
5+
6+
minus_eq_param(param) = x -> x[1] - x[2] == param
7+
less_than_param(param) = x -> x[1] param
8+
sequential_tasks(x) = x[1] x[2] || x[3] x[4]
9+
10+
11+
@variable(model, 0 C[1:n] max_time, Int) # completion
12+
@variable(model, 0 S[1:n] max_time, Int) # start
13+
14+
@constraint(model, C in AllDifferent())
15+
@constraint(model, S in AllDifferent())
16+
17+
for i in 1:n
18+
@constraint(model, [C[i], S[i]] in Predicate(minus_eq_param(processing_times[i])))
19+
# @constraint(model, [C[i]] in Predicate(less_than_param(due_dates[i])))
20+
end
21+
# for i in 1:n, j in 1:n
22+
# i == j && continue
23+
# @constraint(model, [C[i], S[j], C[j], S[i]] in Predicate(sequential_tasks))
24+
# end
25+
26+
return model, C, S
27+
end
28+
29+
"""
30+
scheduling(processing_time, due_date; modeler=:JuMP)
31+
32+
Create a model for the n-queens problem with `n` queens. The `modeler` argument accepts :JuMP (default), which refer to the JuMP model.
33+
"""
34+
scheduling(processing_times, due_dates; modeler=:JuMP) = scheduling(processing_times, due_dates, Val(modeler))

test/JuMP.jl

Lines changed: 85 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,87 @@
1-
@testset "JuMP: constraints" begin
2-
m = Model(CBLS.Optimizer)
3-
4-
err = _ -> 1.0
5-
concept = _ -> true
6-
7-
@variable(m, X[1:10], DiscreteSet(1:4))
8-
9-
@constraint(m, X in Error(err))
10-
@constraint(m, X in Predicate(concept))
11-
12-
@constraint(m, X in AllDifferent())
13-
@constraint(m, X in AllEqual())
14-
@constraint(m, X in AllEqualParam(2))
15-
@constraint(m, X in AlwaysTrue())
16-
@constraint(m, X[1:4] in DistDifferent())
17-
@constraint(m, X[1:2] in Eq())
18-
@constraint(m, X in Ordered())
19-
end
20-
21-
@testset "JuMP: sudoku 9x9" begin
22-
m, X = sudoku(3)
23-
optimize!(m)
24-
solution_ = value.(X)
25-
display(solution_, Val(:sudoku))
26-
end
27-
28-
@testset "JuMP: golomb(5)" begin
29-
m, X = golomb(5)
30-
optimize!(m)
31-
@info "JuMP: golomb(5)" value.(X)
32-
end
33-
34-
@testset "JuMP: magic_square(3)" begin
35-
m, X = magic_square(3)
36-
optimize!(m)
37-
@info "JuMP: magic_square(3)" value.(X)
38-
end
39-
40-
@testset "JuMP: n_queens(5)" begin
41-
m, X = n_queens(5)
42-
optimize!(m)
43-
@info "JuMP: n_queens(5)" value.(X)
44-
end
45-
46-
@testset "JuMP: qap(12)" begin
47-
m, X = qap(12, qap_weights, qap_distances)
48-
optimize!(m)
49-
@info "JuMP: qap(12)" value.(X)
50-
end
51-
52-
@testset "JuMP: basic opt" begin
53-
model = Model(CBLS.Optimizer)
54-
set_optimizer_attribute(model, "iteration", 100)
55-
@test get_optimizer_attribute(model, "iteration") == 100
56-
set_time_limit_sec(model, 5.0)
57-
@test time_limit_sec(model) == 5.0
58-
59-
@variable(model, x in DiscreteSet(0:20))
60-
@variable(model, y in DiscreteSet(0:20))
61-
62-
@constraint(model, [x,y] in Predicate(v -> 6v[1] + 8v[2] >= 100 ))
63-
@constraint(model, [x,y] in Predicate(v -> 7v[1] + 12v[2] >= 120 ))
64-
65-
objFunc = v -> 12v[1] + 20v[2]
66-
@objective(model, Min, ScalarFunction(objFunc))
67-
1+
# @testset "JuMP: constraints" begin
2+
# m = Model(CBLS.Optimizer)
3+
4+
# err = _ -> 1.0
5+
# concept = _ -> true
6+
7+
# @variable(m, X[1:10], DiscreteSet(1:4))
8+
9+
# @constraint(m, X in Error(err))
10+
# @constraint(m, X in Predicate(concept))
11+
12+
# @constraint(m, X in AllDifferent())
13+
# @constraint(m, X in AllEqual())
14+
# @constraint(m, X in AllEqualParam(2))
15+
# @constraint(m, X in AlwaysTrue())
16+
# @constraint(m, X[1:4] in DistDifferent())
17+
# @constraint(m, X[1:2] in Eq())
18+
# @constraint(m, X in Ordered())
19+
# end
20+
21+
# @testset "JuMP: sudoku 9x9" begin
22+
# m, X = sudoku(3)
23+
# optimize!(m)
24+
# solution_ = value.(X)
25+
# display(solution_, Val(:sudoku))
26+
# end
27+
28+
# @testset "JuMP: golomb(5)" begin
29+
# m, X = golomb(5)
30+
# optimize!(m)
31+
# @info "JuMP: golomb(5)" value.(X)
32+
# end
33+
34+
# @testset "JuMP: magic_square(3)" begin
35+
# m, X = magic_square(3)
36+
# optimize!(m)
37+
# @info "JuMP: magic_square(3)" value.(X)
38+
# end
39+
40+
# @testset "JuMP: n_queens(5)" begin
41+
# m, X = n_queens(5)
42+
# optimize!(m)
43+
# @info "JuMP: n_queens(5)" value.(X)
44+
# end
45+
46+
# @testset "JuMP: qap(12)" begin
47+
# m, X = qap(12, qap_weights, qap_distances)
48+
# optimize!(m)
49+
# @info "JuMP: qap(12)" value.(X)
50+
# end
51+
52+
# @testset "JuMP: basic opt" begin
53+
# model = Model(CBLS.Optimizer)
54+
# set_optimizer_attribute(model, "iteration", 100)
55+
# @test get_optimizer_attribute(model, "iteration") == 100
56+
# set_time_limit_sec(model, 5.0)
57+
# @test time_limit_sec(model) == 5.0
58+
59+
# @variable(model, x in DiscreteSet(0:20))
60+
# @variable(model, y in DiscreteSet(0:20))
61+
62+
# @constraint(model, [x,y] in Predicate(v -> 6v[1] + 8v[2] >= 100 ))
63+
# @constraint(model, [x,y] in Predicate(v -> 7v[1] + 12v[2] >= 120 ))
64+
65+
# objFunc = v -> 12v[1] + 20v[2]
66+
# @objective(model, Min, ScalarFunction(objFunc))
67+
68+
# optimize!(model)
69+
70+
# @info "JuMP: basic opt" value(x) value(y) (12*value(x)+20*value(y))
71+
# end
72+
73+
# @testset "JuMP: Chemical equilibrium" begin
74+
# m, X = chemical_equilibrium(atoms_compounds, elements_weights, standard_free_energy)
75+
# # set_optimizer_attribute(m, "iteration", 10000)
76+
# # set_time_limit_sec(m, 120.0)
77+
# optimize!(m)
78+
# @info "JuMP: $compounds_names ⟺ $mixture_name" value.(X)
79+
# end
80+
81+
@testset "JuMP: Scheduling" begin
82+
model, completion_times, start_times = scheduling(processing_times, due_times)
6883
optimize!(model)
69-
70-
@info "JuMP: basic opt" value(x) value(y) (12*value(x)+20*value(y))
71-
end
72-
73-
@testset "JuMP: Chemical equilibrium" begin
74-
m, X = chemical_equilibrium(atoms_compounds, elements_weights, standard_free_energy)
75-
# set_optimizer_attribute(m, "iteration", 10000)
76-
# set_time_limit_sec(m, 120.0)
77-
optimize!(m)
78-
@info "JuMP: $compounds_names$mixture_name" value.(X)
84+
@info solution_summary(model)
85+
@info "JuMP: scheduling" value.(start_times) value.(completion_times) processing_times due_times
86+
@info (value.(start_times)+processing_times == value.(completion_times))
7987
end

test/instances.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ compounds_names = "x₁⋅H + x₂⋅H₂ + x₃⋅H₂O + x₄⋅N + x₅⋅N
7474
mixture_name = "½⋅N₂H₄ + ½⋅O₂"
7575

7676
equation = compounds_names * " = " * mixture_name
77+
78+
# processing_times = [3, 2, 1]
79+
# due_times = [5, 6, 8]
80+
processing_times = [3, 2]
81+
due_times = [5, 6]

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using Test
88

99
@testset "ConstraintModels.jl" begin
1010
include("instances.jl")
11-
include("raw_solver.jl")
12-
include("MOI_wrapper.jl")
11+
# include("raw_solver.jl")
12+
# include("MOI_wrapper.jl")
1313
include("JuMP.jl")
1414
end

0 commit comments

Comments
 (0)