forked from muratakcs/Optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
house.jl
37 lines (29 loc) · 1.21 KB
/
house.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# this array stores the task names (:a, :b, ..., :x)
tasks = []
for i = 'a':'x'
push!(tasks, Symbol(i)) # string(sym) converts back to a string, i.e. string(:hello) returns "hello"
end
# this dictionary stores the project durations
dur = [0, 4, 2, 4, 6, 1, 2, 3, 2, 4, 10, 3, 1, 2, 3, 2, 1, 1, 2, 3, 1, 2, 5, 0]
duration = Dict(zip(tasks,dur))
# this dictionary stores the projects that a given project depends on (ancestors)
pre = ( [], [:a], [:b], [:c], [:d], [:c], [:f], [:f], [:d], [:d,:g], [:i,:j,:h], [:k],
[:l], [:l], [:l], [:e], [:p], [:c], [:o,:t], [:m,:n], [:t], [:q,:r], [:v], [:s,:u,:w])
pred = Dict(zip(tasks,pre));
using JuMP,Clp
m = Model(with_optimizer(Clp.Optimizer))
@variable(m, tstart[tasks])
# one-line implementation of the constraints:
# @constraint(m, link[i in tasks, j in pred[i]], tstart[i] >= tstart[j] + duration[j])
for i in tasks
for j in pred[i]
@constraint(m, tstart[i] >= tstart[j] + duration[j])
end
end
@constraint(m, tstart[:a] == 0)
@objective(m, Min, tstart[:x] + duration[:x]) # total duration is start time of last task + duration of last task.
optimize!(m)
for i in tasks
println(value(tstart[i]))
end
println("minimum duration: ", objective_value(m))