-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the first participant's solution and unit test result. Unit tes…
…t was done with Ruby 1.9.1. ps. also added 99_ashbb just as a sample. :)
- Loading branch information
Showing
6 changed files
with
169 additions
and
3 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,65 @@ | ||
## This is an approximation algorithm | ||
## Sort the jobs, then start processing on the processor which has the least amount of jobs scheduled | ||
## Provably the max time is (1/processors * (sum of processes) + max process time) if we do the smallest job first | ||
## and if we do Longest time first it is 3/2 approximation | ||
## I am doing Longest here first as this will pass most of tests, of course not all | ||
|
||
class FairDistribution | ||
def initialize(jobs, number_of_processes) | ||
@jobs = jobs.sort | ||
@number_of_processes = number_of_processes | ||
@max_sum = @jobs.inject(0) {|sum, i| sum + i} | ||
end | ||
|
||
def time_required | ||
solve.first | ||
end | ||
|
||
def distribution | ||
solve.last | ||
end | ||
|
||
def solve | ||
jobs = Array.new(@jobs) | ||
distribution = [] | ||
@number_of_processes.times do | ||
distribution << [jobs.shift] | ||
end | ||
|
||
while !jobs.empty? | ||
index = find_least_end_time_index(distribution) | ||
distribution[index] += [jobs.shift] | ||
end | ||
return find_max_end_time(distribution), distribution | ||
end | ||
|
||
def get_sum_array(distribution) | ||
sum = [] | ||
distribution.each do |processor| | ||
sum += [processor.inject(0) {|sum, i| sum + i}] | ||
end | ||
sum | ||
end | ||
|
||
def find_max_end_time(distribution) | ||
sum = get_sum_array(distribution) | ||
sum.sort.last | ||
end | ||
|
||
def find_least_end_time_index(distribution) | ||
sum_arr = get_sum_array(distribution) | ||
min_sum = @max_sum | ||
index = 0 | ||
min_index = -1 | ||
|
||
sum_arr.each do |sum| | ||
if sum < min_sum | ||
min_index = index | ||
min_sum = sum | ||
end | ||
index+=1 | ||
end | ||
|
||
min_index | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Loaded suite test_solution_acceptance | ||
Started | ||
FFFFF | ||
Finished in 0.000000 seconds. | ||
|
||
1) Failure: | ||
test_basic1(FairQueueTest) [test_solution_acceptance.rb:15]: | ||
<110> expected but was | ||
<135>. | ||
|
||
2) Failure: | ||
test_basic2(FairQueueTest) [test_solution_acceptance.rb:35]: | ||
<6.33> expected but was | ||
<8.63>. | ||
|
||
3) Failure: | ||
test_basic3(FairQueueTest) [test_solution_acceptance.rb:46]: | ||
<3.73> expected but was | ||
<4.97>. | ||
|
||
4) Failure: | ||
test_basic4(FairQueueTest) [test_solution_acceptance.rb:66]: | ||
<9> expected but was | ||
<12>. | ||
|
||
5) Failure: | ||
test_basic5(FairQueueTest) [test_solution_acceptance.rb:72]: | ||
<3.2> expected but was | ||
<3.67>. | ||
|
||
5 tests, 5 assertions, 5 failures, 0 errors, 0 skips |
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,41 @@ | ||
# ashbb's solution for RPCFN6 | ||
class FairDistribution | ||
attr_reader :distribution | ||
|
||
def initialize jobs, n | ||
@jobs, @n= jobs, n | ||
max = jobs.inject(:+) | ||
@solution = [max, max] | ||
end | ||
|
||
def time_required | ||
100000.times do |i| | ||
random_candidate | ||
time = @candidate.collect{|press| press.inject(:+)}.max | ||
score = @candidate.collect{|press| time - press.inject(:+)}.max | ||
if time <= @solution[0] and score < @solution[1] | ||
@solution = [] | ||
@solution << time << score << @candidate | ||
end | ||
end | ||
@distribution = @solution[2].collect{|e| e - [0]} | ||
p @solution[0], @distribution # debug | ||
@solution[0] | ||
end | ||
|
||
def random_candidate | ||
@candidate = Array.new(@n){[0]} | ||
@jobs.each do |job| | ||
@candidate[rand @n] << job | ||
end | ||
end | ||
end | ||
|
||
=begin | ||
jobs = [0.23, 0.47, 0.73] | ||
number_of_presses = 4 | ||
fd = FairDistribution.new(jobs, number_of_presses) | ||
p fd.time_required | ||
p fd.distribution | ||
=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,16 @@ | ||
Loaded suite test_solution_acceptance | ||
Started | ||
110 | ||
[[10, 24, 30, 45], [15, 20, 75]] | ||
.6.33 | ||
[[5.8], [1.1, 4.4], [2.83, 3.5], [1.0, 4.75]] | ||
.3.73 | ||
[[0.23, 2.3, 1.09], [1.5, 1.75, 0.11, 0.27], [0.73, 3.0], [0.47, 3.2]] | ||
.9 | ||
[[3, 3, 3], [5, 4], [5, 4]] | ||
.3.2 | ||
[[0.23, 0.47, 0.73], [1.5], [3.0], [3.2]] | ||
. | ||
Finished in 18.187500 seconds. | ||
|
||
5 tests, 8 assertions, 0 failures, 0 errors, 0 skips |
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