Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 24, 2024
1 parent b88ade3 commit e1e73a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
19 changes: 13 additions & 6 deletions src/predictors/Sigmoid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 14 additions & 5 deletions src/predictors/SoftPlus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -59,14 +61,21 @@ 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,
x::Vector,
)
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,
Expand Down
19 changes: 13 additions & 6 deletions src/predictors/Tanh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e1e73a5

Please sign in to comment.