I am trying to adapt the first Parametron README.md example to use Cbc because I’ll need binary variables. However, the example does not work out of the box with Cbc.Optimizer()
. I’ve worked through some of the errors about unsupported constraint types by replacing the vectorized constraints with scalar versions. However, I’m stuck at this last error:
using Parametron
using Cbc
using Random, LinearAlgebra
optimizer = Cbc.Optimizer()
model = Model(optimizer)
n = 8; m = 2
x = [Variable(model) for _ = 1 : n]
A = Parameter(rand!, zeros(n, n), model)
b = Parameter(rand!, zeros(n), model)
C = Parameter(rand!, zeros(m, n), model)
d = Parameter(zeros(m), model) do d
rand!(d)
d .*= 2
end
# Minimize absolute errors instead of squared error to use an LP:
residual = [Variable(model) for _=1:n]
for i in 1:n
@constraint(model, residual[i] >= dot(A[i,:], x) - b[i])
@constraint(model, residual[i] >= b[i] - dot(A[i,:], x))
end
@objective(model, Minimize, sum(residual))
for i in 1:m
@constraint(model, dot(C[i,:], x) == d[i])
end
solve!(model)
throws
ERROR: MathOptInterface.SetAttributeNotAllowed{MathOptInterface.ConstraintFunction}: Setting attribute MathOptInterface.ConstraintFunction() cannot be performed. You may want to use a `CachingOptimizer` in `AUTOMATIC` mode or you may need to call `reset_optimizer` before doing this operation if the `CachingOptimizer` is in `MANUAL` mode.
Stacktrace:
[1] #throw_set_error_fallback#18(::MathOptInterface.SetAttributeNotAllowed{MathOptInterface.ConstraintFunction}, ::Function, ::Cbc.Optimizer, ::MathOptInterface.ConstraintFunction, ::MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.GreaterThan{Float64}}, ::MathOptInterface.ScalarAffineFunction{Float64}) at /Users/ifiske/.julia/packages/MathOptInterface/C3lip/src/attributes.jl:684
[2] throw_set_error_fallback(::Cbc.Optimizer, ::MathOptInterface.ConstraintFunction, ::MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.GreaterThan{Float64}}, ::MathOptInterface.ScalarAffineFunction{Float64}) at /Users/ifiske/.julia/packages/MathOptInterface/C3lip/src/attributes.jl:684
[3] set(::Cbc.Optimizer, ::MathOptInterface.ConstraintFunction, ::MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.GreaterThan{Float64}}, ::MathOptInterface.ScalarAffineFunction{Float64}) at /Users/ifiske/.julia/packages/MathOptInterface/C3lip/src/attributes.jl:295
[4] update! at /Users/ifiske/.julia/packages/Parametron/5crnA/src/moi_interop.jl:171 [inlined]
[5] update!(::Parametron.Constraints{Float64}, ::Cbc.Optimizer, ::Array{MathOptInterface.VariableIndex,1}) at /Users/ifiske/.julia/packages/Parametron/5crnA/src/moi_interop.jl:241
[6] update!(::Model{Float64,Cbc.Optimizer}) at /Users/ifiske/.julia/packages/Parametron/5crnA/src/model.jl:141
[7] solve!(::Model{Float64,Cbc.Optimizer}) at /Users/ifiske/.julia/packages/Parametron/5crnA/src/model.jl:156
[8] top-level scope at none:0
I’ve tried figuring out the CachingOptimizer with Parametron, but continue to get other errors. Also, would Parametron keep it’s performance benefits if we use an intermediate layer of a CachingOptimizer?
Thanks!
Ian