Skip to content
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

Add support for nonlinear sign(x) #2448

Closed
odow opened this issue Feb 24, 2024 · 2 comments · Fixed by #2444
Closed

Add support for nonlinear sign(x) #2448

odow opened this issue Feb 24, 2024 · 2 comments · Fixed by #2444

Comments

@odow
Copy link
Member

odow commented Feb 24, 2024

This has come up a few times.

It occurs in GasModels.jl, where they want signpower(x, p) = sign(x) * abs(x)^p, and a variant appeared in https://discourse.julialang.org/t/nonlinear-optimization-with-many-constraints-autodifferentiation-which-julia-solution/110678

We could either define Base.sign(x::AbstractJuMPScalar) = op_ifelse(op_strictly_greater_than(x, 0), 1, -1), or we could add direct support to JuMP and MOI.

@odow
Copy link
Member Author

odow commented Feb 25, 2024

#2444 is one option.

Another is to add documentation for:

function signpower(x::Number, p::Real)
    @assert p > 0
    return ifelse(x > 0, x^p, ifelse(x < 0, -(-x)^p, zero(x)))
end

function signpower(x::AbstractJuMPScalar, p::Real)
    @assert p > 0
    return op_ifelse(
        op_strictly_greater_than(x, 0),
        x^p,
        op_ifelse(op_strictly_less_than(x, 0), -(-x)^p, 0.0)
    )
end

and potentially a fallback like sign(x::AbstractJuMPScalar) = error("Use ifelse instead")

@odow
Copy link
Member Author

odow commented Feb 26, 2024

Moving this to MOI because JuMP will automatically implement the new operator:
https://github.com/jump-dev/JuMP.jl/blob/4be967cc9ecad56218929914308b7d4fbcd79678/src/nlp_expr.jl#L315-L338

@odow odow transferred this issue from jump-dev/JuMP.jl Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant