Convex.jl icon indicating copy to clipboard operation
Convex.jl copied to clipboard

Convenience function for writing to a file

Open ericphanson opened this issue 4 years ago • 2 comments

Now that Convex.jl is backed by MOI, we can take advantage of its ability to write to file formats (https://jump.dev/MathOptInterface.jl/stable/apimanual/#File-formats-1). It's actually pretty easy to do this already, e.g.

using Convex, COSMO
x = HermitianSemidefinite(2)
p = minimize( real(tr(x)), tr(x * [1.0 im; -im 0]) == 0, x[1,1] == 1)
solve!(p, COSMO.Optimizer)

using MathOptInterface
const MOI = MathOptInterface
dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_SDPA)
src = p.model
MOI.copy_to(MOI.Bridges.full_bridge_optimizer(dest, Float64), src)
MOI.write_to_file(dest, "file.sdpa")

and now the model has been written to file.sdpa. I deliberately chose a slightly complicated problem with PSD constraints and complex numbers to show that that isn't an issue; Convex.jl's extended formulations and MOI's bridges lower everything to a simple problem that gets written to the file. This file could then be used to solve the problem with solvers that are not yet connected to MOI, or uploaded to https://neos-server.org/neos/, etc.

We could make this simpler to use; for example, it might not be obvious that one needs to solve the problem first (which is when Convex.jl does its reformulations and populates p.model with the MOI model). Providing an API like Convex.write_to_file(p::Problem, filename::String) could work, as MOI.FileFormats.Model already supports guessing the file format by name.

Note that

x = HermitianSemidefinite(2)
p = minimize( real(tr(x)), tr(x * [1.0 im; -im 0]) == 0, x[1,1] == 1)

is already a readable and compact representation of the problem, and along with a Project.toml and Manifest.toml, is a fully reproducible way to describe the problem, so I think in many cases code is a good way to save the problem, but in a few cases this functionality could be really helpful. For example, it would've helped us quickly start solving problems with Convex.jl and SDPA-GMP instead of needing to wrap the solver first (which we did in https://github.com/ericphanson/SDPAFamily.jl).

This is pretty easy to add but I will at least wait for #393.

ericphanson avatar Aug 05 '20 20:08 ericphanson

Cool to see this works. Obligatory link to https://github.com/odow/NEOS.jl.

odow avatar Dec 01 '20 23:12 odow

Ah cool, I hadn't seen NEOS.jl!

ericphanson avatar Dec 02 '20 09:12 ericphanson