Skip to content

Commit 70efebd

Browse files
committed
Modify constraint sets
1 parent 5842d04 commit 70efebd

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/MOI_wrapper.jl

+25
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,31 @@ function MOI.set(
15901590
return
15911591
end
15921592

1593+
function MOI.set(
1594+
model::Optimizer,
1595+
::MOI.ConstraintSet,
1596+
c::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}},
1597+
s::Vector{S},
1598+
) where {S<:_SCALAR_SETS}
1599+
if length(c) != length(s)
1600+
msg = "number of constraints does not match number of sets"
1601+
throw(DimensionMismatch(msg))
1602+
end
1603+
for ci in c
1604+
MOI.throw_if_not_valid(model, ci)
1605+
end
1606+
N = length(c)
1607+
rows, lower, upper = zeros(Cint, N), zeros(Cdouble, N), zeros(Cdouble, N)
1608+
for (i, (ci, si)) in enumerate(zip(c, s))
1609+
info = _info(model, ci)
1610+
rows[i] = info.row
1611+
lower[i], upper[i] = info.lower, info.upper = _bounds(si)
1612+
end
1613+
ret = Highs_changeRowsBoundsBySet(model, N, rows, lower, upper)
1614+
_check_ret(ret)
1615+
return
1616+
end
1617+
15931618
function MOI.get(
15941619
model::Optimizer,
15951620
::MOI.ConstraintFunction,

test/MOI_wrapper.jl

+86-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ function test_change_col_bounds_by_set_less_than()
543543
return
544544
end
545545

546-
function test_change_col_bounds_by_set_less_than()
546+
function test_change_col_bounds_by_set_equal_to()
547547
model = HiGHS.Optimizer()
548548
MOI.set(model, MOI.Silent(), true)
549549
x = MOI.add_variables(model, 3)
@@ -561,6 +561,91 @@ function test_change_col_bounds_by_set_less_than()
561561
return
562562
end
563563

564+
function test_change_row_bounds_by_set_dimension_mismatch()
565+
model = HiGHS.Optimizer()
566+
MOI.set(model, MOI.Silent(), true)
567+
x = MOI.add_variables(model, 3)
568+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.GreaterThan.(1.0:3.0))
569+
@test_throws(
570+
DimensionMismatch,
571+
MOI.set(model, MOI.ConstraintSet(), c, MOI.GreaterThan.([4.0, 5.0])),
572+
)
573+
return
574+
end
575+
576+
function test_change_row_bounds_by_set_invalid()
577+
model = HiGHS.Optimizer()
578+
MOI.set(model, MOI.Silent(), true)
579+
x = MOI.add_variable(model)
580+
c = MOI.add_constraint(model, 1.0 .* x, MOI.GreaterThan(0.0))
581+
c_invalid = typeof(c)(-123456)
582+
sets = MOI.GreaterThan.(1.0:2.0)
583+
@test_throws(
584+
MOI.InvalidIndex(c_invalid),
585+
MOI.set(model, MOI.ConstraintSet(), [c, c_invalid], sets),
586+
)
587+
return
588+
end
589+
590+
function test_change_row_bounds_by_set_greater_than()
591+
model = HiGHS.Optimizer()
592+
MOI.set(model, MOI.Silent(), true)
593+
x = MOI.add_variables(model, 3)
594+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.GreaterThan.(1.0:3.0))
595+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
596+
f = 1.0 * x[1] + x[2] + x[3]
597+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
598+
MOI.optimize!(model)
599+
@test (MOI.get(model, MOI.ObjectiveValue()), 6; atol = 1e-6)
600+
MOI.set(
601+
model,
602+
MOI.ConstraintSet(),
603+
[c[1], c[3]],
604+
MOI.GreaterThan.([4.0, 5.0]),
605+
)
606+
MOI.optimize!(model)
607+
@test (MOI.get(model, MOI.ObjectiveValue()), 11; atol = 1e-6)
608+
@test MOI.get(model, MOI.ConstraintSet(), c) ==
609+
MOI.GreaterThan.([4.0, 2.0, 5.0])
610+
return
611+
end
612+
613+
function test_change_row_bounds_by_set_less_than()
614+
model = HiGHS.Optimizer()
615+
MOI.set(model, MOI.Silent(), true)
616+
x = MOI.add_variables(model, 3)
617+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.LessThan.(1.0:3.0))
618+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
619+
f = 1.0 * x[1] + x[2] + x[3]
620+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
621+
MOI.optimize!(model)
622+
@test (MOI.get(model, MOI.ObjectiveValue()), 6; atol = 1e-6)
623+
MOI.set(model, MOI.ConstraintSet(), [c[1], c[3]], MOI.LessThan.([4.0, 5.0]))
624+
MOI.optimize!(model)
625+
@test (MOI.get(model, MOI.ObjectiveValue()), 11; atol = 1e-6)
626+
@test MOI.get(model, MOI.ConstraintSet(), c) ==
627+
MOI.LessThan.([4.0, 2.0, 5.0])
628+
return
629+
end
630+
631+
function test_change_row_bounds_by_set_equal_to()
632+
model = HiGHS.Optimizer()
633+
MOI.set(model, MOI.Silent(), true)
634+
x = MOI.add_variables(model, 3)
635+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.EqualTo.(1.0:3.0))
636+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
637+
f = 1.0 * x[1] + x[2] + x[3]
638+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
639+
MOI.optimize!(model)
640+
@test (MOI.get(model, MOI.ObjectiveValue()), 6; atol = 1e-6)
641+
MOI.set(model, MOI.ConstraintSet(), [c[1], c[3]], MOI.EqualTo.([4.0, 5.0]))
642+
MOI.optimize!(model)
643+
@test (MOI.get(model, MOI.ObjectiveValue()), 11; atol = 1e-6)
644+
@test MOI.get(model, MOI.ConstraintSet(), c) ==
645+
MOI.EqualTo.([4.0, 2.0, 5.0])
646+
return
647+
end
648+
564649
end
565650

566651
TestMOIHighs.runtests()

0 commit comments

Comments
 (0)