Skip to content

Commit

Permalink
Merge pull request #22 from chriscoey/alftest
Browse files Browse the repository at this point in the history
build Alfonso optimizer in tests so can change options
  • Loading branch information
chriscoey authored Aug 6, 2018
2 parents 0569f40 + ac00c32 commit 75cba5f
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 225 deletions.
36 changes: 21 additions & 15 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ version = "0.3.2"
[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[BinDeps]]
deps = ["Compat", "Libdl", "SHA", "URIParser"]
git-tree-sha1 = "160d5d926e83e0970dd549ad9fe0ac3e8107e83c"
uuid = "9e28174c-4ba2-5203-b857-d8d62c4213ee"
version = "0.8.8"

[[BinaryProvider]]
deps = ["Compat", "Pkg", "SHA"]
git-tree-sha1 = "94dac52c662ca793008fb689e3daf626b6139943"
Expand All @@ -32,18 +26,21 @@ uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "1.0.0"

[[Conda]]
deps = ["BinDeps", "Compat", "JSON", "Libdl", "VersionParsing"]
git-tree-sha1 = "6358e179fb9ad0af5ca9cd4d7c691c413cc121f2"
deps = ["Compat", "JSON", "VersionParsing"]
git-tree-sha1 = "b76a441539d66a18ba34df28d34dc5b9202164a3"
uuid = "8f4d0f93-b110-5947-807f-2305c1781a2d"
version = "0.8.1"
version = "1.0.0"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[FFTW]]
Expand All @@ -53,6 +50,7 @@ uuid = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
version = "0.2.3"

[[InteractiveUtils]]
deps = ["LinearAlgebra", "Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[IterTools]]
Expand All @@ -74,9 +72,14 @@ uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MathOptInterface]]
Expand All @@ -89,6 +92,7 @@ version = "0.4.1"
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[Pkg]]
deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Polynomials]]
Expand All @@ -98,12 +102,15 @@ uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
version = "0.4.0"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Reexport]]
Expand All @@ -118,27 +125,26 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[URIParser]]
deps = ["Compat", "Unicode"]
git-tree-sha1 = "0f9c5e2d14e03fbd00696f9394e288bdd5adacbc"
uuid = "30578b45-9adc-5946-b283-645ec420af67"
version = "0.3.1"

[[UUIDs]]
deps = ["Random"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
25 changes: 7 additions & 18 deletions examples/envelope/envelope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ available at https://arxiv.org/abs/1712.01792
=#

using Alfonso
import MathOptInterface
const MOI = MathOptInterface
using SparseArrays
using LinearAlgebra
using DelimitedFiles
using Random

function build_envelope(npoly, deg, n, d; native=true, use_data=false, rseed=1)
function build_envelope!(alf::Alfonso.AlfonsoOpt, npoly::Int, deg::Int, n::Int, d::Int; use_data::Bool=false, rseed::Int=1)
# TODO allow n > 1
@assert n == 1

Expand All @@ -26,7 +24,7 @@ function build_envelope(npoly, deg, n, d; native=true, use_data=false, rseed=1)
wtVals = 1.0 .- pts.^2
PWts = [Array((qr(Diagonal(sqrt.(wtVals[:,j]))*P[:,1:LWts[j]])).Q) for j in 1:n]

# set up MOI problem data
# set up problem data
A = repeat(sparse(1.0I, U, U), outer=(1, npoly))
b = w
if use_data
Expand All @@ -46,24 +44,15 @@ function build_envelope(npoly, deg, n, d; native=true, use_data=false, rseed=1)
push!(coneidxs, 1+(k-1)*U:k*U)
end

if native
# use native interface
alf = Alfonso.AlfonsoOpt(maxiter=100, verbose=true)
Alfonso.load_data!(alf, A, b, c, cones, coneidxs)
return alf
else
error("MOI tests not implemented")
# opt = Alfonso.Optimizer()
# return opt
end
return Alfonso.load_data!(alf, A, b, c, cones, coneidxs)
end

# alf = Alfonso.AlfonsoOpt(maxiter=100, verbose=true)

# optionally use fixed data in folder
# select number of polynomials and degrees for the envelope
# select dimension and SOS degree (to be squared)
# alf =
# build_envelope(2, 5, 1, 5, use_data=true)
# build_envelope(2, 5, 1, 5)
# build_envelope!(alf, 2, 5, 1, 5, use_data=true)
# build_envelope!(alf, 2, 5, 1, 5)

# solve it
# @time Alfonso.solve!(alf)
28 changes: 11 additions & 17 deletions examples/lp/lp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ solves a simple LP min c'x s.t. Ax = b, x >= 0
=#

using Alfonso
import MathOptInterface
const MOI = MathOptInterface
using SparseArrays
using LinearAlgebra
using DelimitedFiles
using Random

function build_lp(m, n; native=true, use_data=false, dense=false, nzfrac=1/sqrt(n), rseed=1)
function build_lp!(alf::Alfonso.AlfonsoOpt, m::Int, n::Int; use_data=false, dense=false, nzfrac::Float64=1/sqrt(n), tosparse=false, rseed::Int=1)
# set up problem data
if use_data
# use provided data in data folder
datapath = joinpath(@__DIR__, "data")
Expand All @@ -32,26 +30,22 @@ function build_lp(m, n; native=true, use_data=false, dense=false, nzfrac=1/sqrt(
c = rand(0.0:9.0, n)
end

if tosparse && !issparse(A)
A = sparse(A)
end

cones = Alfonso.ConeData[Alfonso.NonnegData(n),]
coneidxs = AbstractUnitRange[1:n,]

if native
# use native interface
alf = Alfonso.AlfonsoOpt(maxiter=100, verbose=true)
Alfonso.load_data!(alf, A, b, c, cones, coneidxs)
return alf
else
error("MOI tests not implemented")
# opt = Alfonso.Optimizer()
# return opt
end
return Alfonso.load_data!(alf, A, b, c, cones, coneidxs)
end

# alf = Alfonso.AlfonsoOpt(maxiter=100, verbose=true)

# optionally use fixed data in folder
# select the random matrix size, dense/sparse, sparsity fraction
# alf =
# build_lp(500, 1000)
# build_lp(500, 1000, use_data=true)
# build_lp!(alf, 500, 1000)
# build_lp!(alf, 500, 1000, use_data=true)

# solve it
# @time Alfonso.solve!(alf)
44 changes: 17 additions & 27 deletions examples/namedpoly/namedpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ available at https://arxiv.org/abs/1712.01792
=#

using Alfonso
import MathOptInterface
const MOI = MathOptInterface
using SparseArrays
using LinearAlgebra

# list of currently available named polynomials, see https://people.sc.fsu.edu/~jburkardt/py_src/polynomials/polynomials.html
Expand Down Expand Up @@ -50,7 +47,8 @@ const polys = Dict{Symbol,NamedTuple}(
),
)

function build_namedpoly(polyname, d; native=true)
function build_namedpoly!(alf::Alfonso.AlfonsoOpt, polyname::Symbol, d::Int)
# get data for named polynomial
(n, lbs, ubs, deg, fn) = polys[polyname]
if d < ceil(Int, deg/2)
error("requires d >= $(ceil(Int, deg/2))")
Expand All @@ -73,38 +71,30 @@ function build_namedpoly(polyname, d; native=true)
LWts = fill(binomial(n+d-1, n), n)
PWts = [Diagonal(sqrt.(wtVals[:,j]))*P0[:,1:LWts[j]] for j in 1:n]

# set up MOI problem data
# set up problem data
A = ones(1, U)
b = [1.0,]
c = [fn(pts[j,:]...) for j in 1:U]
cones = Alfonso.ConeData[Alfonso.SumOfSqrData(U, P0, PWts),]
coneidxs = AbstractUnitRange[1:U,]

if native
# use native interface
alf = Alfonso.AlfonsoOpt(maxiter=100, verbose=true)
Alfonso.load_data!(alf, A, b, c, cones, coneidxs)
return alf
else
error("MOI tests not implemented")
# opt = Alfonso.Optimizer()
# return opt
end
return Alfonso.load_data!(alf, A, b, c, cones, coneidxs)
end

# alf = Alfonso.AlfonsoOpt(maxiter=100, verbose=true)

# select the named polynomial to minimize and the SOS degree (to be squared)
# alf =
# build_namedpoly(:butcher, 2)
# build_namedpoly(:caprasse, 4)
# build_namedpoly(:goldsteinprice, 7)
# build_namedpoly(:heart, 3)
# build_namedpoly(:lotkavolterra, 3)
# build_namedpoly(:magnetism7, 2)
# build_namedpoly(:motzkin, 7)
# build_namedpoly(:reactiondiffusion, 4)
# build_namedpoly(:robinson, 6)
# build_namedpoly(:rosenbrock, 6)
# build_namedpoly(:schwefel, 5)
# build_namedpoly!(alf, :butcher, 2)
# build_namedpoly!(alf, :caprasse, 4)
# build_namedpoly!(alf, :goldsteinprice, 7)
# build_namedpoly!(alf, :heart, 2)
# build_namedpoly!(alf, :lotkavolterra, 3)
# build_namedpoly!(alf, :magnetism7, 2)
# build_namedpoly!(alf, :motzkin, 7)
# build_namedpoly!(alf, :reactiondiffusion, 4)
# build_namedpoly!(alf, :robinson, 8)
# build_namedpoly!(alf, :rosenbrock, 4)
# build_namedpoly!(alf, :schwefel, 3)

# solve it
# @time Alfonso.solve!(alf)
4 changes: 3 additions & 1 deletion src/nativeinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,9 @@ function solve!(alf::AlfonsoOpt)
end
end

println("\nFinished in $iter iterations\nInternal status is $(alf.status)\n")
if alf.verbose
println("\nFinished in $iter iterations\nInternal status is $(alf.status)\n")
end

#=
calculate final solution and iteration statistics
Expand Down
Loading

0 comments on commit 75cba5f

Please sign in to comment.