-
Notifications
You must be signed in to change notification settings - Fork 18
/
bounds.jl
144 lines (128 loc) · 3.24 KB
/
bounds.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
# Auxiliary function related to bound-constrainted problems
export active, active!, breakpoints, compute_Hs_slope_qs!, project!, project_step!
"""
active(x, ℓ, u; rtol = 1e-8, atol = 1e-8)
Computes the active bounds at x, using tolerance `min(rtol * (uᵢ-ℓᵢ), atol)`.
If ℓᵢ or uᵢ is not finite, only `atol` is used.
"""
function active(
x::AbstractVector{T},
ℓ::AbstractVector{T},
u::AbstractVector{T};
rtol::Real = sqrt(eps(T)),
atol::Real = sqrt(eps(T)),
) where {T <: Real}
n = length(x)
indices = BitVector(undef, n)
active!(indices, x, ℓ, u, atol = atol, rtol = rtol)
return findall(indices)
end
"""
active!(indices, x, ℓ, u; rtol = 1e-8, atol = 1e-8)
Update a `BitVector` of the active bounds at x, using tolerance `min(rtol * (uᵢ-ℓᵢ), atol)`.
If ℓᵢ or uᵢ is not finite, only `atol` is used.
"""
function active!(
indices::BitVector,
x::AbstractVector{T},
ℓ::AbstractVector{T},
u::AbstractVector{T};
rtol::Real = sqrt(eps(T)),
atol::Real = sqrt(eps(T)),
) where {T <: Real}
n = length(x)
for i = 1:n
δ = -Inf < ℓ[i] < u[i] < Inf ? min(rtol * (u[i] - ℓ[i]), atol) : atol
if ℓ[i] == x[i] == u[i]
indices[i] = true
elseif x[i] <= ℓ[i] + δ
indices[i] = true
elseif x[i] >= u[i] - δ
indices[i] = true
else
indices[i] = false
end
end
return indices
end
"""
nbrk, brkmin, brkmax = breakpoints(x, d, ℓ, u)
Find the smallest and largest values of `α` such that `x + αd` lies on the
boundary. `x` is assumed to be feasible. `nbrk` is the number of breakpoints
from `x` in the direction `d`.
"""
function breakpoints(
x::AbstractVector{T},
d::AbstractVector{T},
ℓ::AbstractVector{T},
u::AbstractVector{T},
) where {T <: Real}
nvar = length(x)
nbrk = 0
brkmin = T(Inf)
brkmax = zero(T)
for i = 1:nvar
pos = (d[i] > 0) & (x[i] < u[i])
if pos
step = (u[i] - x[i]) / d[i]
brkmin = min(brkmin, step)
brkmax = max(brkmax, step)
nbrk += 1
end
neg = (d[i] < 0) & (x[i] > ℓ[i])
if neg
step = (ℓ[i] - x[i]) / d[i]
brkmin = min(brkmin, step)
brkmax = max(brkmax, step)
nbrk += 1
end
end
return nbrk, brkmin, brkmax
end
"""
slope, qs = compute_Hs_slope_qs!(Hs, H, s, g)
Computes
Hs = H * s
slope = dot(g,s)
qs = ¹/₂sᵀHs + slope
"""
function compute_Hs_slope_qs!(
Hs::AbstractVector{T},
H::Union{AbstractMatrix, AbstractLinearOperator},
s::AbstractVector{T},
g::AbstractVector{T},
) where {T <: Real}
mul!(Hs, H, s)
slope = dot(g, s)
qs = dot(s, Hs) / 2 + slope
return slope, qs
end
"""
project!(y, x, ℓ, u)
Projects `x` into bounds `ℓ` and `u`, in the sense of
`yᵢ = max(ℓᵢ, min(xᵢ, uᵢ))`.
"""
function project!(
y::AbstractVector{T},
x::AbstractVector{T},
ℓ::AbstractVector{T},
u::AbstractVector{T},
) where {T <: Real}
y .= max.(ℓ, min.(x, u))
end
"""
project_step!(y, x, d, ℓ, u, α = 1.0)
Computes the projected direction `y = P(x + α * d) - x`.
"""
function project_step!(
y::AbstractVector{T},
x::AbstractVector{T},
d::AbstractVector{T},
ℓ::AbstractVector{T},
u::AbstractVector{T},
α::Real = 1.0,
) where {T <: Real}
y .= x .+ α .* d
project!(y, y, ℓ, u)
y .-= x
end