Skip to content

Allow relaxation of ReLUQuadratic #178

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

Merged
merged 9 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/MathOptAIFluxExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ julia> MathOptAI.build_predictor(
)
Pipeline with layers:
* Affine(A, b) [input: 1, output: 16]
* ReLUQuadratic()
* ReLUQuadratic(nothing)
* Affine(A, b) [input: 16, output: 1]
```
"""
Expand Down
2 changes: 1 addition & 1 deletion ext/MathOptAILuxExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ julia> MathOptAI.build_predictor(
)
Pipeline with layers:
* Affine(A, b) [input: 1, output: 16]
* ReLUQuadratic()
* ReLUQuadratic(nothing)
* Affine(A, b) [input: 16, output: 1]
```
"""
Expand Down
4 changes: 2 additions & 2 deletions src/predictors/Pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ julia> f = MathOptAI.Pipeline(
)
Pipeline with layers:
* Affine(A, b) [input: 2, output: 1]
* ReLUQuadratic()
* ReLUQuadratic(nothing)

julia> y, formulation = MathOptAI.add_predictor(model, f, x);

Expand All @@ -42,7 +42,7 @@ Affine(A, b) [input: 2, output: 1]
│ └ moai_Affine[1]
└ constraints [1]
└ x[1] + 2 x[2] - moai_Affine[1] = 0
ReLUQuadratic()
ReLUQuadratic(nothing)
├ variables [2]
│ ├ moai_ReLU[1]
│ └ moai_z[1]
Expand Down
31 changes: 26 additions & 5 deletions src/predictors/ReLU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function add_predictor(
end

"""
ReLUQuadratic() <: AbstractPredictor
ReLUQuadratic(; relaxation_parameter = nothing) <: AbstractPredictor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with the name, but other options could be: relaxation_tolerance, tolerance, atol, complementarity_mu, mu, complementarity_tolerance... not sure if any of those are better

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like relaxation_* as it communicates the qualitative change that happens when this parameter is set. (Although I may kick myself for this when I inevitably forget what this is called in six months.) I could go with tolerance or parameter.


An [`AbstractPredictor`](@ref) that represents the relationship:
```math
Expand All @@ -258,6 +258,15 @@ y \\cdot z = 0 \\\\
y, z \\ge 0
\\end{aligned}
```
If `relaxation_parameter` is set to a value `ϵ`, the constraints become:
```math
\\begin{aligned}
x = y - z \\\\
y \\cdot z \\leq \\epsilon \\\\
y, z \\ge 0
\\end{aligned}
```


## Example

Expand All @@ -269,7 +278,7 @@ julia> model = Model();
julia> @variable(model, -1 <= x[i in 1:2] <= i);

julia> f = MathOptAI.ReLUQuadratic()
ReLUQuadratic()
ReLUQuadratic(nothing)

julia> y, formulation = MathOptAI.add_predictor(model, f, x);

Expand All @@ -279,7 +288,7 @@ julia> y
moai_ReLU[2]

julia> formulation
ReLUQuadratic()
ReLUQuadratic(nothing)
├ variables [4]
│ ├ moai_ReLU[1]
│ ├ moai_ReLU[2]
Expand All @@ -300,7 +309,14 @@ ReLUQuadratic()
└ moai_ReLU[2]*moai_z[2] = 0
```
"""
struct ReLUQuadratic <: AbstractPredictor end
struct ReLUQuadratic <: AbstractPredictor
relaxation_parameter::Union{Nothing,Float64}
function ReLUQuadratic(;
relaxation_parameter::Union{Nothing,Float64} = nothing,
)
return new(relaxation_parameter)
end
end

function add_predictor(
model::JuMP.AbstractModel,
Expand All @@ -314,6 +330,11 @@ function add_predictor(
z = JuMP.@variable(model, [1:m], base_name = "moai_z")
_set_bounds_if_finite.(Ref(cons), z, 0, max.(0, -first.(bounds)))
append!(cons, JuMP.@constraint(model, x .== y - z))
append!(cons, JuMP.@constraint(model, y .* z .== 0))
if predictor.relaxation_parameter === nothing
append!(cons, JuMP.@constraint(model, y .* z .== 0))
else
ϵ = predictor.relaxation_parameter
append!(cons, JuMP.@constraint(model, y .* z .<= ϵ))
end
return y, Formulation(predictor, Any[y; z], cons)
end
23 changes: 23 additions & 0 deletions test/test_predictors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ function test_ReLU_Quadratic()
set_silent(model)
@variable(model, x[1:2])
f = MathOptAI.ReLUQuadratic()
@test f.relaxation_parameter === nothing
y, formulation = MathOptAI.add_predictor(model, f, x)
@test length(y) == 2
@test num_variables(model) == 6
Expand All @@ -237,6 +238,28 @@ function test_ReLU_Quadratic()
return
end

function test_ReLU_Quadratic_relaxed()
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:2])
f = MathOptAI.ReLUQuadratic(; relaxation_parameter = 1e-4)
y, formulation = MathOptAI.add_predictor(model, f, x)
# Maximize sum of all variables to exercise the ReLU relaxation
@objective(model, Max, sum(formulation.variables))
@test length(y) == 2
@test num_variables(model) == 6
@test num_constraints(model, AffExpr, MOI.EqualTo{Float64}) == 2
@test num_constraints(model, QuadExpr, MOI.LessThan{Float64}) == 2
fix.(x, [-1, 2])
optimize!(model)
@assert is_solved_and_feasible(model)
# We do not satisfy equality to a tight tolerance
@test !isapprox(value.(y), [0.0, 2.0]; atol = 1e-6)
# But we satisfy equality to a loose tolerance
@test isapprox(value.(y), [0.0, 2.0]; atol = 1e-2)
return
end

function test_Sigmoid()
model = Model(Ipopt.Optimizer)
set_silent(model)
Expand Down