-
Notifications
You must be signed in to change notification settings - Fork 63
/
cpx_quad.jl
147 lines (125 loc) · 4.64 KB
/
cpx_quad.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Quadratic terms & constraints
#
function add_qpterms!(model::Model, qr::IVec, qc::IVec, qv::FVec)
n = num_var(model)
((m = length(qr)) == length(qc) == length(qv)) || error("Inconsistent argument dimensions.")
nqv = copy(qv)
Q = sparse(qr, qc, nqv)
if istriu(Q) || istril(Q) || issym(Q)
Q = Q + Q' - spdiagm(diag(Q)) # reconstruct full matrix like CPLEX wants
else
error("Matrix Q must be either symmetric or triangular")
end
if nnz(Q) > 0 #nfilled in 0.3
qmatcnt = Array(Cint, n)
for k = 1:n
qmatcnt[k] = Q.colptr[k+1] - Q.colptr[k]
end
stat = @cpx_ccall(copyquad, Cint, (
Ptr{Void},
Ptr{Void},
Ptr{Cint},
Ptr{Cint},
Ptr{Cint},
Ptr{Cdouble}
),
model.env.ptr, model.lp, convert(Array{Cint,1}, Q.colptr[1:end-1].-1), convert(Array{Cint,1},qmatcnt), convert(Array{Cint,1}, Q.rowval.-1), Q.nzval)
if stat != 0
throw(CplexError(model.env, stat))
end
end
nothing
end
function add_qpterms!(model::Model, qr::Vector, qc::Vector, qv::Vector)
add_qpterms!(model, ivec(qr), ivec(qc), fvec(qv))
end
function add_qpterms!(model, H::SparseMatrixCSC{Float64}) # H must be symmetric
n = num_var(model)
(H.m == n && H.n == n) || error("H must be an n-by-n symmetric matrix.")
nnz_h = nnz(H) #nfilled in 0.3
qr = Array(Cint, nnz_h)
qc = Array(Cint, nnz_h)
qv = Array(Float64, nnz_h)
k = 0
colptr::Vector{Int} = H.colptr
nzval::Vector{Float64} = H.nzval
for i = 1 : n
qi::Cint = convert(Cint, i)
for j = colptr[i]:(colptr[i+1]-1)
qj = convert(Cint, H.rowval[j])
if qi <= qj
k += 1
qr[k] = qi
qc[k] = qj
qv[k] = nzval[j]
end
end
end
add_qpterms!(model, qr[1:k], qc[1:k], qv[1:k])
end
function add_qpterms!(model, H::Matrix{Float64}) # H must be symmetric
n = num_var(model)
size(H) == (n, n) || error("H must be an n-by-n symmetric matrix.")
nmax = int(n * (n + 1) / 2)
qr = Array(Cint, nmax)
qc = Array(Cint, nmax)
qv = Array(Float64, nmax)
k::Int = 0
for i = 1 : n
qi = convert(Cint, i)
for j = i : n
v = H[j, i]
if v != 0.
k += 1
qr[k] = qi
qc[k] = convert(Cint, j)
qv[k] = v
end
end
end
add_qpterms!(model, qr[1:k], qc[1:k], qv[1:k])
end
function add_diag_qpterms!(model, H::Vector) # H stores only the diagonal element
n = num_var(model)
n == length(H) || error("Incompatible dimensions.")
q = [convert(Cint,1):convert(Cint,n)]
add_qpterms!(model, q, q, fvec(h))
end
function add_diag_qpterms!(model, hv::Real) # all diagonal elements are H
n = num_var(model)
q = [convert(Cint,1):convert(Cint,n)]
add_qpterms!(model, q, q, fill(float64(hv), n))
end
# add_qconstr!
function add_qconstr!(model::Model, lind::IVec, lval::FVec, qr::IVec, qc::IVec, qv::FVec, rel::Cchar, rhs::Float64)
qnnz = length(qr)
qnnz == length(qc) == length(qv) || error("Inconsistent argument dimensions.")
lnnz = length(lind)
lnnz == length(lval) || error("Inconsistent argument dimensions.")
if qnnz > 0
stat = @cpx_ccall(addqconstr, Cint, (
Ptr{Void}, # env
Ptr{Void}, # model
Cint, # lnnz
Cint, # qnnz
Float64, # rhs
Cchar, # sense
Ptr{Cint}, # lind
Ptr{Float64}, # lval
Ptr{Cint}, # qrow
Ptr{Cint}, # qcol
Ptr{Float64}, # qval
Ptr{Uint8} # name
),
model.env.ptr, model.lp, lnnz, qnnz, rhs, rel, lind.-1, lval, qr.-1, qc.-1, qv, C_NULL)
if stat != 0
throw(CplexError(model.env, stat))
end
model.has_qc = true
end
nothing
end
function add_qconstr!(model::Model, lind::Vector, lval::Vector, qr::Vector, qc::Vector, qv::Vector{Float64}, rel::GChars, rhs::Real)
const sensemap = ['=' => 'E', '<' => 'L', '>' => 'G']
add_qconstr!(model, ivec(lind), fvec(lval), ivec(qr), ivec(qc), fvec(qv), cchar(sensemap[rel]), float64(rhs))
end