joaquimg / BilevelJuMP.jl

Bilevel optimization in JuMP
Other
102 stars 25 forks source link

Using Existing JuMP Model Construction Code with BilevelJuMP #217

Open dsigler1234 opened 2 months ago

dsigler1234 commented 2 months ago

Hello,

I was hoping to use BilevelJuMP with PowerSimulations.jl. There is already existing code to construct my lower level JuMP model in PowerSimulations.jl, which I would like to leverage. Is there a way to build a JuMP model using some other existing package, and then set that as the lower level problem in the BilevelModel?

Thanks,

Devon

joaquimg commented 2 months ago

I have a recommended workflow for that.

Suppose you have the following function:

function create_my_model()
    # initialize the JuMP model
    model = Model()
    # append data to model
    @variable(model, ...
    @constraint(model, ...
    @objective(model, ...
    return model
end

You can split this in two function (PowerModels work this way, you just have to find the right functions, HydroPowerModels.jl does it):

function init_model()
    # initialize the JuMP model
    model = Model()
    return model
end

function build_model!(model)
    # append data to model
    @variable(model, ...
    @constraint(model, ...
    @objective(model, ...
    return model
end

function create_my_model()
    # initialize the JuMP model
    model = init_model()
    build_model!(model)
    return model
end

Now you can re-use the build_model!(model) function:

function create_my_bilevel_model()
    model = BilevelModel()
    # now pass just the lower level model as `Lower(model)`
    # this will add all variables and constraints to the lower part of the model
    build_model!(Lower(model))
    # do something else for the upper...
    ...
    return model
end

Again, this is possible in PowerModels and is already done for multi-stages (not bilevel) models in HydroPowerModels.