Skip to content

Commit 71044dc

Browse files
authored
[Bridges] refactor NormOneBridge.jl from NormInfinityBridge.jl (#2684)
1 parent 927c58b commit 71044dc

File tree

2 files changed

+362
-325
lines changed

2 files changed

+362
-325
lines changed

test/Bridges/Constraint/NormInfinityBridge.jl

-325
Original file line numberDiff line numberDiff line change
@@ -303,287 +303,7 @@ function test_conic_NormInfinityCone_3()
303303
return
304304
end
305305

306-
function test_NormOne()
307-
mock = MOI.Utilities.MockOptimizer(
308-
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
309-
)
310-
config = MOI.Test.Config()
311-
bridged_mock = MOI.Bridges.Constraint.NormOne{Float64}(mock)
312-
MOI.Test.runtests(
313-
bridged_mock,
314-
config,
315-
include = [
316-
"test_basic_VectorOfVariables_NormOneCone",
317-
"test_basic_VectorAffineFunction_NormOneCone",
318-
"test_basic_VectorQuadraticFunction_NormOneCone",
319-
],
320-
)
321-
return
322-
end
323-
324-
function test_conic_NormOneCone_VectorOfVariables()
325-
mock = MOI.Utilities.MockOptimizer(
326-
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
327-
)
328-
config = MOI.Test.Config()
329-
bridged_mock = MOI.Bridges.Constraint.NormOne{Float64}(mock)
330-
mock.optimize! =
331-
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
332-
mock,
333-
[1.0, 0.5, 0.5, 0.5, 0.5],
334-
(MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives) =>
335-
[[1.0, 1.0, 1.0, 0.0, 0.0]],
336-
(MOI.VectorAffineFunction{Float64}, MOI.Zeros) => [[-1], [0]],
337-
)
338-
MOI.Test.test_conic_NormOneCone_VectorOfVariables(bridged_mock, config)
339-
MOI.empty!(bridged_mock)
340-
MOI.Test.test_conic_NormOneCone_VectorAffineFunction(bridged_mock, config)
341-
var_names = ["x", "y", "z"]
342-
MOI.set(
343-
bridged_mock,
344-
MOI.VariableName(),
345-
MOI.get(bridged_mock, MOI.ListOfVariableIndices()),
346-
var_names,
347-
)
348-
nonneg = MOI.get(
349-
mock,
350-
MOI.ListOfConstraintIndices{
351-
MOI.VectorAffineFunction{Float64},
352-
MOI.Nonnegatives,
353-
}(),
354-
)
355-
u, v = MOI.get(mock, MOI.ListOfVariableIndices())[4:5]
356-
MOI.set(mock, MOI.VariableName(), u, "u")
357-
MOI.set(mock, MOI.VariableName(), v, "v")
358-
@test length(nonneg) == 1
359-
MOI.set(mock, MOI.ConstraintName(), nonneg[1], "nonneg")
360-
zeros = MOI.get(
361-
mock,
362-
MOI.ListOfConstraintIndices{
363-
MOI.VectorAffineFunction{Float64},
364-
MOI.Zeros,
365-
}(),
366-
)
367-
@test length(zeros) == 2
368-
MOI.set(mock, MOI.ConstraintName(), zeros[1], "x_eq")
369-
MOI.set(mock, MOI.ConstraintName(), zeros[2], "y_eq")
370-
371-
s = """
372-
variables: x, y, z, u, v
373-
nonneg: [x + -1.0u + -1.0v, u + -1.0y, v + -1.0z, u + y, v + z] in Nonnegatives(5)
374-
x_eq: [-1.0 + x] in Zeros(1)
375-
y_eq: [-0.5 + y] in Zeros(1)
376-
maxobjective: y + z
377-
"""
378-
model = MOI.Utilities.Model{Float64}()
379-
MOI.Utilities.loadfromstring!(model, s)
380-
MOI.Test.util_test_models_equal(
381-
mock,
382-
model,
383-
[var_names; "u"; "v"],
384-
["nonneg", "x_eq", "y_eq"],
385-
)
386-
normone = MOI.get(
387-
bridged_mock,
388-
MOI.ListOfConstraintIndices{
389-
MOI.VectorAffineFunction{Float64},
390-
MOI.NormOneCone,
391-
}(),
392-
)
393-
@test length(normone) == 1
394-
MOI.set(bridged_mock, MOI.ConstraintName(), normone[1], "normone")
395-
zeros = MOI.get(
396-
bridged_mock,
397-
MOI.ListOfConstraintIndices{
398-
MOI.VectorAffineFunction{Float64},
399-
MOI.Zeros,
400-
}(),
401-
)
402-
@test length(zeros) == 2
403-
MOI.set(bridged_mock, MOI.ConstraintName(), zeros[1], "x_eq")
404-
MOI.set(bridged_mock, MOI.ConstraintName(), zeros[2], "y_eq")
405-
406-
s = """
407-
variables: x, y, z
408-
normone: [1.0x, y, z] in NormOneCone(3)
409-
x_eq: [-1.0 + x] in Zeros(1)
410-
y_eq: [-0.5 + y] in Zeros(1)
411-
maxobjective: y + z
412-
"""
413-
model = MOI.Utilities.Model{Float64}()
414-
MOI.Utilities.loadfromstring!(model, s)
415-
MOI.Test.util_test_models_equal(
416-
bridged_mock,
417-
model,
418-
var_names,
419-
["normone", "x_eq", "y_eq"],
420-
)
421-
ci = first(
422-
MOI.get(
423-
bridged_mock,
424-
MOI.ListOfConstraintIndices{
425-
MOI.VectorAffineFunction{Float64},
426-
MOI.NormOneCone,
427-
}(),
428-
),
429-
)
430-
for attr in [MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()]
431-
@test MOI.supports(bridged_mock, attr, typeof(ci))
432-
value = [4.0, 1.0, -2.0]
433-
MOI.set(bridged_mock, attr, ci, value)
434-
@test MOI.get(bridged_mock, attr, ci) value
435-
if attr isa MOI.ConstraintPrimalStart
436-
@test MOI.get(mock, MOI.VariablePrimalStart(), u) == 1
437-
@test MOI.get(mock, MOI.VariablePrimalStart(), v) == 2
438-
@test MOI.get(mock, attr, nonneg[1]) == Float64[1, 0, 4, 2, 0]
439-
else
440-
@test MOI.get(mock, attr, nonneg[1]) == Float64[4, 0, 2, 1, 0]
441-
end
442-
end
443-
_test_delete_bridge(
444-
bridged_mock,
445-
ci,
446-
3,
447-
((MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives, 0),),
448-
)
449-
return
450-
end
451-
452-
function test_conic_NormOneCone()
453-
mock = MOI.Utilities.MockOptimizer(
454-
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
455-
)
456-
config = MOI.Test.Config()
457-
bridged_mock = MOI.Bridges.Constraint.NormOne{Float64}(mock)
458-
mock.optimize! =
459-
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
460-
mock,
461-
[4, -1, -1, -1, 1, 1, 1],
462-
(MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives) =>
463-
[vcat(ones(4), zeros(3)), ones(3)],
464-
)
465-
MOI.Test.test_conic_NormOneCone(bridged_mock, config)
466-
var_names = ["x", "y1", "y2", "y3"]
467-
var_names_all = vcat(var_names, "z1", "z2", "z3")
468-
MOI.set(
469-
mock,
470-
MOI.VariableName(),
471-
MOI.get(mock, MOI.ListOfVariableIndices()),
472-
var_names_all,
473-
)
474-
nonneg = MOI.get(
475-
mock,
476-
MOI.ListOfConstraintIndices{
477-
MOI.VectorAffineFunction{Float64},
478-
MOI.Nonnegatives,
479-
}(),
480-
)
481-
@test length(nonneg) == 2
482-
MOI.set(mock, MOI.ConstraintName(), nonneg[1], "nonneg1")
483-
MOI.set(mock, MOI.ConstraintName(), nonneg[2], "nonneg2")
484-
s = """
485-
variables: x, y1, y2, y3, z1, z2, z3
486-
nonneg1: [x + -1.0 + -1.0z1 + -1.0z2 + -1.0z3, z1 + -1.0y1 + -2.0, z2 + -1.0y2 + -2.0, z3 + -1.0y3 + -2.0, z1 + y1 + 2.0, z2 + y2 + 2.0, z3 + y3 + 2.0] in Nonnegatives(7)
487-
nonneg2: [y1 + 1.0, y2 + 1.0, y3 + 1.0] in Nonnegatives(3)
488-
minobjective: x
489-
"""
490-
model = MOI.Utilities.Model{Float64}()
491-
MOI.Utilities.loadfromstring!(model, s)
492-
MOI.Test.util_test_models_equal(
493-
mock,
494-
model,
495-
var_names_all,
496-
["nonneg1", "nonneg2"],
497-
)
498-
MOI.set(
499-
bridged_mock,
500-
MOI.VariableName(),
501-
MOI.get(bridged_mock, MOI.ListOfVariableIndices()),
502-
var_names,
503-
)
504-
normone = MOI.get(
505-
bridged_mock,
506-
MOI.ListOfConstraintIndices{
507-
MOI.VectorAffineFunction{Float64},
508-
MOI.NormOneCone,
509-
}(),
510-
)
511-
@test length(normone) == 1
512-
MOI.set(bridged_mock, MOI.ConstraintName(), normone[1], "normone")
513-
nonneg = MOI.get(
514-
bridged_mock,
515-
MOI.ListOfConstraintIndices{
516-
MOI.VectorAffineFunction{Float64},
517-
MOI.Nonnegatives,
518-
}(),
519-
)
520-
@test length(nonneg) == 1
521-
MOI.set(bridged_mock, MOI.ConstraintName(), nonneg[1], "nonneg")
522-
s = """
523-
variables: x, y1, y2, y3
524-
normone: [x + -1.0, y1 + 2.0, y2 + 2.0, y3 + 2.0] in NormOneCone(4)
525-
nonneg: [y1 + 1.0, y2 + 1.0, y3 + 1.0] in Nonnegatives(3)
526-
minobjective: x
527-
"""
528-
model = MOI.Utilities.Model{Float64}()
529-
MOI.Utilities.loadfromstring!(model, s)
530-
MOI.Test.util_test_models_equal(
531-
bridged_mock,
532-
model,
533-
var_names,
534-
["normone", "nonneg"],
535-
)
536-
ci = first(
537-
MOI.get(
538-
bridged_mock,
539-
MOI.ListOfConstraintIndices{
540-
MOI.VectorAffineFunction{Float64},
541-
MOI.NormOneCone,
542-
}(),
543-
),
544-
)
545-
nonneg = MOI.get(
546-
mock,
547-
MOI.ListOfConstraintIndices{
548-
MOI.VectorAffineFunction{Float64},
549-
MOI.Nonnegatives,
550-
}(),
551-
)
552-
for attr in [MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()]
553-
@test MOI.supports(bridged_mock, attr, typeof(ci))
554-
value = (
555-
attr isa MOI.ConstraintPrimalStart ? vcat(3, ones(3)) :
556-
vcat(1, fill(-1, 3))
557-
)
558-
MOI.set(bridged_mock, attr, ci, value)
559-
@test MOI.get(bridged_mock, attr, ci) value
560-
nonneg_value = (
561-
attr isa MOI.ConstraintPrimalStart ?
562-
vcat(zeros(4), fill(2.0, 3)) : vcat(ones(4), zeros(3))
563-
)
564-
@test MOI.get(mock, attr, nonneg[1]) nonneg_value
565-
end
566-
_test_delete_bridge(
567-
bridged_mock,
568-
ci,
569-
4,
570-
((MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives, 1),),
571-
)
572-
return
573-
end
574-
575306
function test_runtests_vector_of_variables()
576-
MOI.Bridges.runtests(
577-
MOI.Bridges.Constraint.NormOneBridge,
578-
"""
579-
variables: t, x
580-
[t, x] in NormOneCone(2)
581-
""",
582-
"""
583-
variables: t, x, y
584-
[t + -1.0 * y, y + -1.0 * x, y + x] in Nonnegatives(3)
585-
""",
586-
)
587307
MOI.Bridges.runtests(
588308
MOI.Bridges.Constraint.NormInfinityBridge,
589309
"""
@@ -599,17 +319,6 @@ function test_runtests_vector_of_variables()
599319
end
600320

601321
function test_runtests_vector_affine_function()
602-
MOI.Bridges.runtests(
603-
MOI.Bridges.Constraint.NormOneBridge,
604-
"""
605-
variables: t, x
606-
[t, 2.0 * x + 1.0] in NormOneCone(2)
607-
""",
608-
"""
609-
variables: t, x, y
610-
[t + -1.0 * y, y + -2.0 * x + -1.0, y + 2.0 * x + 1.0] in Nonnegatives(3)
611-
""",
612-
)
613322
MOI.Bridges.runtests(
614323
MOI.Bridges.Constraint.NormInfinityBridge,
615324
"""
@@ -669,40 +378,6 @@ function test_NormInfinity_VectorNonlinearFunction()
669378
return
670379
end
671380

672-
function test_NormOne_VectorNonlinearFunction()
673-
# We can't use the standard runtests because ScalarNonlinearFunction does
674-
# not preserve f(x) ≈ (f(x) - g(x)) + g(x)
675-
inner = MOI.Utilities.Model{Float64}()
676-
model = MOI.Bridges.Constraint.NormOne{Float64}(inner)
677-
t = MOI.add_variable(model)
678-
x = MOI.add_variable(model)
679-
f = MOI.VectorNonlinearFunction([
680-
MOI.ScalarNonlinearFunction(:+, Any[t]),
681-
MOI.ScalarNonlinearFunction(:sin, Any[x]),
682-
])
683-
c = MOI.add_constraint(model, f, MOI.NormOneCone(2))
684-
F, S = MOI.VectorNonlinearFunction, MOI.Nonnegatives
685-
indices = MOI.get(inner, MOI.ListOfConstraintIndices{F,S}())
686-
@test length(indices) == 1
687-
inner_variables = MOI.get(inner, MOI.ListOfVariableIndices())
688-
@test length(inner_variables) == 3
689-
u, v, w = inner_variables
690-
v_sin = MOI.ScalarNonlinearFunction(:sin, Any[v])
691-
g = MOI.VectorNonlinearFunction([
692-
MOI.ScalarNonlinearFunction(
693-
:-,
694-
Any[MOI.ScalarNonlinearFunction(:+, Any[u]), 0.0+1.0*w],
695-
),
696-
MOI.ScalarNonlinearFunction(
697-
:+,
698-
Any[MOI.ScalarNonlinearFunction(:-, Any[v_sin]), w],
699-
),
700-
MOI.ScalarNonlinearFunction(:+, Any[v_sin, w]),
701-
])
702-
@test (MOI.get(inner, MOI.ConstraintFunction(), indices[1]), g)
703-
return
704-
end
705-
706381
end # module
707382

708383
TestConstraintNormInfinity.runtests()

0 commit comments

Comments
 (0)