Skip to content

Commit

Permalink
Fix #6.
Browse files Browse the repository at this point in the history
The user can now choose which measument method will be used.
  • Loading branch information
phrb committed Oct 2, 2015
1 parent 19d972e commit c200af2
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 71 deletions.
17 changes: 9 additions & 8 deletions examples/rosenbrock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ methods = [:simulated_annealing,
instances = [2, 4, 3, 2, 4]
iterations = 1_000

parameters = Dict(:cost => rosenbrock,
:cost_args => Dict{Symbol, Any}(),
:initial_config => configuration,
:iterations => iterations,
:report_after => 50,
:methods => methods,
:instances => instances,
:evaluations => 1)
parameters = Dict(:cost => rosenbrock,
:cost_args => Dict{Symbol, Any}(),
:initial_config => configuration,
:iterations => iterations,
:report_after => 50,
:measurement_method => sequential_measure_mean!,
:methods => methods,
:instances => instances,
:evaluations => 1)

search_task = @task optimize(parameters)

Expand Down
19 changes: 10 additions & 9 deletions examples/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ methods = [:simulated_annealing,
:iterative_probabilistic_improvement]

instances = [1, 1, 1, 1, 1]
iterations = 10_000
iterations = 1_00

parameters = Dict(:cost => sorting_cutoff,
:cost_args => args,
:initial_config => configuration,
:iterations => iterations,
:report_after => 100,
:methods => methods,
:instances => instances,
:evaluations => 4)
parameters = Dict(:cost => sorting_cutoff,
:cost_args => args,
:initial_config => configuration,
:iterations => iterations,
:report_after => 4,
:measurement_method => sequential_measure_mean!,
:methods => methods,
:instances => instances,
:evaluations => 4)

search_task = @task optimize(parameters)

Expand Down
29 changes: 15 additions & 14 deletions src/StochasticSearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ VERSION >= v"0.4" && __precompile__()
module StochasticSearch
using Optim
# Types
export Parameter,
NumberParameter,
export Parameter,
NumberParameter,
IntegerParameter,
FloatParameter,
EnumParameter,
FloatParameter,
EnumParameter,
StringParameter,
Configuration,
Result,
Configuration,
Result,
BoolParameter

# Methods
export perturb!,
perturb_elements!,
export perturb!,
perturb_elements!,
neighbor!,
optimize!,
optimize,
optimize!,
optimize,
update!,
unit_value,
unit_value,
unit_value!

# Measurement Tools
export measure_mean!
export measure_mean!,
sequential_measure_mean!

# Search Building Blocks
export first_improvement,
Expand All @@ -44,8 +45,8 @@ module StochasticSearch


# New Methods for Base Functions
import Base.convert,
Base.show,
import Base.convert,
Base.show,
Base.getindex,
Base.setindex!

Expand Down
23 changes: 17 additions & 6 deletions src/core/measurement/measure.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
measure_mean!(f::Function,
x::Configuration,
args::Dict{Symbol, Any},
evaluations::Int,
results::Array{Float64}) = begin
next_proc = @task chooseproc()
function measure_mean!(parameters::Dict{Symbol, Any}, x::Configuration)
f = parameters[:cost]
args = parameters[:cost_args]
evaluations = parameters[:evaluations]
results = parameters[:costs]
next_proc = @task chooseproc()
@sync begin
for i = 1:evaluations
@async begin
Expand All @@ -13,3 +13,14 @@ measure_mean!(f::Function,
end
mean(results)
end

function sequential_measure_mean!(parameters::Dict{Symbol, Any}, x::Configuration)
f = parameters[:cost]
args = parameters[:cost_args]
evaluations = parameters[:evaluations]
results = parameters[:costs]
for i = 1:evaluations
results[i] = f(x, args)
end
mean(results)
end
2 changes: 1 addition & 1 deletion src/core/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ type StringParameter <: Parameter
end

typealias IntegerParameter NumberParameter{Integer}
typealias FloatParameter NumberParameter{FloatingPoint}
typealias FloatParameter NumberParameter{AbstractFloat}
7 changes: 2 additions & 5 deletions src/core/search/blocks/first_improvement.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
function first_improvement(parameters::Dict{Symbol, Any})
cost = parameters[:cost]
args = parameters[:cost_args]
initial_x = parameters[:initial_config]
initial_cost = parameters[:initial_cost]
cost_calls = parameters[:evaluations]
evaluations = parameters[:evaluations]
costs = parameters[:costs]
cutoff = parameters[:cutoff]
x = deepcopy(initial_x)
x_proposal = deepcopy(initial_x)
measurement = parameters[:measurement_method]
name = "First Improvement"
iteration = 0
while iteration <= cutoff
iteration += 1
neighbor!(x_proposal)
proposal = @fetch (measure_mean!(cost, x_proposal, args,
evaluations, costs))
proposal = @fetch (measurement(parameters, x_proposal))
cost_calls += evaluations
if proposal <= initial_cost
update!(x, x_proposal.parameters)
Expand Down
7 changes: 2 additions & 5 deletions src/core/search/blocks/greedy_construction.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
function greedy_construction(parameters::Dict{Symbol, Any})
cost = parameters[:cost]
args = parameters[:cost_args]
initial_x = parameters[:initial_config]
initial_cost = parameters[:initial_cost]
evaluations = parameters[:evaluations]
costs = parameters[:costs]
target = parameters[:target]
cutoff = parameters[:cutoff]
x = deepcopy(initial_x)
x_proposal = deepcopy(initial_x)
measurement = parameters[:measurement_method]
name = "Greedy Construction"
cost_calls = 0
iteration = 0
while iteration <= cutoff
iteration += 1
neighbor!(x_proposal, target)
proposal = @fetch (measure_mean!(cost, x_proposal, args,
evaluations, costs))
proposal = @fetch (measurement(parameters, x_proposal))
cost_calls += evaluations
if proposal <= initial_cost
update!(x, x_proposal.parameters)
Expand Down
9 changes: 3 additions & 6 deletions src/core/search/blocks/probabilistic_improvement.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
function probabilistic_improvement(parameters::Dict{Symbol, Any})
cost = parameters[:cost]
args = parameters[:cost_args]
initial_x = parameters[:initial_config]
initial_cost = parameters[:initial_cost]
evaluations = parameters[:evaluations]
costs = parameters[:costs]
t = parameters[:t]
x = deepcopy(initial_x)
x_proposal = deepcopy(initial_x)
measurement = parameters[:measurement_method]
name = "Probabilistic Improvement"
cost_calls = 0
iteration = 0
neighbor!(x_proposal)
proposal = @fetch (measure_mean!(cost, x_proposal, args,
evaluations, costs))
cost_calls += evaluations
proposal = @fetch (measurement(parameters, x_proposal))
cost_calls += evaluations
if proposal <= initial_cost
update!(x, x_proposal.parameters)
initial_cost = proposal
Expand Down
7 changes: 2 additions & 5 deletions src/core/search/blocks/random_walk.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
function random_walk(parameters::Dict{Symbol, Any})
cost = parameters[:cost]
args = parameters[:cost_args]
initial_x = parameters[:initial_config]
initial_cost = parameters[:initial_cost]
evaluations = parameters[:evaluations]
cost_calls = parameters[:evaluations]
costs = parameters[:costs]
x = deepcopy(initial_x)
measurement = parameters[:measurement_method]
name = "Random Walk"
neighbor!(x)
f_x = @fetch (measure_mean!(cost, x, args,
evaluations, costs))
f_x = @fetch (measurement(parameters, x))
cost_calls += evaluations
return Result(name, initial_x, x, f_x, 1,
1, cost_calls, false)
Expand Down
3 changes: 3 additions & 0 deletions src/core/search/optimize.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function optimize(parameters::Dict{Symbol, Any})
if !haskey(parameters, :measurement_method)
parameters[:measurement_method] = measure_mean!
end
if !haskey(parameters, :evaluations)
parameters[:evaluations] = 1
end
Expand Down
4 changes: 2 additions & 2 deletions src/core/search/tools/initialize_search_tasks!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function initialize_search_tasks!(parameters::Dict{Symbol, Any})
methods = parameters[:methods]
initial_x = parameters[:initial_config]
evaluations = parameters[:evaluations]
measurement = parameters[:measurement_method]
next_proc = @task chooseproc()

instance_id = 1
Expand All @@ -18,8 +19,7 @@ function initialize_search_tasks!(parameters::Dict{Symbol, Any})
parameters[:costs] = costs
initial_x = neighbor!(initial_x, j)
parameters[:initial_config] = initial_x
parameters[:initial_cost] = measure_mean!(cost, initial_x,
args, evaluations, costs)
parameters[:initial_cost] = measurement(parameters, initial_x)
remotecall(consume(next_proc), eval(methods[i]),
deepcopy(parameters), reference)
instance_id += 1
Expand Down
2 changes: 1 addition & 1 deletion src/core/unit_value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end

function unit_value!(parameter::IntegerParameter, unit::Float64)
@assert 0.0 <= unit <= 1.0
value = int((unit * (parameter.max - parameter.min)) + parameter.min)
value = round(Int, (unit * (parameter.max - parameter.min)) + parameter.min)
parameter.value
end

Expand Down
2 changes: 1 addition & 1 deletion src/util/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ rand_in(min::Integer, max::Integer) = begin
rand(min:max)
end

rand_in(min::FloatingPoint, max::FloatingPoint) = begin
rand_in(min::AbstractFloat, max::AbstractFloat) = begin
rand() * (max - min) + min
end
8 changes: 4 additions & 4 deletions test/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ facts("[NumberParameter]") do
@fact (typeof(p.min) == Float32) --> true
@fact (typeof(p.max) == Float32) --> true
@fact (typeof(p.value) == Float32) --> true
@fact (typeof(p) == NumberParameter{FloatingPoint}) --> true
@fact (typeof(p) == NumberParameter{AbstractFloat}) --> true
@fact (typeof(p) <: FloatParameter) --> true
@fact (p.min == 0) --> true
@fact (p.max == 2) --> true
Expand All @@ -127,14 +127,14 @@ facts("[NumberParameter]") do
interval = 50.6
p = FloatParameter(0., 1000., value, "rand_test")
perturb!(p)
@fact (typeof(p.value) <: FloatingPoint) --> true
@fact (typeof(p.value) <: AbstractFloat) --> true
@fact (p.value <= p.max) --> true
@fact (p.value >= p.min) --> true
perturb!(p, interval)
perturb!(p, interval)
perturb!(p, interval)
value = p.value
@fact (typeof(p.value) <: FloatingPoint) --> true
@fact (typeof(p.value) <: AbstractFloat) --> true
@fact (p.value <= value + interval) --> true
@fact (p.value <= p.max) --> true
@fact (p.value >= p.min) --> true
Expand All @@ -143,7 +143,7 @@ facts("[NumberParameter]") do
perturb!(p, interval)
perturb!(p, interval)
perturb!(p, interval)
@fact (typeof(p.value) <: FloatingPoint) --> true
@fact (typeof(p.value) <: AbstractFloat) --> true
@fact (p.value <= p.max) --> true
@fact (p.value >= p.min) --> true
interval = -1.2
Expand Down
9 changes: 5 additions & 4 deletions test/simulated_annealing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ facts("[Search]") do
"rosenbrock_config")
methods = [:simulated_annealing]
instances = [1]
parameters = Dict(:cost => rosenbrock,
:initial_config => configuration,
:methods => methods,
:instances => instances)
parameters = Dict(:cost => rosenbrock,
:initial_config => configuration,
:methods => methods,
:measurement_method => sequential_measure_mean!,
:instances => instances)

search_task = @task optimize(parameters)
result = consume(search_task)
Expand Down

0 comments on commit c200af2

Please sign in to comment.