Skip to content

added Project.toml to centered_forms branch #53

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

Draft
wants to merge 11 commits into
base: centered_forms
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: TagBot
on:
schedule:
- cron: 0 * * * *
jobs:
TagBot:
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ os:
- osx

julia:
- 0.7
- 1.0
- 1.5
- nightly

notifications:
email: false

matrix:
allow_failures:
- julia: 0.7
- julia: nightly

after_success:
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# NEWS for IntervalOptimisation.jl

## v0.4
- Drop support for Julia 0.7

## v0.3
- Drop support for Julia 0.6. The package is now fully compatible with Julia 1.0.

Expand Down
19 changes: 19 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "IntervalOptimisation"
uuid = "c7c68f13-a4a2-5b9a-b424-07d005f8d9d2"
version = "0.4.2"

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
IntervalArithmetic = "0.15, 0.16, 0.17"
julia = "1.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
[![appveyor badge][appveyor_badge]][appveyor_url]
[![codecov badge][codecov_badge]][codecov_url]

## Documentation [here][documenter_latest]

[travis_badge]: https://travis-ci.org/JuliaIntervals/IntervalOptimisation.jl.svg?branch=master
[travis_url]: https://travis-ci.org/JuliaIntervals/IntervalOptimisation.jl

Expand Down Expand Up @@ -35,7 +33,7 @@ They return an `Interval` that is guaranteed to contain the global minimum (maxi


#### 1D
```
```julia
using IntervalArithmetic, IntervalOptimisation

julia> @time global_min, minimisers = minimise(x -> (x^2 - 2)^2, -10..11);
Expand All @@ -52,7 +50,7 @@ julia> minimisers

#### 2D

```
```julia
julia> @time global_min, minimisers = minimise( X -> ( (x,y) = X; x^2 + y^2 ),
(-10000..10001) × (-10000..10001) );
0.051122 seconds (46.80 k allocations: 2.027 MiB)
Expand Down
7 changes: 0 additions & 7 deletions REQUIRE

This file was deleted.

2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- julia_version: 0.7
- julia_version: 1
- julia_version: 1.5
- julia_version: nightly

platform:
Expand Down
107 changes: 107 additions & 0 deletions src/HeapedVectors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
__precompile__()

module HeapedVectors

import Base: getindex, length, push!, isempty,
pop!, filter!, popfirst!

export HeapedVector

import ..StrategyBase:filter_elements!
using ..StrategyBase

struct HeapedVector{T, F<:Function} <: Strategy
data::Vector{T}
by::F
function HeapedVector(v::Vector{T}, by::F) where {T, F}
new{T, F}(heaping(v, by), by)
end
end

HeapedVector(data::Vector{T}) where {T} = HeapedVector(data, identity)


function heaping(v, by)
ar = typeof(v[1])[]
for i = 1:length(v)
insert!(ar, i, v[i])
floatup!(ar, length(ar), by)
end
return ar
end

function floatup!(ar, index, by)
par = convert(Int, floor(index/2))
if index <= 1
return ar
end
if by(ar[index]) < by(ar[par])
ar[par], ar[index] = ar[index], ar[par]
end
floatup!(ar, par, by)
end


function push!(v::HeapedVector{T}, x::T) where {T}
insert!(v.data, length(v.data)+1, x)
floatup!(v.data, length(v.data), v.by)
return v
end


isempty(v::HeapedVector) = isempty(v.data)


function popfirst!(v::HeapedVector{T}) where {T}
if length(v.data) == 0
return
end

if length(v.data) > 2
v.data[length(v.data)], v.data[1] = v.data[1], v.data[length(v.data)]
minm = pop!(v.data)
bubbledown!(v::HeapedVector{T}, 1)

elseif length(v.data) == 2
v.data[length(v.data)], v.data[1] = v.data[1], v.data[length(v.data)]
minm = pop!(v.data)
else
minm = pop!(v.data)
end
return minm
end


function bubbledown!(v::HeapedVector{T}, index) where{T}
left = index*2
right = index*2+1
smallest = index

if length(v.data)+1 > left && v.by(v.data[smallest]) > v.by(v.data[left])
smallest = left
end

if length(v.data)+1 > right && v.by(v.data[smallest]) > v.by(v.data[right])
smallest = right
end

if smallest != index
v.data[index], v.data[smallest] = v.data[smallest], v.data[index]
bubbledown!(v, smallest)
end
end

function filter_elements!(A::HeapedVector{T}, x::T) where{T}
func(y) = A.by(y) < A.by(x)
filter!(func, A.data)

if length(A.data) == 0
return A
end

heaping(A.data, A.by)
return A

end

end
13 changes: 10 additions & 3 deletions src/IntervalOptimisation.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@


module IntervalOptimisation

export minimise, maximise,
minimize, maximize
export HeapedVector, SortedVector
export mean_value_form_scalar, third_order_taylor_form_scalar

include("StrategyBase.jl")
using .StrategyBase

include("SortedVectors.jl")
using .SortedVectors

using IntervalArithmetic, IntervalRootFinding
include("HeapedVectors.jl")
using .HeapedVectors

using IntervalArithmetic, IntervalRootFinding
using LinearAlgebra

include("optimise.jl")
include("centered_forms.jl")

const minimize = minimise
const maximize = maximise
Expand Down
22 changes: 10 additions & 12 deletions src/SortedVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@ __precompile__()
module SortedVectors

import Base: getindex, length, push!, isempty,
pop!, resize!, popfirst!
pop!, popfirst!

export SortedVector

"""
A `SortedVector` behaves like a standard Julia `Vector`, except that its elements are stored in sorted order, with an optional function `by` that determines the sorting order in the same way as `Base.searchsorted`.
"""
struct SortedVector{T, F<:Function}
import ..StrategyBase:filter_elements!
using ..StrategyBase

struct SortedVector{T, F<:Function} <: Strategy

data::Vector{T}
by::F

function SortedVector(data::Vector{T}, by::F) where {T,F}
new{T,F}(sort(data,by=by), by)
new{T,F}(sort(data, by = by), by)
end
end


SortedVector(data::Vector{T}) where {T} = SortedVector(data, identity)

function show(io::IO, v::SortedVector)
print(io, "SortedVector($(v.data))")
end



getindex(v::SortedVector, i::Int) = v.data[i]
length(v::SortedVector) = length(v.data)

Expand All @@ -43,9 +41,9 @@ pop!(v::SortedVector) = pop!(v.data)

popfirst!(v::SortedVector) = popfirst!(v.data)


function resize!(v::SortedVector, n::Int)
resize!(v.data, n)
function filter_elements!(v::SortedVector{T}, x::T) where {T}
cutoff = searchsortedfirst(v.data, x, by=v.by)
resize!(v.data, cutoff-1)
return v
end

Expand Down
7 changes: 7 additions & 0 deletions src/StrategyBase.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module StrategyBase

export filter_elements!, Strategy
abstract type Strategy end
function filter_elements!(s::Strategy)end

end
33 changes: 33 additions & 0 deletions src/centered_forms.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ForwardDiff: gradient, jacobian, hessian

gradient(f, X::IntervalBox) = gradient(f, X.v)
# jacobian(f, X::IntervalBox) = jacobian(f, X.v)
hessian(f, X::IntervalBox) = hessian(f, X.v)

"""
Calculate the mean-value form of a function ``f:\\mathbb{R}^n \\to \\mathbb{R}``
using the gradient ``\nabla f``;
this gives a second-order approximation.
"""
function mean_value_form_scalar(f, X)
m = IntervalBox(mid(X))

return f(m) + gradient(f, X.v) ⋅ (X - m)
end

mean_value_form_scalar(f) = X -> mean_value_form_scalar(f, X)


"""
Calculate a third-order Taylor form of ``f:\\mathbb{R}^n \\to \\mathbb{R}`` using the Hessian.
"""
function third_order_taylor_form_scalar(f, X)
m = IntervalBox(mid(X))

H = hessian(f, X)
δ = X - m

return f(m) + gradient(f, m) ⋅ δ + 0.5 * sum(δ[i]*H[i,j]*δ[j] for i in 1:length(X) for j in 1:length(X))
end

third_order_taylor_form_scalar(f) = X -> third_order_taylor_form_scalar(f, X)
Loading