-
Notifications
You must be signed in to change notification settings - Fork 21
Global Optimisation #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eeshan9815
wants to merge
6
commits into
JuliaIntervals:master
Choose a base branch
from
eeshan9815:global_min
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2499f85
Fix deprecated syntax for inner constructor of SortedVector
eeshan9815 4099735
Initial implementation of global optimisation
eeshan9815 a9c1eab
Add derivative contractors
eeshan9815 ae55541
Add icp contractors
eeshan9815 b3ef811
Incorporate changes from review comments
eeshan9815 a5b6b5b
Remove parameterization of F
eeshan9815 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ julia 0.5 | |
|
||
IntervalArithmetic 0.9.1 | ||
IntervalRootFinding 0.1 | ||
IntervalConstraintProgramming | ||
DataStructures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using IntervalOptimisation, IntervalArithmetic, IntervalConstraintProgramming | ||
|
||
|
||
# Unconstrained Optimisation | ||
# Example from Eldon Hansen - Global Optimisation using Interval Analysis, Chapter 11 | ||
f(x, y) = (1.5 - x * (1 - y))^2 + (2.25 - x * (1 - y^2))^2 + (2.625 - x * (1 - y^3))^2 | ||
# f (generic function with 2 methods) | ||
|
||
f(X) = f(X...) | ||
# f (generic function with 2 methods) | ||
|
||
X = (-1e6..1e6) × (-1e6..1e6) | ||
# [-1e+06, 1e+06] × [-1e+06, 1e+06] | ||
|
||
minimise_icp(f, X) | ||
# ([0, 2.39527e-07], IntervalArithmetic.IntervalBox{2,Float64}[[2.99659, 2.99748] × [0.498906, 0.499523], [2.99747, 2.99834] × [0.499198, 0.500185], [2.99833, 2.99922] × [0.499198, 0.500185], [2.99921, 3.00011] × [0.499621, 0.500415], [3.0001, 3.001] × [0.499621, 0.500415], [3.00099, 3.0017] × [0.500169, 0.500566], [3.00169, 3.00242] × [0.500169, 0.500566]]) | ||
|
||
|
||
|
||
f(X) = X[1]^2 + X[2]^2 | ||
# f (generic function with 2 methods) | ||
|
||
X = (-∞..∞) × (-∞..∞) | ||
# [-∞, ∞] × [-∞, ∞] | ||
|
||
minimise_icp(f, X) | ||
# ([0, 0], IntervalArithmetic.IntervalBox{2,Float64}[[-0, 0] × [-0, 0], [0, 0] × [-0, 0]]) | ||
|
||
|
||
|
||
|
||
# Constrained Optimisation | ||
# Example 1 from http://archimedes.cheme.cmu.edu/?q=baronexamples | ||
f(X) = -1 * (X[1] + X[2]) | ||
# f (generic function with 1 method) | ||
|
||
X = (-∞..∞) × (-∞..∞) | ||
# [-∞, ∞] × [-∞, ∞] | ||
|
||
constraints = [IntervalOptimisation.constraint(x->(x[1]), -∞..6), IntervalOptimisation.constraint(x->x[2], -∞..4), IntervalOptimisation.constraint(x->(x[1]*x[2]), -∞..4)] | ||
# 3-element Array{IntervalOptimisation.constraint{Float64},1}: | ||
# IntervalOptimisation.constraint{Float64}(#3, [-∞, 6]) | ||
# IntervalOptimisation.constraint{Float64}(#4, [-∞, 4]) | ||
# IntervalOptimisation.constraint{Float64}(#5, [-∞, 4]) | ||
|
||
minimise_icp_constrained(f, X, constraints) | ||
# ([-6.66676, -6.66541], IntervalArithmetic.IntervalBox{2,Float64}[[5.99918, 6] × [0.666233, 0.666758], [5.99918, 6] × [0.665717, 0.666234], [5.99887, 5.99919] × [0.666233, 0.666826], [5.99984, 6] × [0.665415, 0.665718], [5.99856, 5.99888] × [0.666233, 0.666826], [5.99969, 5.99985] × [0.665415, 0.665718]]) | ||
|
||
|
||
|
||
# One-dimensional case | ||
minimise1d(x -> (x^2 - 2)^2, -10..11) | ||
# ([0, 1.33476e-08], IntervalArithmetic.Interval{Float64}[[-1.41426, -1.41358], [1.41364, 1.41429]]) | ||
|
||
minimise1d_deriv(x -> (x^2 - 2)^2, -10..11) | ||
# ([0, 8.76812e-08], IntervalArithmetic.Interval{Float64}[[-1.41471, -1.41393], [1.41367, 1.41444]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
struct IntervalMinimum{T<:Real} | ||
interval::Interval{T} | ||
minimum::T | ||
end | ||
|
||
Base.isless(a::IntervalMinimum{T}, b::IntervalMinimum{T}) where {T<:Real} = isless(a.minimum, b.minimum) | ||
|
||
function minimise1d(f::Function, x::Interval{T}; reltol=1e-3, abstol=1e-3, use_deriv=false, use_second_deriv=false) where {T<:Real} | ||
|
||
Q = binary_minheap(IntervalMinimum{T}) | ||
|
||
global_minimum = f(interval(mid(x))).hi | ||
|
||
arg_minima = Interval{T}[] | ||
|
||
push!(Q, IntervalMinimum(x, global_minimum)) | ||
|
||
while !isempty(Q) | ||
|
||
p = pop!(Q) | ||
|
||
if isempty(p.interval) | ||
continue | ||
end | ||
|
||
if p.minimum > global_minimum | ||
continue | ||
end | ||
|
||
if use_deriv | ||
deriv = ForwardDiff.derivative(f, p.interval) | ||
if 0 ∉ deriv | ||
continue | ||
end | ||
end | ||
|
||
# Second derivative contractor | ||
if use_second_deriv | ||
doublederiv = ForwardDiff.derivative(x->ForwardDiff.derivative(f, x), p.interval) | ||
if doublederiv < 0 | ||
continue | ||
end | ||
end | ||
|
||
m = mid(p.interval) | ||
current_minimum = f(interval(m)).hi | ||
|
||
if current_minimum < global_minimum | ||
global_minimum = current_minimum | ||
end | ||
|
||
|
||
# Contractor 1 | ||
if use_deriv | ||
x = m .+ extended_div((interval(-∞, global_minimum) - f(m)), deriv) | ||
|
||
x = x .∩ p.interval | ||
end | ||
|
||
if diam(p.interval) < abstol | ||
push!(arg_minima, p.interval) | ||
else | ||
|
||
if use_deriv && isempty(x[2]) | ||
x1, x2 = bisect(x[1]) | ||
push!(Q, IntervalMinimum(x1, f(x1).lo), IntervalMinimum(x2, f(x2).lo)) | ||
continue | ||
end | ||
|
||
x1, x2 = bisect(p.interval) | ||
push!(Q, IntervalMinimum(x1, f(x1).lo), IntervalMinimum(x2, f(x2).lo)) | ||
end | ||
end | ||
lb = minimum(inf.(f.(arg_minima))) | ||
|
||
return lb..global_minimum, arg_minima | ||
end | ||
|
||
|
||
struct IntervalBoxMinimum{N, T<:Real} | ||
interval::IntervalBox{N, T} | ||
minimum::T | ||
end | ||
|
||
""" | ||
Datatype to provide constraints to Global Optimisation such as: | ||
``` | ||
Constraint(x->(x^2 - 10), -∞..1) | ||
``` | ||
""" | ||
struct Constraint{T<:Real} | ||
f::Function | ||
c::Interval{T} | ||
end | ||
|
||
Base.isless(a::IntervalBoxMinimum{N, T}, b::IntervalBoxMinimum{N, T}) where {N, T<:Real} = isless(a.minimum, b.minimum) | ||
|
||
function minimise_icp(f::Function, x::IntervalBox{N, T}; reltol=1e-3, abstol=1e-3) where {N, T<:Real} | ||
|
||
Q = binary_minheap(IntervalBoxMinimum{N, T}) | ||
|
||
global_minimum = ∞ | ||
|
||
x = icp(f, x, -∞..global_minimum) | ||
|
||
arg_minima = IntervalBox{N, T}[] | ||
|
||
push!(Q, IntervalBoxMinimum(x, global_minimum)) | ||
|
||
while !isempty(Q) | ||
|
||
p = pop!(Q) | ||
|
||
if isempty(p.interval) | ||
continue | ||
end | ||
|
||
if p.minimum > global_minimum | ||
continue | ||
end | ||
|
||
current_minimum = f(interval.(mid(p.interval))).hi | ||
|
||
if current_minimum < global_minimum | ||
global_minimum = current_minimum | ||
end | ||
|
||
X = icp(f, p.interval, -∞..global_minimum) | ||
|
||
if diam(p.interval) < abstol | ||
push!(arg_minima, p.interval) | ||
|
||
else | ||
x1, x2 = bisect(X) | ||
push!(Q, IntervalBoxMinimum(x1, f(x1).lo), IntervalBoxMinimum(x2, f(x2).lo)) | ||
end | ||
end | ||
|
||
lb = minimum(inf.(f.(arg_minima))) | ||
|
||
return lb..global_minimum, arg_minima | ||
end | ||
|
||
function minimise_icp_constrained(f::Function, x::IntervalBox{N, T}, constraints::Vector{Constraint{T}} = Vector{Constraint{T}}(); reltol=1e-3, abstol=1e-3) where {N, T<:Real} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this a method of |
||
|
||
Q = binary_minheap(IntervalBoxMinimum{N, T}) | ||
|
||
global_minimum = ∞ | ||
|
||
for t in constraints | ||
x = icp(t.f, x, t.c) | ||
end | ||
|
||
x = icp(f, x, -∞..global_minimum) | ||
|
||
arg_minima = IntervalBox{N, T}[] | ||
|
||
push!(Q, IntervalBoxMinimum(x, global_minimum)) | ||
|
||
while !isempty(Q) | ||
|
||
p = pop!(Q) | ||
|
||
if isempty(p.interval) | ||
continue | ||
end | ||
|
||
if p.minimum > global_minimum | ||
continue | ||
end | ||
|
||
# current_minimum = f(interval.(mid(p.interval))).hi | ||
current_minimum = f(p.interval).hi | ||
|
||
if current_minimum < global_minimum | ||
global_minimum = current_minimum | ||
end | ||
|
||
X = icp(f, p.interval, -∞..global_minimum) | ||
|
||
for t in constraints | ||
X = icp(t.f, X, t.c) | ||
end | ||
|
||
if diam(p.interval) < abstol | ||
push!(arg_minima, p.interval) | ||
else | ||
x1, x2 = bisect(X) | ||
push!(Q, IntervalBoxMinimum(x1, f(x1).lo), IntervalBoxMinimum(x2, f(x2).lo)) | ||
end | ||
end | ||
lb = minimum(inf.(f.(arg_minima))) | ||
return lb..global_minimum, arg_minima | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this a method of
minimise
and dispatch on a typeICP
or similar.