Skip to content

Commit

Permalink
Add sorting cutoff tuning example.
Browse files Browse the repository at this point in the history
  • Loading branch information
phrb committed Jun 7, 2015
1 parent 2cddf90 commit 25c358b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
72 changes: 72 additions & 0 deletions examples/sorting.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@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_000
report_after = 1_0

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,
args = args,
iterations = iterations,
report_after = report_after,
evaluations = 6,
instances = [4])
partial = None
for i = 0:iterations
partial = consume(result)
end
10 changes: 5 additions & 5 deletions src/util/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ Base.show{T <: Result}(io::IO, n::T) = begin
@printf io "[Cost] : %6f\n" n.cost_minimum
@printf io "[Found in Iteration] : %d\n" n.iterations
@printf io "[Function Calls] : %d\n" n.cost_calls
@printf io "[Start Configuration] :"
@printf io "[Start Configuration]"
show(io, n.start)
@printf io "[Minimum Configuration]"
show(io, n.minimum)
else
@printf io "[Partial Result] Cost: %6f, " n.cost_minimum
@printf io "[Technique] %s, " n.technique
@printf io "[Found in Iteration] %d, " n.iterations
@printf io "[Current Iteration] %d.\n" n.current_iteration
@printf io "[Partial Result] [Cost] %6f " n.cost_minimum
@printf io "[Technique] %s " n.technique
@printf io "[Found in Iteration] %5d " n.iterations
@printf io "[Current Iteration] %5d\n" n.current_iteration
end
return
end

0 comments on commit 25c358b

Please sign in to comment.