diff --git a/examples/sorting.jl b/examples/sorting.jl new file mode 100644 index 0000000..e9974dc --- /dev/null +++ b/examples/sorting.jl @@ -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 diff --git a/src/util/show.jl b/src/util/show.jl index b943865..a695d38 100644 --- a/src/util/show.jl +++ b/src/util/show.jl @@ -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