@@ -121,9 +121,6 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
121121 MOI. ScalarNonlinearFunction,
122122 Nothing,
123123 }
124- # Constraint counters.
125- number_zeroone_constraints:: Int
126- number_integer_constraints:: Int
127124 # Complementarity cache
128125 complementarity_cache:: _ComplementarityCache
129126 # Constraint mappings.
@@ -155,8 +152,6 @@ function Optimizer(; license_manager::Union{LMcontext,Nothing}=nothing, kwargs..
155152 Cint[],
156153 MOI. FEASIBILITY_SENSE,
157154 nothing ,
158- 0 ,
159- 0 ,
160155 _ComplementarityCache (),
161156 Dict {MOI.ConstraintIndex,Union{Cint,Vector{Cint}}} (),
162157 license_manager,
@@ -194,8 +189,6 @@ function MOI.empty!(model::Optimizer)
194189 MOI. empty! (model. nlp_model)
195190 model. sense = MOI. FEASIBILITY_SENSE
196191 model. objective = nothing
197- model. number_zeroone_constraints = 0
198- model. number_integer_constraints = 0
199192 model. complementarity_cache = _ComplementarityCache ()
200193 model. constraint_mapping = Dict ()
201194 model. license_manager = model. license_manager
@@ -215,8 +208,6 @@ function MOI.is_empty(model::Optimizer)
215208 model. sense == MOI. FEASIBILITY_SENSE &&
216209 model. number_solved == 0 &&
217210 isa (model. objective, Nothing) &&
218- model. number_zeroone_constraints == 0 &&
219- model. number_integer_constraints == 0 &&
220211 ! _has_complementarity (model. complementarity_cache) &&
221212 ! model. nlp_loaded
222213end
@@ -346,6 +337,8 @@ function MOI.set(model::Optimizer, attr::MOI.UserDefinedFunction, args)
346337 return
347338end
348339
340+ # Variables
341+
349342MOI. get (model:: Optimizer , :: MOI.NumberOfVariables ) = length (model. variable_info)
350343
351344function MOI. get (model:: Optimizer , :: MOI.ListOfVariableIndices )
@@ -447,19 +440,39 @@ end
447440
448441# MOI.NumberOfConstraints
449442
450- function MOI. get (model:: Optimizer , :: MOI.NumberOfConstraints{MOI.VariableIndex,MOI.ZeroOne} )
451- return model. number_zeroone_constraints
443+ _get_F_S (:: MOI.ConstraintIndex{F,S} ) where {F,S} = (F, S)
444+
445+ function MOI. get (model:: Optimizer , :: MOI.ListOfConstraintTypesPresent )
446+ ret = Tuple{Type,Type}[]
447+ for k in keys (model. constraint_mapping)
448+ F, S = _get_F_S (k)
449+ if ! ((F, S) in ret)
450+ push! (ret, (F, S))
451+ end
452+ end
453+ return ret
452454end
453455
454- function MOI. get (model:: Optimizer , :: MOI.NumberOfConstraints{MOI.VariableIndex,MOI.Integer} )
455- return model. number_integer_constraints
456+ function MOI. get (model:: Optimizer , :: MOI.ListOfConstraintIndices{F,S} ) where {F,S}
457+ ret = MOI. ConstraintIndex{F,S}[]
458+ for k in keys (model. constraint_mapping)
459+ if k isa MOI. ConstraintIndex{F,S}
460+ push! (ret, k)
461+ end
462+ end
463+ sort! (ret; by= x -> x. value)
464+ return ret
456465end
457466
458467function MOI. get (model:: Optimizer , :: MOI.NumberOfConstraints{F,S} ) where {F,S}
459468 f = Base. Fix2 (isa, MOI. ConstraintIndex{F,S})
460469 return count (f, keys (model. constraint_mapping); init= 0 )
461470end
462471
472+ function MOI. is_valid (model:: Optimizer , ci:: MOI.ConstraintIndex )
473+ return haskey (model. constraint_mapping, ci)
474+ end
475+
463476# ##
464477# ## MOI.VariableIndex -in- LessThan
465478# ##
@@ -468,7 +481,11 @@ function MOI.is_valid(
468481 model:: Optimizer ,
469482 ci:: MOI.ConstraintIndex{MOI.VariableIndex,MOI.LessThan{Float64}} ,
470483)
471- return model. variable_info[ci. value]. has_upper_bound
484+ info = get (model. variable_info, ci. value, nothing )
485+ if info === nothing
486+ return false
487+ end
488+ return info. has_upper_bound
472489end
473490
474491function MOI. add_constraint (
@@ -503,7 +520,11 @@ function MOI.is_valid(
503520 model:: Optimizer ,
504521 ci:: MOI.ConstraintIndex{MOI.VariableIndex,MOI.GreaterThan{Float64}} ,
505522)
506- return model. variable_info[ci. value]. has_lower_bound
523+ info = get (model. variable_info, ci. value, nothing )
524+ if info === nothing
525+ return false
526+ end
527+ return info. has_lower_bound
507528end
508529
509530function MOI. add_constraint (
@@ -538,8 +559,11 @@ function MOI.is_valid(
538559 model:: Optimizer ,
539560 ci:: MOI.ConstraintIndex{MOI.VariableIndex,MOI.Interval{Float64}} ,
540561)
541- return model. variable_info[ci. value]. has_lower_bound &&
542- model. variable_info[ci. value]. has_upper_bound
562+ info = get (model. variable_info, ci. value, nothing )
563+ if info === nothing
564+ return false
565+ end
566+ return info. has_lower_bound && info. has_upper_bound
543567end
544568
545569function MOI. add_constraint (
@@ -577,7 +601,11 @@ function MOI.is_valid(
577601 model:: Optimizer ,
578602 ci:: MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{Float64}} ,
579603)
580- return model. variable_info[ci. value]. is_fixed
604+ info = get (model. variable_info, ci. value, nothing )
605+ if info === nothing
606+ return false
607+ end
608+ return info. is_fixed
581609end
582610
583611function MOI. add_constraint (
642670
643671function MOI. add_constraint (model:: Optimizer , x:: MOI.VariableIndex , :: MOI.ZeroOne )
644672 MOI. throw_if_not_valid (model, x)
645- model. number_zeroone_constraints += 1
646673 lb, ub = nothing , nothing
647674 p = Ref {Cdouble} (NaN )
648675 if model. variable_info[x. value]. has_lower_bound
@@ -662,7 +689,9 @@ function MOI.add_constraint(model::Optimizer, x::MOI.VariableIndex, ::MOI.ZeroOn
662689 if ub != = nothing
663690 @_checked KN_set_var_upbnd (model. inner, _c_column (x), ub)
664691 end
665- return MOI. ConstraintIndex {MOI.VariableIndex,MOI.ZeroOne} (x. value)
692+ ci = MOI. ConstraintIndex {MOI.VariableIndex,MOI.ZeroOne} (x. value)
693+ model. constraint_mapping[ci] = convert (Cint, x. value)
694+ return ci
666695end
667696
668697# ##
672701function MOI. add_constraint (model:: Optimizer , x:: MOI.VariableIndex , set:: MOI.Integer )
673702 _throw_if_solved (model, x, set)
674703 MOI. throw_if_not_valid (model, x)
675- model. number_integer_constraints += 1
676704 @_checked KN_set_var_type (model. inner, _c_column (x), KN_VARTYPE_INTEGER)
677- return MOI. ConstraintIndex {MOI.VariableIndex,MOI.Integer} (x. value)
705+ ci = MOI. ConstraintIndex {MOI.VariableIndex,MOI.Integer} (x. value)
706+ model. constraint_mapping[ci] = convert (Cint, x. value)
707+ return ci
678708end
679709
680710# ##
@@ -997,14 +1027,9 @@ function MOI.add_constraint(
9971027 indv[(n_comp+ 1 ): end ],
9981028 comp_type,
9991029 )
1000- return MOI. ConstraintIndex {typeof(func),typeof(set)} (n_comp_cons)
1001- end
1002-
1003- function MOI. get (
1004- model:: Optimizer ,
1005- :: MOI.NumberOfConstraints{MOI.VectorOfVariables,MOI.Complements} ,
1006- )
1007- return model. complementarity_cache. n
1030+ ci = MOI. ConstraintIndex {typeof(func),typeof(set)} (n_comp_cons)
1031+ model. constraint_mapping[ci] = convert (Cint, n_comp_cons)
1032+ return ci
10081033end
10091034
10101035# MOI.NLPBlock
0 commit comments