3
3
# Use of this source code is governed by an MIT-style license that can be found
4
4
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
5
5
6
- # FIXME (odow): ConstraintName is missing MOI.supports, doesn't account for
7
- # duplicate names, and doesn't error properly for MOI.VariableIndex constraints.
6
+ function MOI. supports (
7
+ :: Optimizer ,
8
+ :: MOI.ConstraintName ,
9
+ :: Type{MOI.ConstraintIndex{F,S}} ,
10
+ ) where {
11
+ F<: Union {
12
+ MOI. ScalarAffineFunction{Float64},
13
+ MOI. ScalarQuadraticFunction{Float64},
14
+ MOI. VectorAffineFunction{Float64},
15
+ MOI. VectorOfVariables,
16
+ },
17
+ S,
18
+ }
19
+ return true
20
+ end
21
+
8
22
function MOI. get (
9
23
o:: Optimizer ,
10
24
:: MOI.ConstraintName ,
11
- ci:: MOI.ConstraintIndex ,
12
- ):: String
13
- return GC. @preserve o unsafe_string (SCIPconsGetName (cons (o, ci)))
25
+ ci:: MOI.ConstraintIndex{F} ,
26
+ ):: String where {
27
+ F<: Union {
28
+ MOI. ScalarAffineFunction{Float64},
29
+ MOI. ScalarQuadraticFunction{Float64},
30
+ MOI. VectorAffineFunction{Float64},
31
+ MOI. VectorOfVariables,
32
+ },
33
+ }
34
+ return unsafe_string (SCIPconsGetName (cons (o, ci)))
14
35
end
15
36
16
37
function MOI. set (
17
38
o:: Optimizer ,
18
39
:: MOI.ConstraintName ,
19
- ci:: MOI.ConstraintIndex ,
40
+ ci:: MOI.ConstraintIndex{F} ,
20
41
name:: String ,
21
- )
42
+ ) where {
43
+ F<: Union {
44
+ MOI. ScalarAffineFunction{Float64},
45
+ MOI. ScalarQuadraticFunction{Float64},
46
+ MOI. VectorAffineFunction{Float64},
47
+ MOI. VectorOfVariables,
48
+ },
49
+ }
22
50
@SCIP_CALL SCIPchgConsName (o, cons (o, ci), name)
51
+ o. name_to_constraint_index = nothing
23
52
return nothing
24
53
end
25
54
@@ -32,6 +61,54 @@ function MOI.set(
32
61
return throw (MOI. VariableIndexConstraintNameError ())
33
62
end
34
63
64
+ function _rebuild_name_to_constraint_index (o:: Optimizer )
65
+ o. name_to_constraint_index =
66
+ Dict {String,Union{Nothing,MOI.ConstraintIndex}} ()
67
+ for ((F, S), set) in o. constypes
68
+ if F <: MOI.VariableIndex
69
+ continue
70
+ end
71
+ for cref in set
72
+ ci = MOI. ConstraintIndex {F,S} (cref. val)
73
+ name = MOI. get (o, MOI. ConstraintName (), ci)
74
+ if isempty (name)
75
+ continue
76
+ elseif haskey (o. name_to_constraint_index, name)
77
+ o. name_to_constraint_index[name] = nothing # Duplicate name
78
+ else
79
+ o. name_to_constraint_index[name] = ci
80
+ end
81
+ end
82
+ end
83
+ return
84
+ end
85
+
86
+ function MOI. get (o:: Optimizer , :: Type{MOI.ConstraintIndex} , name:: String )
87
+ if o. name_to_constraint_index === nothing
88
+ _rebuild_name_to_constraint_index (o)
89
+ end
90
+ ci = get (o. name_to_constraint_index, name, missing )
91
+ if ismissing (ci)
92
+ return nothing
93
+ elseif isnothing (ci)
94
+ error (" Duplicate name" )
95
+ else
96
+ return ci
97
+ end
98
+ end
99
+
100
+ function MOI. get (
101
+ o:: Optimizer ,
102
+ :: Type{MOI.ConstraintIndex{F,S}} ,
103
+ name:: String ,
104
+ ) where {F,S}
105
+ ci = MOI. get (o, MOI. ConstraintIndex, name)
106
+ if ! (ci isa MOI. ConstraintIndex{F,S})
107
+ return nothing
108
+ end
109
+ return ci
110
+ end
111
+
35
112
function MOI. is_valid (o:: Optimizer , c:: MOI.ConstraintIndex{F,S} ) where {F,S}
36
113
cons_set = get (o. constypes, (F, S), nothing )
37
114
if cons_set === nothing
@@ -42,20 +119,3 @@ function MOI.is_valid(o::Optimizer, c::MOI.ConstraintIndex{F,S}) where {F,S}
42
119
end
43
120
return haskey (o. inner. conss, SCIP. ConsRef (c. value))
44
121
end
45
-
46
- function MOI. get (o:: Optimizer , :: Type{MOI.ConstraintIndex} , name:: String )
47
- ptr = SCIPfindCons (o, name)
48
- if ptr == C_NULL
49
- return nothing
50
- end
51
- cref = get (o. reference, ptr, nothing )
52
- if cref === nothing
53
- return cref
54
- end
55
- for ((F, S), setref) in o. constypes
56
- if cref in setref
57
- return MOI. ConstraintIndex {F,S} (cref. val)
58
- end
59
- end
60
- return error (" Constraint type not found for constraint $cref " )
61
- end
0 commit comments