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

[Feature Request] Parameters and Variable (or Solver) Extension

Open utotch opened this issue 8 years ago • 2 comments

I'm a heavy user of Convex.jl, but I often feel that I want some additional feature of it in comparison with CVXPY.

(1) Parameters CVXPY has a "Parameters" feature that only change non-variable value and solve the same problem frequently. http://www.cvxpy.org/en/latest/tutorial/intro/index.html#parameters

As you know, this kind of use often appears in Model Predictive Control, Moving Horizon Estimation, etc. I feel Parameters feature is very useful.

(2) Variable / Solver extension CVXPY can be easily extended default ability of "convex problem" solving algorithm and can solve some non-convex problems e.g. Convex-Concave procedure, non-convex QP, etc. DCCP https://github.com/cvxgrp/dccp QCQP https://github.com/cvxgrp/qcqp NCVX https://github.com/cvxgrp/ncvx

-- I think these kinds of features are very useful. I tried to extend Convex.jl by using traits etc, but I don't understand elegant way.

What do you think?

utotch avatar Apr 16 '18 04:04 utotch

I found that we can already use a Parameter mechanism by doing as follows:

# Parameter
# I'm jerous of Parameter in cvxpy
# https://www.cvxpy.org/api_reference/cvxpy.expressions.html#parameter
# we don't need to recreate prob object and we can reuse it by using Constant as Parameter
# solve problem for various q value
using Convex
using SCS; solver = SCSSolver(verbose=0)

P = [1 0; 0 2]
qs = [[3.0,4.0], [5.0,6.0], [7.0,8.0]] # Should be Array{Float64}
x = Variable(2)
q_param = Constant(qs[1]) # Parameter (use Constant)
@show q_param.id_hash
prob = minimize(0.5*quadform(x,P)+dot(q_param,x), [0 <= x])
for q in qs
#    @show q_param.value
#    @show q
    copyto!(q_param.value, q) # q_param.value is immutable but we can copy value to it
    Convex.solve!(prob, solver)
    @show x.value
end

utotch avatar Feb 22 '19 03:02 utotch

The functions fix! and free! can be used for this as well. You can fix! a variable to a value (fix!(x, v)), then solve the problem. Then you can fix! it again to a new variable and solve again (this was broken until https://github.com/JuliaOpt/Convex.jl/pull/299 so it may not have worked if you tried before). You can also free!(x) to make it a variable again etc.

Does that cover your use case? (If so, maybe it's then a matter of updating the documentation to make it clear how do to this).

ericphanson avatar Aug 13 '19 22:08 ericphanson

Closing because documented in https://jump.dev/Convex.jl/stable/advanced/#Fixing-and-freeing-variables.

See also https://github.com/jump-dev/Convex.jl/issues/383.

odow avatar Jan 19 '24 00:01 odow