-
Notifications
You must be signed in to change notification settings - Fork 23
/
mixed.jl
161 lines (151 loc) · 4.09 KB
/
mixed.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
MixedMode(; default = SOS1Mode())
A mode that allows to mix different modes for different constraints and variables.
* `default` is the default mode to use for all constraints and variables that are
not explicitly mapped to a mode.
"""
mutable struct MixedMode{T} <: AbstractBilevelSolverMode{T}
default::AbstractBilevelSolverMode{T}
constraint_mode_map_c::Dict{CI,AbstractBilevelSolverMode{T}}
constraint_mode_map_v::Dict{VI,AbstractBilevelSolverMode{T}}
cache::ComplementBoundCache
function_cache::Dict{Int,MOI.AbstractScalarFunction}
function MixedMode(; default = SOS1Mode())
return new{Float64}(
default,
Dict{CI,AbstractBilevelSolverMode{Float64}}(),
Dict{VI,AbstractBilevelSolverMode{Float64}}(),
ComplementBoundCache(),
Dict{Int,MOI.AbstractScalarFunction}(),
)
end
end
function reset!(mode::MixedMode)
mode.cache = ComplementBoundCache()
mode.function_cache = Dict{Int,MOI.AbstractScalarFunction}()
for v in values(mode.constraint_mode_map_c)
reset!(v)
end
for v in values(mode.constraint_mode_map_v)
reset!(v)
end
return nothing
end
function build_full_map!(
mode::MixedMode,
upper_idxmap,
lower_idxmap,
lower_dual_idxmap,
lower_primal_dual_map,
)
_build_bound_map!(
mode.cache,
upper_idxmap,
lower_idxmap,
lower_dual_idxmap,
lower_primal_dual_map,
)
return nothing
end
accept_vector_set(::MixedMode{T}, ::Complement) where {T} = nothing
function add_aggregate_constraints(m, mode::MixedMode{T}, copy_names) where {T}
if isempty(mode.function_cache)
return nothing
end
eps = Dict{Int,T}()
for list in (
mode.constraint_mode_map_c,
mode.constraint_mode_map_v,
Dict(nothing => mode.default),
)
for md in values(list)
val, idx = _get_eps_idx(md)
if haskey(eps, idx) && idx > 0
eps[idx] = max(eps[idx], val)
else #idx >= 0
eps[idx] = val
end
end
end
for (idx, func) in mode.function_cache
_add_aggregate_constraints(m, func, eps[idx], idx, copy_names)
end
return nothing
end
function add_complement(
mode::MixedMode{T},
m,
comp::Complement,
idxmap_primal,
idxmap_dual,
copy_names::Bool,
pass_start::Bool,
) where {T}
_mode = get_mode(mode, comp.constraint, idxmap_primal)
accept_vector_set(_mode, comp)
ret = add_complement(
_mode,
m,
comp,
idxmap_primal,
idxmap_dual,
copy_names,
pass_start,
)
add_function_to_cache(mode, _mode)
return ret
end
add_function_to_cache(mode::MixedMode{T}, _mode) where {T} = nothing
function add_function_to_cache(
mode::MixedMode{T},
_mode::ProductMode{T},
) where {T}
idx = _mode.aggregation_group
if _mode.function_cache !== nothing && idx > 0
if haskey(mode.function_cache, idx)
mode.function_cache[idx] = MOIU.operate(
+,
T,
mode.function_cache[idx],
_mode.function_cache,
)
else
mode.function_cache[idx] = _mode.function_cache
end
_mode.function_cache = nothing
end
return nothing
end
function get_mode(
mode::MixedMode{T},
ci::CI{F,S},
map,
) where {
T,
F<:MOI.VariableIndex,
S<:Union{MOI.EqualTo{T},MOI.LessThan{T},MOI.GreaterThan{T}},
}
# key = map[VI(ci.value)]
key = VI(ci.value)
if haskey(mode.constraint_mode_map_v, key)
return mode.constraint_mode_map_v[key]
else
return mode.default
end
end
function get_mode(mode::MixedMode{T}, ci::CI{F,S}, map) where {T,F,S}
key = ci
if haskey(mode.constraint_mode_map_c, key)
return mode.constraint_mode_map_c[key]
else
return mode.default
end
end
function _get_eps_idx(mode)
return 0.0, 0
end
function _get_eps_idx(mode::ProductMode{T}) where {T}
idx = mode.aggregation_group
eps = mode.epsilon
return eps, idx
end