Skip to content

Commit 5499980

Browse files
committed
initial commit
0 parents  commit 5499980

File tree

9 files changed

+256
-0
lines changed

9 files changed

+256
-0
lines changed

.appveyor.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
environment:
2+
matrix:
3+
- julia_version: 0.7
4+
- julia_version: 1
5+
- julia_version: nightly
6+
7+
platform:
8+
- x86 # 32-bit
9+
- x64 # 64-bit
10+
11+
matrix:
12+
allow_failures:
13+
- julia_version: nightly
14+
15+
branches:
16+
only:
17+
- master
18+
- /release-.*/
19+
20+
notifications:
21+
- provider: Email
22+
on_build_success: false
23+
on_build_failure: false
24+
on_build_status_changed: false
25+
26+
install:
27+
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))
28+
29+
build_script:
30+
- echo "%JL_BUILD_SCRIPT%"
31+
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"
32+
33+
test_script:
34+
- echo "%JL_TEST_SCRIPT%"
35+
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"
36+
37+
# # Uncomment to support code coverage upload. Should only be enabled for packages
38+
# # which would have coverage gaps without running on Windows
39+
# on_success:
40+
# - echo "%JL_CODECOV_SCRIPT%"
41+
# - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%"

.codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
comment: false

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: julia
2+
3+
os:
4+
- osx
5+
- linux
6+
7+
julia:
8+
- 0.7
9+
- 1.0
10+
- nightly
11+
12+
matrix:
13+
allow_failures:
14+
- julia: nightly
15+
16+
notifications:
17+
email: false
18+
19+
script: # the default script is equivalent to the following
20+
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
21+
- julia -e 'Pkg.clone(pwd()); Pkg.build("QPSReader"); Pkg.test("QPSReader"; coverage=true)';
22+
23+
after_success:
24+
- julia -e 'cd(Pkg.dir("QPSReader")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())';
25+
- julia -e 'cd(Pkg.dir("QPSReader")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())';

Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
authors = ["Dominique Orban <[email protected]>"]
2+
name = "QuadraticModels"
3+
uuid = "f468eda6-eac5-11e8-05a5-ff9e497bcd19"
4+
version = "0.1.0"
5+
6+
[deps]

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# QuadraticModels
2+
3+
Linux and macOS: [![Build Status](https://travis-ci.org/JuliaSmoothOptimizers/QuadraticModels.jl.svg?branch=master)](https://travis-ci.org/JuliaSmoothOptimizers/QuadraticModels.jl)
4+
Windows: [![Build Status](https://ci.appveyor.com/api/projects/status/github/JuliaSmoothOptimizers/QuadraticModels.jl?branch=master&svg=true)](https://ci.appveyor.com/project/tkelman/example-jl/branch/master)
5+
[![Coverage Status](https://coveralls.io/repos/JuliaSmoothOptimizers/QuadraticModels.jl/badge.svg?branch=master)](https://coveralls.io/r/JuliaSmoothOptimizers/QuadraticModels.jl?branch=master)
6+
[![codecov.io](http://codecov.io/github/JuliaSmoothOptimizers/QuadraticModels.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaSmoothOptimizers/QuadraticModels.jl?branch=master)
7+
8+
A package to model linear and quadratic optimization problems using the [NLPModels.jl](https://github.com/JuliaSmoothOptimizers/NLPModels.jl) data structures.
9+
10+
The problems represented have the form
11+
12+
<p align="center">
13+
optimize &nbsp; c₀ + cᵀ x + ½ xᵀ Q x
14+
&nbsp;&nbsp;
15+
subject to &nbsp; L ≤ Ax ≤ U and ℓ ≤ x ≤ u,
16+
</p>
17+
18+
where the square symmetric matrix Q is zero for linear optimization problems.

REQUIRE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
julia 0.7
2+
FastClosures 0.2.1
3+
LinearOperators 0.5.0
4+
NLPModels 0.6.1
5+
Requires 0.3.0

src/QuadraticModels.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module QuadraticModels
2+
3+
using FastClosures
4+
using LinearOperators
5+
using NLPModels
6+
using Requires
7+
8+
using LinearAlgebra
9+
using SparseArrays
10+
11+
import NLPModels:
12+
objgrad, objgrad!, obj,grad, grad!,
13+
hess_coord, hess, hess_op, hprod,
14+
cons, cons!,
15+
jac_coord, jac, jac_op, jprod, jtprod
16+
17+
export QuadraticModel
18+
19+
include("qpmodel.jl")
20+
21+
function __init__()
22+
@require QPSReader = "758ba83c-e923-11e8-036a-3f93d0cb3d0c" include("qps.jl")
23+
end
24+
25+
end # module

src/qpmodel.jl

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
mutable struct QPData
2+
c0::Float64 # constant term in objective
3+
c::AbstractVector{Float64} # linear term
4+
H::AbstractMatrix{Float64} # quadratic term
5+
opH::AbstractLinearOperator # assumed with preallocation!
6+
A::AbstractMatrix{Float64} # constraint matrix
7+
end
8+
9+
mutable struct QPCounters
10+
neval_hprod::Int # number of products with H
11+
neval_jprod::Int # number of products with A
12+
neval_jtprod::Int # number of products with A'
13+
QPCounters() = new(0, 0, 0)
14+
end
15+
16+
abstract type AbstractQuadraticModel <: AbstractNLPModel end
17+
18+
mutable struct QuadraticModel <: AbstractQuadraticModel
19+
meta::NLPModelMeta
20+
counters::QPCounters
21+
data::QPData
22+
23+
function QuadraticModel(c::AbstractVector{Float64}, H::AbstractMatrix{Float64},
24+
opH::AbstractLinearOperator,
25+
A::AbstractMatrix{Float64},
26+
lcon::AbstractVector{Float64}, ucon::AbstractVector{Float64},
27+
lvar::AbstractVector{Float64}, uvar::AbstractVector{Float64};
28+
c0::Float64=0.0, kwargs...)
29+
ncon, nvar = size(A)
30+
nnzh = issparse(H) ? nnz(H) : (nvar * (nvar + 1) / 2)
31+
nnzj = issparse(A) ? nnz(A) : (nvar * ncon)
32+
new(NLPModelMeta(nvar,
33+
lvar=lvar, uvar=uvar,
34+
ncon=size(A,1), lcon=lcon, ucon=ucon,
35+
nnzj=nnzj,
36+
nnzh=nnzh,
37+
lin=1:ncon, nln=Int[], islp=(ncon == 0); kwargs...),
38+
QPCounters(),
39+
QPData(c0, c, H, opH, A))
40+
end
41+
end
42+
43+
function QuadraticModel(model::AbstractNLPModel)
44+
nvar = model.meta.nvar
45+
ncon = model.meta.ncon
46+
z = zeros(nvar)
47+
c = grad(model, z)
48+
H = hess(model, model.meta.x0)
49+
A = jac(model, z)
50+
QuadraticModel(c, H, opHermitian(H), A,
51+
model.meta.lcon, model.meta.ucon,
52+
model.meta.lvar, model.meta.uvar)
53+
end
54+
55+
linobj(qp::AbstractQuadraticModel, args...) = qp.data.c
56+
57+
function objgrad(qp::AbstractQuadraticModel, x::AbstractVector)
58+
g = Vector{eltype(x)}(length(x))
59+
objgrad!(qp, x, g)
60+
end
61+
62+
function objgrad!(qp::AbstractQuadraticModel, x::AbstractVector, g::AbstractVector)
63+
v = qp.data.opH * x
64+
@. g = qp.data.c + v
65+
f = qp.data.c0 + dot(qp.data.c, x) + 0.5 * dot(v, x)
66+
qp.counters.neval_hprod += 1
67+
(f, g)
68+
end
69+
70+
function obj(qp::AbstractQuadraticModel, x::AbstractVector)
71+
v = qp.data.opH * x
72+
f = qp.data.c0 + dot(qp.data.c, x) + 0.5 * dot(v, x)
73+
qp.counters.neval_hprod += 1
74+
f
75+
end
76+
77+
function grad(qp::AbstractQuadraticModel, x::AbstractVector)
78+
g = Vector{eltype(x)}(undef, qp.meta.nvar)
79+
grad!(qp, x, g)
80+
end
81+
82+
function grad!(qp::AbstractQuadraticModel, x::AbstractVector, g::AbstractVector)
83+
v = qp.data.opH * x
84+
@. g = qp.data.c + v
85+
qp.counters.neval_hprod += 1
86+
g
87+
end
88+
89+
hess_coord(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...) = findnz(qp.data.H)
90+
91+
hess(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...) = qp.data.H
92+
93+
hess_op(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...) = qp.data.opH
94+
95+
function cons(qp::AbstractQuadraticModel, x::AbstractVector)
96+
c = Vector{eltype(x)}(undef, qp.meta.ncon)
97+
cons!(qp, x, c)
98+
end
99+
100+
function cons!(qp::AbstractQuadraticModel, x::AbstractVector, c::AbstractVector)
101+
mul!(c, qp.data.A, x)
102+
qp.counters.neval_jprod += 1
103+
c
104+
end
105+
106+
jac_coord(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...) = findnz(qp.data.A)
107+
108+
jac(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...) = qp.data.A
109+
110+
jac_op(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...) = LinearOperator(qp.data.A)
111+
112+
function hprod(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...)
113+
@closure v -> begin
114+
qp.counters.neval_hprod += 1
115+
qp.data.opH * v
116+
end
117+
end
118+
119+
function jprod(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...)
120+
@closure v -> begin
121+
qp.counters.neval_jprod += 1
122+
qp.data.A * v
123+
end
124+
end
125+
126+
function jtprod(qp::AbstractQuadraticModel, ::AbstractVector; kwargs...)
127+
@closure v -> begin
128+
qp.counters.neval_jtprod += 1
129+
qp.data.A' * v
130+
end
131+
end

src/qps.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function QuadraticModel(qps::QPSData)
2+
QuadraticModel(qps.c, qps.Q, opHermitian(qps.Q), qps.A,
3+
qps.lcon, qps.ucon, qps.lvar, qps.uvar, c0=qps.c0)
4+
end

0 commit comments

Comments
 (0)