-
Notifications
You must be signed in to change notification settings - Fork 6
/
quadratic_problems.jl
84 lines (71 loc) · 1.9 KB
/
quadratic_problems.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
function qp1_test()
#=
min x^2 + xy + y^2 + yz + z^2
s.t.
x + 2y + 3z >= 4 (c1)
x + y >= 1 (c2)
x,y \in R
=#
model = TestModel{Float64}()
v = MOI.add_variables(model, 3)
cf1 =
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0, 2.0, 3.0], v), 0.0)
c1 = MOI.add_constraint(model, cf1, MOI.GreaterThan(4.0))
c2 = MOI.add_constraint(
model,
MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm.([1.0, 1.0], [v[1], v[2]]),
0.0,
),
MOI.GreaterThan(1.0),
)
obj = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm.(
[2.0, 1.0, 2.0, 1.0, 2.0],
v[[1, 1, 2, 2, 3]],
v[[1, 2, 2, 3, 3]],
),
MOI.ScalarAffineTerm{Float64}[],
0.0,
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}(),
obj,
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
return model
end
function qp2_test()
#=
min 2 x^2 + y^2 + xy + x + y + 1
s.t.
x, y >= 0
x + y = 1
=#
model = TestModel{Float64}()
x = MOI.add_variable(model)
y = MOI.add_variable(model)
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm.([1.0, 1.0], [x, y]),
0.0,
),
MOI.EqualTo(1.0),
)
vc1 = MOI.add_constraint(model, x, MOI.GreaterThan(0.0))
vc2 = MOI.add_constraint(model, y, MOI.GreaterThan(0.0))
obj = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm.([4.0, 2.0, 1.0], [x, y, x], [x, y, y]),
MOI.ScalarAffineTerm.([1.0, 1.0], [x, y]),
1.0,
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}(),
obj,
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
return model
end