-
Notifications
You must be signed in to change notification settings - Fork 17
/
functions.jl
310 lines (248 loc) · 8.24 KB
/
functions.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright (c) 2018: Matthew Wilhelm, Robert Gottlieb, Dimitri Alston,
# Matthew Stuber, and the University of Connecticut (UConn).
# This code is licensed under the MIT license (see LICENSE.md for full details).
################################################################################
# EAGO
# A development environment for robust and global optimization.
# https://github.com/PSORLab/EAGO.jl
################################################################################
# src/eago_optimizer/functions/functions.jl
# Defines variable info and function types.
################################################################################
include(joinpath(@__DIR__, "nonlinear","auxiliary_variables.jl"))
"""
$(TYPEDEF)
An abstract super-type used for representing constraints built by EAGO's backend.
"""
abstract type AbstractEAGOConstraint end
"""
lower_interval_bound(::GlobalOptimizer, ::T)
Compute the lower interval bound for an `AbstractEAGOConstraint` representing an
inequality constraint.
# Options for T (all are subtypes of AbstractEAGOConstraint):
- AffineFunctionIneq
- BufferedQuadraticIneq
- BufferedSOC
- BufferedNonlinearFunction{V,N,T} where {V,N,T}
"""
function lower_interval_bound end
"""
interval_bound(::GlobalOptimizer, ::T)
Compute a tuple representing the lower and upper interval bounds for an
`AbstractEAGOConstraint` representing an equality constraint.
# Options for T (all are subtypes of AbstractEAGOConstraint):
- AffineFunctionEq
- AffineFunctionIneq
- BufferedQuadraticEq
- BufferedQuadraticIneq
- BufferedNonlinearFunction{V,N,T} where {V,N,T}
"""
function interval_bound end
"""
eliminate_fixed_variables!(::T, ::Vector{VariableInfo})
Eliminate fixed variables by rearrangment or restructuring of the `AbstractEAGOConstraint`.
# Options for T (all are subtypes of AbstractEAGOConstraint):
- AffineFunctionEq
- AffineFunctionIneq
- BufferedQuadraticIneq
- BufferedNonlinearFunction{N,T} where {N, T<:RelaxTag}
- NonlinearExpression{V,N,T} where {V, N, T<:RelaxTag}
"""
function eliminate_fixed_variables! end
###
###
### Affine function
###
###
"""
$(TYPEDEF)
Representation of an affine inequality. Currently only used for
bound tightening.
$(TYPEDFIELDS)
"""
mutable struct AffineFunctionIneq <: AbstractEAGOConstraint
terms::Vector{Tuple{Float64,Int}}
constant::Float64
len::Int
end
const AFI = AffineFunctionIneq
AffineFunctionIneq() = AffineFunctionIneq(Tuple{Float64,Int}[], 0.0, 0)
function AffineFunctionIneq(f::SAF, s::LT)
terms = map(x -> (x.coefficient, x.variable.value), f.terms)
AffineFunctionIneq(terms, f.constant - s.upper, length(f.terms))
end
function AffineFunctionIneq(f::SAF, s::GT)
terms = map(x -> (-x.coefficient, x.variable.value), f.terms)
AffineFunctionIneq(terms, s.lower - f.constant, length(f.terms))
end
function AffineFunctionIneq(f::VI; is_max = false)
if is_max
return AffineFunctionIneq(Tuple{Float64,Int}[(-1.0, f.value)], 0.0, 1)
end
AffineFunctionIneq(Tuple{Float64,Int}[(1.0, f.value)], 0.0, 1)
end
"""
$(TYPEDEF)
Representation of an affine equality. Currently only used for
bound tightening.
$(TYPEDFIELDS)
"""
mutable struct AffineFunctionEq <: AbstractEAGOConstraint
terms::Vector{Tuple{Float64,Int}}
constant::Float64
len::Int
end
const AFE = AffineFunctionEq
AffineFunctionEq() = AffineFunctionEq(Tuple{Float64,Int}[], 0.0, 0)
function AffineFunctionEq(func::SAF, set::ET)
terms = map(x -> (x.coefficient, x.variable.value), func.terms)
return AffineFunctionEq(terms, func.constant - set.value, length(func.terms))
end
###
### Parsing definitions
###
function eliminate_fixed_variables!(f::T, v::Vector{VariableInfo}) where T <: Union{AffineFunctionIneq,
AffineFunctionEq}
deleted_count = 0
i = 1
while i + deleted_count <= f.len
coeff, indx = @inbounds f.terms[i]
variable_info = @inbounds v[indx]
if variable_info.is_fixed
f.constant += coeff*variable_info.lower_bound
deleteat!(f.terms, i)
deleted_count += 1
else
i += 1
end
end
f.len -= deleted_count
return nothing
end
###
###
### Quadratic function
###
###
"""
$(TYPEDEF)
Representation of a general quadratic equality constraint with a buffer.
$(TYPEDFIELDS)
"""
mutable struct BufferedQuadraticEq <: AbstractEAGOConstraint
func::SQF
minus_func::SQF
buffer::Dict{Int, Float64}
saf::SAF
len::Int
end
const BQE = BufferedQuadraticEq
"""
$(TYPEDEF)
Representation of a general quadratic inequality constraint with a buffer.
$(TYPEDFIELDS)
"""
mutable struct BufferedQuadraticIneq <: AbstractEAGOConstraint
func::SQF
buffer::Dict{Int, Float64}
saf::SAF
len::Int
end
const BQI = BufferedQuadraticIneq
#=
mutable struct BufferedConvexQuadratic <: AbstractEAGOConstraint
func::SQF
buffer::Dict{Int, Float64}
saf::SAF
len::Int
end
=#
"""
$(TYPEDSIGNATURES)
Create a buffer dictionary from a ScalarQuadraticFunction.
"""
function create_buffer_dict(func::SQF)
buffer = Dict{Int, Float64}()
for term in func.quadratic_terms
buffer[term.variable_1.value] = 0.0
buffer[term.variable_2.value] = 0.0
end
for term in func.affine_terms
buffer[term.variable.value] = 0.0
end
return buffer
end
BufferedQuadraticIneq() = BufferedQuadraticIneq(SQF(SQT[], SAT[], 0.0), Dict{Int, Float64}(), SAF(SAT[], 0.0), 0)
function BufferedQuadraticIneq(func::SQF, set::LT)
buffer = create_buffer_dict(func)
saf = SAF([SAT(0.0, VI(k)) for k in keys(buffer)], 0.0)
len = length(buffer)
cfunc = copy(func)
cfunc.constant -= set.upper
return BufferedQuadraticIneq(cfunc, buffer, saf, len)
end
function BufferedQuadraticIneq(func::SQF, set::GT)
buffer = create_buffer_dict(func)
saf = SAF([SAT(0.0, VI(k)) for k in keys(buffer)], 0.0)
len = length(buffer)
cfunc = MOIU.operate(-, Float64, func)
cfunc.constant += set.lower
return BufferedQuadraticIneq(cfunc, buffer, saf, len)
end
BufferedQuadraticEq() = BufferedQuadraticEq(SQF(SQT[], SAT[], 0.0), SQF(SQT[], SAT[], 0.0), Dict{Int, Float64}(), SAF(SAT[], 0.0), 0)
function BufferedQuadraticEq(func::SQF, set::ET)
buffer = create_buffer_dict(func)
saf = SAF([SAT(0.0, VI(k)) for k in keys(buffer)], 0.0)
len = length(buffer)
cfunc1 = copy(func)
cfunc1.constant -= set.value
cfunc2 = MOIU.operate(-, Float64, func)
cfunc2.constant += set.value
return BufferedQuadraticEq(cfunc1, cfunc2, buffer, saf, len)
end
#=
function BufferedConvexQuadratic(f::BufferedQuadraticIneq)
BufferedConvexQuadratic(copy(f.func), copy(f.buffer), copy(f.saf), f.len)
end
=#
function eliminate_fixed_variables!(f::T, v::Vector{VariableInfo}) where T <: Union{BufferedQuadraticIneq,
BufferedQuadraticIneq}
deleted_count = 0
i = 1
while i + deleted_count <= f.len
term = f.sqf.terms[i]
variable_info_1 = v[term.variable_1.value]
variable_info_2 = v[term.variable_2.value]
if variable_info_1.is_fixed && variable_info_2.is_fixed
f.sqf.constant += coeff*variable_info_1.lower_bound*variable_info_2.lower_bound
deleteat!(f.sqf.terms, i)
deleted_count += 1
else
i += 1
end
end
f.len -= deleted_count
return nothing
end
###
###
### Cone
###
###
"""
$(TYPEDEF)
Representation of a second-order cone with a buffer.
"""
mutable struct BufferedSOC <: AbstractEAGOConstraint
variables::VECOFVAR
buffer::Dict{Int, Float64}
saf::SAF
len::Int
end
function BufferedSOC(func::VECOFVAR, set::SOC)
len = length(func.variables)
buffer = Dict{Int, Float64}([(variable.value, 0.0) for variable in func.variables])
saf = SAF(fill(SAT(0.0, VI(1)), len), 0.0)
return BufferedSOC(copy(func), buffer, saf, len)
end
include("nonlinear/nonlinear.jl")