Skip to content

Commit dfea155

Browse files
authored
[FileFormats.CBF] improve heuristic for when to write variable cones (#2494)
1 parent bca8a31 commit dfea155

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/FileFormats/CBF/write.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ function _add_cones(
151151
is_variable_cone = false
152152
break
153153
end
154-
push!(data.variables_with_domain, xi)
155154
end
156155
str = _cone_string(data, S)
157156
if !is_variable_cone
158157
_add_function(data, f, S)
159158
set = MOI.get(model, MOI.ConstraintSet(), ci)
160159
push!(data.cones, (str, MOI.dimension(set)))
161160
else
161+
for xi in f.variables
162+
push!(data.variables_with_domain, xi)
163+
end
162164
push!(data.variable_cones, (f.variables, str))
163165
end
164166
end

test/FileFormats/CBF/CBF.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,53 @@ function test_write_variable_cones()
501501
return
502502
end
503503

504+
function test_write_variable_cones_with_conflicting_sets()
505+
model = CBF.Model()
506+
x, _ = MOI.add_constrained_variables(model, MOI.Nonnegatives(2))
507+
y = MOI.add_variable(model)
508+
f = MOI.VectorOfVariables([y, x[2]])
509+
# The choice of Nonnegatives and Nonpositives is explicitly chosen because
510+
# Nonnegatives are parsed before Nonpositives, and it tests that we can skip
511+
# over a function containing `y`, and then constrain it in a later set.
512+
MOI.add_constraint(model, f, MOI.Nonnegatives(2))
513+
MOI.add_constraint(model, MOI.VectorOfVariables([y]), MOI.Nonpositives(1))
514+
g = 1.0 * x[1] + 2.0 * x[2] + 3.0 * y
515+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
516+
MOI.set(model, MOI.ObjectiveFunction{typeof(g)}(), g)
517+
io = IOBuffer()
518+
write(io, model)
519+
seekstart(io)
520+
@test read(io, String) == """
521+
VER
522+
3
523+
524+
OBJSENSE
525+
MIN
526+
527+
VAR
528+
3 2
529+
L+ 2
530+
L- 1
531+
532+
CON
533+
2 1
534+
L+ 2
535+
536+
OBJACOORD
537+
3
538+
0 1.0
539+
1 2.0
540+
2 3.0
541+
542+
ACOORD
543+
2
544+
0 2 1.0
545+
1 1 1.0
546+
547+
"""
548+
return
549+
end
550+
504551
function test_roundtrip_ExponentialCone()
505552
model = CBF.Model()
506553
x, _ = MOI.add_constrained_variables(model, MOI.ExponentialCone())

0 commit comments

Comments
 (0)