Skip to content

Commit 5e191e8

Browse files
authored
[Nonlinear] support copy_to into ModelWithQuad and ModelWithOracles (#3157)
* [Nonlinear] support copy_to into ModelWithQuad and ModelWithOracles Nonlinear.Model accepts its constraints and objective incrementally, the variables being owned by the outer layer, so it supports the incremental interface. ModelWithQuad and ModelWithOracles keep forwarding the trait to their inner model since they forward add_constraint to it. Add MOI.empty! for ModelWithQuad, needed by default_copy_to, and define MOI.copy_to for both layers. This lets Nonlinear.model(backend) be filled with MOI.copy_to and the bounds be queried back with MOI.Utilities.variable_bounds. * remove comm * Cover copy_to into ModelWithOracles
1 parent 77688a0 commit 5e191e8

5 files changed

Lines changed: 105 additions & 1 deletion

File tree

‎src/Nonlinear/layers/ModelWithOracles.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function MOI.supports_incremental_interface(model::ModelWithOracles)
6868
return MOI.supports_incremental_interface(model.inner)
6969
end
7070

71+
function MOI.copy_to(dest::ModelWithOracles, src::MOI.ModelLike)
72+
return MOI.Utilities.default_copy_to(dest, src)
73+
end
74+
7175
function MOI.supports(model::ModelWithOracles, attr::MOI.AbstractModelAttribute)
7276
return MOI.supports(model.inner, attr)
7377
end

‎src/Nonlinear/layers/ModelWithQuad.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ function MOI.supports_incremental_interface(model::ModelWithQuad)
8383
return MOI.supports_incremental_interface(model.inner)
8484
end
8585

86+
function MOI.copy_to(dest::ModelWithQuad, src::MOI.ModelLike)
87+
return MOI.Utilities.default_copy_to(dest, src)
88+
end
89+
8690
# Model attributes not owned by this layer, including `ObjectiveSense`, are
8791
# deliberately forwarded through the MOI API.
8892
function MOI.supports(model::ModelWithQuad, attr::MOI.AbstractModelAttribute)
@@ -112,6 +116,16 @@ function MOI.is_empty(model::ModelWithQuad)
112116
MOI.is_empty(model.inner)
113117
end
114118

119+
function MOI.empty!(model::ModelWithQuad{T}) where {T}
120+
MOI.empty!(model.variables)
121+
empty!(model.list_of_variable_indices)
122+
MOI.empty!(model.inner)
123+
model.qp = QPBlockData{T}()
124+
model.qp.parameters = _parameter_values(model.inner)
125+
model.objective_sink = _NONE
126+
return
127+
end
128+
115129
function ModelWithQuad{T}(inner) where {T}
116130
return ModelWithQuad{T}(QPBlockData{T}(), inner)
117131
end

‎src/Nonlinear/model.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ end
232232
# available, but model layers and solvers communicate with this model only via
233233
# these methods.
234234

235+
MOI.supports_incremental_interface(::Model) = true
236+
235237
const _ScalarSet{T} =
236238
Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T},MOI.Interval{T}}
237239

‎test/Nonlinear/test_model_with_oracles.jl‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,37 @@ function test_feature_filtering()
232232
return
233233
end
234234

235+
function test_copy_to()
236+
src = MOI.Utilities.Model{Float64}()
237+
x = MOI.add_variable(src)
238+
MOI.add_constraint(src, x, MOI.LessThan(2.0))
239+
set = MOI.VectorNonlinearOracle(;
240+
dimension = 1,
241+
l = [0.0],
242+
u = [1.0],
243+
eval_f = (g, x) -> (g[1] = x[1]^2),
244+
jacobian_structure = [(1, 1)],
245+
eval_jacobian = (J, x) -> (J[1] = 2 * x[1]),
246+
)
247+
c = MOI.add_constraint(src, MOI.VectorOfVariables([x]), set)
248+
# The oracle layer is outermost, as when it wraps a custom backend model.
249+
inner = MOI.Nonlinear.ModelWithQuad(MOI.Nonlinear.Model())
250+
dest = MOI.Nonlinear.ModelWithOracles(inner)
251+
index_map = MOI.copy_to(dest, src)
252+
@test MOI.is_valid(dest, index_map[c])
253+
@test MOI.Utilities.variable_bounds(dest).upper == [2.0]
254+
d = MOI.Nonlinear.Evaluator(
255+
dest,
256+
MOI.Nonlinear.SparseReverseMode(),
257+
[index_map[x]],
258+
)
259+
MOI.initialize(d, [:Jac])
260+
g = fill(NaN, 1)
261+
MOI.eval_constraint(d, g, [3.0])
262+
@test g == [9.0]
263+
return
264+
end
265+
235266
function test_forwarding()
236267
inner = MOI.Utilities.Model{Float64}()
237268
model = MOI.Nonlinear.ModelWithOracles(inner)

‎test/Nonlinear/test_model_with_quad.jl‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ function test_qp_attribute_types()
469469
end
470470

471471
function test_quad_only_with_empty_inner()
472+
@test MOI.supports_incremental_interface(Nonlinear.Model())
472473
model = Nonlinear.ModelWithQuad(Nonlinear.Model())
474+
@test MOI.supports_incremental_interface(model)
473475
x = MOI.add_variable(model)
474476
Nonlinear.add_constraint(
475477
model,
@@ -486,9 +488,60 @@ function test_quad_only_with_empty_inner()
486488
return
487489
end
488490

491+
function test_copy_to()
492+
src = MOI.Utilities.Model{Float64}()
493+
x, y = MOI.add_variables(src, 2)
494+
p, _ = MOI.add_constrained_variable(src, MOI.Parameter(2.0))
495+
MOI.add_constraint(src, x, MOI.GreaterThan(-1.0))
496+
MOI.add_constraint(src, y, MOI.Interval(0.0, 3.0))
497+
f = 1.0 * x * x + 1.0 * p * y
498+
c_quad = MOI.add_constraint(src, f, MOI.LessThan(4.0))
499+
g = MOI.ScalarNonlinearFunction(:sin, Any[y])
500+
c_nl = MOI.add_constraint(src, g, MOI.EqualTo(0.5))
501+
obj = MOI.ScalarNonlinearFunction(:exp, Any[x])
502+
MOI.set(src, MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction}(), obj)
503+
MOI.set(src, MOI.ObjectiveSense(), MOI.MIN_SENSE)
504+
dest = Nonlinear.model(Nonlinear.SparseReverseMode())
505+
index_map = MOI.copy_to(dest, src)
506+
@test index_map[x] == MOI.VariableIndex(1)
507+
@test index_map[y] == MOI.VariableIndex(2)
508+
@test MOI.is_valid(dest, index_map[p])
509+
@test MOI.is_valid(dest, index_map[c_quad])
510+
@test MOI.is_valid(dest, index_map[c_nl])
511+
bounds = MOI.Utilities.variable_bounds(dest)
512+
@test bounds.lower == [-1.0, 0.0]
513+
@test bounds.upper == [Inf, 3.0]
514+
vars = [index_map[x], index_map[y]]
515+
d = Nonlinear.Evaluator(dest, Nonlinear.SparseReverseMode(), vars)
516+
MOI.initialize(d, [:Grad, :Jac])
517+
xv = [1.5, 0.25]
518+
@test MOI.eval_objective(d, xv) ≈ exp(1.5)
519+
cons = fill(NaN, 2)
520+
MOI.eval_constraint(d, cons, xv)
521+
@test cons ≈ [1.5^2 + 2.0 * 0.25, sin(0.25)]
522+
@test MOI.NLPBlockData(d).constraint_bounds ==
523+
[MOI.NLPBoundsPair(-Inf, 4.0), MOI.NLPBoundsPair(0.5, 0.5)]
524+
# Copying into a non-empty model replaces its content.
525+
MOI.set(
526+
src,
527+
MOI.ConstraintSet(),
528+
MOI.ConstraintIndex{MOI.VariableIndex,MOI.Parameter{Float64}}(p.value),
529+
MOI.Parameter(3.0),
530+
)
531+
index_map = MOI.copy_to(dest, src)
532+
@test MOI.get(dest, MOI.NumberOfVariables()) == 3
533+
vars = [index_map[x], index_map[y]]
534+
d = Nonlinear.Evaluator(dest, Nonlinear.SparseReverseMode(), vars)
535+
MOI.initialize(d, [:Grad, :Jac])
536+
MOI.eval_constraint(d, cons, xv)
537+
@test cons ≈ [1.5^2 + 3.0 * 0.25, sin(0.25)]
538+
@test length(MOI.NLPBlockData(d).constraint_bounds) == 2
539+
return
540+
end
541+
489542
function test_moi_bounds_and_objectives()
490543
model = Nonlinear.model(Nonlinear.SparseReverseMode())
491-
@test !MOI.supports_incremental_interface(model)
544+
@test MOI.supports_incremental_interface(model)
492545
@test MOI.supports(model, MOI.ObjectiveSense())
493546
@test MOI.get(model, MOI.ObjectiveFunctionType()) ==
494547
MOI.ScalarAffineFunction{Float64}

0 commit comments

Comments
 (0)