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

[feature] Support MOI.VectorAffineFunction #170

Open
oyamad opened this issue May 14, 2019 · 6 comments · May be fixed by #171
Open

[feature] Support MOI.VectorAffineFunction #170

oyamad opened this issue May 14, 2019 · 6 comments · May be fixed by #171

Comments

@oyamad
Copy link
Member

oyamad commented May 14, 2019

Would it be costly to support MOI.VectorAffineFunction also?

MOI.Utilities.@model(_MOIModel,
(), (MOI.EqualTo, MOI.LessThan,), (), (),
(), (MOI.ScalarAffineFunction,), (), ())

@blegat
Copy link
Member

blegat commented May 14, 2019

It is already supported through bridges:

MOI.copy_to(MOI.Bridges.full_bridge_optimizer(_model, Float64), model)

full_bridge_optimizer adds many bridges including http://www.juliaopt.org/MathOptInterface.jl/dev/apireference/#MathOptInterface.Bridges.ScalarizeBridge which transforms VectorAffineFunction to ScalarAffineFunction.

@oyamad
Copy link
Member Author

oyamad commented May 14, 2019

@blegat Thanks. My problem seems to be that I haven't been able to understand the manual of MOI...

To be concrete, consider the linear program max c x s.t. A x <= b, where

A = [1 1; -1 0; 0 -1]
b = [1, 0, 0]
c = [1, 0]

Let's use CDDLib.Optimizer:

T = Float64  # or Rational{BigInt}
optimizer = CDDLib.Optimizer{T}()
x = MOI.add_variables(optimizer, length(c))
MOI.set(optimizer, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(),
        MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}.(c, x), 0))
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)

I know the following works:

for i in 1:size(A, 1)
    MOI.add_constraint(
        optimizer,
        MOI.ScalarAffineFunction{T}(
            MOI.ScalarAffineTerm{T}.(A[i, :], x), 0
        ),
        MOI.LessThan{T}(b[i])
    )
end
MOI.optimize!(optimizer)
2-element Array{Float64,1}:
 1.0
 0.0

But it feels a bit stupid to pass the content of the matrix A row by row, so I wanted to pass A itself (along with b) without decomposing it. Is it possible? The following doesn't work:

MOI.add_constraint(
    optimizer,
    MOI.VectorAffineFunction{T}(A, -b),
    MOI.Nonpositives(length(b))
)
MethodError: no method matching Array{MathOptInterface.VectorAffineTerm{Float64},1}(::Array{Int64,2})

@blegat
Copy link
Member

blegat commented May 14, 2019

See this discourse post for how to formulate the constraint with VectorAffineFunction.

@oyamad
Copy link
Member Author

oyamad commented May 14, 2019

Ah sorry, I should have read that thread carefully.

Now the issue seems to be that MOI.Nonpositives is not supported:

T = Float64  # or Rational{BigInt}
optimizer = CDDLib.Optimizer{T}()
x = MOI.add_variables(optimizer, length(c))
MOI.set(optimizer, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(),
        MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}.(c, x), 0))
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)

terms = MOI.VectorAffineTerm{T}.(
    1:size(A, 1), MOI.ScalarAffineTerm{T}.(A, reshape(x, 1, length(x)))
)
f = MOI.VectorAffineFunction{T}(vec(terms), -b)
MOI.add_constraint(optimizer, f, MOI.Nonpositives(size(A, 1)))
MathOptInterface.UnsupportedConstraint{MathOptInterface.VectorAffineFunction{Float64},MathOptInterface.Nonpositives}: `MathOptInterface.VectorAffineFunction{Float64}`-in-`MathOptInterface.Nonpositives` constraints is not supported by the model.

@blegat
Copy link
Member

blegat commented May 14, 2019

You should do optimizer = MOI.Bridges.full_bridge_optimiezr(CDDLib.Optimizer{T}(), T)

oyamad added a commit to oyamad/Polyhedra.jl that referenced this issue May 15, 2019
@oyamad oyamad linked a pull request May 15, 2019 that will close this issue
@oyamad
Copy link
Member Author

oyamad commented May 15, 2019

Great, that works now.

In PR #171 I added a description of the usage to the documentation.

oyamad added a commit to oyamad/Polyhedra.jl that referenced this issue May 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants