Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QP-rand problem #52

Merged
merged 5 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
Noise = "81d43f40-5267-43b7-ae1c-8b967f377efa"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
ADNLPModels = "^0.3, 0.4, 0.7"
Expand All @@ -24,7 +25,8 @@ julia = "^1.3.0"
ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"
QuadraticModels = "f468eda6-eac5-11e8-05a5-ff9e497bcd19"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ADNLPModels", "DifferentialEquations", "MLDatasets", "Test"]
test = ["ADNLPModels", "DifferentialEquations", "MLDatasets", "QuadraticModels", "Test"]
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"
QuadraticModels = "f468eda6-eac5-11e8-05a5-ff9e497bcd19"
RegularizedProblems = "ea076b23-609f-44d2-bb12-a4ae45328278"

[compat]
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Documenter
using ADNLPModels, DifferentialEquations, MLDatasets
using ADNLPModels, DifferentialEquations, QuadraticModels
using RegularizedProblems

makedocs(
Expand Down
5 changes: 4 additions & 1 deletion src/RegularizedProblems.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RegularizedProblems

using LinearAlgebra
using LinearAlgebra, SparseArrays
using Random, Requires
using NLPModels
using Distributions, Noise
Expand All @@ -22,6 +22,9 @@ function __init__()
@require MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458" begin
include("nonlin_svm_model.jl")
end
@require QuadraticModels = "f468eda6-eac5-11e8-05a5-ff9e497bcd19" begin
include("qp_rand_model.jl")
end
end

end
35 changes: 35 additions & 0 deletions src/qp_rand_model.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export qp_rand_model
using .QuadraticModels

"""
model, x0 = qp_rand_model(n; dens = 1.0e-4, convex = false)

Return an instance of a `QuadraticModel` representing

½ xᵀHx + cᵀx s.t. l ≤ x ≤ u,
geoffroyleconte marked this conversation as resolved.
Show resolved Hide resolved

with H = A + A' or H = A * A' + I (see the `convex` keyword argument) where A is a random square matrix with density `dens`, `l = -e -tₗ` and `u = e + tᵤ` where `tₗ` and `tᵤ` are sampled from a uniform distribution between 0 and 1.
geoffroyleconte marked this conversation as resolved.
Show resolved Hide resolved

## Arguments

* `n :: Int`: size of the problem,

## Keyword arguments

* `dens :: Real`: density of `A`` used to generate the quadratic model (default: `1.0e-4`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with 0 < dens ≤ 1 ?

* `convex :: Bool`: true to generate a convex `H` (default: `false`).
geoffroyleconte marked this conversation as resolved.
Show resolved Hide resolved

## Return Value

An instance of a `QuadraticModel`.
"""
function qp_rand_model(n::Int; dens::R = 1.0e-4, convex::Bool = false) where {R <: Real}
A = sprandn(R, n, n, dens)
dpo marked this conversation as resolved.
Show resolved Hide resolved
H = convex ? (A * A') : (A + A') #+ I
geoffroyleconte marked this conversation as resolved.
Show resolved Hide resolved
c = randn(R, n)
l = -one(R) .- rand(R, n)
u = one(R) .+ rand(R, n)
qp = QuadraticModel(c, H; lvar = l, uvar = u)
x0 = zeros(R, n)
qp, x0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x0 should be part of qp.meta. Also, the docstring does not say that you return x0.

end
15 changes: 14 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LinearAlgebra, Test
using ADNLPModels, DifferentialEquations, NLPModels, MLDatasets
using ADNLPModels, DifferentialEquations, NLPModels, MLDatasets, QuadraticModels
using RegularizedProblems

function test_well_defined(model, nls_model, sol)
Expand Down Expand Up @@ -151,3 +151,16 @@ end
@test all(nls_model.meta.uvar .== Inf)
test_objectives(model, nls_model)
end

@testset "QP-rand" begin
n, dens = 100, 0.1
model, x0 = qp_rand_model(n; dens = dens, convex = false)
@test all(-2.0 .≤ model.meta.lvar .≤ 0.0)
@test all(0.0 .≤ model.meta.uvar .≤ 2.0)
@test all(model.meta.x0 .== x0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is. No need to return x0 above.


model, x0 = qp_rand_model(n; dens = dens, convex = true)
@test all(-2.0 .≤ model.meta.lvar .≤ 0.0)
@test all(0.0 .≤ model.meta.uvar .≤ 2.0)
@test all(model.meta.x0 .== x0)
end
Loading