@@ -754,21 +754,25 @@ function resolve(model::Model, context::Context, variable::IndexedVariable{<:Spl
754754 )
755755end
756756
757+ # The variational constraints plugin should not attempt to create new variables in the model
758+ # Even if the `maycreate` flag was set to `True` at this point we assume that the variable has been created already
759+ unroll_nocreate (something) = unroll (set_maycreate (something, False ()))
760+
757761function resolve (model:: Model , context:: Context , variable:: IndexedVariable{Nothing} )
758- global_label = unroll (context[getname (variable)])
762+ global_label = unroll_nocreate (context[getname (variable)])
759763 return __resolve (model, global_label)
760764end
761765
762766function resolve (model:: Model , context:: Context , variable:: IndexedVariable )
763- global_label = unroll (context[getname (variable)])[index (variable)... ]
767+ global_label = unroll_nocreate (context[getname (variable)])[index (variable)... ]
764768 return __resolve (model, global_label)
765769end
766770
767771resolve (model:: Model , context:: Context , variable:: IndexedVariable{CombinedRange{NTuple{N, Int}, NTuple{N, Int}}} ) where {N} =
768772 throw (UnresolvableFactorizationConstraintError (" Cannot resolve factorization constraint for a combined range of dimension > 2." ))
769773
770774function resolve (model:: Model , context:: Context , variable:: IndexedVariable{<:CombinedRange} )
771- global_label = view (unroll (context[getname (variable)]), firstindex (index (variable)): lastindex (index (variable)))
775+ global_label = view (unroll_nocreate (context[getname (variable)]), firstindex (index (variable)): lastindex (index (variable)))
772776 return __resolve (model, global_label)
773777end
774778
@@ -803,7 +807,8 @@ function resolve(model::Model, context::Context, constraint::FactorizationConstr
803807 end
804808 lhs = map (variable -> resolve (model, context, variable), vfiltered)
805809 rhs = map (
806- variable -> ResolvedFactorizationConstraintEntry ((resolve (model, context, unroll (context[getname (variable)]), MeanField ()),)),
810+ variable ->
811+ ResolvedFactorizationConstraintEntry ((resolve (model, context, unroll_nocreate (context[getname (variable)]), MeanField ()),)),
807812 vfiltered
808813 )
809814 return ResolvedFactorizationConstraint (ResolvedConstraintLHS (lhs), rhs)
930935function apply_constraints! (
931936 model:: Model , context:: Context , marginal_constraint:: MarginalFormConstraint{T, F} where {T <: IndexedVariable , F}
932937)
933- applicable_nodes = unroll (context[getvariables (marginal_constraint)])
938+ applicable_nodes = unroll_nocreate (context[getvariables (marginal_constraint)])
934939 for node in applicable_nodes
935940 if hasextra (model[node], VariationalConstraintsMarginalFormConstraintKey)
936941 @warn lazy " Node $node already has functional form constraint $(opt[:q]) applied, therefore $constraint_data will not be applied"
@@ -945,7 +950,7 @@ function apply_constraints!(model::Model, context::Context, marginal_constraint:
945950end
946951
947952function apply_constraints! (model:: Model , context:: Context , message_constraint:: MessageFormConstraint )
948- applicable_nodes = unroll (context[getvariables (message_constraint)])
953+ applicable_nodes = unroll_nocreate (context[getvariables (message_constraint)])
949954 for node in applicable_nodes
950955 if hasextra (model[node], VariationalConstraintsMessagesFormConstraintKey)
951956 @warn lazy " Node $node already has functional form constraint $(opt[:q]) applied, therefore $constraint_data will not be applied"
0 commit comments