diff --git a/src/predictors/ReLU.jl b/src/predictors/ReLU.jl index 5e17fc9..3bd0a44 100644 --- a/src/predictors/ReLU.jl +++ b/src/predictors/ReLU.jl @@ -59,10 +59,8 @@ ReducedSpace(ReLU()) struct ReLU <: AbstractPredictor end function add_predictor(model::JuMP.AbstractModel, predictor::ReLU, x::Vector) - ub = last.(_get_variable_bounds.(x)) y = JuMP.@variable(model, [1:length(x)], base_name = "moai_ReLU") - cons = Any[] - _set_bounds_if_finite.(Ref(cons), y, 0, max.(0, ub)) + cons = _set_direct_bounds(x -> max(0, x), 0, nothing, x, y) append!(cons, JuMP.@constraint(model, y .== max.(0, x))) return y, Formulation(predictor, y, cons) end @@ -132,14 +130,12 @@ function add_predictor( x::Vector, ) m = length(x) - bounds = _get_variable_bounds.(x) y = JuMP.@variable(model, [1:m], base_name = "moai_ReLU") - cons = Any[] - _set_bounds_if_finite.(Ref(cons), y, 0, max.(0, last.(bounds))) + cons = _set_direct_bounds(x -> max(0, x), 0, nothing, x, y) formulation = Formulation(predictor, Any[], cons) append!(formulation.variables, y) for i in 1:m - lb, ub = bounds[i] + lb, ub = _get_variable_bounds(x[i]) z = JuMP.@variable(model, binary = true) JuMP.set_name(z, "moai_z[$i]") push!(formulation.variables, z) @@ -217,8 +213,7 @@ function add_predictor( m = length(x) bounds = _get_variable_bounds.(x) y = JuMP.@variable(model, [i in 1:m], base_name = "moai_ReLU") - cons = Any[] - _set_bounds_if_finite.(Ref(cons), y, 0, max.(0, last.(bounds))) + cons = _set_direct_bounds(x -> max(0, x), 0, nothing, x, y) z = JuMP.@variable(model, [1:m], lower_bound = 0, base_name = "moai_z") _set_bounds_if_finite.(Ref(cons), z, nothing, -first.(bounds)) append!(cons, JuMP.@constraint(model, x .== y - z)) @@ -294,8 +289,7 @@ function add_predictor( m = length(x) bounds = _get_variable_bounds.(x) y = JuMP.@variable(model, [1:m], base_name = "moai_ReLU") - cons = Any[] - _set_bounds_if_finite.(Ref(cons), y, 0, max.(0, last.(bounds))) + cons = _set_direct_bounds(x -> max(0, x), 0, nothing, x, y) 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)) diff --git a/src/predictors/Tanh.jl b/src/predictors/Tanh.jl index cd1b913..8aad44d 100644 --- a/src/predictors/Tanh.jl +++ b/src/predictors/Tanh.jl @@ -58,8 +58,6 @@ ReducedSpace(Tanh()) """ struct Tanh <: AbstractPredictor end -_eval(::Tanh, x::Real) = tanh(x) - function add_predictor(model::JuMP.AbstractModel, predictor::Tanh, x::Vector) y = JuMP.@variable(model, [1:length(x)], base_name = "moai_Tanh") cons = _set_variable_bounds(tanh, -1, 1, x, y) diff --git a/src/utilities.jl b/src/utilities.jl index 241d185..6eb9e88 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -44,7 +44,6 @@ _get_variable_bounds(::Any) = -Inf, Inf # Default fallback: skip setting variable bound _set_bounds_if_finite(::Vector, ::Any, ::Any, ::Any) = nothing - function _set_direct_bounds(f::F, l, u, x::Vector, y::Vector) where {F} cons = Any[] for (xi, yi) in zip(x, y)