From e1e73a5624a472a0b2eb24321111f8c1d39acc5c Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 25 Oct 2024 12:05:59 +1300 Subject: [PATCH] Update --- src/predictors/Sigmoid.jl | 19 +++++++++++++------ src/predictors/SoftPlus.jl | 19 ++++++++++++++----- src/predictors/Tanh.jl | 19 +++++++++++++------ 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/predictors/Sigmoid.jl b/src/predictors/Sigmoid.jl index 73695ef..8c94c02 100644 --- a/src/predictors/Sigmoid.jl +++ b/src/predictors/Sigmoid.jl @@ -17,7 +17,7 @@ julia> using JuMP, MathOptAI julia> model = Model(); -julia> @variable(model, x[1:2]); +julia> @variable(model, -1 <= x[i in 1:2] <= i); julia> f = MathOptAI.Sigmoid() Sigmoid() @@ -35,10 +35,10 @@ Sigmoid() │ ├ moai_Sigmoid[1] │ └ moai_Sigmoid[2] └ constraints [6] - ├ moai_Sigmoid[1] ≥ 0 - ├ moai_Sigmoid[1] ≤ 1 - ├ moai_Sigmoid[2] ≥ 0 - ├ moai_Sigmoid[2] ≤ 1 + ├ moai_Sigmoid[1] ≥ 0.2689414213699951 + ├ moai_Sigmoid[1] ≤ 0.7310585786300049 + ├ moai_Sigmoid[2] ≥ 0.2689414213699951 + ├ moai_Sigmoid[2] ≤ 0.8807970779778823 ├ moai_Sigmoid[1] - (1.0 / (1.0 + exp(-x[1]))) = 0 └ moai_Sigmoid[2] - (1.0 / (1.0 + exp(-x[2]))) = 0 @@ -58,10 +58,17 @@ ReducedSpace(Sigmoid()) """ struct Sigmoid <: AbstractPredictor end +_eval(::Sigmoid, x::Real) = 1 / (1 + exp(-x)) + function add_predictor(model::JuMP.AbstractModel, predictor::Sigmoid, x::Vector) y = JuMP.@variable(model, [1:length(x)], base_name = "moai_Sigmoid") cons = Any[] - _set_bounds_if_finite.(Ref(cons), y, 0, 1) + for i in 1:length(x) + x_l, x_u = _get_variable_bounds(x[i]) + y_l = x_l === nothing ? 0 : _eval(predictor, x_l) + y_u = x_u === nothing ? 1 : _eval(predictor, x_u) + _set_bounds_if_finite(cons, y[i], y_l, y_u) + end append!(cons, JuMP.@constraint(model, y .== 1 ./ (1 .+ exp.(-x)))) return y, Formulation(predictor, y, cons) end diff --git a/src/predictors/SoftPlus.jl b/src/predictors/SoftPlus.jl index b142f78..870e74f 100644 --- a/src/predictors/SoftPlus.jl +++ b/src/predictors/SoftPlus.jl @@ -17,7 +17,7 @@ julia> using JuMP, MathOptAI julia> model = Model(); -julia> @variable(model, x[1:2]); +julia> @variable(model, -1 <= x[i in 1:2] <= i); julia> f = MathOptAI.SoftPlus(; beta = 2.0) SoftPlus(2.0) @@ -34,9 +34,11 @@ SoftPlus(2.0) ├ variables [2] │ ├ moai_SoftPlus[1] │ └ moai_SoftPlus[2] -└ constraints [4] - ├ moai_SoftPlus[1] ≥ 0 - ├ moai_SoftPlus[2] ≥ 0 +└ constraints [6] + ├ moai_SoftPlus[1] ≥ 0.0634640055214863 + ├ moai_SoftPlus[1] ≤ 1.0634640055214863 + ├ moai_SoftPlus[2] ≥ 0.0634640055214863 + ├ moai_SoftPlus[2] ≤ 2.0090749639589047 ├ moai_SoftPlus[1] - (log(1.0 + exp(2 x[1])) / 2.0) = 0 └ moai_SoftPlus[2] - (log(1.0 + exp(2 x[2])) / 2.0) = 0 @@ -59,6 +61,8 @@ struct SoftPlus <: AbstractPredictor SoftPlus(; beta::Float64 = 1.0) = new(beta) end +_eval(f::SoftPlus, x::Real) = log(1 + exp(f.beta * x)) / f.beta + function add_predictor( model::JuMP.AbstractModel, predictor::SoftPlus, @@ -66,7 +70,12 @@ function add_predictor( ) y = JuMP.@variable(model, [1:length(x)], base_name = "moai_SoftPlus") cons = Any[] - _set_bounds_if_finite.(Ref(cons), y, 0, nothing) + for i in 1:length(x) + x_l, x_u = _get_variable_bounds(x[i]) + y_l = x_l === nothing ? 0 : _eval(predictor, x_l) + y_u = x_u === nothing ? nothing : _eval(predictor, x_u) + _set_bounds_if_finite(cons, y[i], y_l, y_u) + end beta = predictor.beta append!( cons, diff --git a/src/predictors/Tanh.jl b/src/predictors/Tanh.jl index 25dcc0e..ddfe02c 100644 --- a/src/predictors/Tanh.jl +++ b/src/predictors/Tanh.jl @@ -17,7 +17,7 @@ julia> using JuMP, MathOptAI julia> model = Model(); -julia> @variable(model, x[1:2]); +julia> @variable(model, -1 <= x[i in 1:2] <= i); julia> f = MathOptAI.Tanh() Tanh() @@ -35,10 +35,10 @@ Tanh() │ ├ moai_Tanh[1] │ └ moai_Tanh[2] └ constraints [6] - ├ moai_Tanh[1] ≥ -1 - ├ moai_Tanh[1] ≤ 1 - ├ moai_Tanh[2] ≥ -1 - ├ moai_Tanh[2] ≤ 1 + ├ moai_Tanh[1] ≥ -0.7615941559557649 + ├ moai_Tanh[1] ≤ 0.7615941559557649 + ├ moai_Tanh[2] ≥ -0.7615941559557649 + ├ moai_Tanh[2] ≤ 0.9640275800758169 ├ moai_Tanh[1] - tanh(x[1]) = 0 └ moai_Tanh[2] - tanh(x[2]) = 0 @@ -58,10 +58,17 @@ 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 = Any[] - _set_bounds_if_finite.(Ref(cons), y, -1, 1) + for i in 1:length(x) + x_l, x_u = _get_variable_bounds(x[i]) + y_l = x_l === nothing ? -1 : _eval(predictor, x_l) + y_u = x_u === nothing ? 1 : _eval(predictor, x_u) + _set_bounds_if_finite(cons, y[i], y_l, y_u) + end append!(cons, JuMP.@constraint(model, y .== tanh.(x))) return y, Formulation(predictor, y, cons) end