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

Optimize the approach to clear quadratic objective #197

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -936,19 +936,15 @@ function MOI.set(
_check_ret(ret)
model.is_objective_function_set = true
if model.hessian !== nothing
index = HighsInt[row - 1 for col in 1:num_vars for row in col:num_vars]
start = HighsInt[0]
for col in 1:num_vars
push!(start, start[end] + num_vars - col + 1)
end
start = zeros(HighsInt, num_vars)
ret = Highs_passHessian(
model,
num_vars,
length(index),
0,
kHighsHessianFormatTriangular,
start,
index,
fill(0.0, length(index)),
C_NULL,
C_NULL,
)
_check_ret(ret)
model.hessian = nothing
Expand Down
19 changes: 19 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,25 @@ function test_change_row_bounds_by_set_equal_to()
return
end

function test_set_affine_after_quadratic()
model = HiGHS.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(0.25))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = (x - 1.0)^2 + x + 1.0
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.optimize!(model)
@test ≈(MOI.get(model, MOI.VariablePrimal(), x), 0.5; atol = 1e-5)
@test ≈(MOI.get(model, MOI.ObjectiveValue()), 1.75; atol = 1e-5)
g = 2.0 * x + 3.0
MOI.set(model, MOI.ObjectiveFunction{typeof(g)}(), g)
MOI.optimize!(model)
@test ≈(MOI.get(model, MOI.VariablePrimal(), x), 0.25; atol = 1e-5)
@test ≈(MOI.get(model, MOI.ObjectiveValue()), 3.5; atol = 1e-5)
return
end

end

TestMOIHighs.runtests()
Loading