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

Support the HiGHS callback in MOI #228

Closed
odow opened this issue Sep 22, 2024 · 1 comment · Fixed by #230
Closed

Support the HiGHS callback in MOI #228

odow opened this issue Sep 22, 2024 · 1 comment · Fixed by #230

Comments

@odow
Copy link
Member

odow commented Sep 22, 2024

Required for jump-dev/open-energy-modeling-benchmarks#17

@odow
Copy link
Member Author

odow commented Sep 23, 2024

using HiGHS
import MathOptInterface as MOI
model = HiGHS.Optimizer()
x = MOI.add_variables(model, 2)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
ci = MOI.add_constraint(model, x[1] + 2.0 * x[2], MOI.LessThan(3.0))

function run_user_callback(
    callback_type::Cint,
    message::Ptr{Cchar},
    p_data_out::Ptr{HiGHS.HighsCallbackDataOut},
    p_data_in::Ptr{HiGHS.HighsCallbackDataIn},
    user_callback_data::Ptr{Cvoid},
)
    data_out = unsafe_load(p_data_out)::HiGHS.HighsCallbackDataOut
    # @show data_out
    if p_data_in != C_NULL
        data_in = unsafe_load(p_data_in)::HiGHS.HighsCallbackDataIn
    end
    print(unsafe_string(message))
    return
end

callback_cfn = @cfunction(
    run_user_callback,
    Cvoid,
    (
        Cint, 
        Ptr{Cchar}, 
        Ptr{HiGHS.HighsCallbackDataOut}, 
        Ptr{HiGHS.HighsCallbackDataIn}, 
        Ptr{Cvoid},
    ),
)
HiGHS.Highs_setCallback(model, callback_cfn, C_NULL)
HiGHS.Highs_startCallback(model, HiGHS.kHighsCallbackLogging)
MOI.optimize!(model)

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