Skip to content

Commit 891fbd8

Browse files
committed
Modify constraint sets
1 parent 08ccedd commit 891fbd8

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
@@ -1638,6 +1638,31 @@ function MOI.set(
16381638
return
16391639
end
16401640

1641+
function MOI.set(
1642+
model::Optimizer,
1643+
::MOI.ConstraintSet,
1644+
c::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}},
1645+
s::Vector{S},
1646+
) where {S<:_SCALAR_SETS}
1647+
if length(c) != length(s)
1648+
msg = "number of constraints does not match number of sets"
1649+
throw(DimensionMismatch(msg))
1650+
end
1651+
for ci in c
1652+
MOI.throw_if_not_valid(model, ci)
1653+
end
1654+
N = length(c)
1655+
rows, lower, upper = zeros(Cint, N), zeros(Cdouble, N), zeros(Cdouble, N)
1656+
for (i, (ci, si)) in enumerate(zip(c, s))
1657+
info = _info(model, ci)
1658+
rows[i] = info.row
1659+
lower[i], upper[i] = info.lower, info.upper = _bounds(si)
1660+
end
1661+
ret = Highs_changeRowsBoundsBySet(model, N, rows, lower, upper)
1662+
_check_ret(ret)
1663+
return
1664+
end
1665+
16411666
function MOI.get(
16421667
model::Optimizer,
16431668
::MOI.ConstraintFunction,

test/MOI_wrapper.jl

+86-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ function test_change_col_bounds_by_set_less_than()
610610
return
611611
end
612612

613-
function test_change_col_bounds_by_set_less_than()
613+
function test_change_col_bounds_by_set_equal_to()
614614
model = HiGHS.Optimizer()
615615
MOI.set(model, MOI.Silent(), true)
616616
x = MOI.add_variables(model, 3)
@@ -628,6 +628,91 @@ function test_change_col_bounds_by_set_less_than()
628628
return
629629
end
630630

631+
function test_change_row_bounds_by_set_dimension_mismatch()
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.GreaterThan.(1.0:3.0))
636+
@test_throws(
637+
DimensionMismatch,
638+
MOI.set(model, MOI.ConstraintSet(), c, MOI.GreaterThan.([4.0, 5.0])),
639+
)
640+
return
641+
end
642+
643+
function test_change_row_bounds_by_set_invalid()
644+
model = HiGHS.Optimizer()
645+
MOI.set(model, MOI.Silent(), true)
646+
x = MOI.add_variable(model)
647+
c = MOI.add_constraint(model, 1.0 .* x, MOI.GreaterThan(0.0))
648+
c_invalid = typeof(c)(-123456)
649+
sets = MOI.GreaterThan.(1.0:2.0)
650+
@test_throws(
651+
MOI.InvalidIndex(c_invalid),
652+
MOI.set(model, MOI.ConstraintSet(), [c, c_invalid], sets),
653+
)
654+
return
655+
end
656+
657+
function test_change_row_bounds_by_set_greater_than()
658+
model = HiGHS.Optimizer()
659+
MOI.set(model, MOI.Silent(), true)
660+
x = MOI.add_variables(model, 3)
661+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.GreaterThan.(1.0:3.0))
662+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
663+
f = 1.0 * x[1] + x[2] + x[3]
664+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
665+
MOI.optimize!(model)
666+
@test (MOI.get(model, MOI.ObjectiveValue()), 6; atol = 1e-6)
667+
MOI.set(
668+
model,
669+
MOI.ConstraintSet(),
670+
[c[1], c[3]],
671+
MOI.GreaterThan.([4.0, 5.0]),
672+
)
673+
MOI.optimize!(model)
674+
@test (MOI.get(model, MOI.ObjectiveValue()), 11; atol = 1e-6)
675+
@test MOI.get(model, MOI.ConstraintSet(), c) ==
676+
MOI.GreaterThan.([4.0, 2.0, 5.0])
677+
return
678+
end
679+
680+
function test_change_row_bounds_by_set_less_than()
681+
model = HiGHS.Optimizer()
682+
MOI.set(model, MOI.Silent(), true)
683+
x = MOI.add_variables(model, 3)
684+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.LessThan.(1.0:3.0))
685+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
686+
f = 1.0 * x[1] + x[2] + x[3]
687+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
688+
MOI.optimize!(model)
689+
@test (MOI.get(model, MOI.ObjectiveValue()), 6; atol = 1e-6)
690+
MOI.set(model, MOI.ConstraintSet(), [c[1], c[3]], MOI.LessThan.([4.0, 5.0]))
691+
MOI.optimize!(model)
692+
@test (MOI.get(model, MOI.ObjectiveValue()), 11; atol = 1e-6)
693+
@test MOI.get(model, MOI.ConstraintSet(), c) ==
694+
MOI.LessThan.([4.0, 2.0, 5.0])
695+
return
696+
end
697+
698+
function test_change_row_bounds_by_set_equal_to()
699+
model = HiGHS.Optimizer()
700+
MOI.set(model, MOI.Silent(), true)
701+
x = MOI.add_variables(model, 3)
702+
c = MOI.add_constraint.(model, 1.0 .* x, MOI.EqualTo.(1.0:3.0))
703+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
704+
f = 1.0 * x[1] + x[2] + x[3]
705+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
706+
MOI.optimize!(model)
707+
@test (MOI.get(model, MOI.ObjectiveValue()), 6; atol = 1e-6)
708+
MOI.set(model, MOI.ConstraintSet(), [c[1], c[3]], MOI.EqualTo.([4.0, 5.0]))
709+
MOI.optimize!(model)
710+
@test (MOI.get(model, MOI.ObjectiveValue()), 11; atol = 1e-6)
711+
@test MOI.get(model, MOI.ConstraintSet(), c) ==
712+
MOI.EqualTo.([4.0, 2.0, 5.0])
713+
return
714+
end
715+
631716
end
632717

633718
TestMOIHighs.runtests()

0 commit comments

Comments
 (0)