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 all 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
34 changes: 34 additions & 0 deletions src/qp_rand_model.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export qp_rand_model
using .QuadraticModels

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

Return an instance of a `QuadraticModel` representing

min cᵀx + ½ xᵀHx s.t. l ≤ x ≤ u,

with H = A + A' or H = A * A' (see the `convex` keyword argument) where A is a random square matrix with density `dens`, `l = -e - tₗ` and `u = e + tᵤ` where `e` is the vector of ones, and `tₗ` and `tᵤ` are sampled from a uniform distribution between 0 and 1.

## Arguments

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

## Keyword arguments

* `dens :: Real`: density of `A` with `0 < dens ≤ 1` used to generate the quadratic model (default: `1.0e-4`).
* `convex :: Bool`: true to generate positive definite `H` (default: `false`).

## Return Value

An instance of a `QuadraticModel`.
"""
function qp_rand_model(n::Int; dens::R = 1.0e-4, convex::Bool = false) where {R <: Real}
@assert 0 < dens ≤ 1
A = sprandn(R, n, n, dens)
dpo marked this conversation as resolved.
Show resolved Hide resolved
H = convex ? (A * A') : (A + A')
c = randn(R, n)
l = -one(R) .- rand(R, n)
u = one(R) .+ rand(R, n)
QuadraticModel(c, H; lvar = l, uvar = u, x0 = zeros(R, n))
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 = 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 .== 0)

model = 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 .== 0)
end
Loading