Can I query is_continuous about a JuMP model?

We can learn from the print(model) (๐Ÿ…) that if a model has a variable which is not purely continuous (e.g. Integer/binary/semi~).

julia> model
A JuMP Model
โ”œ solver: Gurobi
โ”œ objective_sense: FEASIBILITY_SENSE
โ”œ num_variables: 1
โ”œ num_constraints: 1
โ”‚ โ”” JuMP.VariableRef in MOI.ZeroOne: 1 ๐Ÿ…
โ”” Names registered in the model
  โ”” :x

Then, can I programmatically query this related property?
e.g. the provisional JuMP.is_continuous(model) as follows

import JuMP, Gurobi
model = JuMP.Model(Gurobi.Optimizer)
JuMP.@variable(model, x[1:3] >= 0)
JuMP.set_integer(x[3])
JuMP.is_continuous(model) # false
JuMP.unset_integer(x[3])
JuMP.is_continuous(model) # true
JuMP.@variable(model, y, Bin)
JuMP.is_continuous(model) # false

Since linear optimization (LP) is the staple part of continuous optimization, it is indeed more favorable to have JuMP.is_linear,

model = JuMP.Model(Gurobi.Optimizer)
JuMP.@variable(model, z)
JuMP.@constraint(model, z * z <= 0)
JuMP.is_linear(model) || JuMP.is_continuous(model) && error("NLP") # ERROR: NLP

What is the motivation for adding is_continuous? In what settings is it useful?

JuMP doesnt have strict categories like โ€œthis model is linearโ€.

You could loop over list_of_constraint_types(model) to decide what type of model you have.

You could do

function is_linear(model)
    if objective_sense(model) != FEASIBILITY_SENSE
        F = objective_function_type(model)
        # But what about Vector{AffExpr}?
        if !(F <: VariableRef || F <: AffExpr)
            return false
        end
    end
    for (F, S) in list_of_constraint_types(model)
        # But what about F = Vector{AffExpr}? Some sets are linear...
        if !(F <: VariableRef || F <: AffExpr)
            return false
        end
    end
    return true
end

and

function is_continuous(model)
    for (F, S) in list_of_constraint_types(model)
        if S <: Union{
            MOI.ZeroOne,
            MOI.Integer,
            MOI.Semiinteger,
            MOI.Semicontinuous,
            # What about MOI.SOS1, MOI.SOS2, MOI.Indicator, MOI.Complements,
            # MOI.AllDifferent, MOI.BinPacking... there are others
        }
            return false
        end
    end
    return true
end

An issue is that the concept of โ€œis linearโ€ and โ€œis continuousโ€ is not perfectly defined.

2 Likes

We could make it much easier, i.e.,
is there a method to associate a String to a model? (e.g. a so-called name?)

model = JuMP.Model()
JuMP.write_note(model, "mip_master"::String)
# many lines ...
my_note = JuMP.read_note(model)
occursin("mip", my_note) && print("I know it is a mip!")

Currently do we already have these two functions?

What is meant by โ€œSome sets are linearโ€ฆโ€? Please give me a link? I wonder what sets are linear.

Yes, this is a good idea. It can also simplify this function you wrote

JuMP already has set_name for ConstraintRef and GenericVariableRef, what about also for Model (alias for JuMP.GenericModel{Float64}).
And then use JuMP.get_name(model) to fetch it (the get_name do not exist though). These 2 API should be not very strenuous?

In this case users will feel more convenience, e.g. memorizing attributes of different models they have created.

You have always been able to use the MOI interface:

julia> model = Model();

julia> MOI.get(model, MOI.Name())
""

julia> MOI.set(model, MOI.Name(), "My Model")

julia> MOI.get(model, MOI.Name())
"My Model"

Iโ€™ve opened a PR in JuMP to add support for set_name(::Model, ::String). We already had name(::Model).

See Add set_name(model::GenericModel, name::AbstractString) by odow ยท Pull Request #3967 ยท jump-dev/JuMP.jl ยท GitHub

See Standard form problem ยท JuMP

VectorOfVariables or VectorAffineFunction in Zeros, Nonpositives, Nonnegatives, and Hyperrectangle, depending on how you wanted to define things, perhaps also NormOneCone and NormInfinityCone, and then if those, NormCone, but only if p is 1.0 or Inf.

JuMP does not have a neat standard form like you might find in a textbook. Questions like is_linear are surprisingly nuanced.

1 Like