Skip to content

Commit 9b6e6b9

Browse files
committed
Update
1 parent e29c451 commit 9b6e6b9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Utilities/penalty_relaxation.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ function _add_penalty_to_objective(
107107
return MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(one(T), x), zero(T))
108108
end
109109

110+
function _add_penalty_to_objective(
111+
model::MOI.ModelLike,
112+
::Type{F},
113+
penalty::T,
114+
x::Vector{MOI.VariableIndex},
115+
) where {T,F<:MOI.ScalarNonlinearFunction}
116+
attr = MOI.ObjectiveFunction{F}()
117+
f = MOI.get(model, attr)
118+
g = Any[MOI.ScalarNonlinearFunction(:*, Any[penalty, xi]) for xi in x]
119+
MOI.set(model, attr, MOI.ScalarNonlinearFunction(:+, vcat(f, g)))
120+
return MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(one(T), x), zero(T))
121+
end
122+
110123
function _add_penalty_to_objective(
111124
::MOI.ModelLike,
112125
::Type{F},

test/Utilities/penalty_relaxation.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ function test_relax_variable_index_objective()
101101
return
102102
end
103103

104+
function test_relax_scalar_nonlinear_objective()
105+
_test_roundtrip(
106+
"""
107+
variables: x, y
108+
minobjective: ScalarNonlinearFunction(exp(x))
109+
c1: x + y <= 1.0
110+
""",
111+
"""
112+
variables: x, y, a
113+
minobjective: ScalarNonlinearFunction(+(exp(x), *(1.0, a)))
114+
c1: x + y + -1.0 * a <= 1.0
115+
a >= 0.0
116+
""",
117+
)
118+
return
119+
end
120+
104121
function test_relax_affine_lessthan()
105122
_test_roundtrip(
106123
"""

0 commit comments

Comments
 (0)