-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathquadprog.jl
78 lines (65 loc) · 2.51 KB
/
quadprog.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
mutable struct QuadprogSolution
status
objval
sol
attrs
end
function no_qp_solver()
error("""
A quadratic programming solver must be specified as the final argument.
You can use some of the solvers listed on the solvers table of http://www.juliaopt.org/ that has a checkmark the Linear/Quadratic column. See the solver's documentation to confirm that it supports quadratic objectives.
A (free) example is Ipopt.jl. Once Ipopt is installed and imported via "using Ipopt", you can specify IpoptSolver() as the solver.
Solver options are specified by using keyword arguments to IpoptSolver().
""")
end
function quadprog(c::InputVector, Q::AbstractMatrix, A::AbstractMatrix, rowlb::InputVector, rowub::InputVector, lb::InputVector, ub::InputVector, solver::AbstractMathProgSolver)
m = LinearQuadraticModel(solver)
nrow,ncol = size(A)
c = expandvec(c, ncol)
rowlbtmp = expandvec(rowlb, nrow)
rowubtmp = expandvec(rowub, nrow)
lb = expandvec(lb, ncol)
ub = expandvec(ub, ncol)
# rowlb is allowed to be vector of senses
if eltype(rowlbtmp) == Char
realtype = eltype(rowubtmp)
sense = rowlbtmp
rhs = rowubtmp
@assert realtype <: Real
warn_no_inf(realtype)
rowlb = Array{realtype}(undef, nrow)
rowub = Array{realtype}(undef, nrow)
for i in 1:nrow
if sense[i] == '<'
rowlb[i] = typemin(realtype)
rowub[i] = rhs[i]
elseif sense[i] == '>'
rowlb[i] = rhs[i]
rowub[i] = typemax(realtype)
elseif sense[i] == '='
rowlb[i] = rhs[i]
rowub[i] = rhs[i]
else
error("Unrecognized sense '$(sense[i])'")
end
end
else
rowlb = rowlbtmp
rowub = rowubtmp
end
loadproblem!(m, A, lb, ub, c, rowlb, rowub, :Min)
setquadobj!(m, Q)
optimize!(m)
stat = status(m)
if stat == :Optimal
attrs = Dict()
return QuadprogSolution(stat, getobjval(m), getsolution(m), attrs)
else
return QuadprogSolution(stat, nothing, [], Dict())
end
end
quadprog(c,Q,A,rowlb,rowub,solver::AbstractMathProgSolver) = quadprog(c,Q,A,rowlb,rowub,0,Inf,solver)
# Old versions
quadprog(c,Q,A,rowlb,rowub) = no_qp_solver()
quadprog(c::InputVector, Q::AbstractMatrix, A::AbstractMatrix, rowlb::InputVector, rowub::InputVector, lb::InputVector, ub::InputVector) = no_qp_solver()
export quadprog