-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'first_improvement' building block.
- Loading branch information
Showing
13 changed files
with
245 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
@everywhere using StochasticSearch | ||
|
||
@everywhere function rosenbrock(x::Configuration) | ||
return (1.0 - x["i0"].value)^2 + 100.0 * (x["i1"].value - x["i0"].value^2)^2 | ||
end | ||
|
||
configuration = Configuration([NumberParameter(-2.0, 2.0, 0.0,"i0"), | ||
NumberParameter(-2.0, 2.0, 0.0,"i1")], | ||
"rosenbrock_config") | ||
|
||
iterations = 1_000 | ||
report_after = 1_00 | ||
|
||
result = @task optimize(rosenbrock, | ||
configuration, | ||
[:iterative_first_improvement], | ||
iterations = iterations, | ||
report_after = report_after, | ||
evaluations = 1, | ||
instances = [1]) | ||
partial = None | ||
for i = 0:iterations | ||
partial = consume(result) | ||
end |
This file contains 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,73 @@ | ||
@everywhere using StochasticSearch | ||
|
||
@everywhere function insertionsort!(A, i, j) | ||
for m = i + 1:j | ||
value = A[m] | ||
n = m - 1 | ||
while n >= i && A[n] > value | ||
A[n + 1] = A[n] | ||
n = n - 1 | ||
end | ||
A[n + 1] = value | ||
end | ||
end | ||
|
||
@everywhere function quicksort!(A,cutoff,i=1,j=length(A)) | ||
if j > i | ||
pivot = A[rand(i:j)] | ||
left, right = i, j | ||
while left <= right | ||
while A[left] < pivot | ||
left += 1 | ||
end | ||
while A[right] > pivot | ||
right -= 1 | ||
end | ||
if left <= right | ||
A[left], A[right] = A[right], A[left] | ||
left += 1 | ||
right -= 1 | ||
end | ||
end | ||
if j - i <= cutoff | ||
insertionsort!(A, i, j) | ||
else | ||
quicksort!(A,cutoff,i,right) | ||
quicksort!(A,cutoff,left,j) | ||
end | ||
end | ||
return A | ||
end | ||
|
||
@everywhere function sorting_cutoff(config::Configuration, args::Dict{ASCIIString, Any}) | ||
A = copy(args["array"]) | ||
cutoff = config.value["cutoff"] | ||
@elapsed quicksort!(A, cutoff) | ||
end | ||
|
||
array_size = 100_000 | ||
cutoff = 15 | ||
iterations = 2_00 | ||
report_after = 4 | ||
|
||
args = Dict{ASCIIString, Any}() | ||
args["array"] = rand(array_size) | ||
|
||
# Making sure code is already compiled. | ||
@elapsed quicksort!(rand(10), 5) | ||
|
||
configuration = Configuration([NumberParameter(0, array_size, cutoff, "cutoff")], | ||
"Sorting Cutoff") | ||
|
||
result = @task optimize(sorting_cutoff, | ||
configuration, | ||
[:iterative_first_improvement, :simulated_annealing], | ||
args = args, | ||
iterations = iterations, | ||
report_after = report_after, | ||
evaluations = 6, | ||
instances = [1, 1]) | ||
partial = None | ||
for i = 0:iterations | ||
partial = consume(result) | ||
end |
This file contains 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 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 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 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,44 @@ | ||
first_improvement(cost::Function, | ||
args::Dict{ASCIIString, Any}, | ||
initial_x::Configuration, | ||
initial_cost::Float64, | ||
evaluations::Int, | ||
f_xs::Array{Float64}, | ||
cutoff::Int) = begin | ||
x = deepcopy(initial_x) | ||
x_proposal = deepcopy(initial_x) | ||
name = "First Improvement" | ||
f_calls = 0 | ||
iteration = 0 | ||
f_x = initial_cost | ||
while iteration <= cutoff | ||
iteration += 1 | ||
neighbor!(x_proposal) | ||
f_proposal = @fetch (measure_mean!(cost, | ||
x_proposal, | ||
args, | ||
evaluations, | ||
f_xs)) | ||
f_calls += evaluations | ||
if f_proposal <= f_x | ||
update!(x, x_proposal.parameters) | ||
f_x = f_proposal | ||
return Result(name, | ||
initial_x, | ||
x, | ||
f_x, | ||
iteration, | ||
iteration, | ||
f_calls, | ||
false) | ||
end | ||
end | ||
Result(name, | ||
initial_x, | ||
x, | ||
f_x, | ||
iteration, | ||
iteration, | ||
f_calls, | ||
false) | ||
end |
This file contains 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 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,37 @@ | ||
iterative_first_improvement(cost::Function, | ||
args::Dict{ASCIIString, Any}, | ||
initial_x::Configuration, | ||
initial_cost::Float64; | ||
cutoff::Int = 10, | ||
evaluations::Int = 3, | ||
iterations::Int = 100_000) = begin | ||
x = deepcopy(initial_x) | ||
name = "Iterative First Improvement" | ||
f_calls = 0 | ||
iteration = 0 | ||
f_xs = Float64[] | ||
for i = 1:evaluations | ||
push!(f_xs, 0.0) | ||
end | ||
f_x = initial_cost | ||
f_calls += evaluations | ||
while iteration <= iterations | ||
iteration += 1 | ||
neighbor!(x) | ||
# First Improvement never produces a worse result. | ||
result = first_improvement(cost, | ||
args, | ||
x, | ||
f_x, | ||
evaluations, | ||
f_xs, | ||
cutoff) | ||
f_calls += result.cost_calls | ||
result.cost_calls = f_calls | ||
result.start = initial_x | ||
result.technique = name | ||
result.iterations = iteration | ||
result.current_iteration = iteration | ||
produce(result) | ||
end | ||
end |
This file contains 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 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 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,30 @@ | ||
using StochasticSearch, FactCheck, Base.Test | ||
|
||
facts("[Search]") do | ||
context("iterative_first_improvement") do | ||
function rosenbrock(x::Configuration) | ||
return (1.0 - x["i0"].value)^2 + 100.0 * (x["i1"].value - x["i0"].value^2)^2 | ||
end | ||
configuration = Configuration([NumberParameter(-2.0,2.0,0.0,"i0"), | ||
NumberParameter(-2.0,2.0,0.0,"i1")], | ||
"rosenbrock_config") | ||
iterations = 1_000 | ||
report_after = 333 | ||
run_task = @task optimize(rosenbrock, | ||
configuration, | ||
[:iterative_first_improvement], | ||
iterations = iterations, | ||
report_after = report_after) | ||
result = None | ||
for i = 1:iterations | ||
result = consume(run_task) | ||
end | ||
rr = rosenbrock(result.minimum) | ||
rc = result.cost_minimum | ||
@test_approx_eq rc rr | ||
@fact (configuration["i0"].value != result.minimum["i0"].value) --> true | ||
@fact (rosenbrock(result.minimum) <= rosenbrock(configuration)) --> true | ||
@fact_throws ErrorException optimize(rosenbrock, configuration, [:bozo_search]) | ||
println(rosenbrock(result.minimum)) | ||
end | ||
end |
This file contains 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 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