forked from jump-dev/Gurobi.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp_04.jl
107 lines (86 loc) · 2.39 KB
/
lp_04.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Start from simple LP
# Solve it
# Copy and solve again
# Chg coeff, solve, change back solve
# del constr and solve
# del var and solve
# a simple LP example
#
# maximize x + y
#
# s.t. 2 x + 1 y <= 4
# 1 x + 2 y <= 4
# x >= 0, y >= 0
#
# solution: x = 1.3333333, y = 1.3333333, objv = 2.66666666
using Gurobi, Test
@testset "LP 04" begin
env = Gurobi.Env()
setparam!(env, "OutputFlag", 0)
setparams!(env, Method=2) # using barrier method
method = getparam(env, "Method")
@test method == 2
model = Gurobi.Model(env, "lp_01", :maximize)
# add variables
add_cvars!(model, [1., 1.], [0., 0.], Inf)
update_model!(model)
# add constraints
add_constrs!(model, Cint[1, 3], Cint[1, 2, 1, 2],
[2., 1., 1., 2.], '<', [4., 4.])
update_model!(model)
# perform optimization
optimize(model)
@test isapprox(get_solution(model), [1.3333, 1.3333], atol=1e-4)
@test isapprox(get_objval(model), 2.6666, atol=1e-4)
# PART 2:
# copy and solve
model2 = copy(model)
optimize(model2)
@test isapprox(get_solution(model2), [1.3333, 1.3333], atol=1e-4)
@test isapprox(get_objval(model2), 2.6666, atol=1e-4)
# PART 3:
# change coeff and solve
# maximize x + y
#
# s.t. 2 x + 2 y <= 4
# 1 x + 2 y <= 4
# x >= 0, y >= 0
#
# solution: x = 0, y = 2, objv = 2
# or : any λ(0, 2) + (1-λ)(2, 0)
chg_coeffs!(model, [1], [2], [2.])
update_model!(model)
optimize(model)
sol = get_solution(model)
@test !any(sol .< 0)
@test !any(sol .> 2)
@test sum(sol) == 2.0
@test isapprox(get_objval(model), 2.0, atol=1e-4)
# PART 4:
# del constr and solve
# maximize x + y
#
# s.t. 1 x + 2 y <= 4
# x >= 0, y >= 0
#
# solution: x = 4, y = 0, objv = 4
del_constrs!(model, [1])
update_model!(model)
optimize(model)
@test isapprox(get_solution(model), [4.0, 0.0], atol=1e-4)
@test isapprox(get_objval(model), 4.0, atol=1e-4)
# PART 5:
# del var and solve
# maximize y
#
# s.t. 2 y <= 4
# y >= 0
#
# solution: y = 2, objv = 2
del_vars!(model, [1])
update_model!(model)
optimize(model)
@test isapprox(get_solution(model), [2.0], atol=1e-4)
@test isapprox(get_objval(model), 2.0, atol=1e-4)
GC.gc() # test finalizers
end