Skip to content

Commit

Permalink
Merge pull request #203 from joaquimg/jg/errqplow
Browse files Browse the repository at this point in the history
Improve NL model information
  • Loading branch information
joaquimg authored Jun 26, 2023
2 parents 146a6a9 + e08d1b6 commit c477d90
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 249 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ makedocs(;
"modes.md",
"lower_duals.md",
"conic_lower.md",
"non_linear.md",
"quad_to_bin.md",
]),
"Examples" => joinpath.("examples", [
Expand Down
110 changes: 0 additions & 110 deletions docs/src/tutorials/conic_lower.md

This file was deleted.

138 changes: 0 additions & 138 deletions docs/src/tutorials/lower_duals.md

This file was deleted.

93 changes: 93 additions & 0 deletions docs/src/tutorials/non_linear.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# # Non Linear models

# BilevelJuMP has limited support for non-linear models.
# The `@NLconstraint` and the `@NLobjective` macros are supported
# for the upper level, but not for the lower level.
# Moreover, these macros can only be used if the selected solver
# supports non-linear constraints and objectives.

# The `@constraint` and `@objective` macros
# can be used for both levels to represent linear and quadratic
# constraints and objectives


# ## Quadratic constraints and objectives

using BilevelJuMP, Ipopt
model = BilevelModel(Ipopt.Optimizer, mode = BilevelJuMP.ProductMode(1e-5))

@variable(Upper(model), x >= 2)
@variable(Lower(model), 3 <= y <= 5)

# We can add a non-linear objective to the upper level with `@NLobjective`

@NLobjective(Upper(model), Min, x^2 - y)

# We can also add a non-linear constraint to the upper level with `@NLconstraint`

@NLconstraint(Upper(model), x^2 + y^2 <= 100)

# `@NLobjective` is *not supported* in the lower level, but we can use
# `@constraint` to add a quadratic objective to the lower level.

@objective(Lower(model), Min, y^2)

#-

optimize!(model)

# All the quadratic objectives and constraints of the upper level can also
# be added with the `@constraint` and `@objective` macros. Hence, we can
# write the quivalent model:

using BilevelJuMP, Ipopt
model = BilevelModel(Ipopt.Optimizer, mode = BilevelJuMP.ProductMode(1e-5))

@variable(Upper(model), x >= 2)
@variable(Lower(model), 3 <= y <= 5)

#-

@objective(Upper(model), Min, x^2 - y)

#-

@constraint(Upper(model), x^2 + y^2 <= 100)

#-

@objective(Lower(model), Min, y^2)

#-

optimize!(model)

# ## General non-linear constraints and objectives

# General non quadratic objectives and general non-linear constraints
# *can not* be added to the lower level, but they can be added to the upper level.

using BilevelJuMP, Ipopt
model = BilevelModel(Ipopt.Optimizer, mode = BilevelJuMP.ProductMode(1e-5))

@variable(Upper(model), x >= 2)
@variable(Lower(model), 3 <= y <= 5)

#-

@NLobjective(Upper(model), Min, x^4 - sin(y))

#-

@NLconstraint(Upper(model), x^3 + y^3 <= 1000)

#-

@objective(Lower(model), Min, y^2)

#-

optimize!(model)

# !!! info
# Conic constraints are supported in the lower level (see Conic Bilevel tutorial).
5 changes: 4 additions & 1 deletion src/jump_nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ end
no_nlp() = error("Nonlinear data must be passed to the Upper(.) model")
function no_nlp_lower()
return error(
"Nonlinear data (objective, constraints, parameters) is not allowed in the lower level",
"Nonlinear data (objective, constraints, parameters) is not allowed in the lower level. " *
"If you are trying to use the @NLconstraint or @NLobjective macros for quadratic expressions, " *
"please use the @constraint or @objective macros instead. " *
"Expressions that are not quadratic nor linear are not supported in the lower level."
)
end

Expand Down

0 comments on commit c477d90

Please sign in to comment.